Skip to content

feat(css): pseudo-elements had no computed values - #112

Merged
jamesconwaydev merged 6 commits into
mainfrom
css-pseudo-elements
Nov 19, 2026
Merged

jamesconwaydev merged 6 commits into
mainfrom
css-pseudo-elements

Conversation

@jamesconwaydev

@jamesconwaydev jamesconwaydev commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

Styler only styled elements (#111). A rule like #target::before { color: green } matched nothing, and there was no way to ask what getComputedStyle(el, "::before") gives. The WPT cascade extractor also dropped getComputedStyle's second argument. So the 13 container-query cases from #100 that read ::before, ::marker and the rest were read off the element itself, and listed as failing.

Change

All of it is in crates/parsec-css. There are no new dependencies, and nothing in crates/parsec changed.

  • Matching. A rule's selector is split at its pseudo-element by parsec-css's own tokenizer, the way rules.rs's has_pseudo_element reads one, so strings, attribute values and :is(…) can't fool it. The part before the pseudo-element is matched against the originating element: p::before as p, ::marker as *, a > ::before as a > *. The one-colon :before, :after, :first-line and :first-letter count too. Specificity is the full selector's, so the pseudo-element adds (0,0,1). Layers, !important, @scope and @media work as they do for elements. For @container, the originating element can be the container. If anything follows the pseudo-element (::before:hover, ::before::marker), the rule never matches, since a static page has no hover and nested pseudo-elements aren't styled.
  • API. You get cascade::Pseudo (Before, After, Marker, Placeholder, FirstLine, FirstLetter, Selection, Highlight(name), Backdrop, FileSelectorButton), with Pseudo::parse("::before"), plus Styled::get_pseudo(el, &Pseudo::Before, "color"), Styled::computed_pseudo(el, &p) and Styled::pseudo_exists(el, &p). I went with a typed enum instead of the issue's string so a typo fails where you write it, and so get_pseudo doesn't parse a name on every call. pseudo_exists returns Option<bool>. It gives Some for ::before and ::after (when content isn't none or normal) and for ::marker (on a list item whose marker shows), and Some(false) for all three under display: none. It gives None for the rest: whether there's a first line, a selection or a backdrop depends on text, layout or the top layer.
  • Computed values, per CSS Pseudo-Elements 4:
    • Inheritance is from the originating element. ::first-letter inherits from ::first-line. A highlight (::selection, ::highlight()) inherits from the parent element's same highlight, for every property. Its non-applicable properties are copied from the originating element, and the root's highlight takes its color from the root.
    • Only the properties that apply to each pseudo-element count. A declaration of one that doesn't apply is dropped, so that property inherits or is initial as usual. The sets follow §2.1.2, §2.2.4, §3.2 and §4.3, plus what browsers apply there that WPT checks (animations on ::first-line, float on ::first-letter). ::marker's set comes from CSS Lists 3 §3.1.1 and marker-supported-properties.html.
    • content: normal is none on ::before and ::after and stays normal on ::marker. attr() in content is the attribute's value as a string, or the fallback, or "" if the attribute is missing. counter(), the quote keywords and strings stay as written.
    • A marker's display comes from list-style-position: inline for inside, block for outside.
    • The UA sheet gets CSS Lists 3's ::marker rule, with text-indent: 0 as browsers have it, CSS Position 4's ::backdrop { position: fixed; inset: 0 }, and the HTML Standard's dialog::backdrop background.
  • Cost. Styling matches pseudo-element rules against each element's bucket of candidates and stores the matches with their cascade keys. Values are worked out only when someone asks for them. A rule on * alone, like the UA's ::marker or a page's *::before, is kept once, not per element. Pseudo-element rules no longer sit in the element index, where they used to be tried and fail on every element.

Tests

  • WPT cascade, at-rules and pseudo-elements: 1,343 of 1,474, up from 1,198 of 1,345.

    • The extractor now records the pseudo-element on each read.
    • It runs css/css-pseudo's static tests, 98 of 98 passing. These include first-line-allowed-properties.html and first-letter-allowed-properties.html, so it now writes the declarations a test sets through document.styleSheets[0].cssRules[0].style into that <style>'s text.
    • Where a test compares two reads (getComputedStyle(target, '::first-line').position against getComputedStyle(target).position), the runner compares them too.
    • 18 cases leave known-failures.txt: the 13 pseudo-element cases from Container queries: style() ranges, container query units, shadow DOM #100, 4 ::before cases in display-none.html, and #pseudo-1 in container-for-shadow-dom.html.
    • 7 entries are new or renamed, all for reasons outside this change. 5 are shadow-DOM ::before cases that were already listed without their pseudo-element. 2 are new cases in layer-keyframes-override.html, which needs an animation sampled.
  • WPT computed values: 11,356 of 12,111, up from 11,287 of 12,041. test_pseudo_computed_value is recorded now: 69 of 70 ::marker, ::before and ::after cases pass. The one failing case is a marker's list-style. The test wants outside none disc, but WPT's own list-style-computed cases want the shortest form, which is what this gives (outside).

  • Media queries (1,370 of 1,370) and CSS parsing are unchanged.

  • Unit tests cover:

    • selector splitting, including strings, :is(), states after the pseudo-element and the one-colon forms;
    • name parsing and attr() substitution;
    • ::before and ::after inheritance, content and existence;
    • cascade order with layers and !important, *::before with attr(), @scope, and @container on the originating element;
    • markers: UA defaults, ignored properties, display, existence;
    • ::first-line, ::first-letter and ::placeholder restrictions and inheritance;
    • highlight inheritance.
  • Fuzzing: the target now reads every pseudo-element, and the input parsed as one, on a <p>, an <li> and an <input>. It ran clean for 5 minutes: 35,600 inputs, at about 120 a second. The rate is lower than before because each input now computes 33 pseudo-element styles, and the one-minute load was around 5.

  • Cascade bench: cargo run --release --manifest-path bench/Cargo.toml -- css, all 13 pages, median of 5, on an M2 Max shared with other builds. Main and this branch were run interleaved, each run starting with the one-minute load under 3 unless noted:

    Run Main This branch Change
    1 2,465.8 ms 2,516.2 ms +2.0%
    2 2,465.1 ms 2,481.4 ms +0.7%
    3 2,428.0 ms 2,468.4 ms +1.7%
    4 (main started at load 3.7) 2,457.9 ms 2,437.9 ms −0.8%
    5 2,389.5 ms 2,493.2 ms +4.3%

    Runs 1 and 2 are from before the last two commits, which copy each pseudo-element rule's declarations lazily and drop a duplicate parent lookup. Run 5 has the whole list 3–6% slower, including pages with no <style>, which this change hardly touches; I read that as the machine. To get past the noise, I also timed the cascade alone as the best of 15, interleaved three times. The five-minute load was 5–8 throughout.

    Page Main This branch
    whatwg-full (UA sheet only) 1,599–1,628 ms 1,609–1,615 ms
    reddit-programming (284 KB of <style>, 89 pseudo-element rules) 237–245 ms 241–248 ms
    python-stdtypes (UA sheet only) 66–70 ms 69–72 ms
    wikipedia-population 109–111 ms 111–114 ms

    So no pseudo-element value is worked out unless it's asked for. What's left is matching the pseudo-element rules' originating selectors. These used to fail at once on their pseudo-element compound, and now they match the element and record the result. That costs up to about 2% on the pages with CSS. The UA-only pages are within noise of main.

cargo fmt --all, cargo clippy --workspace --all-targets -- -D warnings, cargo test --workspace and cargo deny check pass.

Closes #111

Styler styled elements only, so a rule ending in ::before, ::marker or
::first-line was matched against nothing, and nothing could say what
getComputedStyle(el, "::before") gives.

A rule's selector is now split at its pseudo-element with the CSS
tokenizer; the rest is matched against the originating element, with
the pseudo-element counted in the specificity, and @container may find
the originating element itself. The matches are kept, and
Styled::get_pseudo, computed_pseudo and pseudo_exists work the values
out when asked: inherited from the originating element (::first-letter
from ::first-line, highlights from the parent's highlight), with only
the properties that apply to each counted, content: normal as none on
::before and ::after, attr() in content substituted, and a marker's
display from its list item. The UA sheet gets CSS Lists 3's ::marker
rule and ::backdrop's.

Rules on * alone, like the UA's ::marker, are kept once rather than per
element, so a page costs no more to style for having pseudo-element
rules nobody reads.
The cascade extractor dropped getComputedStyle's second argument, so
the 13 container-query cases that read ::before and the like were read
off the element itself and listed as failing.

The extractor now records the pseudo-element, and the runner reads it
with get_pseudo. It also runs css/css-pseudo's static tests: it writes
declarations a test sets on a page rule's style into that <style>, and
checks a read against another read where the test compares two. The
computed-value extractor records test_pseudo_computed_value, which the
runner checks with a #target::marker rule added.

Cascade: 1,343 of 1,474, from 1,198 of 1,345. Computed values: 11,356
of 12,111, from 11,287 of 12,041. Both known-failures lists rewritten;
the one new computed failure is a marker's list-style, which the test
wants as "outside none disc" where WPT's list-style tests want the
shortest form.
README and ROADMAP now list pseudo-elements' computed values as done
(#111), with the new WPT counts.
A pseudo-element rule's declarations are written out as text so its
values can be worked out later. That's now done the first time an
element matches the rule, not for every rule each time a document is
styled.
Keeping the parent element for highlight inheritance added a second
lookup of the parent's style per element; one does both.
@jamesconwaydev
jamesconwaydev merged commit df4fd1b into main Nov 19, 2026
56 of 58 checks passed
@jamesconwaydev
jamesconwaydev deleted the css-pseudo-elements branch November 19, 2026 16:25
@jamesconwaydev jamesconwaydev mentioned this pull request Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Computed styles for pseudo-elements (::before, ::after, ::marker)

1 participant