Skip to content

Commit c31c042

Browse files
jakejackson1claude
andcommitted
Let a block inside an inline element inherit its style, and draw the text after the block in it again
Under the standard cssMode, a block opened inside open inline elements, as in <span class="note">…<div>…</div>…</span> or a card written as <a href="…"><div>…</div></a>, inherits their text state, and a link around it covers its text. The inline elements' saved states are set aside on the enclosing block while the block is open and put back when it closes, so the text after the block is drawn in their style and their end tags restore what was there before them. Kept blocks laid out again and blocks reopened after a forced page break find them set aside already. Legacy is unchanged. InheritedProperties::blockTextState() gives the state a block inherits, and every reader of it goes through it: the inherited properties, an em or % font size, the weight bolder and lighter step from, and currentColor. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent a3939ee commit c31c042

16 files changed

Lines changed: 742 additions & 17 deletions

‎CHANGELOG-GRAVITYPDF.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ Bugfixes
241241
* Characters mPDF moved into a substitute font (`useSubstitutions`, and the fonts for supplementary planes) were wrapped in a `<span>` that stylesheet rules reached, so `span { border: 1px solid }` drew a box around them, and the span counted among its siblings for `:nth-child()`. In the standard CSS mode no rule reaches that span and it is not counted, as a browser has no such element. The legacy mode styles it as before. [#634]
242242
* Some inherited properties stopped short of where a browser takes them. A `text-shadow` did not reach a block's child blocks or list items. `word-spacing`, `hyphens` and `text-outline` did not reach the content of a positioned block. `text-transform`, `text-shadow`, `font-variant` and the other font features did not reach a table's cells. A table nested in a cell took none of the cell's colour, font, size, weight, style, transform or shadow. Each of these channels now carries every inherited property mPDF supports, from the one list in `Mpdf\Css\InheritedProperties`. Under the standard `cssMode` only; set `'cssMode' => \Mpdf\CssMode::LEGACY` to keep the old output. [#539] [#648]
243243
* `text-transform: none` on an inline element, such as a `<span>` in an uppercase paragraph, did not undo the transform it inherits. Under the standard `cssMode` only; set `'cssMode' => \Mpdf\CssMode::LEGACY` to keep the transform. [#552] [#539] [#648]
244+
* A block inside an inline element, such as `<span class="note">…<div>…</div>…</span>` or a card written as `<a href="…"><div>…</div></a>`, lost the inline element's colour, font and other inherited text properties, and so did the inline element's text after the block. A link lost its colour and underline on the block's text and did not cover it. In a table cell the block kept the style, but the text after the inline element did too. A block now inherits the text properties of the inline elements it opens in, and the text after it is drawn in their style until they end. Text before the inline element, on the line before the block, is no longer drawn in the inline element's colour or covered by its link. Under the standard `cssMode` only; set `'cssMode' => \Mpdf\CssMode::LEGACY` to keep the old output. [#541] [#653]
244245

245246
### Images and SVG
246247

@@ -912,3 +913,5 @@ These changes do not change output.
912913
[#652]: https://github.com/GravityPDF/mpdf/pull/652
913914
[#674]: https://github.com/GravityPDF/mpdf/pull/674
914915
[#675]: https://github.com/GravityPDF/mpdf/pull/675
916+
[#541]: https://github.com/GravityPDF/mpdf/issues/541
917+
[#653]: https://github.com/GravityPDF/mpdf/pull/653

‎src/Css/CssMerger.php‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,8 @@ private function elementColor($inherit)
440440

441441
/**
442442
* The colour an element takes when it sets none, as mPDF passes it on in each context: a block takes the colour of
443-
* the block it is opened in, a table and its parts the table's, and anything else the colour of the text around it
443+
* the block or inline elements it is opened in, a table and its parts the table's, and anything else the colour of
444+
* the text around it
444445
*
445446
* @param string $inherit Inheritance context
446447
* @return string|null Null for the document's default colour
@@ -452,8 +453,8 @@ private function inheritedColor($inherit)
452453
}
453454

454455
if ($inherit === 'BLOCK') {
455-
$level = $this->getBlockLevel();
456-
$colorarray = isset($this->mpdf->blk[$level]['InlineProperties']['colorarray']) ? $this->mpdf->blk[$level]['InlineProperties']['colorarray'] : '';
456+
$saved = InheritedProperties::blockTextState($this->mpdf->blk, $this->getBlockLevel());
457+
$colorarray = isset($saved['colorarray']) ? $saved['colorarray'] : '';
457458
} else {
458459
$colorarray = $this->mpdf->colorarray;
459460
}
@@ -813,8 +814,8 @@ protected function mergeInheritedBlockProperties($inherit, $tag)
813814
$this->cssProperties['TEXT-INDENT'] = $previousBlock['text_indent'];
814815
}
815816

816-
if (isset($previousBlock['InlineProperties'])) {
817-
$saved = $previousBlock['InlineProperties'];
817+
$saved = InheritedProperties::blockTextState($this->mpdf->blk, $previousBlockLevel);
818+
if ($saved !== null) {
818819
if ($this->mpdf->cssMode === CssMode::LEGACY) {
819820
// mPDF v7 did not hand a block's text shadow on to its child blocks
820821
unset($saved['textshadow']);

‎src/Css/InheritedProperties.php‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
*
1111
* - a block to its child blocks: CssMerger::mergeInheritedBlockProperties() reads TEXT back from the block's saved
1212
* text state through InlinePropertyConverter, and BLOCK from the block's level of the block stack;
13+
* - inline elements to a block opened inside them: BlockTag sets their text state aside on the enclosing block, and
14+
* blockTextState() gives it to mergeInheritedBlockProperties() in place of the enclosing block's own;
1315
* - a positioned block to its content: Mpdf::WriteFixedPosHTML() puts TEXT and BLOCK from the block's merged CSS on
1416
* the <div> that stands in for the block;
1517
* - a table to its cells: Table::open() puts TEXT from the table's merged CSS in base_table_properties, which each
@@ -125,4 +127,22 @@ public static function of(array $properties, array $names)
125127

126128
return $inherited;
127129
}
130+
131+
/**
132+
* The text state a block opened inside the block at a level inherits: that of the inline elements it opened in,
133+
* which BlockTag sets aside on the enclosing block under CssMode::STANDARD, or else the enclosing block's own
134+
*
135+
* @param array $blocks The block stack, as Mpdf::$blk
136+
* @param int $level
137+
*
138+
* @return array|null As Mpdf::saveInlineProperties() saves it, or null when the block at that level has none
139+
*/
140+
public static function blockTextState(array $blocks, $level)
141+
{
142+
if (isset($blocks[$level]['openInline'])) {
143+
return $blocks[$level]['openInline']['state'];
144+
}
145+
146+
return isset($blocks[$level]['InlineProperties']) ? $blocks[$level]['InlineProperties'] : null;
147+
}
128148
}

‎src/Css/RelativeFontValues.php‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ public static function weightForStyle($weight, $bold)
103103

104104
/**
105105
* The computed font-weight of the parent of the element setCSS() is styling, which bolder and lighter step from. A
106-
* block starts from the state its parent block saved, and a table cell from its table's weight; an inline element
107-
* is styled in its parent's state
106+
* block starts from the state it inherits, and a table cell from its table's weight; an inline element is styled in
107+
* its parent's state
108108
*
109109
* @param Mpdf $mpdf
110110
* @param string $type As setCSS() takes it
@@ -113,8 +113,9 @@ public static function weightForStyle($weight, $bold)
113113
*/
114114
public static function parentWeight(Mpdf $mpdf, $type)
115115
{
116-
if ($type === 'BLOCK' && $mpdf->blklvl > 0 && isset($mpdf->blk[$mpdf->blklvl - 1]['InlineProperties']['weight'])) {
117-
return $mpdf->blk[$mpdf->blklvl - 1]['InlineProperties']['weight'];
116+
$inherited = $type === 'BLOCK' ? InheritedProperties::blockTextState($mpdf->blk, $mpdf->blklvl - 1) : null;
117+
if (isset($inherited['weight'])) {
118+
return $inherited['weight'];
118119
}
119120

120121
if ($type === 'TABLECELL') {

‎src/Mpdf.php‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19272,10 +19272,11 @@ function setCSS($arrayaux, $type = '', $tag = '')
1927219272
if (isset($arrayaux['FONT-SIZE'])) {
1927319273
// The standard CSS mode reads body's size against html's, which the merge left in root_font_size
1927419274
$standardBody = $tag == 'BODY' && $this->cssMode === CssMode::STANDARD;
19275+
$inherited = $type == 'BLOCK' ? InheritedProperties::blockTextState($this->blk, $this->blklvl - 1) : null;
1927519276
if ($standardBody) {
1927619277
$parentSize = $this->root_font_size / Mpdf::SCALE;
19277-
} elseif ($type == 'BLOCK' && $this->blklvl > 0 && isset($this->blk[$this->blklvl - 1]['InlineProperties']) && isset($this->blk[$this->blklvl - 1]['InlineProperties']['size'])) {
19278-
$parentSize = $this->blk[$this->blklvl - 1]['InlineProperties']['size'];
19278+
} elseif (isset($inherited['size'])) {
19279+
$parentSize = $inherited['size'];
1927919280
} elseif ($type == 'TABLECELL') {
1928019281
$parentSize = $this->default_font_size / Mpdf::SCALE;
1928119282
} else {

‎src/Tag/BlockTag.php‎

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use Mpdf\Conversion\DecToHebrew;
99
use Mpdf\Conversion\DecToOther;
1010
use Mpdf\Conversion\DecToRoman;
11+
use Mpdf\Css\InheritedProperties;
12+
use Mpdf\CssMode;
1113
use Mpdf\Mpdf;
1214
use Mpdf\Shaper\OtlData;
1315
use Mpdf\Utils\Arrays;
@@ -112,7 +114,11 @@ public function open($attr, &$ahtml, &$ihtml)
112114

113115
$this->mpdf->InlineBDF = []; // mPDF 6
114116
$this->mpdf->InlineBDFctr = 0; // mPDF 6
115-
$this->mpdf->InlineProperties = [];
117+
if ($this->mpdf->cssMode === CssMode::LEGACY) {
118+
$this->mpdf->InlineProperties = [];
119+
} elseif (!$this->mpdf->tableLevel) {
120+
$this->setOpenInlineElementsAside();
121+
}
116122
$this->mpdf->divbegin = true;
117123

118124
$this->mpdf->linebreakjustfinished = false;
@@ -539,6 +545,11 @@ public function open($attr, &$ahtml, &$ihtml)
539545
}
540546

541547
$this->mpdf->setCSS($properties, 'BLOCK', $tag); //name(id/class/style) found in the CSS array!
548+
if ($this->mpdf->cssMode !== CssMode::LEGACY) {
549+
// A link around the block covers its text
550+
$inherited = InheritedProperties::blockTextState($this->mpdf->blk, $this->mpdf->blklvl - 1);
551+
$this->mpdf->HREF = isset($inherited['HREF']) ? $inherited['HREF'] : '';
552+
}
542553
$currblk['InlineProperties'] = $this->mpdf->saveInlineProperties();
543554

544555
if (isset($properties['VISIBILITY'])) {
@@ -1398,7 +1409,9 @@ public function close(&$ahtml, &$ihtml)
13981409

13991410
$this->mpdf->lastblocklevelchange = -1;
14001411
// Reset Inline-type properties
1401-
if (isset($this->mpdf->blk[$this->mpdf->blklvl]['InlineProperties'])) {
1412+
if (isset($this->mpdf->blk[$this->mpdf->blklvl]['openInline'])) {
1413+
$this->putBackOpenInlineElements();
1414+
} elseif (isset($this->mpdf->blk[$this->mpdf->blklvl]['InlineProperties'])) {
14021415
$this->mpdf->restoreInlineProperties($this->mpdf->blk[$this->mpdf->blklvl]['InlineProperties']);
14031416
}
14041417

@@ -1423,4 +1436,44 @@ public function close(&$ahtml, &$ihtml)
14231436
}
14241437
}
14251438

1439+
/**
1440+
* Sets aside the inline elements a block opens inside, under CssMode::STANDARD: their saved states and the text
1441+
* state they set go on the enclosing block. The block inherits that text state, and close() puts both back, so the
1442+
* text after the block is drawn in their style and their end tags restore what was there before them.
1443+
*
1444+
* A block opened a second time, as a kept block laid out again or a block reopened after a forced page break, finds
1445+
* them already set aside and keeps them, as by then the text state has changed
1446+
*/
1447+
private function setOpenInlineElementsAside()
1448+
{
1449+
$block = &$this->mpdf->blk[$this->mpdf->blklvl];
1450+
1451+
if (!isset($block['openInline']) && array_filter($this->mpdf->InlineProperties)) {
1452+
$block['openInline'] = [
1453+
'properties' => $this->mpdf->InlineProperties,
1454+
'state' => $this->mpdf->saveInlineProperties(),
1455+
];
1456+
1457+
// The line before the block is printed next, and its text with no colour or link of its own is drawn in the
1458+
// current state
1459+
if (isset($block['InlineProperties'])) {
1460+
$this->mpdf->restoreInlineProperties($block['InlineProperties']);
1461+
}
1462+
}
1463+
1464+
$this->mpdf->InlineProperties = [];
1465+
}
1466+
1467+
/**
1468+
* Puts back the inline elements setOpenInlineElementsAside() set aside on the block that is current again
1469+
*/
1470+
private function putBackOpenInlineElements()
1471+
{
1472+
$block = &$this->mpdf->blk[$this->mpdf->blklvl];
1473+
1474+
$this->mpdf->InlineProperties = $block['openInline']['properties'];
1475+
$this->mpdf->restoreInlineProperties($block['openInline']['state']);
1476+
unset($block['openInline']);
1477+
}
1478+
14261479
}

0 commit comments

Comments
 (0)