Repository navigation
Description
Part of #520 (step 14). Upstream: mpdf#318 (:first-child), mpdf#7 (adjacent sibling), mpdf#830 (inline ancestors), mpdf#134 (attribute selectors), mpdf#1838.
Measured on gravitypdf at b6a07921, PHP 8.5.4. All of these are dropped: div > p, div>p, h1 + p, h1 ~ p, [data-x], p[data-x=y], p:first-child, li:nth-child(1), p:not(.a), span em, .x b where .x is a span, a span.
Compile each rule the legacy parser rejects into:
- its compound selectors and the combinators between them
- its specificity as (ids, classes and attributes and pseudo-classes, tags)
- its position in the stylesheet
- its normal and important declarations
The compiler needs a selector tokenizer. Today a selector list is split with explode(',') (src/Css/CssParser.php:225) and each selector on whitespace (:257). That breaks :is(a, b), :not(.a, .b), [title="a,b"], [title="a b"] and div>p. Parse the selector before it is uppercased (:223), and fold case per component: tag names and attribute names case-insensitively, and id and class names against the uppercased element values for now.
Match. File each rule under its rightmost compound, by id, then class, then tag, then *, as browsers do. For each element, collect the rules filed under its id, its classes, its tag and *, and match each right to left against the stack from #533.
Where it fits. With the legacy cascade, the matcher serves only the rules the legacy parser rejects. Their declarations are applied with the descendant rules, sorted among themselves by specificity and then position. div > p then behaves as div p does today, and exactly like a browser under #535, which reuses this matcher rather than replacing it.
Selectors it adds.
>,+and~:first-child,:nth-child(),:first-of-typeand:nth-of-type()on any element.tr/td:nth-childis matched from a real index, replacing the$mpdf->row/$mpdf->colarithmetic repeated three times inCssMerger, and the counttd:nth-child()counts grid columns, so acolspanorrowspanbefore the cell shifts it #528 keeps.- descendant rules through inline ancestors (
span em,.x b) [a],[a=v],[a~=v],[a|=v],[a^=v],[a$=v],[a*=v], with or without a tag, with[lang=x]as one ordinary case. Attribute names are uppercased when the HTML is tokenized, and values are not (exceptidandclass). In HTML, values of a fixed list of attributes (type,lang,dir,alignand others) match case-insensitively and the rest case-sensitively. Follow that.:lang()matching an inherited language, from the frames:not(),:is()and:where().:where()counts as zero; the others take the specificity of their most specific argument.
Compatibility. Rules that are ignored today start to apply, in the default mode. That changes the output of documents that contain them, as intended. Say so in the PR, and list the snapshot fixtures that changed.
Tests. Each selector above, matched and not matched; each through an inline ancestor; each inside a table cell and a positioned block; and precedence among these rules by specificity and position.
Activity
jakejackson1 commentedon Sep 30, 2026
Done in #619, #620, #621, #627, #628 and #629.