Skip to content

Truly independent web browser

LibWeb: Let a reverting declaration win over its logical counterpart

Andreas Kling88e2164a7d8f
diff --git a/Libraries/LibWeb/Rust/src/css/style/cascade.rs b/Libraries/LibWeb/Rust/src/css/style/cascade.rs
index 438fe65f2b2..a93d09344ac 100644
--- a/Libraries/LibWeb/Rust/src/css/style/cascade.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/cascade.rs
@@ -146,6 +146,49 @@ impl CascadePriority {
}
}
+ /// The priority below every declaration's.
+ #[must_use]
+ pub fn lowest() -> Self {
+ Self {
+ origin_importance: 0,
+ context: 0,
+ element_attachment: ElementAttachment::Rule,
+ layer: 0,
+ specificity: Specificity::default(),
+ scope_proximity: 0,
+ sheet_rank: 0,
+ rule_rank: 0,
+ }
+ }
+
+ /// Whether a `revert` or `revert-layer` declared at this priority rolls back past a declaration
+ /// at `other`, so that the declaration takes no part in what the property reverts to.
+ #[must_use]
+ pub fn reverts_past(&self, operator: CascadeOperator, other: &Self) -> bool {
+ // UA, user, and author, in the order `CascadeStratum` groups origins.
+ let origin_group = |origin_importance: u8| match origin_importance {
+ 0 | 8 => 0,
+ 1 | 7 => 1,
+ _ => 2,
+ };
+ match operator {
+ // https://drafts.csswg.org/css-cascade-5/#valdef-all-revert
+ CascadeOperator::Revert => origin_group(other.origin_importance) >= origin_group(self.origin_importance),
+ // https://drafts.csswg.org/css-cascade-5/#valdef-all-revert-layer
+ CascadeOperator::RevertLayer => {
+ // NB: Inline styles occupy a separate cascade step, even though they share the implicit outer
+ // layer's rank with unlayered style rules, so a revert-layer in them only rolls back other
+ // inline styles. This matches CascadeStratum::is_below().
+ other.origin_importance == self.origin_importance
+ && other.context == self.context
+ && other.layer == self.layer
+ && (self.element_attachment != ElementAttachment::InlineStyle
+ || other.element_attachment == ElementAttachment::InlineStyle)
+ }
+ _ => false,
+ }
+ }
+
/// Where among an element's encapsulation contexts an author declaration's priority places it:
/// the `context_depth` it was built from. `None` for the other origins.
#[must_use]
diff --git a/Libraries/LibWeb/Rust/src/css/style/publication.rs b/Libraries/LibWeb/Rust/src/css/style/publication.rs
index 5d4d292fb0d..d5ec12c12fb 100644
--- a/Libraries/LibWeb/Rust/src/css/style/publication.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/publication.rs
@@ -3498,8 +3498,40 @@ impl RetainedState {
let mut element_relative = false;
let mut substituted_container_units = 0;
'winners: for winner in self.winner_groups.winners_in_state(state) {
+ // A declaration that reverts is remembered as declared, for the logical property group
+ // it belongs to.
+ let declared_winner = winner;
// A revert whose continuation resumes at nothing leaves the property undeclared.
let Some(mut winner) = self.winner_groups.resolved_winner(winner) else {
+ // NB: It computes as `unset`, but still wins over a logical counterpart that it reverted past, so
+ // a reverted margin-inline-start declared after a margin-left still wins over it. Below every
+ // declaration in cascade order, it loses to the counterparts that it did not revert past.
+ if declared_winner.property >= crate::css::property_metadata::FIRST_LONGHAND_PROPERTY_ID
+ && let operator @ (CascadeOperator::Revert | CascadeOperator::RevertLayer) =
+ declared_winner.key.operator
+ && crate::css::property_metadata::property_logical_group(declared_winner.property).is_some()
+ && pseudo_kind.is_none_or(|kind| {
+ crate::css::property_metadata::pseudo_element_supports_property(kind, declared_winner.property)
+ })
+ && let Ok(Some((index, ..))) = self.written_winner_value(node, &declared_winner)
+ {
+ declarations.push((
+ CascadePriority::lowest(),
+ 0,
+ WinnerDeclaration::new(
+ declared_winner.property,
+ declared_winner.important,
+ WinnerValue::Substituted {
+ value: unset_value(),
+ source: declared_winner.source,
+ },
+ ),
+ Some(winner_store::RevertedDeclaration {
+ position: (declared_winner.priority, index),
+ operator,
+ }),
+ ));
+ }
continue;
};
if winner.key.animation_relevance != 0 {
@@ -3749,10 +3781,30 @@ impl RetainedState {
self.counters.bump(Counter::EngineComputedRecordBailValue);
return Err(Unanswered::Refused);
}
+ let revert = if winner == declared_winner {
+ None
+ } else {
+ let operator = match declared_winner.key.operator {
+ operator @ (CascadeOperator::Revert | CascadeOperator::RevertLayer) => operator,
+ _ => reverted
+ .first()
+ .map_or(CascadeOperator::Revert, |&(_, operator)| operator),
+ };
+ // NB: Without the reverting declaration's position, the winner still applies, and its logical
+ // property group falls back to ordering by cascade position.
+ match self.written_winner_value(node, &declared_winner) {
+ Ok(Some((declared_index, ..))) => Some(winner_store::RevertedDeclaration {
+ position: (declared_winner.priority, declared_index),
+ operator,
+ }),
+ _ => None,
+ }
+ };
declarations.push((
winner.priority,
index,
WinnerDeclaration::new(winner.property, winner.important, value),
+ revert,
));
}
// What a function's container conditions read is recorded with the node's record, as a
@@ -3761,12 +3813,18 @@ impl RetainedState {
self.note_container_effects_for_host(node, effects);
}
declarations.sort_by_key(|(priority, index, ..)| (*priority, *index));
- let mut store = WinnerStore::new(
- declarations
- .into_iter()
- .map(|(_, _, declaration)| declaration)
- .collect(),
- );
+ let mut positions = Vec::with_capacity(declarations.len());
+ let mut reverted_declarations = Vec::new();
+ let mut store_declarations = Vec::with_capacity(declarations.len());
+ for (store_index, (priority, index, declaration, revert)) in declarations.into_iter().enumerate() {
+ positions.push((priority, index));
+ if let Some(revert) = revert {
+ reverted_declarations.push((store_index, revert));
+ }
+ store_declarations.push(declaration);
+ }
+ let mut store = WinnerStore::new(store_declarations);
+ store.set_reverted_declarations(positions, reverted_declarations);
store.reads_sibling_position = style_query_references
.borrow()
.as_ref()
diff --git a/Libraries/LibWeb/Rust/src/css/style/publication/winner_store.rs b/Libraries/LibWeb/Rust/src/css/style/publication/winner_store.rs
index 6249b4d2c91..657dd0a5a07 100644
--- a/Libraries/LibWeb/Rust/src/css/style/publication/winner_store.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/publication/winner_store.rs
@@ -86,11 +86,23 @@ impl WinnerDeclaration {
}
}
+/// A winner that rolled back below a `revert` or `revert-layer`, as it was declared.
+#[derive(Clone, Copy)]
+pub(super) struct RevertedDeclaration {
+ /// The reverting declaration's priority and its index in its block.
+ pub(super) position: (CascadePriority, usize),
+ pub(super) operator: CascadeOperator,
+}
+
#[derive(Default)]
pub(in crate::css::style) struct WinnerStore {
// NB: Cascade order supplies both source slots and logical/physical tie breaking.
declarations: Vec<WinnerDeclaration>,
by_property: Vec<u16>,
+ /// Each declaration's priority and index in its block, when known, and the declarations that
+ /// reverted, by their index in `declarations`.
+ positions: Vec<(CascadePriority, usize)>,
+ reverted_declarations: Vec<(usize, RevertedDeclaration)>,
/// A style query read the sibling position while substituting these declarations.
pub(super) reads_sibling_position: bool,
}
@@ -106,10 +118,49 @@ impl WinnerStore {
Self {
declarations,
by_property,
+ positions: Vec::new(),
+ reverted_declarations: Vec::new(),
reads_sibling_position: false,
}
}
+ pub(super) fn set_reverted_declarations(
+ &mut self,
+ positions: Vec<(CascadePriority, usize)>,
+ reverted_declarations: Vec<(usize, RevertedDeclaration)>,
+ ) {
+ debug_assert_eq!(positions.len(), self.declarations.len());
+ // NB: The positions only order declarations against a revert, so a store without one keeps none.
+ if reverted_declarations.is_empty() {
+ return;
+ }
+ self.positions = positions;
+ self.reverted_declarations = reverted_declarations;
+ }
+
+ fn reverted_declaration(&self, index: usize) -> Option<&RevertedDeclaration> {
+ self.reverted_declarations
+ .iter()
+ .find_map(|(reverted_index, reverted)| (*reverted_index == index).then_some(reverted))
+ }
+
+ /// https://drafts.csswg.org/css-logical/#box
+ /// Whether the declaration at `reverting` reverted past the one at `other`, its counterpart in a
+ /// logical property group: declared later than `other`, the revert leaves the group's value as if
+ /// `other` were not declared either.
+ fn reverts_past_counterpart(&self, reverting: usize, other: usize) -> bool {
+ let Some(reverted) = self.reverted_declaration(reverting) else {
+ return false;
+ };
+ if self.reverted_declaration(other).is_some() {
+ return false;
+ }
+ let Some(other_position) = self.positions.get(other) else {
+ return false;
+ };
+ reverted.position > *other_position && reverted.position.0.reverts_past(reverted.operator, &other_position.0)
+ }
+
/// NB: Compare the resolved declarations while retaining context-dependent computation guards.
pub(super) fn environment_value_delta(&self, other: &Self, engine: &RetainedState) -> Option<SmallVec<[u16; 8]>> {
if self.reads_sibling_position || other.reads_sibling_position {
@@ -383,6 +434,16 @@ impl CascadedValues for WinnerView<'_> {
fn property_with_higher_priority(&self, first: u16, second: u16) -> u16 {
match (self.store.declaration(first), self.store.declaration(second)) {
+ (Some((first_index, _)), Some((second_index, _)))
+ if self.store.reverts_past_counterpart(first_index, second_index) =>
+ {
+ first
+ }
+ (Some((first_index, _)), Some((second_index, _)))
+ if self.store.reverts_past_counterpart(second_index, first_index) =>
+ {
+ second
+ }
(Some((first_index, _)), Some((second_index, _))) if first_index >= second_index => first,
(Some(_), None) => first,
_ => second,
diff --git a/Tests/LibWeb/Text/expected/css/revert-logical-after-physical.txt b/Tests/LibWeb/Text/expected/css/revert-logical-after-physical.txt
new file mode 100644
index 00000000000..bfbee6adb8a
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/revert-logical-after-physical.txt
@@ -0,0 +1,4 @@
+rule: 0px
+rule-layer: 0px
+inline: 0px
+under-inline-revert-layer: 5px
diff --git a/Tests/LibWeb/Text/expected/css/revert-physical-after-all-unset.txt b/Tests/LibWeb/Text/expected/css/revert-physical-after-all-unset.txt
new file mode 100644
index 00000000000..ebd3c11801f
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/revert-physical-after-all-unset.txt
@@ -0,0 +1,4 @@
+same-rule: 1px solid
+later-rule: 1px solid
+longhands: 1px solid
+logical-later: 0px none
diff --git a/Tests/LibWeb/Text/input/css/revert-logical-after-physical.html b/Tests/LibWeb/Text/input/css/revert-logical-after-physical.html
new file mode 100644
index 00000000000..06b646ca82a
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/revert-logical-after-physical.html
@@ -0,0 +1,21 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ #rule { margin-left: 3px; margin-inline-start: revert; }
+ #rule-layer { margin-left: 3px; margin-inline-start: revert-layer; }
+ #under-inline-revert-layer { margin-left: 5px; }
+</style>
+<!-- A logical property that reverts to nothing computes as unset, but still wins over its physical
+ counterpart declared before it. -->
+<div id="rule"></div>
+<div id="rule-layer"></div>
+<div id="inline" style="margin-left: 3px; margin-inline-start: revert"></div>
+<!-- A revert-layer in inline style only rolls back other inline style, so the rule's margin-left
+ still applies below it. -->
+<div id="under-inline-revert-layer" style="margin-inline-start: revert-layer"></div>
+<script>
+ test(() => {
+ for (const element of document.querySelectorAll("div"))
+ println(`${element.id}: ${getComputedStyle(element).marginLeft}`);
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/css/revert-physical-after-all-unset.html b/Tests/LibWeb/Text/input/css/revert-physical-after-all-unset.html
new file mode 100644
index 00000000000..2ec54e56501
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/revert-physical-after-all-unset.html
@@ -0,0 +1,23 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ .same-rule { all: unset; border: revert; }
+ .later-rule { all: unset; }
+ .later-rule { border: revert; }
+ .longhands { all: unset; border-top-width: revert; border-top-style: revert; }
+ .logical-later { border-top: revert; border-block-start: unset; }
+</style>
+<!-- A physical property that reverts keeps its own place in the cascade relative to its logical
+ associate, which `all` declares, so it rolls back to the user agent's value. -->
+<button class="same-rule">same-rule</button>
+<button class="later-rule">later-rule</button>
+<button class="longhands">longhands</button>
+<button class="logical-later">logical-later</button>
+<script>
+ test(() => {
+ for (const button of document.querySelectorAll("button")) {
+ const style = getComputedStyle(button);
+ println(`${button.className}: ${style.borderTopWidth} ${style.borderTopStyle}`);
+ }
+ });
+</script>