Skip to content

Commit 85abf4f

Browse files
jakejackson1claude
andcommitted
Apply descendant rules through inline ancestors and blocks inside table cells (mpdf#830)
The legacy engine only looks for the ancestors a descendant rule names among the blocks and table parts that open a level of its rules, so span em, or .note b for a <span class="note">, never applied, nor div b for a div inside a table cell, nor table tbody td where the tbody is left out. Each descendant rule the legacy parser stores is compiled too. The matcher applies it only to an element with an ancestor that opened no level, and only where the legacy engine could not match it, so nothing is applied twice and the order the legacy engine gives the rest stays. Each open element's frame records whether it opened a level, as the merger reports when it styles the element. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent f3c7bb9 commit 85abf4f

14 files changed

Lines changed: 591 additions & 38 deletions

‎CHANGELOG-GRAVITYPDF.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ New features
9393
### CSS selectors
9494

9595
* **Child and sibling combinators, and structural pseudo-classes.** `div > p`, `h1 + p`, `h1 ~ p`, and `:first-child`, `:nth-child()`, `:first-of-type` and `:nth-of-type()` on any element, are matched against the elements open around the one being styled. Rules using them were dropped before, so a document that has them changes. They are applied with the descendant rules, after them, in order of specificity and then of source order. [mpdf/mpdf#7] [mpdf/mpdf#318] [#538] [#620]
96+
* **Descendant rules through inline elements.** A descendant rule whose ancestor is an inline element, a block inside a table cell, or the `tbody` a table leaves out, such as `span em`, or `.note b` for a `<span class="note">`, applies. [mpdf/mpdf#830] [#538]
9697

9798
Performance
9899
-----------
@@ -325,6 +326,7 @@ These changes do not change output.
325326
[mpdf/mpdf#7]: https://github.com/mpdf/mpdf/issues/7
326327
[mpdf/mpdf#318]: https://github.com/mpdf/mpdf/issues/318
327328
[mpdf/mpdf#747]: https://github.com/mpdf/mpdf/issues/747
329+
[mpdf/mpdf#830]: https://github.com/mpdf/mpdf/issues/830
328330
[mpdf/mpdf#833]: https://github.com/mpdf/mpdf/issues/833
329331
[mpdf/mpdf#1089]: https://github.com/mpdf/mpdf/issues/1089
330332
[mpdf/mpdf#1334]: https://github.com/mpdf/mpdf/issues/1334

‎src/Css/CssMerger.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ protected function mergeTableCascadingCss($inherit, $tag, $attr, $classes)
188188
return;
189189
}
190190

191+
$this->mpdf->markStyledElementAsLevel();
192+
191193
if ($inherit === 'TOPTABLE') {
192194
// Save Cascading CSS e.g. "div.topic p" at this block level
193195
if (isset($this->mpdf->blk[$this->mpdf->blklvl]['cascadeCSS'])) {
@@ -279,6 +281,7 @@ protected function mergeBlockCascadingCss($inherit, $tag, $attr, $classes)
279281
// Set the new block info
280282
if ($this->sideEffects) {
281283
$this->mpdf->blk[$this->mpdf->blklvl] = $currentBlock;
284+
$this->mpdf->markStyledElementAsLevel();
282285
}
283286

284287
// Block properties which are inherited

‎src/Css/CssParser.php‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ class CssParser
109109
private $maxClassDepth = 1;
110110

111111
/**
112-
* @var array[] The rules of the last CSS parsed whose selector the legacy parser cannot read, each compiled:
113-
* [compiled selector, declarations], in the order they were written
112+
* @var array[] The rules of the last CSS parsed for the matcher, each compiled: [compiled selector, declarations,
113+
* whether the legacy parser stores it too], in the order they were written
114114
*/
115115
private $compiledRules = [];
116116

@@ -208,9 +208,11 @@ public function getCascadeCss()
208208
}
209209

210210
/**
211-
* The rules of the last CSS parsed whose selector the legacy parser cannot read, compiled for the matcher
211+
* The rules of the last CSS parsed for the matcher: those whose selector the legacy parser cannot read, and the
212+
* descendant rules it stores, which the matcher applies through the ancestors the legacy engine does not look at
212213
*
213-
* @return array[] Each [compiled selector, declarations], in the order they were written
214+
* @return array[] Each [compiled selector, declarations, whether the legacy parser stores it too], in the order
215+
* they were written
214216
*/
215217
public function getCompiledRules()
216218
{
@@ -319,6 +321,9 @@ private function processCssSelector($written, $classProperties)
319321

320322
$cascadeCSS = Arrays::uniqueRecursiveMerge($cascadeCSS, $classProperties);
321323
$cascadeCSS['depth'] = $level;
324+
325+
// The legacy engine only looks for the ancestors a descendant rule names among blocks and table parts
326+
$this->compileRule($written, $classProperties, true);
322327
}
323328

324329
/**
@@ -345,9 +350,10 @@ private function isLegacySelector(array $levels)
345350
*
346351
* @param string $selector As written
347352
* @param array $classProperties
353+
* @param bool $legacy Whether the legacy parser stores it too
348354
* @return void
349355
*/
350-
private function compileRule($selector, array $classProperties)
356+
private function compileRule($selector, array $classProperties, $legacy = false)
351357
{
352358
if (!$classProperties) {
353359
return;
@@ -358,7 +364,7 @@ private function compileRule($selector, array $classProperties)
358364
// The universal selector changes which elements existing documents style, so it waits for #530 and the
359365
// standard cascade option
360366
if ($compiled !== null && !$compiled['universal']) {
361-
$this->compiledRules[] = [$compiled, $classProperties];
367+
$this->compiledRules[] = [$compiled, $classProperties, $legacy];
362368
}
363369
}
364370

‎src/Css/RuleSet.php‎

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@
66
* Compiled stylesheet rules, filed under the rightmost compound of their selector as browsers file them: by its id,
77
* else its first class, else its tag, else with the rules any element may match. An element then only has to be
88
* matched against the rules filed under its own id, classes and tag, and those.
9+
*
10+
* The descendant rules the legacy engine applies too are filed apart. They are only looked at for an element with an
11+
* ancestor that opened no level of the legacy descendant rules, such as an inline element, since the legacy engine
12+
* matches the rest.
913
*/
1014
class RuleSet
1115
{
1216

1317
/**
1418
* @var array[] Each rule, by its position in the stylesheets read: [compiled selector, declarations, the ancestors
15-
* it requires as requiredAncestors() gives them]
19+
* it requires as requiredAncestors() gives them, whether the legacy engine applies it too]
1620
*/
1721
private $rules = [];
1822

@@ -22,6 +26,11 @@ class RuleSet
2226
*/
2327
private $index = ['id' => [], 'class' => [], 'tag' => [], 'any' => []];
2428

29+
/**
30+
* @var array The descendant rules the legacy engine applies too, filed in the same way
31+
*/
32+
private $legacyIndex = ['id' => [], 'class' => [], 'tag' => [], 'any' => []];
33+
2534
/**
2635
* @var SelectorMatcher
2736
*/
@@ -40,13 +49,21 @@ public function __construct()
4049
*
4150
* @param array $selector A selector SelectorCompiler::compile() compiled
4251
* @param array $declarations Its normalised declarations
52+
* @param bool $legacy For a rule the legacy engine applies too: match it only where the legacy engine cannot,
53+
* such as through an inline element or another ancestor that opens no level of its
54+
* descendant rules
4355
*/
44-
public function add(array $selector, array $declarations)
56+
public function add(array $selector, array $declarations, $legacy = false)
4557
{
4658
$position = count($this->rules);
47-
$this->rules[] = [$selector, $declarations, self::requiredAncestors($selector)];
59+
$this->rules[] = [$selector, $declarations, self::requiredAncestors($selector), $legacy];
4860

49-
self::file($this->index, $selector['compounds'][count($selector['compounds']) - 1], $position);
61+
$subject = $selector['compounds'][count($selector['compounds']) - 1];
62+
if ($legacy) {
63+
self::file($this->legacyIndex, $subject, $position);
64+
} else {
65+
self::file($this->index, $subject, $position);
66+
}
5067
}
5168

5269
/**
@@ -78,7 +95,8 @@ public function candidates($tag, $id, array $classes)
7895
public function matchingDeclarations($tag, $id, array $classes, callable $path)
7996
{
8097
$candidates = $this->candidates($tag, $id, $classes);
81-
if (!$candidates) {
98+
$legacyCandidates = self::collect($this->legacyIndex, $tag, $id, $classes);
99+
if (!$candidates && !$legacyCandidates) {
82100
return [];
83101
}
84102

@@ -87,17 +105,21 @@ public function matchingDeclarations($tag, $id, array $classes, callable $path)
87105
return [];
88106
}
89107

108+
if ($legacyCandidates && self::hasAncestorOpeningNoLevel($path)) {
109+
$candidates = array_merge($candidates, $legacyCandidates);
110+
}
111+
90112
$ancestors = self::ancestorsOf($path);
91113
$matched = [];
92114
foreach ($candidates as $position) {
93-
list($selector, , $required) = $this->rules[$position];
115+
list($selector, , $required, $legacy) = $this->rules[$position];
94116
foreach ($required as $key) {
95117
if (!isset($ancestors[$key])) {
96118
continue 2;
97119
}
98120
}
99121

100-
if ($this->matcher->matches($selector, $path)) {
122+
if ($this->matcher->matches($selector, $path) && !($legacy && $this->matcher->matches($selector, $path, true))) {
101123
$matched[] = $position;
102124
}
103125
}
@@ -123,7 +145,8 @@ public function matchingDeclarations($tag, $id, array $classes, callable $path)
123145
/**
124146
* @param int $position
125147
*
126-
* @return array The rule at a position, as it is kept: [compiled selector, declarations, required ancestors]
148+
* @return array The rule at a position, as it is kept: [compiled selector, declarations, required ancestors,
149+
* whether the legacy engine applies it too]
127150
*/
128151
public function rule($position)
129152
{
@@ -227,4 +250,20 @@ private static function ancestorsOf(array $path)
227250

228251
return $keys;
229252
}
253+
254+
/**
255+
* @param array[] $path
256+
*
257+
* @return bool Whether an ancestor of the element opened no level of the legacy descendant rules
258+
*/
259+
private static function hasAncestorOpeningNoLevel(array $path)
260+
{
261+
for ($depth = count($path) - 2; $depth > 0; $depth--) {
262+
if (!$path[$depth]['level']) {
263+
return true;
264+
}
265+
}
266+
267+
return false;
268+
}
230269
}

‎src/Css/SelectorMatcher.php‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@ class SelectorMatcher
1818
*
1919
* @param array $selector A selector SelectorCompiler::compile() compiled
2020
* @param array[] $path The open elements from the document down to the element
21+
* @param bool $legacyView Whether to match as the legacy engine does: a descendant combinator only looks at the
22+
* ancestors that opened a level of the legacy descendant rules
2123
*
2224
* @return bool
2325
*/
24-
public function matches(array $selector, array $path)
26+
public function matches(array $selector, array $path, $legacyView = false)
2527
{
2628
$depth = count($path) - 1;
2729

28-
return $this->matchesFrom($selector, count($selector['compounds']) - 1, $path, $depth - 1, $path[$depth]['nthChild'] - 1);
30+
return $this->matchesFrom($selector, count($selector['compounds']) - 1, $path, $depth - 1, $path[$depth]['nthChild'] - 1, $legacyView);
2931
}
3032

3133
/**
@@ -36,10 +38,11 @@ public function matches(array $selector, array $path)
3638
* @param array[] $path
3739
* @param int $parent The depth of the element's parent on the path, -1 for the document itself
3840
* @param int $index The element's index among its parent's children
41+
* @param bool $legacyView
3942
*
4043
* @return bool
4144
*/
42-
private function matchesFrom(array $selector, $compound, array $path, $parent, $index)
45+
private function matchesFrom(array $selector, $compound, array $path, $parent, $index, $legacyView)
4346
{
4447
if (!$this->matchesCompound($selector['compounds'][$compound], $path, $parent, $index)) {
4548
return false;
@@ -52,23 +55,24 @@ private function matchesFrom(array $selector, $compound, array $path, $parent, $
5255
$compound--;
5356
switch ($selector['combinators'][$compound]) {
5457
case '>':
55-
return $parent >= 0 && $this->matchesFrom($selector, $compound, $path, $parent - 1, $path[$parent]['nthChild'] - 1);
58+
return $parent >= 0 && $this->matchesFrom($selector, $compound, $path, $parent - 1, $path[$parent]['nthChild'] - 1, $legacyView);
5659

5760
case ' ':
5861
for ($depth = $parent; $depth >= 0; $depth--) {
59-
if ($this->matchesFrom($selector, $compound, $path, $depth - 1, $path[$depth]['nthChild'] - 1)) {
62+
if ((!$legacyView || $path[$depth]['level'])
63+
&& $this->matchesFrom($selector, $compound, $path, $depth - 1, $path[$depth]['nthChild'] - 1, $legacyView)) {
6064
return true;
6165
}
6266
}
6367

6468
return false;
6569

6670
case '+':
67-
return $parent >= 0 && $index > 0 && $this->matchesFrom($selector, $compound, $path, $parent, $index - 1);
71+
return $parent >= 0 && $index > 0 && $this->matchesFrom($selector, $compound, $path, $parent, $index - 1, $legacyView);
6872

6973
default:
7074
for ($sibling = $index - 1; $parent >= 0 && $sibling >= 0; $sibling--) {
71-
if ($this->matchesFrom($selector, $compound, $path, $parent, $sibling)) {
75+
if ($this->matchesFrom($selector, $compound, $path, $parent, $sibling, $legacyView)) {
7276
return true;
7377
}
7478
}

‎src/CssManager.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class CssManager
8888
public $tbCSSlvl = 0;
8989

9090
/**
91-
* @var RuleSet The rules whose selector the legacy parser cannot read, compiled, from every stylesheet read
91+
* @var RuleSet The rules compiled for the matcher, from every stylesheet read
9292
*/
9393
private $rules;
9494

@@ -131,14 +131,14 @@ public function readCss($html)
131131
$this->cascadeCSS = Arrays::uniqueRecursiveMerge($this->cascadeCSS, $this->cssParser->getCascadeCss());
132132

133133
foreach ($this->cssParser->getCompiledRules() as $rule) {
134-
$this->rules->add($rule[0], $rule[1]);
134+
$this->rules->add($rule[0], $rule[1], $rule[2]);
135135
}
136136

137137
return $html;
138138
}
139139

140140
/**
141-
* The rules whose selector the legacy parser cannot read, compiled, from every stylesheet read so far
141+
* The rules compiled for the matcher, from every stylesheet read so far
142142
*
143143
* @return RuleSet
144144
*/

0 commit comments

Comments
 (0)