Skip to content

Remove an HTML comment without leaving a space in its place - #637

Merged
jakejackson1 merged 3 commits into
gravitypdffrom
fix/636-html-comment-space
Sep 30, 2026
Merged

jakejackson1 merged 3 commits into
gravitypdffrom
fix/636-html-comment-space

Conversation

@jakejackson1

@jakejackson1 jakejackson1 commented Sep 30, 2026 •

Copy link
Copy Markdown
Member

Closes #636. Part of #520.

Was stacked on #635 (feature/536-stylesheet-tokenizer), which has merged; rebased onto gravitypdf.

Problem

In the default parser mode, an HTML comment between two words drew a space:

$mpdf->WriteHTML('<p>a<!-- x -->b</p>');                             // drew "a b"
$mpdf->WriteHTML('<p>a<!-- x -->b</p>', HTMLParserMode::HTML_BODY);  // drew "ab"

CssParser::parse() calls CommentParser::removeHtmlComments() before the HTML is read, and that method replaced each comment with a space. AdjustHTML() removes comments with nothing left behind, but in the default mode it only saw the HTML after that space had been put in. A browser draws "ab".

The same space meant that <p><!-- x --></p> held white space in the default mode.

Two related misreads were in the same place:

Document Mode Drew
<script>var s = "<!--";</script><p>a<!-- x -->b</p> default var s = " b: the comment ran from inside the script to the next -->, taking </script> with it
<p>a<!-- <script> -->b</p><script>x</script><p>c</p> body a: scripts were removed before comments, so the <script> inside the comment ran to the real </script>

Upstream's CommentParser at upstream/development has the same ' ' replacement.

Change

  • CommentParser::removeHtmlComments() removes each comment and leaves nothing in its place. It steps over a <script> element whole, because a script's content is text and a comment marker in it starts no comment. It returns early when there is no <!--.
  • AdjustHTML() calls the same method, and removes scripts after it rather than before. Comments and scripts are therefore removed in document order in both parser modes. CommentParser::SCRIPT holds the one pattern for a script element, which both use. As a result </script > now closes a script, and a tag such as <scripts> is no longer taken for one.
  • The <!--mpdf … mpdf--> convention is unchanged: the markers are removed and their content is kept, and a comment inside that content is removed too.
  • Comments in <style> are unchanged. removeCommentsFromStyleBlocks() still blanks <!-- and --> inside a style block before this runs, and Parse stylesheets with a tokenizer, so a brace, semicolon or comment marker in a string no longer breaks the rules around it #635's tokenizer reads the CSS comments.
  • Conditional comments are plain comments, as they are in a browser. <!--[if mso]>…<![endif]--> is removed whole. <!--[if !mso]><!-->shown<!--<![endif]--> keeps shown.
  • Other readers checked: the <pre> and <textarea> handling in AdjustHTML() runs after comments are removed. A comment inside <pre> keeps the line breaks around it, so a\n<!-- x -->\nb draws a, an empty line and b, as a browser does. The table-cell, list and <br> white-space trimming in AdjustHTML() gives the same result with or without the old space.

This is a fix for misread input, so it applies under both cssMode values. Nothing in it is gated.

Compatibility

A document that puts a comment between two words, with no white space on either side, loses the space it used to get there: Book<!-- x -->keeper now reads "Bookkeeper". To keep the space, write one beside the comment. Comments with white space around them render as before.

Knock-on for #634

With this change, <p><!-- x --></p> holds no text in the default mode, so p:empty matches it, as it does in body mode and in a browser. #634 merged first, so this PR updates it:

  • LookAheadSelectorTest::emptyElements(): the case 'a comment, which the CSS reader turns into a space' becomes 'a comment, which leaves nothing in its place' and expects true. The 'comments and white space' case stays false, because of the spaces around its comments.
  • The docblocks of testWhatCountsAsEmpty() and testAnElementHoldingOnlyACommentInTheBodyMode(), and the comment in TracksOpenElements::readAhead(), no longer describe the space.
  • Match :last-child, :only-child, :empty and the other pseudo-classes that look at what follows an element #634's changelog note on :empty says an element holding only a comment is empty.

