Skip to content

Commit 5f7821c

Browse files
Let a table inside an inline element inherit its style, as a block does
Under the standard cssMode, a table opened inside open inline elements, as in <span class="note">…<table>…</table>…</span>, sets their state aside on the enclosing block as #653 does for a block, so it inherits their colour, font, size and weight, and a relative font size on it is taken of theirs. The text after the table is drawn in their style again until their end tags. A table nested in a cell keeps the cell's open inline elements, so an end tag after it restores what was before. Legacy is unchanged. The set-aside and put-back helpers move from BlockTag to Tag, and the put-back restores the block's own state when nothing was set aside, reading it through InheritedProperties::blockTextState(). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 61eb72f commit 5f7821c

7 files changed

Lines changed: 115 additions & 67 deletions

File tree

‎CHANGELOG-GRAVITYPDF.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ Read this section before upgrading from upstream mPDF. Each entry says what chan
6262
* `revert` and `revert-layer` give the value of the built-in defaults and the default stylesheet, such as a heading's size, a list's margin or a cell's padding, and are `unset` where those give none. An HTML attribute such as `cellpadding` counts as an author rule, so `revert` goes past it too.
6363
* A shorthand passes the keyword to each of its longhands, so `border: inherit` takes every side's width, style and colour, and `font: initial` resets the size, family, style, weight, line height and variants.
6464
* **A table inherits from the block around it**, as in a browser, under the standard `cssMode`. It used to start from the document's defaults, so `<div style="color: red; font-family: monospace">` around a table left its cells black and in the body's font. Now a table takes every inherited property from the block, list item, header or positioned block it is in, and a nested table takes its cell's `text-align` and `line-height` too. [#543] [#656]
65+
* A table inside an inline element, as in `<span class="note">…<table>…</table>…</span>`, takes the element's style, as a block does since [#653]. The text after the table, up to the element's end tag, is drawn in that style again. Before, it lost it, and in a table cell the style ran on past the end tag.
6566
* `table { font-size: 80% }` is taken of the block's size, not the document's.
6667
* The built-in default style no longer gives a table `hyphens: manual` and `font-kerning: auto`, which a browser's does not, so a table inherits them. It keeps the table's `line-height: 1.2` where the block's is `normal`, as tables have always been drawn.
6768
* A `th` in a table in a right-aligned block is aligned right, since it inherits an alignment.

‎src/Css/InheritedProperties.php‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
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;
13+
* - inline elements to a block or table opened inside them: BlockTag and Table set their text state aside on the
14+
* enclosing block, and blockTextState() gives it to mergeInheritedBlockProperties() in place of the enclosing
15+
* block's own;
1516
* - a positioned block to its content: Mpdf::WriteFixedPosHTML() puts TEXT and BLOCK from the block's merged CSS on
1617
* the <div> that stands in for the block;
1718
* - a block to a table in it: CssMerger::mergeInheritedBlockProperties() gives the table TEXT and BLOCK from the block,
@@ -134,8 +135,9 @@ public static function of(array $properties, array $names)
134135
}
135136

