Skip to content

Commit bab64c9

Browse files
jakejackson1claude
andcommitted
Read a keyword on body against html, which #642 makes body's parent
#642 matches html as body's parent and merges its rules into body's CSS. A CSS-wide keyword on body now reads html's values: inherit and unset take html's colour or font size, and inherit on a property that is not inherited takes html's value, or the initial one. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent b5b995d commit bab64c9

2 files changed

Lines changed: 30 additions & 7 deletions

File tree

‎src/Css/CssMerger.php‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -232,15 +232,19 @@ private function mergeInCascadeOrder($inherit, $tag, array $attr)
232232
$this->mergePresentationalHints($tag, $attr);
233233
$this->mergeTableSpecificCss($tag, $attr);
234234

235+
$html = null;
235236
if ($tag === 'BODY') {
236237
// Merged outside the document's elements, and written to by Mpdf::SetDefaultBodyCSS() and the like
237238
$important = $this->cssManager->getImportantCss();
238239
$importantDefault = $this->cssManager->getDefaultImportantCss();
239240
$body = isset($this->cssManager->CSS['BODY']) ? $this->cssManager->CSS['BODY'] : [];
240241
$this->setMergedCss($body, false);
241242
$rules = [];
242-
$importantRules = array_merge(isset($important['BODY']) ? [$important['BODY']] : [], $this->mergeDocumentRules());
243+
list($importantBody, $html) = $this->mergeDocumentRules();
244+
$importantRules = array_merge(isset($important['BODY']) ? [$important['BODY']] : [], $importantBody);
243245
$importantDefaultRules = isset($importantDefault['BODY']) ? [$importantDefault['BODY']] : [];
246+
// html is body's parent, which a keyword on body reads
247+
$inherited = array_merge($inherited, $html);
244248
} else {
245249
list($rules, $importantRules) = $this->cssManager->getRules()->matchingDeclarations($tag, $id, $classes, $path);
246250
}
@@ -249,7 +253,7 @@ private function mergeInCascadeOrder($inherit, $tag, array $attr)
249253
list($inline, $importantInline) = isset($attr['STYLE']) ? $this->inlineStyleParser->parseByImportance($attr['STYLE']) : [[], []];
250254
$this->mergeEach(array_merge([$inline], $importantRules, [$importantInline], $importantDefaultRules), $dominance);
251255

252-
$this->resolveWideKeywords($inherited, $userAgent, $path, $inherit !== 'INLINE' && $inherit !== '');
256+
$this->resolveWideKeywords($inherited, $userAgent, $path, $inherit !== 'INLINE' && $inherit !== '', $html);
253257
if ($this->sideEffects) {
254258
$this->mpdf->recordStyledElementComputed($this->cssProperties);
255259
}
@@ -275,9 +279,10 @@ private function mergeInCascadeOrder($inherit, $tag, array $attr)
275279
* @param callable $path Gives the open elements from the document down to the element, or null for none
276280
* @param bool $fromParent Whether an inherited property the element does not start from is read from its parent's
277281
* frame: for a block, a table and a table part
282+
* @param array|null $parentProperties The properties of a parent that has no frame, as html has none for body
278283
* @return void
279284
*/
280-
private function resolveWideKeywords(array $inherited, array $userAgent, callable $path, $fromParent)
285+
private function resolveWideKeywords(array $inherited, array $userAgent, callable $path, $fromParent, $parentProperties = null)
281286
{
282287
$parent = null;
283288
$sides = [];
@@ -310,7 +315,7 @@ private function resolveWideKeywords(array $inherited, array $userAgent, callabl
310315
$resolved = $inherited[$property];
311316
} elseif ($keyword === 'inherit' && (!$isInherited || $fromParent)) {
312317
if ($parent === null) {
313-
$parent = $this->parentComputed($path);
318+
$parent = $parentProperties !== null ? $parentProperties : $this->parentComputed($path);
314319
}
315320
$resolved = $this->declaredValue($parent, $property);
316321
if ($resolved === null && !$isInherited) {
@@ -433,7 +438,8 @@ private function initialValue($property)
433438
* html's font size, read against the default font size, is the root's, which rem and body's own size are read
434439
* against
435440
*
436-
* @return array[] The !important declarations of body's rules, which apply after body's style
441+
* @return array[] The !important declarations of body's rules, which apply after body's style, and the properties
442+
* html's rules give
437443
*/
438444
private function mergeDocumentRules()
439445
{
@@ -442,6 +448,7 @@ private function mergeDocumentRules()
442448
$initial = $this->mpdf->initial_font_size;
443449
$this->mpdf->root_font_size = $initial;
444450

451+
$htmlProperties = [];
445452
list($html, $importantHtml) = $rules->documentDeclarations(true, $path);
446453
foreach (array_merge($html, $importantHtml) as $properties) {
447454
if (isset($properties['FONT-SIZE'])) {
@@ -452,12 +459,13 @@ private function mergeDocumentRules()
452459
}
453460
}
454461
$this->setMergedCss($properties, false);
462+
$htmlProperties = array_merge($htmlProperties, $properties);
455463
}
456464

457465
list($body, $importantBody) = $rules->documentDeclarations(false, $path);
458466
$this->mergeEach($body, false);
459467

460-
return $importantBody;
468+
return [$importantBody, $htmlProperties];
461469
}
462470

463471
/**

‎tests/Mpdf/CssWideKeywordTest.php‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,8 @@ public function borderParts()
343343

344344
/**
345345
* inherit reaches through an element that inherits the value itself, and at the top of the document takes the
346-
* value body has. A block takes an inherited value mPDF does not hand it from its parent
346+
* value body has, and on body the value html has. A block takes an inherited value mPDF does not hand it from its
347+
* parent
347348
*
348349
* @return array[]
349350
*/
@@ -385,6 +386,20 @@ public function chains()
385386
'text-shadow: 0.5mm 0.5mm #ff0000',
386387
'',
387388
],
389+
'a colour from html, on body' => [
390+
'html { color: #008000; } body { color: #ff0000; } body { {V} }',
391+
'<p>subject</p>',
392+
'color: inherit',
393+
'color: #008000',
394+
'color: transparent',
395+
],
396+
'a font size from html, on body' => [
397+
'html { font-size: 14pt; } body { font-size: 8pt; } body { {V} }',
398+
'<p>subject</p>',
399+
'font-size: unset',
400+
'font-size: 14pt',
401+
'font-size: auto',
402+
],
388403
'a font size from body' => [
389404
'body { font-size: 14pt; } p { font-size: 8pt; } p.k { {V} }',
390405
'<p class="k">subject</p><p>near miss</p>',

0 commit comments

Comments
 (0)