Skip to content

Commit 2124062

Browse files
jakejackson1claude
andcommitted
Apply html's and body's !important declarations in their layers
#638 split each rule's !important declarations from its others. html's now apply over its others, before body's own rules, as a parent's value would, and body's apply after its style attribute, with the other important author declarations. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 7df17ae commit 2124062

3 files changed

Lines changed: 24 additions & 4 deletions

File tree

‎src/Css/CssMerger.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,8 @@ private function mergeInCascadeOrder($inherit, $tag, array $attr)
214214
// Merged outside the document's elements, and written to by Mpdf::SetDefaultBodyCSS() and the like
215215
$important = $this->cssManager->getImportantCss();
216216
$importantDefault = $this->cssManager->getDefaultImportantCss();
217-
$this->setMergedCss(isset($this->cssManager->CSS['BODY']) ? $this->cssManager->CSS['BODY'] : [], false);
217+
$body = isset($this->cssManager->CSS['BODY']) ? $this->cssManager->CSS['BODY'] : [];
218+
$this->setMergedCss($body, false);
218219
$rules = [];
219220
$importantRules = array_merge(isset($important['BODY']) ? [$important['BODY']] : [], $this->mergeDocumentRules());
220221
$importantDefaultRules = isset($importantDefault['BODY']) ? [$importantDefault['BODY']] : [];

‎tests/Mpdf/Css/RuleSetTest.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,11 @@ public function testGivesTheDeclarationsOfHtmlAndBody()
213213
$document = [$path[0]];
214214

215215
$this->assertSame(
216-
[['COLOR' => 'rule 1'], ['COLOR' => 'rule 0'], ['COLOR' => 'rule 6']],
216+
[[['COLOR' => 'rule 1'], ['COLOR' => 'rule 0'], ['COLOR' => 'rule 6']], []],
217217
$rules->documentDeclarations(true, $document)
218218
);
219219
$this->assertSame(
220-
[['COLOR' => 'rule 2'], ['COLOR' => 'rule 3'], ['COLOR' => 'rule 6'], ['COLOR' => 'rule 4'], ['COLOR' => 'rule 5']],
220+
[[['COLOR' => 'rule 2'], ['COLOR' => 'rule 3'], ['COLOR' => 'rule 6'], ['COLOR' => 'rule 4'], ['COLOR' => 'rule 5']], []],
221221
$rules->documentDeclarations(false, $document)
222222
);
223223
}
@@ -259,7 +259,7 @@ public function testHasHtmlAmongTheAncestorsOfEveryElement()
259259
return $this->path();
260260
};
261261

262-
$this->assertSame([['COLOR' => 'html'], ['COLOR' => 'root']], $rules->matchingDeclarations('P', '', [], $path));
262+
$this->assertSame([['COLOR' => 'html'], ['COLOR' => 'root']], $rules->matchingDeclarations('P', '', [], $path)[0]);
263263
}
264264

265265
/**

‎tests/Mpdf/FixedPseudoClassTest.php‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,23 @@ public function testHtmlColourOutlastsAReset()
233233
], $colours);
234234
}
235235

236+
/**
237+
* html's !important declarations beat its others, and reach body as a parent's would, so body's own declarations
238+
* beat them. body's !important declarations beat its style attribute
239+
*/
240+
public function testHtmlAndBodyImportantDeclarations()
241+
{
242+
$standard = ['cssMode' => CssMode::STANDARD];
243+
$html = '<style>html { color: #f00 !important; } html { color: #00f; }</style>';
244+
245+
$this->assertSame(self::RED, $this->drawnColours($html . '<p>text</p>', $standard)['text']);
246+
$this->assertSame(self::GREEN, $this->drawnColours($html . '<style>body { color: #008000; }</style><p>text</p>', $standard)['text']);
247+
$this->assertSame(
248+
self::GREEN,
249+
$this->drawnColours('<style>body { color: #008000 !important; }</style><body style="color: #00f"><p>text</p></body>', $standard)['text']
250+
);
251+
}
252+
236253
/**
237254
* :root and html match the language of the <html> tag
238255
*
@@ -301,6 +318,8 @@ public function remSizes()
301318
'root' => [':root { font-size: 62.5%; } p { font-size: 2rem; }', $p, 13.75, 22],
302319
'the last rule for html' => ['html { font-size: 50%; } html { font-size: 62.5%; } p { font-size: 2rem; }', $p, 13.75, 22],
303320
'the more specific rule for html' => [':root { font-size: 62.5%; } html { font-size: 50%; } p { font-size: 2rem; }', $p, 13.75, 22],
321+
'an important rule for html' => ['html { font-size: 62.5% !important; } html { font-size: 50%; } p { font-size: 2rem; }', $p, 13.75, 22],
322+
'html\'s important size under body\'s own' => ['html { font-size: 62.5% !important; } body { font-size: 1.6rem; }', $p, 11, 17.6],
304323
'body without a size of its own takes html\'s' => ['html { font-size: 62.5%; }', $p, 6.875, 11],
305324
'body in rem' => ['html { font-size: 62.5%; } body { font-size: 1.6rem; }', $p, 11, 17.6],
306325
'body in em reads html' => ['html { font-size: 62.5%; } body { font-size: 1.6em; }', $p, 11, 17.6],

0 commit comments

Comments
 (0)