Skip to content

Commit b9581ea

Browse files
jakejackson1claude
andcommitted
Match html and body by the universal selector through #642's document rules
#642 matches html as body's parent and merges both through documentDeclarations(), which takes the rules filed for any element, so the separate merge of those rules for body goes. A universal rule now reaches html too: it sets the size rem is read against, and * > *, * * and *:not(body) reach body through html, as in a browser. Its !important declarations apply in their layer. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 9ea175b commit b9581ea

3 files changed

Lines changed: 37 additions & 15 deletions

File tree

‎tests/Mpdf/Css/RuleSetTest.php‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -166,21 +166,27 @@ public function testGivesTheImportantDeclarationsApartInTheSameOrder()
166166
}
167167

168168
/**
169-
* The document's own frame is matched as body, by the rules filed for any element alone: the universal selector
170-
* and the other rules whose subject names no tag, id or class, when they match the root
169+
* html and body are matched by the rules filed for any element too: the universal selector and the other rules
170+
* whose subject names no tag, id or class, when they match there. html is body's parent and has no parent, and
171+
* neither has siblings
171172
*/
172-
public function testMatchesTheDocumentAsBodyWithTheRulesForAnyElement()
173+
public function testMatchesHtmlAndBodyWithTheRulesForAnyElement()
173174
{
174175
$rules = new RuleSet();
175-
foreach (['*', 'body', '* > *', ':not(p)', ':not(body)', '*:first-child', '*.a', '*:lang(fr)'] as $position => $selector) {
176+
foreach (['*', 'body', '* > *', ':not(p)', ':not(body)', '*:first-child', '*.a', '*:lang(fr)', '* + *'] as $position => $selector) {
176177
$rules->add($this->compiler->compile($selector), ['COLOR' => 'rule ' . $position]);
177178
}
178179

179-
$path = function () {
180-
return array_slice($this->path(), 0, 1);
181-
};
180+
$document = [$this->path()[0]];
182181

183-
$this->assertSame([['COLOR' => 'rule 0'], ['COLOR' => 'rule 3']], $rules->matchingDeclarations('', '', [], $path));
182+
$this->assertSame(
183+
[[['COLOR' => 'rule 0'], ['COLOR' => 'rule 3'], ['COLOR' => 'rule 4']], []],
184+
$rules->documentDeclarations(true, $document)
185+
);
186+
$this->assertSame(
187+
[[['COLOR' => 'rule 0'], ['COLOR' => 'rule 2'], ['COLOR' => 'rule 1'], ['COLOR' => 'rule 3']], []],
188+
$rules->documentDeclarations(false, $document)
189+
);
184190
}
185191

186192
/**

‎tests/Mpdf/Css/SelectorCompilerTest.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public function selectors()
112112
$tag = function ($tag) {
113113
return ['tag' => $tag, 'ids' => [], 'classes' => [], 'pseudos' => []];
114114
};
115-
$any = ['compounds' => [['tag' => null, 'ids' => [], 'classes' => [], 'attributes' => [], 'pseudos' => []]], 'combinators' => [], 'specificity' => [0, 0, 0]];
115+
$any = ['compounds' => [['tag' => null, 'ids' => [], 'classes' => [], 'attributes' => [], 'pseudos' => []]], 'combinators' => [], 'specificity' => [0, 0, 0], 'never' => false];
116116

117117
return [
118118
'type' => ['p', [$tag('P')], []],

‎tests/Mpdf/UniversalSelectorTest.php‎

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,10 @@ public function testTheLegacyModeDropsThem($context, $css, array $groups)
241241
}
242242

243243
/**
244-
* The universal selector reaches body, as the document's root: text written straight into the body takes its
245-
* colour, and body's own rules outweigh it wherever they are written. Rules that need body to have a parent or a
246-
* sibling do not reach it
244+
* The universal selector reaches html and body: text written straight into the body takes its colour, and body's
245+
* own rules outweigh it wherever they are written. html is body's parent, so rules that need body to have a
246+
* parent, or html to be there, reach it, and text takes their colour from one or the other. Rules that need body to
247+
* have a sibling do not reach it
247248
*
248249
* @dataProvider bodyRules
249250
*
@@ -270,15 +271,30 @@ public function bodyRules()
270271
'a body rule written after it' => ['* { color: #f00; } body { color: #00f; }', self::BLUE],
271272
'a body rule written before it' => ['body { color: #00f; } * { color: #f00; }', self::BLUE],
272273
'the later of two' => ['* { color: #00f; } * { color: #f00; }', self::RED],
274+
'an important universal rule over a body rule' => ['* { color: #f00 !important; } body { color: #00f; }', self::RED],
275+
'an important body rule over an important universal rule' => ['body { color: #00f !important; } * { color: #f00 !important; }', self::BLUE],
273276
'not another tag' => [':not(p) { color: #f00; }', self::RED],
274-
'not body' => ['*:not(body) { color: #f00; }', self::BLACK],
275-
'a child of anything' => ['* > * { color: #f00; }', self::BLACK],
276-
'a descendant of anything' => ['* * { color: #f00; }', self::BLACK],
277+
'not body, which html is' => ['*:not(body) { color: #f00; }', self::RED],
278+
'a child of anything, as body is of html' => ['* > * { color: #f00; }', self::RED],
279+
'a descendant of anything, as body is of html' => ['* * { color: #f00; }', self::RED],
280+
'a child of anything but html' => [':not(html) > * { color: #f00; }', self::BLACK],
277281
'a sibling of anything' => ['* + *, * ~ * { color: #f00; }', self::BLACK],
278282
'with a class it does not have' => ['*.c { color: #f00; }', self::BLACK],
279283
];
280284
}
281285

286+
/**
287+
* The universal selector reaches html, so its font size is the one rem is read against
288+
*/
289+
public function testSetsTheSizeRemIsReadAgainst()
290+
{
291+
$mpdf = $this->drawDocument('<style>* { font-size: 20pt; } p.rem { font-size: 1rem; } p.half { font-size: 0.5rem; }</style><p class="rem">rem</p><p class="half">half</p>', ['cssMode' => CssMode::STANDARD]);
292+
$sizes = $this->keyedByText($mpdf, $mpdf->drawnFontSize);
293+
294+
$this->assertEqualsWithDelta(20, $sizes['rem'], 0.001);
295+
$this->assertEqualsWithDelta(10, $sizes['half'], 0.001);
296+
}
297+
282298
/**
283299
* A universal rule's background reaches body, which paints the page
284300
*/

0 commit comments

Comments
 (0)