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.

  1. An (almost) empty paragraph in a block element: <div><p></p></div>
  2. 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.

Comments

wrwrwr’s picture

StatusFileSize
new3.87 KB
new1.48 KB

It seems somehow the patches didn't get attached.

drumm’s picture

Version: 5.6 » 7.x-dev
Status: Needs review » Needs work

This 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.

wrwrwr’s picture

To 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?).

wrwrwr’s picture

Status: Needs work » Needs review
dries’s picture

Version: 7.x-dev » 6.x-dev
Status: Needs review » Needs work

I 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.

wrwrwr’s picture

Rerolled for Drupal 5 & 6, just without tests and with different offsets.

wrwrwr’s picture

Status: Needs work » Needs review

Ah, the status again :)

Re-test of filter-line-breaking-unpaired-paragraph-tag.patch from comment #3 was requested by ismail76.

sun.core’s picture

Status: Needs review » Reviewed & tested by the community

Looks good. I don't see how this could break anything.

gábor hojtsy’s picture

Status: Reviewed & tested by the community » Fixed

Thanks, committed.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.