In two specific cases line breaking results in an invalid markup. Both cases arise from the filter not removing all empty paragraphs, and then removing unneeded paragraph tags (supposedly enclosing block elements) one by one.
- An (almost) empty paragraph in a block element:
<div><p></p></div> - Nonprocessed content (pre, script, style) in a blockquote:
<blockquote><pre>aaa</pre></blockquote>
_filter_autop in filter.module
...
$chunk = preg_replace('|<p>\s*</p>\n|', '', $chunk);
...
$chunk = preg_replace('!<p>\s*(</?'. $block .'[^>]*>)!', "$1", $chunk);
$chunk = preg_replace('!(</?'. $block .'[^>]*>)\s*</p>!', "$1", $chunk);
First line fails to remove an empty paragraph not followed by a newline, the second line removes the paragraph start (p is also a block tag), and the last fails to remove the end tag. Under certain conditions the filter itself makes empty paragraphs not followed by a newline.
This can be fixed by making the newline after empty paragraph optional (adding a '?' to the regular expression), and moving the empty paragraphs removing three lines down (to solve the blockquote problem).
Attaching a patch to perform those changes. You can find it running here: http://www.wrwrwr.org/node/10. Same code seems to be present in 5.x and 6.x branches, however I've only tested with 5.6. The second patch does the same and adds some comments — it's not so easy to quickly read all those regular expressions ;)
Part of this issue has been reported already: http://drupal.org/node/177139.
| Comment | File | Size | Author |
|---|---|---|---|
| #6 | filter-line-breaking-unpaired-paragraph-tag-D5.patch | 1.49 KB | wrwrwr |
| #6 | filter-line-breaking-unpaired-paragraph-tag-D6.patch | 1.48 KB | wrwrwr |
| #3 | filter-line-breaking-unpaired-paragraph-tag.patch | 2.34 KB | wrwrwr |
| #1 | filter.module.patch | 1.48 KB | wrwrwr |
| #1 | filter.module-with-comments.patch | 3.87 KB | wrwrwr |
Comments
Comment #1
wrwrwr commentedIt seems somehow the patches didn't get attached.
Comment #2
drummThis part of the code is the same in the development version of Drupal except for some spacing changes. This should be fixed in the development version, where there is more active code review and testing, and then backported from there.
Comment #3
wrwrwr commentedTo reproduce the issue with 7.x you need to disable the HTML corrector or reorder it before the line breaker, otherwise it fixes what the other breaks :)
Regenerated the patch against the head (same change, still works), also adding a test (is it ok to test internal functions like that?).
Comment #4
wrwrwr commentedComment #5
dries commentedI committed this patch to CVS HEAD. Thanks for the tests too! :)
However, the patch didn't apply against DRUPAL-6 nor DRUPAL-5 so we'll have to re-roll it. I've updated the status.
#177139: Filter adds a /p after a blockquote where it shouldn't. might be a duplicate.
Comment #6
wrwrwr commentedRerolled for Drupal 5 & 6, just without tests and with different offsets.
Comment #7
wrwrwr commentedAh, the status again :)
Comment #9
sun.core commentedLooks good. I don't see how this could break anything.
Comment #10
gábor hojtsyThanks, committed.