Issue: When using HTML Purifier to process user-inputted content, line-breaks are not being translated into <br />
tags.
Consider the following user-inputted content:
Lorem ipsum dolor sit amet.
This is another line.
<pre>
.my-css-class {
color: blue;
}
</pre>
Lorem ipsum:
<ul>
<li>Lorem</li>
<li>Ipsum</li>
<li>Dolor</li>
</ul>
Dolor sit amet,
MyName
When processed using HTML Purifier, the above is being altered to the following:
Lorem ipsum dolor sit amet. This is another line.
.my-css-class { color: blue; }
Lorem ipsum:
Dolor sit amet, MyName
- Lorem
- Ipsum
- Dolor
As you can see, "MyName" which was intended to be on a separate line by the user, is being displayed altogether with the previous line.
How to fix?
Using the PHP nl2br()
function, of course. However, new issues arise whether we use it before or after purifying the content.
Here is an example when using nl2br() before HTML Purifier:
Lorem ipsum dolor sit amet.
This is another line..my-css-class { color: blue; }
Lorem ipsum:
- Lorem
- Ipsum
- Dolor
Dolor sit amet,
MyName
What happens is that nl2br() adds <br />
for each line-break, therefore even the ones in the <pre>
block are being processed, as well as the line-breaks after each <li>
tag.
What I tried
I tried a custom nl2br() function which replaces line-breaks with <br />
tags, and then removes all <br />
tags from <pre>
blocks. It works great, however the issue remains for the <li>
items.
Trying the same approach for <ul>
blocks would also remove all <br />
tags from the <li>
children, unless we would use a more complex regex to remove <br />
tags that are inside <ul>
elements but outside <li>
elements. But then what about nested <ul>
within a <li>
item? To handle all those situations we'd have to have an even more complex regex!
- If this is the right approach, could you help me out with the regex?
- If it's not the right approach, how could I solve this problem? I am also open to alternatives to HTML Purifier.
Other resources that I've already looked at:
This issue can be solved partially (if not completely) with a custom
nl2br()
function:This must be applied to the content before it is HTML-Purified. Never re-process a purified content, unless you know what you're doing.
Please note that because each line-break and double line-breaks are already kept, you should not use the
AutoFormat.AutoParagraph
feature of HTML Purifier:That's it!
Furthermore, because allowing basic HTML tags was originally intended to improve user experience by not adding another markup syntax, you might want to allow users to post code, and especially HTML code, which would not be interpreted/removed by HTML Purifier.
HTML Purifier currently allows to post code but requires complex CDATA markers:
Hard to remember and to write. To simplify the user experience as much as possible I believe it is best to allow users to add code by embedding it with simple
<code>
(for inline code) and<pre>
(for blocks of code) tags. Here is how to do that:Note that like the nl2br processing, it must be done before the content is HTML Purified. Also, keep in mind that if the user puts
<code>
or<pre>
tags in his own posted code, then it will close the parent<code>
or<pre>
tag enclosing his code. This cannot be solved, and also applies with the original CDATA markers or with any markup, even the one used on StackOverflow (for example using the ` symbol in a code sample will close the code tag).Finally, for a great user experience there are other things that we might want to automate like for example the links which we want to be made clickable. Luckily this can be done by HTML Purifier
AutoFormat.Linkify
feature.Here is the final code that includes everything for an ultimate setup:
Cheers!
maybe this will help.