136137
/**
137-
* The text state a block opened inside the block at a level inherits: that of the inline elements it opened in,
138-
* which BlockTag sets aside on the enclosing block under CssMode::STANDARD, or else the enclosing block's own
138+
* The text state a block or table opened inside the block at a level inherits: that of the inline elements it
139+
* opened in, which BlockTag and Table set aside on the enclosing block under CssMode::STANDARD, or else the
140+
* enclosing block's own
139141
*
140142
* @param array $blocks The block stack, as Mpdf::$blk
141143
* @param int $level

‎src/Tag/BlockTag.php‎

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1409,11 +1409,7 @@ public function close(&$ahtml, &$ihtml)
14091409

14101410
$this->mpdf->lastblocklevelchange = -1;
14111411
// Reset Inline-type properties
1412-
if (isset($this->mpdf->blk[$this->mpdf->blklvl]['openInline'])) {
1413-
$this->putBackOpenInlineElements();
1414-
} elseif (isset($this->mpdf->blk[$this->mpdf->blklvl]['InlineProperties'])) {
1415-
$this->mpdf->restoreInlineProperties($this->mpdf->blk[$this->mpdf->blklvl]['InlineProperties']);
1416-
}
1412+
$this->restoreBlockTextState();
14171413

14181414
$this->mpdf->x = $this->mpdf->lMargin + $this->mpdf->blk[$this->mpdf->blklvl]['outer_left_margin'];
14191415

@@ -1436,44 +1432,4 @@ public function close(&$ahtml, &$ihtml)
14361432
}
14371433
}
14381434

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-
14791435
}

‎src/Tag/Table.php‎

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,13 @@ public function open($attr, &$ahtml, &$ihtml)
2323
$this->mpdf->colvAlign = '';
2424
} // *COLUMNS*
2525

26-
// The page break comes first, so the blocks it opens again on the new page are started below
2726
if (!$this->mpdf->tableLevel) {
27+
if ($this->mpdf->cssMode === CssMode::STANDARD) {
28+
// It inherits the inline elements it is opened in, as a block does, set aside before the page break
29+
$this->setOpenInlineElementsAside();
30+
}
31+
32+
// The page break comes first, so the blocks it opens again on the new page are started below
2833
$preview = $this->cssManager->PreviewBlockCSS('TABLE', $attr);
2934
if ($this->forcesPageBreak($preview, 'PAGE-BREAK-BEFORE')) {
3035
$this->forcePageBreak(strtoupper($preview['PAGE-BREAK-BEFORE']));
@@ -53,6 +58,10 @@ public function open($attr, &$ahtml, &$ihtml)
5358
if ($this->mpdf->tableLevel) { // i.e. now a nested table coming...
5459
// Save current level table
5560
$this->mpdf->cell['PARENTCELL'] = $this->mpdf->saveInlineProperties();
61+
if ($this->mpdf->cssMode === CssMode::STANDARD) {
62+
// The inline elements open in the cell, whose end tags after the nested table restore what was before them
63+
$this->mpdf->cell['PARENTINLINE'] = $this->mpdf->InlineProperties;
64+
}
5665
$this->mpdf->table[$this->mpdf->tableLevel][$this->mpdf->tbctr[$this->mpdf->tableLevel]]['baseProperties'] = $this->mpdf->base_table_properties;
5766
$this->mpdf->table[$this->mpdf->tableLevel][$this->mpdf->tbctr[$this->mpdf->tableLevel]]['cells'] = $this->mpdf->cell;
5867
$this->mpdf->table[$this->mpdf->tableLevel][$this->mpdf->tbctr[$this->mpdf->tableLevel]]['currrow'] = $this->mpdf->row;
@@ -76,12 +85,14 @@ public function open($attr, &$ahtml, &$ihtml)
7685
$cellLineHeight = $this->mpdf->cell[$this->mpdf->row][$this->mpdf->col]['cellLineHeight'];
7786
$cellLineStackingStrategy = $this->mpdf->cell[$this->mpdf->row][$this->mpdf->col]['cellLineStackingStrategy'];
7887
$cellLineStackingShift = $this->mpdf->cell[$this->mpdf->row][$this->mpdf->col]['cellLineStackingShift'];
79-
// Under the standard cascade it inherits the cell's line-height and text-align through its merged CSS too
80-
$outerTable = $this->mpdf->table[$this->mpdf->tableLevel - 1][$this->mpdf->tbctr[$this->mpdf->tableLevel - 1]];
81-
$cellInherited = array_filter([
82-
'LINE-HEIGHT' => $cellLineHeight,
83-
'TEXT-ALIGN' => isset($outerTable['cellTextAlign']) ? $outerTable['cellTextAlign'] : '',
84-
]);
88+
if ($this->mpdf->cssMode === CssMode::STANDARD) {
89+
// It inherits the cell's line-height and text-align through its merged CSS too
90+
$outerTable = $this->mpdf->table[$this->mpdf->tableLevel - 1][$this->mpdf->tbctr[$this->mpdf->tableLevel - 1]];
91+
$cellInherited = array_filter([
92+
'LINE-HEIGHT' => $cellLineHeight,
93+
'TEXT-ALIGN' => isset($outerTable['cellTextAlign']) ? $outerTable['cellTextAlign'] : '',
94+
]);
95+
}
8596
}
8697

8798
if (isset($this->mpdf->tbctr[$this->mpdf->tableLevel])) {
@@ -178,7 +189,7 @@ public function open($attr, &$ahtml, &$ihtml)
178189
if ($this->cssManager->tbCSSlvl == 1) {
179190
$properties = $this->cssManager->MergeCSS('TOPTABLE', 'TABLE', $attr);
180191
} else {
181-
$properties = $this->cssManager->MergeCSS('TABLE', 'TABLE', $attr, $this->mpdf->cssMode === CssMode::STANDARD ? $cellInherited : []);
192+
$properties = $this->cssManager->MergeCSS('TABLE', 'TABLE', $attr, $cellInherited);
182193
}
183194

184195
$w = '';
@@ -314,12 +325,12 @@ public function open($attr, &$ahtml, &$ihtml)
314325
$this->mpdf->base_table_properties['FONT-FAMILY'] = $this->mpdf->FontFamily;
315326

316327
if (isset($properties['FONT-SIZE'])) {
317-
$block = $this->mpdf->blk[$this->mpdf->blklvl];
328+
$inheritedState = InheritedProperties::blockTextState($this->mpdf->blk, $this->mpdf->blklvl);
318329
if ($this->mpdf->tableLevel > 1) {
319330
$parentSize = $this->sizeConverter->convert($this->mpdf->base_table_properties['FONT-SIZE']);
320-
} elseif ($this->mpdf->cssMode === CssMode::STANDARD && isset($block['InlineProperties']['size'])) {
321-
// Its size is relative to the block's, as the size it inherits is the block's
322-
$parentSize = $block['InlineProperties']['size'];
331+
} elseif ($this->mpdf->cssMode === CssMode::STANDARD && isset($inheritedState['size'])) {
332+
// Its size is relative to the size it inherits, the block's or the inline elements' it is opened in
333+
$parentSize = $inheritedState['size'];
323334
} else {
324335
$parentSize = $this->mpdf->default_font_size / Mpdf::SCALE;
325336
}
@@ -741,6 +752,10 @@ public function close(&$ahtml, &$ihtml)
741752
}
742753
unset($this->mpdf->cell['PARENTCELL']);
743754
}
755+
if (isset($this->mpdf->cell['PARENTINLINE'])) {
756+
$this->mpdf->InlineProperties = $this->mpdf->cell['PARENTINLINE'];
757+
unset($this->mpdf->cell['PARENTINLINE']);
758+
}
744759
$this->mpdf->row = $this->mpdf->table[$this->mpdf->tableLevel][$this->mpdf->tbctr[$this->mpdf->tableLevel]]['currrow'];
745760
$this->mpdf->col = $this->mpdf->table[$this->mpdf->tableLevel][$this->mpdf->tbctr[$this->mpdf->tableLevel]]['currcol'];
746761
$objattr = [];
@@ -1259,9 +1274,7 @@ public function close(&$ahtml, &$ihtml)
12591274
$this->mpdf->SetFont($this->mpdf->default_font, '', 0, false);
12601275
$this->mpdf->SetLineHeight();
12611276

1262-
if (isset($this->mpdf->blk[$this->mpdf->blklvl]['InlineProperties'])) {
1263-
$this->mpdf->restoreInlineProperties($this->mpdf->blk[$this->mpdf->blklvl]['InlineProperties']);
1264-
}
1277+
$this->restoreBlockTextState();
12651278

12661279
if ($page_break_after) {
12671280
$this->forcePageBreak($page_break_after);

‎src/Tag/Tag.php‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,4 +318,50 @@ abstract public function open($attr, &$ahtml, &$ihtml);
318318

319319
abstract public function close(&$ahtml, &$ihtml);
320320

321+
/**
322+
* Sets aside the inline elements a block or table opens inside, under CssMode::STANDARD: their saved states and the
323+
* text state they set go on the enclosing block. The block or table inherits that text state, and
324+
* restoreBlockTextState() puts both back when it closes, so the text after it is drawn in their style and their end tags restore what was there before them.
325+
*
326+
* A block opened a second time, as a kept block laid out again or a block reopened after a forced page break, finds
327+
* them already set aside and keeps them, as by then the text state has changed
328+
*/
329+
protected function setOpenInlineElementsAside()
330+
{
331+
$block = &$this->mpdf->blk[$this->mpdf->blklvl];
332+
333+
if (!isset($block['openInline']) && array_filter($this->mpdf->InlineProperties)) {
334+
$block['openInline'] = [
335+
'properties' => $this->mpdf->InlineProperties,
336+
'state' => $this->mpdf->saveInlineProperties(),
337+
];
338+
339+
// The line before the block or table is printed next, and its text with no colour or link of its own is drawn in the
340+
// current state
341+
if (isset($block['InlineProperties'])) {
342+
$this->mpdf->restoreInlineProperties($block['InlineProperties']);
343+
}
344+
}
345+
346+
$this->mpdf->InlineProperties = [];
347+
}
348+
349+
/**
350+
* Restores, when a block or table closes, the text state of the block that is current again: that of the inline
351+
* elements setOpenInlineElementsAside() set aside on it, which are put back, or else the block's own
352+
*/
353+
protected function restoreBlockTextState()
354+
{
355+
$state = InheritedProperties::blockTextState($this->mpdf->blk, $this->mpdf->blklvl);
356+
if ($state !== null) {
357+
$this->mpdf->restoreInlineProperties($state);
358+
}
359+
360+
$block = &$this->mpdf->blk[$this->mpdf->blklvl];
361+
if (isset($block['openInline'])) {
362+
$this->mpdf->InlineProperties = $block['openInline']['properties'];
363+
unset($block['openInline']);
364+
}
365+
}
366+
321367
}

