mirror of
https://github.com/typst/typst
synced 2025-05-15 09:35:28 +08:00
Implement basic .contains
ancestry selector
This commit is contained in:
parent
e4f8e57c53
commit
ba4d1dacaf
@ -412,7 +412,7 @@ impl Content {
|
||||
/// Queries the content tree for all elements that match the given selector.
|
||||
///
|
||||
/// Elements produced in `show` rules will not be included in the results.
|
||||
pub fn query(&self, selector: Selector) -> Vec<Content> {
|
||||
pub fn query(&self, selector: &Selector) -> Vec<Content> {
|
||||
let mut results = Vec::new();
|
||||
self.traverse(&mut |element| {
|
||||
if selector.matches(&element, None) {
|
||||
|
@ -93,6 +93,9 @@ pub enum Selector {
|
||||
Before { selector: Arc<Self>, end: Arc<Self>, inclusive: bool },
|
||||
/// Matches all matches of `selector` after `start`.
|
||||
After { selector: Arc<Self>, start: Arc<Self>, inclusive: bool },
|
||||
/// Matches all children of `selector` matchin `c`
|
||||
/// TODO: Better name
|
||||
Contains { selector: Arc<Self>, c: Arc<Self> },
|
||||
}
|
||||
|
||||
impl Selector {
|
||||
@ -139,7 +142,10 @@ impl Selector {
|
||||
}
|
||||
Self::Location(location) => target.location() == Some(*location),
|
||||
// Not supported here.
|
||||
Self::Regex(_) | Self::Before { .. } | Self::After { .. } => false,
|
||||
Self::Regex(_)
|
||||
| Self::Before { .. }
|
||||
| Self::After { .. }
|
||||
| Self::Contains { .. } => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -221,6 +227,11 @@ impl Selector {
|
||||
inclusive,
|
||||
}
|
||||
}
|
||||
|
||||
#[func]
|
||||
pub fn contains(self, c: LocatableSelector) -> Selector {
|
||||
Self::Contains { selector: Arc::new(self), c: Arc::new(c.0) }
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Location> for Selector {
|
||||
@ -266,6 +277,7 @@ impl Repr for Selector {
|
||||
inclusive_arg
|
||||
)
|
||||
}
|
||||
Self::Contains { .. } => todo!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -352,7 +364,8 @@ impl FromValue for LocatableSelector {
|
||||
}
|
||||
}
|
||||
Selector::Before { selector, end: split, .. }
|
||||
| Selector::After { selector, start: split, .. } => {
|
||||
| Selector::After { selector, start: split, .. }
|
||||
| Selector::Contains { selector, c: split } => {
|
||||
for selector in [selector, split] {
|
||||
validate(selector)?;
|
||||
}
|
||||
@ -431,7 +444,8 @@ impl FromValue for ShowableSelector {
|
||||
| Selector::Location(_)
|
||||
| Selector::Can(_)
|
||||
| Selector::Before { .. }
|
||||
| Selector::After { .. } => {
|
||||
| Selector::After { .. }
|
||||
| Selector::Contains { .. } => {
|
||||
bail!("this selector cannot be used with show")
|
||||
}
|
||||
}
|
||||
|
@ -185,6 +185,11 @@ impl Introspector {
|
||||
}
|
||||
list
|
||||
}
|
||||
Selector::Contains { selector, c } => self
|
||||
.query(selector)
|
||||
.iter()
|
||||
.flat_map(|children| children.query(c))
|
||||
.collect(),
|
||||
// Not supported here.
|
||||
Selector::Can(_) | Selector::Regex(_) => EcoVec::new(),
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user