Repository navigation
Remove an HTML comment without leaving a space in its place - #637
Merged
Merged
Conversation
jakejackson1
marked this pull request as draft
September 30, 2026 10:05
Member
Author
|
Held as a draft until #635 merges. #634 has since landed with a test that expects |
Base automatically changed from
feature/536-stylesheet-tokenizer
to
gravitypdf
September 30, 2026 10:06
27 of 28 tasks
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
force-pushed
the
fix/636-html-comment-space
branch
from
September 30, 2026 10:17
2d936ec to
d4c8599
Compare
jakejackson1
marked this pull request as ready for review
September 30, 2026 10:28
This was referenced Sep 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #636. Part of #520.
Was stacked on #635 (
feature/536-stylesheet-tokenizer), which has merged; rebased ontogravitypdf.Problem
In the default parser mode, an HTML comment between two words drew a space:
CssParser::parse()callsCommentParser::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:
<script>var s = "<!--";</script><p>a<!-- x -->b</p>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>a: scripts were removed before comments, so the<script>inside the comment ran to the real</script>Upstream's
CommentParseratupstream/developmenthas 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::SCRIPTholds 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.<!--mpdf … mpdf-->convention is unchanged: the markers are removed and their content is kept, and a comment inside that content is removed too.<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.<!--[if mso]>…<![endif]-->is removed whole.<!--[if !mso]><!-->shown<!--<![endif]-->keepsshown.<pre>and<textarea>handling inAdjustHTML()runs after comments are removed. A comment inside<pre>keeps the line breaks around it, soa\n<!-- x -->\nbdrawsa, an empty line andb, as a browser does. The table-cell, list and<br>white-space trimming inAdjustHTML()gives the same result with or without the old space.This is a fix for misread input, so it applies under both
cssModevalues. 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 -->keepernow 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, sop:emptymatches 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 expectstrue. The'comments and white space'case staysfalse, because of the spaces around its comments.testWhatCountsAsEmpty()andtestAnElementHoldingOnlyACommentInTheBodyMode(), and the comment inTracksOpenElements::readAhead(), no longer describe the space.:emptysays an element holding only a comment is empty.Here,
CommentParserTestasserts that<p><!-- x --></p>becomes<p></p>.Tests
HtmlCommentTestrenders 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 bothcssModevalues. The cases:<pre>between two words, and inside<pre>keeping the line breaks around it<!--mpdf … mpdf-->content, and a comment inside itCommentParserTestchecks whatremoveHtmlComments()leaves, including the empty<p></p>and capitalised mPDF markers.HtmlCommentTestcases fail, and 7 of the 10CommentParserTestcases. 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.php -lon PHP 5.6, andremoveHtmlComments()gives the same result there.🤖 Generated with Claude Code