mirror of
https://github.com/typst/typst
synced 2025-05-15 17:45:27 +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.
|
/// Queries the content tree for all elements that match the given selector.
|
||||||
///
|
///
|
||||||
/// Elements produced in `show` rules will not be included in the results.
|
/// 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();
|
let mut results = Vec::new();
|
||||||
self.traverse(&mut |element| {
|
self.traverse(&mut |element| {
|
||||||
if selector.matches(&element, None) {
|
if selector.matches(&element, None) {
|
||||||
|
@ -93,6 +93,9 @@ pub enum Selector {
|
|||||||
Before { selector: Arc<Self>, end: Arc<Self>, inclusive: bool },
|
Before { selector: Arc<Self>, end: Arc<Self>, inclusive: bool },
|
||||||
/// Matches all matches of `selector` after `start`.
|
/// Matches all matches of `selector` after `start`.
|
||||||
After { selector: Arc<Self>, start: Arc<Self>, inclusive: bool },
|
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 {
|
impl Selector {
|
||||||
@ -139,7 +142,10 @@ impl Selector {
|
|||||||
}
|
}
|
||||||
Self::Location(location) => target.location() == Some(*location),
|
Self::Location(location) => target.location() == Some(*location),
|
||||||
// Not supported here.
|
// 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,
|
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 {
|
impl From<Location> for Selector {
|
||||||
@ -266,6 +277,7 @@ impl Repr for Selector {
|
|||||||
inclusive_arg
|
inclusive_arg
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
Self::Contains { .. } => todo!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -352,7 +364,8 @@ impl FromValue for LocatableSelector {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Selector::Before { selector, end: split, .. }
|
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] {
|
for selector in [selector, split] {
|
||||||
validate(selector)?;
|
validate(selector)?;
|
||||||
}
|
}
|
||||||
@ -431,7 +444,8 @@ impl FromValue for ShowableSelector {
|
|||||||
| Selector::Location(_)
|
| Selector::Location(_)
|
||||||
| Selector::Can(_)
|
| Selector::Can(_)
|
||||||
| Selector::Before { .. }
|
| Selector::Before { .. }
|
||||||
| Selector::After { .. } => {
|
| Selector::After { .. }
|
||||||
|
| Selector::Contains { .. } => {
|
||||||
bail!("this selector cannot be used with show")
|
bail!("this selector cannot be used with show")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -185,6 +185,11 @@ impl Introspector {
|
|||||||
}
|
}
|
||||||
list
|
list
|
||||||
}
|
}
|
||||||
|
Selector::Contains { selector, c } => self
|
||||||
|
.query(selector)
|
||||||
|
.iter()
|
||||||
|
.flat_map(|children| children.query(c))
|
||||||
|
.collect(),
|
||||||
// Not supported here.
|
// Not supported here.
|
||||||
Selector::Can(_) | Selector::Regex(_) => EcoVec::new(),
|
Selector::Can(_) | Selector::Regex(_) => EcoVec::new(),
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user