Skip to content

Commit 2b24398

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. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 4fd4c2b commit 2b24398

13 files changed

Lines changed: 682 additions & 8 deletions

‎CHANGELOG-GRAVITYPDF.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ Bugfixes
194194
* `linear-gradient(to bottom, …)` and `to top` were drawn upside down, and so was the vertical half of `to bottom right` and the other corners. Angles ran counter-clockwise from pointing right, so `90deg` drew bottom to top. A gradient now runs as CSS Images gives: `0deg` points up and angles turn clockwise, `turn` is read, and a corner keyword leaves the other two corners halfway along. `-moz-`, `-webkit-` and `-o-` gradients keep their legacy angles, and their side keyword, such as `left`, names the side the gradient starts from. [#577]
195195
* 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]
196196
* `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]
197+
* 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 every other inherited property, 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 from 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]
197198

198199
### Images and SVG
199200

@@ -833,3 +834,5 @@ These changes do not change output.
833834
[#632]: https://github.com/GravityPDF/mpdf/issues/632
834835
[#539]: https://github.com/GravityPDF/mpdf/issues/539
835836
[#648]: https://github.com/GravityPDF/mpdf/pull/648
837+
[#541]: https://github.com/GravityPDF/mpdf/issues/541
838+
[#653]: https://github.com/GravityPDF/mpdf/pull/653

‎src/Css/CssMerger.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ protected function mergeInheritedBlockProperties($inherit, $tag)
460460
}
461461

462462
if (isset($previousBlock['InlineProperties'])) {
463-
$saved = $previousBlock['InlineProperties'];
463+
$saved = $this->mpdf->inheritedTextState($previousBlockLevel);
464464
if ($this->mpdf->cssMode === CssMode::LEGACY) {
465465
// mPDF v7 did not hand a block's text shadow on to its child blocks
466466
unset($saved['textshadow']);

‎src/Mpdf.php‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7158,6 +7158,25 @@ function saveInlineProperties()
71587158
return $saved;
71597159
}
71607160

7161+
/**
7162+
* The text state a block opened inside the block at a level inherits: that of the inline elements it opened in,
7163+
* which BlockTag sets aside on the enclosing block under CssMode::STANDARD, or else the enclosing block's own
7164+
*
7165+
* @internal
7166+
*
7167+
* @param int $level
7168+
*
7169+
* @return array|null As saveInlineProperties() saves it, or null when the block at that level has none
7170+
*/
7171+
public function inheritedTextState($level)
7172+
{
7173+
if (isset($this->blk[$level]['openInline'])) {
7174+
return $this->blk[$level]['openInline']['state'];
7175+
}
7176+
7177+
return isset($this->blk[$level]['InlineProperties']) ? $this->blk[$level]['InlineProperties'] : null;
7178+
}
7179+
71617180
function restoreInlineProperties(&$saved)
71627181
{
71637182
$FontFamily = $saved['family'];
@@ -19178,8 +19197,9 @@ function setCSS($arrayaux, $type = '', $tag = '')
1917819197
$v = $arrayaux['FONT-SIZE'];
1917919198
$firstLetter = substr($v, 0, 1);
1918019199
if (is_numeric($firstLetter) || ($firstLetter === '.')) {
19181-
if ($type == 'BLOCK' && $this->blklvl > 0 && isset($this->blk[$this->blklvl - 1]['InlineProperties']) && isset($this->blk[$this->blklvl - 1]['InlineProperties']['size'])) {
19182-
$mmsize = $this->sizeConverter->convert($v, $this->blk[$this->blklvl - 1]['InlineProperties']['size']);
19200+
$inherited = ($type == 'BLOCK' && $this->blklvl > 0) ? $this->inheritedTextState($this->blklvl - 1) : null;
19201+
if (isset($inherited['size'])) {
19202+
$mmsize = $this->sizeConverter->convert($v, $inherited['size']);
1918319203
} elseif ($type == 'TABLECELL') {
1918419204
$mmsize = $this->sizeConverter->convert($v, $this->default_font_size / Mpdf::SCALE);
1918519205
} else {

‎src/Tag/BlockTag.php‎

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Mpdf\Conversion\DecToHebrew;
99
use Mpdf\Conversion\DecToOther;
1010
use Mpdf\Conversion\DecToRoman;
11+
use Mpdf\CssMode;
1112
use Mpdf\Mpdf;
1213
use Mpdf\Shaper\OtlData;
1314
use Mpdf\Utils\Arrays;
@@ -112,7 +113,11 @@ public function open($attr, &$ahtml, &$ihtml)
112113

113114
$this->mpdf->InlineBDF = []; // mPDF 6
114115
$this->mpdf->InlineBDFctr = 0; // mPDF 6
115-
$this->mpdf->InlineProperties = [];
116+
if ($this->mpdf->cssMode === CssMode::LEGACY) {
117+
$this->mpdf->InlineProperties = [];
118+
} elseif (!$this->mpdf->tableLevel) {
119+
$this->setOpenInlineElementsAside();
120+
}
116121
$this->mpdf->divbegin = true;
117122

118123
$this->mpdf->linebreakjustfinished = false;
@@ -539,6 +544,11 @@ public function open($attr, &$ahtml, &$ihtml)
539544
}
540545

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

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

13991409
$this->mpdf->lastblocklevelchange = -1;
14001410
// Reset Inline-type properties
1401-
if (isset($this->mpdf->blk[$this->mpdf->blklvl]['InlineProperties'])) {
1411+
if (isset($this->mpdf->blk[$this->mpdf->blklvl]['openInline'])) {
1412+
$this->putBackOpenInlineElements();
1413+
} elseif (isset($this->mpdf->blk[$this->mpdf->blklvl]['InlineProperties'])) {
14021414
$this->mpdf->restoreInlineProperties($this->mpdf->blk[$this->mpdf->blklvl]['InlineProperties']);
14031415
}
14041416

@@ -1423,4 +1435,45 @@ public function close(&$ahtml, &$ihtml)
14231435
}
14241436
}
14251437

1438+
/**
1439+
* Sets aside the inline elements a block opens inside, under CssMode::STANDARD. Their saved states and the text
1440+
* state they set go on the enclosing block: the block inherits that text state, and close() puts both back, so the
1441+
* text after the block is read in them and their end tags restore what was there before them.
1442+
*
1443+
* A block opened again finds them already set aside, put back with the rest of the state: a kept block laid out
1444+
* again after it unwinds, and a block reopened after a forced page break, by when the page break has changed the
1445+
* text state
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 drawn next, and its text that sets no colour or link of its own takes the
1458+
// state it is drawn in
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)