Support for and/or selectors in show rules (#3326)

This commit is contained in:
Laurenz 2024-02-02 15:25:19 +01:00 committed by GitHub
parent 356032bf8c
commit 2594b36701
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 34 additions and 13 deletions

View File

@ -176,9 +176,9 @@ impl Selector {
self,
/// The other selectors to match on.
#[variadic]
others: Vec<LocatableSelector>,
others: Vec<Selector>,
) -> Selector {
Self::Or(others.into_iter().map(|s| s.0).chain(Some(self)).collect())
Self::Or(others.into_iter().chain(Some(self)).collect())
}
/// Selects all elements that match this and all of the the other selectors.
@ -187,9 +187,9 @@ impl Selector {
self,
/// The other selectors to match on.
#[variadic]
others: Vec<LocatableSelector>,
others: Vec<Selector>,
) -> Selector {
Self::And(others.into_iter().map(|s| s.0).chain(Some(self)).collect())
Self::And(others.into_iter().chain(Some(self)).collect())
}
/// Returns a modified selector that will only match elements that occur
@ -407,13 +407,17 @@ cast! {
impl FromValue for ShowableSelector {
fn from_value(value: Value) -> StrResult<Self> {
fn validate(selector: &Selector) -> StrResult<()> {
fn validate(selector: &Selector, nested: bool) -> StrResult<()> {
match selector {
Selector::Elem(_, _) => {}
Selector::Label(_) => {}
Selector::Regex(_) => {}
Selector::Or(_)
| Selector::And(_)
Selector::Regex(_) if !nested => {}
Selector::Or(list) | Selector::And(list) => {
for selector in list {
validate(selector, true)?;
}
}
Selector::Regex(_)
| Selector::Location(_)
| Selector::Can(_)
| Selector::Before { .. }
@ -429,7 +433,7 @@ impl FromValue for ShowableSelector {
}
let selector = Selector::from_value(value)?;
validate(&selector)?;
validate(&selector, false)?;
Ok(Self(selector))
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.0 KiB

View File

@ -0,0 +1,21 @@
// Test and/or selectors in show rules.
---
// Looking forward to `heading.where(level: 1 | 2)` :)
#show heading.where(level: 1).or(heading.where(level: 2)): set text(red)
= L1
== L2
=== L3
==== L4
---
// Test element selector combined with label selector.
#show selector(strong).or(<special>): highlight
I am *strong*, I am _emphasized_, and I am #[special<special>].
---
// Ensure that text selector cannot be nested in and/or. That's too complicated,
// at least for now.
// Error: 7-41 this selector cannot be used with show
#show heading.where(level: 1).or("more"): set text(red)

View File

@ -37,7 +37,3 @@ the ```rs &mut T``` reference.
= Red
== Blue
=== Green
---
// Error: 7-35 this selector cannot be used with show
#show selector(heading).or(figure): none