Here, CommentParserTest asserts that <p><!-- x --></p> becomes <p></p>.

Tests

  • HtmlCommentTest renders each document and the same document as a browser reads it, with no comment in it. It asserts that the text drawn and the box of each line are the same. Every case runs under both parser modes (DEFAULT_MODE, HTML_BODY) and both cssMode values. The cases:
    • between two words, at the start and the end of a text run, between inline elements, over several lines, and with line breaks around it
    • inside <pre> between two words, and inside <pre> keeping the line breaks around it
    • inside a table cell
    • <!--mpdf … mpdf--> content, and a comment inside it
    • a conditional comment, and one that shows its content
    • a comment marker in a script, and a script tag in a comment
  • CommentParserTest checks what removeHtmlComments() leaves, including the empty <p></p> and capitalised mPDF markers.
  • With the source change reverted, 26 of the 60 HtmlCommentTest cases fail, and 7 of the 10 CommentParserTest cases. The rest guard behaviour that the fix must keep: the line breaks inside <pre> and around a comment, the mPDF convention, and body-mode cases that already worked.
  • HtmlCommentSnapshotTest (html-comment) puts each case under a caption that says what should be seen: a comment between two words, between inline elements, in a table cell, in preformatted text, as a conditional comment, after a script holding <!--, and inside mPDF content. Rendered before this change, each case reads "Book keeper", and the script case also draws the script's source. No existing fixture changed.

Checks

  • composer test: OK. 6939 tests, 31 skipped for missing optional packages or ungenerated data. The snapshot suite ran with Imagick and Ghostscript.
  • composer cs: clean.
  • PHPStan: no errors.
  • The changed and new files pass php -l on PHP 5.6, and removeHtmlComments() gives the same result there.

🤖 Generated with Claude Code

@jakejackson1 jakejackson1 added bug Something isn't working create-upstream-pr labels Sep 30, 2026
@jakejackson1
jakejackson1 marked this pull request as draft September 30, 2026 10:05
@jakejackson1

Copy link
Copy Markdown
Member Author

Held as a draft until #635 merges. #634 has since landed with a test that expects <p><!-- x --></p> not to count as :empty in the default mode (LookAheadSelectorTest::emptyElements()). This PR makes that paragraph empty, as in a browser, so once #635 is in, this branch gets rebased onto gravitypdf with that case flipped to true, and then marked ready again.

Base automatically changed from feature/536-stylesheet-tokenizer to gravitypdf September 30, 2026 10:06
jakejackson1 and others added 2 commits September 30, 2026 20:13
A comment between two words drew a space in the default parser mode, because
the CSS reader replaced each comment with one. It now leaves nothing, as a
browser does and as the body mode already did, in either cssMode.

Comments and scripts are removed in document order in both parser modes, so a
comment marker inside a script no longer starts a comment, and a script tag
inside a comment no longer hides the text after it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A comment no longer leaves a space in its place, so an element holding
only a comment is empty, as in a browser. Update the :empty test case,
its docblock and #634's changelog note to match.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jakejackson1
jakejackson1 force-pushed the fix/636-html-comment-space branch from 2d936ec to d4c8599 Compare September 30, 2026 10:17
@jakejackson1
jakejackson1 marked this pull request as ready for review September 30, 2026 10:28
@jakejackson1
jakejackson1 merged commit 6be28a9 into gravitypdf Sep 30, 2026
10 checks passed
@jakejackson1
jakejackson1 deleted the fix/636-html-comment-space branch September 30, 2026 10:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working create-upstream-pr

Projects

None yet

Development

Successfully merging this pull request may close these issues.

An HTML comment between two words draws a space in the default parser mode

1 participant