Repository navigation
feat(css): pseudo-elements had no computed values - #112
Merged
Merged
Conversation
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.
# Conflicts: # README.md
Keeping the parent element for highlight inheritance added a second lookup of the parent's style per element; one does both.
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Styleronly styled elements (#111). A rule like#target::before { color: green }matched nothing, and there was no way to ask whatgetComputedStyle(el, "::before")gives. The WPT cascade extractor also droppedgetComputedStyle's second argument. So the 13 container-query cases from #100 that read::before,::markerand 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 incrates/parsecchanged.parsec-css's own tokenizer, the wayrules.rs'shas_pseudo_elementreads one, so strings, attribute values and:is(…)can't fool it. The part before the pseudo-element is matched against the originating element:p::beforeasp,::markeras*,a > ::beforeasa > *. The one-colon:before,:after,:first-lineand:first-lettercount too. Specificity is the full selector's, so the pseudo-element adds (0,0,1). Layers,!important,@scopeand@mediawork 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.cascade::Pseudo(Before,After,Marker,Placeholder,FirstLine,FirstLetter,Selection,Highlight(name),Backdrop,FileSelectorButton), withPseudo::parse("::before"), plusStyled::get_pseudo(el, &Pseudo::Before, "color"),Styled::computed_pseudo(el, &p)andStyled::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 soget_pseudodoesn't parse a name on every call.pseudo_existsreturnsOption<bool>. It givesSomefor::beforeand::after(whencontentisn'tnoneornormal) and for::marker(on a list item whose marker shows), andSome(false)for all three underdisplay: none. It givesNonefor the rest: whether there's a first line, a selection or a backdrop depends on text, layout or the top layer.::first-letterinherits 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 itscolorfrom the root.::first-line,floaton::first-letter).::marker's set comes from CSS Lists 3 §3.1.1 andmarker-supported-properties.html.content: normalisnoneon::beforeand::afterand staysnormalon::marker.attr()incontentis 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.displaycomes fromlist-style-position:inlinefor inside,blockfor outside.::markerrule, withtext-indent: 0as browsers have it, CSS Position 4's::backdrop { position: fixed; inset: 0 }, and the HTML Standard'sdialog::backdropbackground.*alone, like the UA's::markeror 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.
css/css-pseudo's static tests, 98 of 98 passing. These includefirst-line-allowed-properties.htmlandfirst-letter-allowed-properties.html, so it now writes the declarations a test sets throughdocument.styleSheets[0].cssRules[0].styleinto that<style>'s text.getComputedStyle(target, '::first-line').positionagainstgetComputedStyle(target).position), the runner compares them too.known-failures.txt: the 13 pseudo-element cases from Container queries: style() ranges, container query units, shadow DOM #100, 4::beforecases indisplay-none.html, and#pseudo-1incontainer-for-shadow-dom.html.::beforecases that were already listed without their pseudo-element. 2 are new cases inlayer-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_valueis recorded now: 69 of 70::marker,::beforeand::aftercases pass. The one failing case is a marker'slist-style. The test wantsoutside none disc, but WPT's ownlist-style-computedcases 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:
:is(), states after the pseudo-element and the one-colon forms;attr()substitution;::beforeand::afterinheritance,contentand existence;!important,*::beforewithattr(),@scope, and@containeron the originating element;display, existence;::first-line,::first-letterand::placeholderrestrictions and 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: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.<style>, 89 pseudo-element rules)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 --workspaceandcargo deny checkpass.Closes #111