‎tests/Mpdf/BlockInsideInlineTest.php‎

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
use Yoast\PHPUnitPolyfills\TestCases\TestCase;
66

77
/**
8-
* A block opened inside inline elements inherits their state, and the text after the block is read in their state
9-
* again, until their end tags. A link around a block covers the block's text. Under legacy the block starts from the
10-
* enclosing block's state, and so does the text after it (#541).
8+
* A block or table opened inside inline elements inherits their state, and the text after it is read in their state
9+
* again, until their end tags. A link around a block covers the block's text. Under legacy the block or table starts
10+
* from the enclosing block's state, and so does the text after it (#541, #543).
1111
*/
1212
class BlockInsideInlineTest extends TestCase
1313
{
@@ -191,6 +191,35 @@ public function documents()
191191
$loses,
192192
['xx' => 1, 'qq' => 2],
193193
],
194+
'a table in a span' => [
195+
'<span class="a">xx<table><tr><td>qq</td></tr></table>yy</span>zz',
196+
$inherits,
197+
$loses,
198+
],
199+
'a table in a span in a block' => [
200+
'<div class="o"><span class="a">xx<table><tr><td>qq</td></tr></table>yy</span>zz</div>',
201+
['xx' => $g, 'qq' => $g, 'yy' => $g, 'zz' => $b],
202+
// Legacy starts the table from the document's defaults
203+
['xx' => $g, 'qq' => $p, 'yy' => $b, 'zz' => $b],
204+
],
205+
'a table in a span in a cell' => [
206+
$this->inContext('table cell', '<span class="a">xx<table><tr><td>qq</td></tr></table>yy</span>zz'),
207+
$inherits,
208+
// Legacy: the nested table takes the cell's colour but not its weight
209+
['xx' => $g, 'qq' => ['0.000 1.000 0.000 rg', ''], 'yy' => $g, 'zz' => $g],
210+
],
211+
'a table in a span in a kept block that is laid out again on the next page' => [
212+
self::FILLER . '<div style="page-break-inside: avoid"><p>ww</p><span class="a">xx<table><tr><td>qq</td></tr></table>yy</span>zz</div>',
213+
$inherits,
214+
$loses,
215+
['ww' => 2, 'qq' => 2],
216+
],
217+
'the table in a span breaks the page before it' => [
218+
'<span class="a">xx<table style="page-break-before: always"><tr><td>qq</td></tr></table>yy</span>zz',
219+
$inherits,
220+
$loses,
221+
['xx' => 1, 'qq' => 2],
222+
],
194223
'the block in a span breaks the page after it' => [
195224
'<span class="a">xx<div style="page-break-after: always">qq</div>yy</span>zz',
196225
$inherits,

‎tests/Mpdf/TableInheritsFromBlockTest.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ public function fontSizes()
146146
'ems of the block' => ['<div style="font-size: 20pt"><table style="font-size: 1.5em"><tr><td>qq</td></tr></table></div>', 30.0, 16.5],
147147
'a percentage of the list item' => ['<ul><li style="font-size: 20pt"><table style="font-size: 50%"><tr><td>qq</td></tr></table></li></ul>', 10.0, 5.5],
148148
'a percentage of the cell' => ['<table><tr><td style="font-size: 20pt"><table style="font-size: 50%"><tr><td>qq</td></tr></table></td></tr></table>', 10.0, 5.5],
149+
'a percentage of a span' => ['<span style="font-size: 20pt">zz<table style="font-size: 50%"><tr><td>qq</td></tr></table></span>', 10.0, 5.5],
149150
'the block\'s size, inherited' => ['<div style="font-size: 20pt"><table><tr><td>qq</td></tr></table></div>', 20.0, 11.0],
150151
'rem in a cell of a sized table' => ['<table style="font-size: 20pt"><tr><td style="font-size: 1.5rem">qq</td></tr></table>', 16.5, 30.0],
151152
'rem in a table in a sized block' => ['<div style="font-size: 20pt"><table><tr><td style="font-size: 1rem">qq</td></tr></table></div>', 11.0, 11.0],

0 commit comments

Comments
 (0)