Skip to content

Commit d4c8599

Browse files
jakejackson1claude
andcommitted
Match :empty on an element that holds only an HTML comment
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>
1 parent d49a3e5 commit d4c8599

3 files changed

Lines changed: 9 additions & 10 deletions

File tree

‎CHANGELOG-GRAVITYPDF.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ New features
113113
* **`:not()`, `:is()` and `:where()`.** Each takes a selector list whose selectors may have combinators, such as `p:not(.note, div > p)` or `:is(h2, h3) + p`. `:is()` and `:where()` leave out a selector they cannot read, as browsers do. `:not()` and `:is()` weigh as their most specific argument, and `:where()` as nothing. Rules using them were dropped before. [#538] [#628]
114114
* **Pseudo-classes that look at what follows an element.** `:last-child`, `:nth-last-child()`, `:only-child`, `:last-of-type`, `:nth-last-of-type()`, `:only-of-type` and `:empty` match in the flow, in tables, headers and footers, and positioned blocks. Rules using them were dropped before, so a document that has them changes. Set `'cssMode' => \Mpdf\CssMode::LEGACY` to keep dropping them. [#537] [#634]
115115
* `WriteHTML()` reads ahead through the HTML it is given to count each element's children. An element still open at the end of a call that leaves it open is not known in full, so these do not match its children in that call, and nor does `:not()` of them.
116-
* `:empty` matches as in browsers: an element holding white space is not empty. In the default mode mPDF's CSS reader turns each HTML comment into a space, so an element holding only a comment is not empty either. A table cell holding only white space is empty, as mPDF strips it.
116+
* `:empty` matches as in browsers: an element holding white space is not empty, and one holding only a comment is. A table cell holding only white space is empty, as mPDF strips it.
117117

118118
Performance
119119
-----------

‎src/TracksOpenElements.php‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,7 @@ private function readAhead(array $tokens, $floor, $standIn, $close)
324324

325325
foreach ($tokens as $i => $token) {
326326
if ($i % 2 === 0) {
327-
// White space is text, as browsers have it. Comments are gone by now, though the CSS reader leaves a space
328-
// for each
327+
// White space is text, as browsers have it. Comments are gone by now
329328
if ($token !== '') {
330329
$this->openElements[count($this->openElements) - 1]['empty'] = false;
331330
}

‎tests/Mpdf/LookAheadSelectorTest.php‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,10 @@ public function testTheKeptBlockUnwindsForEveryCase()
239239
}
240240

241241
/**
242-
* :empty matches an element with no child elements and no text, as browsers have it: white space is text. A
243-
* comment is not, but in the default mode the CSS reader turns each comment into a space before the HTML is read,
244-
* so an element holding one is not empty; the body mode, which does not read CSS, drops comments outright. The
245-
* white space at the end of a table cell is stripped before the HTML is read, so a cell holding only that is empty
242+
* :empty matches an element with no child elements and no text, as browsers have it: white space is text, and a
243+
* comment is not, so an element holding only a comment is empty and one holding comments and white space is not.
244+
* The white space at the end of a table cell is stripped before the HTML is read, so a cell holding only that is
245+
* empty
246246
*
247247
* @dataProvider emptyElements
248248
*
@@ -257,8 +257,8 @@ public function testWhatCountsAsEmpty($element, $empty)
257257
}
258258

259259
/**
260-
* HTML written in the body mode does not go through the CSS reader, so a comment is dropped and the element that
261-
* held only the comment is empty, as in a browser
260+
* HTML written in the body mode drops a comment too, so the element that held only the comment is empty, as in a
261+
* browser
262262
*/
263263
public function testAnElementHoldingOnlyACommentInTheBodyMode()
264264
{
@@ -280,7 +280,7 @@ public function emptyElements()
280280
'nothing in it' => ['<p></p>', true],
281281
'a space' => ['<p> </p>', false],
282282
'line breaks and tabs' => ["<p>\n\t\n</p>", false],
283-
'a comment, which the CSS reader turns into a space' => ['<p><!-- nothing --></p>', false],
283+
'a comment, which leaves nothing in its place' => ['<p><!-- nothing --></p>', true],
284284
'comments and white space' => ['<p> <!-- one --> <!-- two --> </p>', false],
285285
'a closed start tag' => ['<div />', true],
286286
'a void element' => ['<hr />', true],

0 commit comments

Comments
 (0)