Skip to content

Commit c7feafa

Browse files
jakejackson1claude
andauthored
Render the new selectors and shorthand resets as before in cssMode legacy (#633)
* Render the new selectors and shorthand resets as before in cssMode legacy Legacy mode compiles no rule, so a rule whose selector mPDF v7 could not read is dropped, as it was before the selector matcher: child and sibling combinators, structural pseudo-classes outside tables, attribute selectors other than [lang], :lang() through an ancestor, descendant rules through inline ancestors, and :not(), :is() and :where(). The font, border and background shorthands leave the longhands they do not name. With nothing compiled in legacy mode, the matching of legacy rules through the ancestors the legacy engine skips is gone: the legacy flag on compiled rules, the rule set's legacy index, the matcher's legacy view and the level mark on open elements. Fixes to how values and selectors are read apply in both modes. Rendered in legacy mode, 137 of the 142 snapshot documents match the tree before the matcher (258839c) byte for byte; the other five differ by the shorthand resets, the small-caps fix of #581, the selector list split of #619 and the cell count of #629. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Make the kept-block cascade context unwind and check that it does The kept-block context wrote so much filler that its block started on the second page and was never put back, so it did not test a block laid out twice. The filler is now a fixed height that leaves the block starting on the first page and running over it. Every case in the cascade and legacy-mode context tests now asserts where its text is drawn: put back once and drawn on the second page for the kept block, on the second page after the forced page break, and in the header, the footer or the positioned block for those. TextRecordingMpdf counts unwinds and records which of those it is drawing in. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2 parents e685435 + f1b1c53 commit c7feafa

39 files changed

Lines changed: 641 additions & 459 deletions

‎CHANGELOG-GRAVITYPDF.md‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Read this section before upgrading from upstream mPDF. Each entry says what chan
2929
* **`<annotation file="...">` is off by default.** Set `allowHtmlAnnotationFiles` to `true` to read the `file` attribute. [#375]
3030
* **A `<select>` without `size` is written as a combo box**, as browsers draw it. [#419]
3131
* **A `<select multiple>` without `size` is four rows tall instead of one**, as in browsers. Content after it on the page moves down. Add `size="1"` to keep the old height. [#437]
32-
* **The `border` and `background` shorthands reset the longhands they do not name**, as in a browser. [#552] [#614]
32+
* **The `border` and `background` shorthands reset the longhands they do not name**, as in a browser, in standard mode. [#552] [#614]
3333
* `border`, or one side such as `border-top`, sets the width, style and colour of each side it covers. `p { border-top-color: red } p { border: 1px solid blue }` used to draw a red top and now draws a blue one.
3434
* `background` sets the colour, image, repeat, position, size, origin and clip. A `background-position`, `-repeat` or `-size` given before a `background` no longer applies to its image.
3535
* To keep the old look, restate the longhand after the shorthand, in the same rule or a later one.
@@ -41,6 +41,7 @@ Read this section before upgrading from upstream mPDF. Each entry says what chan
4141
* Each element takes, in order: the values it inherits; the built-in defaults and the rules of the default stylesheet (`defaultCssFile`), which any author rule beats; HTML attributes such as `<hr color>`, `width` and `vspace`, as author rules of no specificity; the stylesheets; then `style=""`.
4242
* Class and id names still match whatever their case.
4343
* An author's `a { }` rule now reaches the links of the table of contents and the index too. Style `a.mpdf_toc_a` and `a.mpdf_index_link` to keep them plain.
44+
* In legacy mode, a rule whose selector mPDF v7 could not read is dropped: child and sibling combinators, structural pseudo-classes outside tables, attribute selectors other than `[lang]`, `:lang()` through an ancestor, descendant rules through inline ancestors, and `:not()`, `:is()` and `:where()`. The `font`, `border` and `background` shorthands leave the longhands they do not name, as mPDF v7 did. Fixes to how values and selectors are read apply in both modes. [#633]
4445

4546
New features
4647
------------
@@ -176,7 +177,7 @@ Bugfixes
176177
* With several layers, mPDF still draws only the first, now over the colour given in the last.
177178
* A `border` or `background` shorthand with a part that is none of its parts, such as `border: 1px solid bogus`, is dropped, as a browser drops it, so the value it would have replaced still applies. `border: 1px solid bogus` used to draw a black border. [#552] [#583]
178179
* `tr:nth-child()` counted the rows of the whole table less its header and footer rows, so a second `<tbody>` carried on the count of the first, and `tr:first-child` missed the first footer row when `<tfoot>` came after the body. Rows are now counted within their `<thead>`, `<tbody>` or `<tfoot>`, or within the run of rows written straight into the table. [#528] [#582]
179-
* `font: small-caps 14pt serif` drew the text in full-size capitals, as `text-transform: uppercase` does. It is now drawn in small capitals, as `font-variant: small-caps` draws it. The `font` shorthand also kept a `line-height` and a `font-variant` it inherited; like the style and weight, they are now reset to `normal` when the shorthand does not name them, as in a browser. To keep a line height, give it in the shorthand, as in `font: 14pt/1.5 serif`, or declare `line-height` after it. A `font` shorthand with a negative line height, such as `font: 12pt/-1 serif`, is dropped whole. `font-variant: normal` now resets `font-variant-position` as well. [#552] [#581]
180+
* `font: small-caps 14pt serif` drew the text in full-size capitals, as `text-transform: uppercase` does. It is now drawn in small capitals, as `font-variant: small-caps` draws it. The `font` shorthand also kept a `line-height` and a `font-variant` it inherited; like the style and weight, in standard mode they are now reset to `normal` when the shorthand does not name them, as in a browser. To keep a line height, give it in the shorthand, as in `font: 14pt/1.5 serif`, or declare `line-height` after it. A `font` shorthand with a negative line height, such as `font: 12pt/-1 serif`, is dropped whole. `font-variant: normal` now resets `font-variant-position` as well. [#552] [#581]
180181
* A percentage `width`, `min-width` or `max-width` on an image in a table cell was resolved against the block around the table, not the cell. In a 60mm cell, `max-width: 20%` came out as 36mm. [#223] [#505]
181182
* An `@page :left` or `@page :right` rule was ignored unless the style sheet also had a plain `@page` rule. With one, the margins of a `:right` rule were applied to left pages too. [#555] [#556]
182183
* When a later `WriteHTML()` call turned the document right to left, the text on the page already started was set between the swapped side margins. That page now keeps its margins, and the swap starts with the next page. [#554] [#558]
@@ -829,3 +830,4 @@ These changes do not change output.
829830
[#629]: https://github.com/GravityPDF/mpdf/pull/629
830831
[#631]: https://github.com/GravityPDF/mpdf/pull/631
831832
[#632]: https://github.com/GravityPDF/mpdf/issues/632
833+
[#633]: https://github.com/GravityPDF/mpdf/pull/633

‎src/Css/CssMerger.php‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,6 @@ public function merge($inherit, $tag, $attr)
152152
$this->mergeStylesheetSelectors($tag, $attr, $classes, $languageCode);
153153
$this->mergeTagSpecificSelectors($tag, $attr, $classes, $languageCode);
154154
$this->mergeDescendantSelectors($inherit, $tag, $attr, $classes, $languageCode);
155-
// The rules only the matcher reads, such as div > p, h1 + p and li:first-child, go after the descendant rules
156-
$this->mergeMatchingRules($this->cssManager->getRules(), $tag, $attr['ID'], $this->classesOf($attr), [$this->mpdf, 'getStyledElementPath']);
157155
$this->mergeInlineStyle($tag, $attr);
158156

159157
return $this->cssProperties;
@@ -279,8 +277,6 @@ protected function mergeTableCascadingCss($inherit, $tag, $attr, $classes)
279277
return;
280278
}
281279

282-
$this->mpdf->markStyledElementAsLevel();
283-
284280
if ($inherit === 'TOPTABLE') {
285281
// Save Cascading CSS e.g. "div.topic p" at this block level
286282
if (isset($this->mpdf->blk[$this->mpdf->blklvl]['cascadeCSS'])) {
@@ -373,7 +369,6 @@ protected function mergeBlockCascadingCss($inherit, $tag, $attr, $classes)
373369
// Set the new block info
374370
if ($this->sideEffects) {
375371
$this->mpdf->blk[$this->mpdf->blklvl] = $currentBlock;
376-
$this->mpdf->markStyledElementAsLevel();
377372
}
378373
}
379374

‎src/Css/CssParser.php‎

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ class CssParser
122122
private $nthChildFormulas = ['TR' => [], 'TD' => [], 'TH' => []];
123123

124124
/**
125-
* @var array[] The rules of the last CSS parsed for the matcher, each compiled: [compiled selector, declarations,
126-
* whether the legacy parser stores it too], in the order they were written
125+
* @var array[] The rules of the last CSS parsed for the matcher, each compiled: [compiled selector, declarations],
126+
* in the order they were written
127127
*/
128128
private $compiledRules = [];
129129

@@ -222,12 +222,9 @@ public function getCascadeCss()
222222
}
223223

224224
/**
225-
* The rules of the last CSS parsed for the matcher: those whose selector the legacy parser cannot read, and the
226-
* descendant and :lang() rules it stores, which the matcher applies where the legacy engine cannot match them.
227-
* In standard mode, every rule it read
225+
* The rules of the last CSS parsed for the matcher, in standard mode. Legacy mode compiles none
228226
*
229-
* @return array[] Each [compiled selector, declarations, whether the legacy parser stores it too], in the order
230-
* they were written
227+
* @return array[] Each [compiled selector, declarations], in the order they were written
231228
*/
232229
public function getCompiledRules()
233230
{
@@ -318,36 +315,25 @@ private function processCssSelector($written, $classProperties)
318315
$tag = $level === 1 ? $this->selectorParser->parseSimpleSelector($tags) : null;
319316
$simple = $tag !== null && $this->isLegacySelector([$tag]);
320317

318+
// Legacy mode applies simple rules from these keys, and standard mode keeps them for what reads
319+
// CssManager::$CSS directly: BODY, and SVG's classes
320+
if ($simple) {
321+
$this->storeSimpleRule($tag, $classProperties);
322+
}
323+
321324
if ($this->mpdf->cssMode === CssMode::STANDARD) {
322325
$this->compileRule($written, $classProperties);
323326

324-
// Simple rules are still stored by key, for what reads CssManager::$CSS directly: BODY, and SVG's classes
325-
if ($simple) {
326-
$this->storeSimpleRule($tag, $classProperties);
327-
}
328-
329327
return;
330328
}
331329

330+
// Legacy mode drops a rule the legacy parser cannot read
332331
if ($level === 1) {
333-
if (!$simple) {
334-
$this->compileRule($written, $classProperties);
335-
return;
336-
}
337-
338-
// The legacy engine only matches :lang() against an element's own lang attribute, not one it inherits
339-
if (strpos($selector, ':LANG(') !== false) {
340-
$this->compileRule($written, $classProperties, true);
341-
}
342-
343-
$this->storeSimpleRule($tag, $classProperties);
344-
345332
return;
346333
}
347334

348335
$cascade = $this->selectorParser->parseCascadedSelector($tags);
349336
if (!$this->isLegacySelector($cascade)) {
350-
$this->compileRule($written, $classProperties);
351337
return;
352338
}
353339

@@ -359,9 +345,6 @@ private function processCssSelector($written, $classProperties)
359345

360346
$cascadeCSS = Arrays::uniqueRecursiveMerge($cascadeCSS, $classProperties);
361347
$cascadeCSS['depth'] = $level;
362-
363-
// The legacy engine only looks for the ancestors a descendant rule names among blocks and table parts
364-
$this->compileRule($written, $classProperties, true);
365348
}
366349

367350
/**
@@ -426,10 +409,9 @@ private function isLegacySelector(array $levels)
426409
*
427410
* @param string $selector As written
428411
* @param array $classProperties
429-
* @param bool $legacy Whether the legacy parser stores it too
430412
* @return void
431413
*/
432-
private function compileRule($selector, array $classProperties, $legacy = false)
414+
private function compileRule($selector, array $classProperties)
433415
{
434416
if (!$classProperties) {
435417
return;
@@ -439,7 +421,7 @@ private function compileRule($selector, array $classProperties, $legacy = false)
439421

440422
// The universal selector changes which elements existing documents style, so it waits for #530
441423
if ($compiled !== null && !$compiled['universal']) {
442-
$this->compiledRules[] = [$compiled, $classProperties, $legacy];
424+
$this->compiledRules[] = [$compiled, $classProperties];
443425
}
444426
}
445427

‎src/Css/NormalizeProperties.php‎

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Mpdf\Css;
44

55
use Mpdf\Color\ColorConverter;
6+
use Mpdf\CssMode;
67
use Mpdf\Mpdf;
78
use Mpdf\MpdfException;
89
use Mpdf\PageFormat;
@@ -253,8 +254,8 @@ public function canParse($property, $value)
253254
*
254255
* Expands the CSS font shorthand into individual components:
255256
* font-family, font-size, line-height, font-style, font-weight and the font-variant longhands.
256-
* A component the value does not name is reset to normal. A value with no size and family,
257-
* such as a system font keyword (caption, menu), is dropped.
257+
* In standard mode, a component the value does not name is reset to normal. In either mode, a value with no
258+
* size and family, such as a system font keyword (caption, menu), is dropped.
258259
*
259260
* @param string $value Font property value
260261
* @return void
@@ -278,7 +279,9 @@ protected function processFontProperty($value)
278279
$this->processFontFamilyProperty('FONT-FAMILY', $family);
279280

280281
$this->properties['FONT-SIZE'] = $size;
281-
$this->properties['LINE-HEIGHT'] = $lineHeight !== '' ? $lineHeight : 'normal';
282+
if ($lineHeight !== '' || $this->resetsOmittedParts()) {
283+
$this->properties['LINE-HEIGHT'] = $lineHeight !== '' ? $lineHeight : 'normal';
284+
}
282285

283286
// Check for font-style
284287
if (preg_match('/(italic|oblique)/i', $keywords)) {
@@ -295,7 +298,9 @@ protected function processFontProperty($value)
295298
}
296299

297300
// Check for small-caps, the only font-variant value the shorthand can name
298-
$this->processFontVariantProperty('normal');
301+
if ($this->resetsOmittedParts()) {
302+
$this->processFontVariantProperty('normal');
303+
}
299304
if (stripos($keywords, 'small-caps') !== false) {
300305
$this->properties['FONT-VARIANT-CAPS'] = 'small-caps';
301306
}
@@ -402,8 +407,8 @@ protected function processFontFamilyProperty($propertyKey, $value)
402407
* Process BORDER shorthand and individual border properties.
403408
*
404409
* Handles BORDER, BORDER-TOP, BORDER-RIGHT, BORDER-BOTTOM, BORDER-LEFT properties
405-
* by normalizing them to consistent "width style color" format. Each side's width, style and colour
406-
* longhands are set too, so the shorthand replaces longhands given before it in the cascade.
410+
* by normalizing them to consistent "width style color" format. In standard mode, each side's width, style and
411+
* colour longhands are set too, so the shorthand replaces longhands given before it in the cascade.
407412
*
408413
* @param string $propertyKey Property key (BORDER, BORDER-TOP, etc.)
409414
* @param string $value Property value
@@ -421,11 +426,14 @@ protected function processBorderProperty($propertyKey, $value)
421426
list($width, $style, $color) = explode(' ', $value);
422427

423428
$sides = $propertyKey === 'BORDER' ? ['BORDER-TOP', 'BORDER-RIGHT', 'BORDER-BOTTOM', 'BORDER-LEFT'] : [$propertyKey];
429+
$resets = $this->resetsOmittedParts();
424430
foreach ($sides as $side) {
425431
$this->properties[$side] = $value;
426-
$this->properties[$side . '-WIDTH'] = $width;
427-
$this->properties[$side . '-STYLE'] = $style;
428-
$this->properties[$side . '-COLOR'] = $color;
432+
if ($resets) {
433+
$this->properties[$side . '-WIDTH'] = $width;
434+
$this->properties[$side . '-STYLE'] = $style;
435+
$this->properties[$side . '-COLOR'] = $color;
436+
}
429437
}
430438
}
431439

@@ -929,6 +937,44 @@ protected function parseBorderParts($prop)
929937
return $parts + ['w' => 'medium', 's' => 'none', 'c' => '#000000'];
930938
}
931939

940+
/**
941+
* Whether a shorthand sets the parts it does not name to their initial values, replacing longhands given before it
942+
* in the cascade, as CSS has it. In legacy mode it leaves them as they were
943+
*
944+
* @return bool
945+
*/
946+
private function resetsOmittedParts()
947+
{
948+
return $this->mpdf->cssMode === CssMode::STANDARD;
949+
}
950+
951+
/**
952+
* Sets only the parts a background shorthand names, besides its colour and image, as mPDF v7 read it: the
953+
* repeat, position and size only with an image, which they have nothing to apply to without
954+
*
955+
* @param array $bg The shorthand as parseCssBackground() reads it
956+
* @return void
957+
*/
958+
private function mergeBackgroundParts(array $bg)
959+
{
960+
if ($this->properties['BACKGROUND-IMAGE'] !== '') {
961+
if (isset($bg['r'])) {
962+
$this->properties['BACKGROUND-REPEAT'] = $bg['r'];
963+
}
964+
if (!empty($bg['p'])) {
965+
$this->properties['BACKGROUND-POSITION'] = $bg['p'];
966+
}
967+
if (isset($bg['s'])) {
968+
$this->properties['BACKGROUND-SIZE'] = $bg['s'];
969+
}
970+
}
971+
972+
if (isset($bg['o'])) {
973+
$this->properties['BACKGROUND-ORIGIN'] = $bg['o'];
974+
$this->properties['BACKGROUND-CLIP'] = $bg['k'];
975+
}
976+
}
977+
932978
/**
933979
* Process background related CSS properties.
934980
*
@@ -949,9 +995,15 @@ protected function processBackgroundProperty($property, $value)
949995
break;
950996
}
951997

952-
// A part not given takes its initial value, replacing a longhand given before it in the cascade
953998
$this->properties['BACKGROUND-COLOR'] = isset($bg['c']) ? $bg['c'] : 'transparent';
954999
$this->properties['BACKGROUND-IMAGE'] = isset($bg['i']) ? $bg['i'] : '';
1000+
1001+
if (!$this->resetsOmittedParts()) {
1002+
$this->mergeBackgroundParts($bg);
1003+
break;
1004+
}
1005+
1006+
// A part not given takes its initial value, replacing a longhand given before it in the cascade
9551007
$this->properties['BACKGROUND-REPEAT'] = isset($bg['r']) ? $bg['r'] : 'repeat';
9561008
$this->properties['BACKGROUND-POSITION'] = !empty($bg['p']) ? $bg['p'] : '0% 0%';
9571009
$this->properties['BACKGROUND-SIZE'] = isset($bg['s']) ? $bg['s'] : 'auto';

0 commit comments

Comments
 (0)