Truly independent web browser
LibWeb: Support `::highlight(*)`
Tim Ledbetter2b9c062d0edd
diff --git a/Libraries/LibWeb/Rust/src/css/selector_arena.rs b/Libraries/LibWeb/Rust/src/css/selector_arena.rsindex 37ceaec7ada..4927bcfc2a9 100644--- a/Libraries/LibWeb/Rust/src/css/selector_arena.rs+++ b/Libraries/LibWeb/Rust/src/css/selector_arena.rs@@ -373,6 +373,7 @@ enum PseudoElementRareValue {/// Ranges into the arena's text ranges.classes: PoolRange,},+ Universal,}/// The `@namespace` declarations of the sheet an arena's selectors are of, as process atoms.@@ -684,6 +685,7 @@ impl SelectorArena {classes: texts(classes),})}+ PseudoElementRareValue::Universal => PseudoElementValue::Universal,},identifier_identities: PoolRange::of(&self.atoms, rare.identifier_identities),})@@ -1199,10 +1201,14 @@ impl<'a> Selector<'a> {}#[must_use]- pub fn target_highlight_name(self) -> StyleAtomID {- self.target_highlight()- .and_then(|selector| selector.identifier_identities.first().copied())- .unwrap_or(StyleAtomID::NONE)+ pub fn targets_custom_highlight(self, name: StyleAtomID) -> bool {+ let Some(selector) = self.target_highlight() else {+ return false;+ };+ match selector.value {+ PseudoElementValue::Universal => !name.is_none(),+ _ => selector.identifier_identities.first() == Some(&name),+ }}#[must_use]@@ -1269,6 +1275,11 @@ impl<'a> Selector<'a> {&& argument.classes.is_empty(){Specificity::default()+ }+ // https://drafts.csswg.org/css-pseudo-4/#selectordef-highlight-highlight-name-selector+ // The specificity of a ::highlight() selector with a * argument is zero.+ else if matches!(pseudo_element.value, PseudoElementValue::Universal) {+ Specificity::default()} else {let pseudo_specificity = Specificity {types: 1,@@ -1453,6 +1464,7 @@ pub enum PseudoElementValue<'a> {CompoundSelector(Selector<'a>),Identifiers(TextList<'a>),TransitionName(TransitionNameAndClassSelector<'a>),+ Universal,}// https://drafts.csswg.org/css-view-transitions-1/#typedef-pt-name-selector@@ -2043,6 +2055,7 @@ impl Builder {let classes = self.push_text_list(argument.classes.iter().map(|class| (class.units(), false)));PseudoElementRareValue::TransitionName { name, classes }}+ tree::PseudoElementValue::Universal => PseudoElementRareValue::Universal,};let identities_start = index_u32(self.atoms.len());for &atom in &pseudo_element.identifier_identities {@@ -2477,6 +2490,7 @@ fn simple_to_tree(simple: &SimpleSelector<'_>) -> tree::SimpleSelector {classes: argument.classes.iter().map(SelectorString::from).collect(),}))}+ PseudoElementValue::Universal => tree::PseudoElementValue::Universal,},identifier_identities: pseudo_element.identifier_identities.into(),}))diff --git a/Libraries/LibWeb/Rust/src/css/selector_parser.rs b/Libraries/LibWeb/Rust/src/css/selector_parser.rsindex 5b000f45130..3987db247d6 100644--- a/Libraries/LibWeb/Rust/src/css/selector_parser.rs+++ b/Libraries/LibWeb/Rust/src/css/selector_parser.rs@@ -1025,17 +1025,22 @@ impl<'a> SelectorParser<'a> {}// https://drafts.csswg.org/css-pseudo-4/#typedef-highlight-name-selector// <highlight-name-selector> = '*' | <custom-ident>- // FIXME: Support the '*' argument, which selects every custom highlight.PseudoElementParameterType::HighlightNameSelector => {- let name = stream.next().and_then(ComponentValue::ident).ok_or(())?;- if is_css_wide_keyword(name) || ascii_eq(name, "default") {- return Err(());- }+ let argument = stream.next().ok_or(())?;+ let value = if argument.is_delim(b'*') {+ PseudoElementValue::Universal+ } else {+ let name = argument.ident().ok_or(())?;+ if is_css_wide_keyword(name) || ascii_eq(name, "default") {+ return Err(());+ }+ PseudoElementValue::Identifiers(Box::new([name.into()]))+ };stream.discard_whitespace();if !stream.is_empty() {return Err(());}- Ok(PseudoElementValue::Identifiers(Box::new([name.into()])))+ Ok(value)}PseudoElementParameterType::IdentList => {let mut identifiers = Vec::new();diff --git a/Libraries/LibWeb/Rust/src/css/selector_serialization.rs b/Libraries/LibWeb/Rust/src/css/selector_serialization.rsindex d8a8f33a2f5..a1f6ceed127 100644--- a/Libraries/LibWeb/Rust/src/css/selector_serialization.rs+++ b/Libraries/LibWeb/Rust/src/css/selector_serialization.rs@@ -229,6 +229,7 @@ fn serialize_simple_selector(sink: &mut TextSink, selector: &SimpleSelector, con}sink.push_ascii(")");}+ PseudoElementValue::Universal => sink.push_ascii("(*)"),PseudoElementValue::TransitionName(argument) => {sink.push_ascii("(");match &argument.name {diff --git a/Libraries/LibWeb/Rust/src/css/selector_tree.rs b/Libraries/LibWeb/Rust/src/css/selector_tree.rsindex 0f61328157f..fe0e0f8c4b6 100644--- a/Libraries/LibWeb/Rust/src/css/selector_tree.rs+++ b/Libraries/LibWeb/Rust/src/css/selector_tree.rs@@ -54,6 +54,7 @@ pub enum PseudoElementValue {CompoundSelector(Arc<CompiledSelector>),Identifiers(Box<[SelectorString]>),TransitionName(Box<TransitionNameAndClassSelector>),+ Universal,}// https://drafts.csswg.org/css-view-transitions-1/#typedef-pt-name-selectordiff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/matching.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/matching.rsindex 136ced34d74..dd78936cf5d 100644--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/matching.rs+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/matching.rs@@ -39,7 +39,8 @@ pub(super) struct ElementMatch {impl ElementMatch {pub(super) fn styles(&self, index: &RuleIndex, target: super::PseudoElementTarget) -> bool {self.pseudo_kind == target.kind- && (!target.is_custom_highlight() || index.highlight_name_of_entry(self.entry) == target.highlight_name)+ && (target.kind != super::super::publication::pseudo_kind::HIGHLIGHT+ || index.entry_styles_custom_highlight(self.entry, target.highlight_name))}/// Whether the entry styles a pseudo-element.diff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/rules.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/rules.rsindex 8cbc74eafe3..ef734e427b2 100644--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/rules.rs+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/rules.rs@@ -2284,8 +2284,8 @@ impl RuleIndex {.resolve(self.entries.view(entry as usize).selector())}- pub(super) fn highlight_name_of_entry(&self, entry: u32) -> StyleAtomID {- self.selector_of_entry(entry).target_highlight_name()+ pub(super) fn entry_styles_custom_highlight(&self, entry: u32, name: StyleAtomID) -> bool {+ self.selector_of_entry(entry).targets_custom_highlight(name)}/// The arena holding the selector of entry `entry`.diff --git a/Tests/LibWeb/Ref/expected/wpt-import/css/css-highlight-api/custom-highlight-universal-deep-chain-001-ref.html b/Tests/LibWeb/Ref/expected/wpt-import/css/css-highlight-api/custom-highlight-universal-deep-chain-001-ref.htmlnew file mode 100644index 00000000000..cbe91cfeb6b--- /dev/null+++ b/Tests/LibWeb/Ref/expected/wpt-import/css/css-highlight-api/custom-highlight-universal-deep-chain-001-ref.html@@ -0,0 +1,10 @@+<!doctype html>+<meta charset="utf-8">+<style>+ .highlighted {+ background-color: pink;+ color: green;+ }+</style>+<body>+<div><div><span><span class="highlighted">One two </span>three…</span></div></div>diff --git a/Tests/LibWeb/Ref/expected/wpt-import/css/css-highlight-api/custom-highlight-universal-inheritance-001-ref.html b/Tests/LibWeb/Ref/expected/wpt-import/css/css-highlight-api/custom-highlight-universal-inheritance-001-ref.htmlnew file mode 100644index 00000000000..97f67a8f211--- /dev/null+++ b/Tests/LibWeb/Ref/expected/wpt-import/css/css-highlight-api/custom-highlight-universal-inheritance-001-ref.html@@ -0,0 +1,10 @@+<!doctype html>+<meta charset="utf-8">+<style>+ .highlighted {+ background-color: pink;+ color: green;+ }+</style>+<body>+<div><span class="highlighted">One two </span>three…</div>diff --git a/Tests/LibWeb/Ref/expected/wpt-import/css/css-highlight-api/custom-highlight-universal-painting-003-ref.html b/Tests/LibWeb/Ref/expected/wpt-import/css/css-highlight-api/custom-highlight-universal-painting-003-ref.htmlnew file mode 100644index 00000000000..9f74dae249a--- /dev/null+++ b/Tests/LibWeb/Ref/expected/wpt-import/css/css-highlight-api/custom-highlight-universal-painting-003-ref.html@@ -0,0 +1,13 @@+<!doctype html>+<meta charset="utf-8">+<style>+ .foo {+ background-color: yellow;+ color: blue;+ }+ .bar {+ background-color: yellow;+ color: purple;+ }+</style>+<body><span class="foo">One </span><span class="bar">two </span><span>three…</span>diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-highlight-api/custom-highlight-universal-deep-chain-001.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-highlight-api/custom-highlight-universal-deep-chain-001.htmlnew file mode 100644index 00000000000..041cc109db8--- /dev/null+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-highlight-api/custom-highlight-universal-deep-chain-001.html@@ -0,0 +1,25 @@+<!DOCTYPE html>+<meta charset="UTF-8">+<title>Custom Highlight: ::highlight(*) on :root reaches a named highlight through rule-less intermediate elements</title>+<link rel="author" href="mailto:[email protected]">+<link rel="help" href="https://drafts.csswg.org/css-pseudo-4/#selectordef-highlight-highlight-name-selector">+<link rel="match" href="../../../../expected/wpt-import/css/css-highlight-api/custom-highlight-universal-deep-chain-001-ref.html">+<meta name="assert" value="The ::highlight(*) base style propagates through rule-less intermediate elements.">+<style>+ :root::highlight(*) {+ background-color: pink;+ }+ span::highlight(foo) {+ color: green;+ }+</style>+<body>+<div><div><span id="target">One two three…</span></div></div>+<script>+ let target = document.getElementById("target");+ let r = new Range();+ r.setStart(target.firstChild, 0);+ r.setEnd(target.firstChild, 8);++ CSS.highlights.set("foo", new Highlight(r));+</script>diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-highlight-api/custom-highlight-universal-inheritance-001.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-highlight-api/custom-highlight-universal-inheritance-001.htmlnew file mode 100644index 00000000000..b2c09ffbed6--- /dev/null+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-highlight-api/custom-highlight-universal-inheritance-001.html@@ -0,0 +1,25 @@+<!DOCTYPE html>+<meta charset="UTF-8">+<title>Custom Highlight: ::highlight(*) on :root reaches a named highlight on a descendant</title>+<link rel="author" href="mailto:[email protected]">+<link rel="help" href="https://drafts.csswg.org/css-pseudo-4/#selectordef-highlight-highlight-name-selector">+<link rel="match" href="../../../../expected/wpt-import/css/css-highlight-api/custom-highlight-universal-inheritance-001-ref.html">+<meta name="assert" value="A named highlight with no rule on its ancestors inherits from the ancestor's ::highlight(*) style.">+<style>+ :root::highlight(*) {+ background-color: pink;+ }+ div::highlight(foo) {+ color: green;+ }+</style>+<body>+<div>One two three…</div>+<script>+ let div = document.querySelector("div");+ let r = new Range();+ r.setStart(div.firstChild, 0);+ r.setEnd(div.firstChild, 8);++ CSS.highlights.set("foo", new Highlight(r));+</script>diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-highlight-api/custom-highlight-universal-no-named-rule-001.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-highlight-api/custom-highlight-universal-no-named-rule-001.htmlnew file mode 100644index 00000000000..4cd7664e00b--- /dev/null+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-highlight-api/custom-highlight-universal-no-named-rule-001.html@@ -0,0 +1,21 @@+<!DOCTYPE html>+<meta charset="UTF-8">+<title>Custom Highlight: a registered highlight with no named rule paints with the ::highlight(*) style</title>+<link rel="author" href="mailto:[email protected]">+<link rel="help" href="https://drafts.csswg.org/css-pseudo-4/#selectordef-highlight-highlight-name-selector">+<link rel="match" href="../../../../expected/wpt-import/css/css-highlight-api/custom-highlight-universal-painting-001-ref.html">+<meta name="assert" value="A registered highlight with no ::highlight() rule falls back to the element's ::highlight(*) style when painted.">+<style>+ ::highlight(*) {+ background-color: yellow;+ color: blue;+ }+</style>+<body><span>One </span><span>two </span><span>three…</span>+<script>+ let r = new Range();+ r.setStart(document.body, 0);+ r.setEnd(document.body, 2);++ CSS.highlights.set("bar", new Highlight(r));+</script>diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-highlight-api/custom-highlight-universal-painting-001.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-highlight-api/custom-highlight-universal-painting-001.htmlnew file mode 100644index 00000000000..de5334dbac4--- /dev/null+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-highlight-api/custom-highlight-universal-painting-001.html@@ -0,0 +1,24 @@+<!DOCTYPE html>+<meta charset="UTF-8">+<title>Custom Highlight: ::highlight(*) acts as a base for a named highlight</title>+<link rel="author" href="mailto:[email protected]">+<link rel="help" href="https://drafts.csswg.org/css-pseudo-4/#selectordef-highlight-highlight-name-selector">+<link rel="match" href="../../../../expected/wpt-import/css/css-highlight-api/custom-highlight-universal-painting-001-ref.html">+<meta name="assert" value="The ::highlight(*) declarations merge into a named highlight's cascade; the named rule overrides conflicting properties.">+<style>+ ::highlight(*) {+ background-color: yellow;+ color: red;+ }+ ::highlight(foo) {+ color: blue;+ }+</style>+<body><span>One </span><span>two </span><span>three…</span>+<script>+ let r = new Range();+ r.setStart(document.body, 0);+ r.setEnd(document.body, 2);++ CSS.highlights.set("foo", new Highlight(r));+</script>diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-highlight-api/custom-highlight-universal-painting-002.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-highlight-api/custom-highlight-universal-painting-002.htmlnew file mode 100644index 00000000000..03a8abae753--- /dev/null+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-highlight-api/custom-highlight-universal-painting-002.html@@ -0,0 +1,24 @@+<!DOCTYPE html>+<meta charset="UTF-8">+<title>Custom Highlight: a named highlight overrides ::highlight(*) declared later in source</title>+<link rel="author" href="mailto:[email protected]">+<link rel="help" href="https://drafts.csswg.org/css-pseudo-4/#selectordef-highlight-highlight-name-selector">+<link rel="match" href="../../../../expected/wpt-import/css/css-highlight-api/custom-highlight-universal-painting-001-ref.html">+<meta name="assert" value="::highlight(*) contributes 0 specificity, so a named rule wins conflicting properties even when the universal rule comes later in source order.">+<style>+ ::highlight(foo) {+ color: blue;+ }+ ::highlight(*) {+ background-color: yellow;+ color: red;+ }+</style>+<body><span>One </span><span>two </span><span>three…</span>+<script>+ let r = new Range();+ r.setStart(document.body, 0);+ r.setEnd(document.body, 2);++ CSS.highlights.set("foo", new Highlight(r));+</script>diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-highlight-api/custom-highlight-universal-painting-003.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-highlight-api/custom-highlight-universal-painting-003.htmlnew file mode 100644index 00000000000..58784b59a62--- /dev/null+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-highlight-api/custom-highlight-universal-painting-003.html@@ -0,0 +1,30 @@+<!DOCTYPE html>+<meta charset="UTF-8">+<title>Custom Highlight: ::highlight(*) acts as a base for multiple named highlights</title>+<link rel="author" href="mailto:[email protected]">+<link rel="help" href="https://drafts.csswg.org/css-pseudo-4/#selectordef-highlight-highlight-name-selector">+<link rel="match" href="../../../../expected/wpt-import/css/css-highlight-api/custom-highlight-universal-painting-003-ref.html">+<meta name="assert" value="The universal base merges into each named highlight's cascade independently.">+<style>+ ::highlight(*) {+ background-color: yellow;+ }+ ::highlight(foo) {+ color: blue;+ }+ ::highlight(bar) {+ color: purple;+ }+</style>+<body><span>One </span><span>two </span><span>three…</span>+<script>+ let r1 = new Range();+ r1.setStart(document.body, 0);+ r1.setEnd(document.body, 1);+ CSS.highlights.set("foo", new Highlight(r1));++ let r2 = new Range();+ r2.setStart(document.body, 1);+ r2.setEnd(document.body, 2);+ CSS.highlights.set("bar", new Highlight(r2));+</script>diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-highlight-api/custom-highlight-universal-escaped-star.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-highlight-api/custom-highlight-universal-escaped-star.txtnew file mode 100644index 00000000000..2ff2a6f97b5--- /dev/null+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-highlight-api/custom-highlight-universal-escaped-star.txt@@ -0,0 +1,6 @@+Harness status: OK++Found 1 tests++1 Pass+Pass ::highlight(\*) computed style is the escaped literal name's rule, merged with the universal base\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-highlight-api/custom-highlight-universal-parsing-and-computed-style.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-highlight-api/custom-highlight-universal-parsing-and-computed-style.txtnew file mode 100644index 00000000000..1b049192019--- /dev/null+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-highlight-api/custom-highlight-universal-parsing-and-computed-style.txt@@ -0,0 +1,15 @@+Harness status: OK++Found 10 tests++10 Pass+Pass "::highlight(*)" should be a valid selector+Pass "div::highlight(*)" should be a valid selector+Pass ":root::highlight(*)" should be a valid selector+Pass "::highlight()" should be an invalid selector+Pass "::highlight(* foo)" should be an invalid selector+Pass "::highlight(*.class)" should be an invalid selector+Pass "::highlight(foo*)" should be an invalid selector+Pass "::highlight(**)" should be an invalid selector+Pass named highlight merges with the universal base on the same element+Pass named highlight without its own rule falls back to the universal style\ No newline at end of filediff --git a/Tests/LibWeb/Text/input/wpt-import/css/css-highlight-api/custom-highlight-universal-escaped-star.html b/Tests/LibWeb/Text/input/wpt-import/css/css-highlight-api/custom-highlight-universal-escaped-star.htmlnew file mode 100644index 00000000000..bebbd7ee9ff--- /dev/null+++ b/Tests/LibWeb/Text/input/wpt-import/css/css-highlight-api/custom-highlight-universal-escaped-star.html@@ -0,0 +1,19 @@+<!DOCTYPE html>+<meta charset="UTF-8">+<title>Custom Highlight: ::highlight(*) computed style must not be confused with the escaped name \*</title>+<link rel="author" title="Tim Nguyen" href="https://github.com/nt1m">+<link rel="help" href="https://drafts.csswg.org/css-pseudo-4/#selectordef-highlight-highlight-name-selector">+<script src="../../resources/testharness.js"></script>+<script src="../../resources/testharnessreport.js"></script>+<style>+ #outer::highlight(\*) { color: red; }+ #outer::highlight(*) { color: green; background-color: yellow; }+</style>+<div id="outer">text</div>+<script>+test(() => {+ const style = getComputedStyle(outer, "::highlight(\\*)");+ assert_equals(style.color, "rgb(255, 0, 0)");+ assert_equals(style.backgroundColor, "rgb(255, 255, 0)");+}, "::highlight(\\*) computed style is the escaped literal name's rule, merged with the universal base");+</script>diff --git a/Tests/LibWeb/Text/input/wpt-import/css/css-highlight-api/custom-highlight-universal-parsing-and-computed-style.html b/Tests/LibWeb/Text/input/wpt-import/css/css-highlight-api/custom-highlight-universal-parsing-and-computed-style.htmlnew file mode 100644index 00000000000..dc4d7c75637--- /dev/null+++ b/Tests/LibWeb/Text/input/wpt-import/css/css-highlight-api/custom-highlight-universal-parsing-and-computed-style.html@@ -0,0 +1,36 @@+<!DOCTYPE html>+<meta charset="UTF-8">+<title>Custom Highlight: ::highlight(*) parsing, serialization, and computed style</title>+<link rel="author" href="mailto:[email protected]">+<link rel="help" href="https://drafts.csswg.org/css-pseudo-4/#selectordef-highlight-highlight-name-selector">+<script src="../../resources/testharness.js"></script>+<script src="../../resources/testharnessreport.js"></script>+<script src="../../css/support/parsing-testcommon.js"></script>+<style>+ #outer::highlight(*) { color: red; background-color: yellow; }+ #outer::highlight(foo) { color: green; }+</style>+<div id="outer">text</div>+<script>+test_valid_selector("::highlight(*)");+test_valid_selector("div::highlight(*)");+test_valid_selector(":root::highlight(*)");++test_invalid_selector("::highlight()");+test_invalid_selector("::highlight(* foo)");+test_invalid_selector("::highlight(*.class)");+test_invalid_selector("::highlight(foo*)");+test_invalid_selector("::highlight(**)");++test(() => {+ const style = getComputedStyle(outer, "::highlight(foo)");+ assert_equals(style.color, "rgb(0, 128, 0)");+ assert_equals(style.backgroundColor, "rgb(255, 255, 0)");+}, "named highlight merges with the universal base on the same element");++test(() => {+ const style = getComputedStyle(outer, "::highlight(bar)");+ assert_equals(style.color, "rgb(255, 0, 0)");+ assert_equals(style.backgroundColor, "rgb(255, 255, 0)");+}, "named highlight without its own rule falls back to the universal style");+</script>