diff --git a/crates/typst-library/src/foundations/selector.rs b/crates/typst-library/src/foundations/selector.rs index 58a43224b..312e5299f 100644 --- a/crates/typst-library/src/foundations/selector.rs +++ b/crates/typst-library/src/foundations/selector.rs @@ -93,8 +93,8 @@ pub enum Selector { Before { selector: Arc, end: Arc, inclusive: bool }, /// Matches all matches of `selector` after `start`. After { selector: Arc, start: Arc, inclusive: bool }, - /// Matches all children of `selector` matching `children_selector` - Contains { selector: Arc, children_selector: Arc }, + /// Matches all children of `ancestor` matching `selector` + Within { selector: Arc, ancestor: Arc }, } impl Selector { @@ -144,7 +144,7 @@ impl Selector { Self::Regex(_) | Self::Before { .. } | Self::After { .. } - | Self::Contains { .. } => false, + | Self::Within { .. } => false, } } } @@ -227,13 +227,12 @@ impl Selector { } } - /// Returns a modified selector that only matches children of what `self` - /// selects that match `children_selector`. + /// Returns a modified selector that only matches `self` if it is contained in an `ancestor`. #[func] - pub fn contains(self, children_selector: LocatableSelector) -> Selector { - Self::Contains { + pub fn within(self, ancestor: LocatableSelector) -> Selector { + Self::Within { selector: Arc::new(self), - children_selector: Arc::new(children_selector.0), + ancestor: Arc::new(ancestor.0), } } } @@ -281,7 +280,7 @@ impl Repr for Selector { inclusive_arg ) } - Self::Contains { selector, children_selector } => { + Self::Within { selector, ancestor: children_selector } => { eco_format!("{}.contains({})", selector.repr(), children_selector.repr()) } } @@ -371,7 +370,7 @@ impl FromValue for LocatableSelector { } Selector::Before { selector, end: split, .. } | Selector::After { selector, start: split, .. } - | Selector::Contains { selector, children_selector: split } => { + | Selector::Within { selector, ancestor: split } => { for selector in [selector, split] { validate(selector)?; } @@ -451,7 +450,7 @@ impl FromValue for ShowableSelector { | Selector::Can(_) | Selector::Before { .. } | Selector::After { .. } - | Selector::Contains { .. } => { + | Selector::Within { .. } => { bail!("this selector cannot be used with show") } } diff --git a/crates/typst-library/src/introspection/introspector.rs b/crates/typst-library/src/introspection/introspector.rs index 83ccb77aa..c09a1f3b6 100644 --- a/crates/typst-library/src/introspection/introspector.rs +++ b/crates/typst-library/src/introspection/introspector.rs @@ -185,10 +185,10 @@ impl Introspector { } list } - Selector::Contains { selector, children_selector } => self - .query(selector) + Selector::Within { selector, ancestor } => self + .query(ancestor) .iter() - .flat_map(|children| children.query(children_selector)) + .flat_map(|children| children.query(selector)) .collect(), // Not supported here. Selector::Can(_) | Selector::Regex(_) => EcoVec::new(),