mirror of
https://github.com/typst/typst
synced 2025-05-29 00:15:35 +08:00
Support for and/or selectors in show rules (#3326)
This commit is contained in:
parent
356032bf8c
commit
2594b36701
@ -176,9 +176,9 @@ impl Selector {
|
|||||||
self,
|
self,
|
||||||
/// The other selectors to match on.
|
/// The other selectors to match on.
|
||||||
#[variadic]
|
#[variadic]
|
||||||
others: Vec<LocatableSelector>,
|
others: Vec<Selector>,
|
||||||
) -> 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.
|
/// Selects all elements that match this and all of the the other selectors.
|
||||||
@ -187,9 +187,9 @@ impl Selector {
|
|||||||
self,
|
self,
|
||||||
/// The other selectors to match on.
|
/// The other selectors to match on.
|
||||||
#[variadic]
|
#[variadic]
|
||||||
others: Vec<LocatableSelector>,
|
others: Vec<Selector>,
|
||||||
) -> 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
|
/// Returns a modified selector that will only match elements that occur
|
||||||
@ -407,13 +407,17 @@ cast! {
|
|||||||
|
|
||||||
impl FromValue for ShowableSelector {
|
impl FromValue for ShowableSelector {
|
||||||
fn from_value(value: Value) -> StrResult<Self> {
|
fn from_value(value: Value) -> StrResult<Self> {
|
||||||
fn validate(selector: &Selector) -> StrResult<()> {
|
fn validate(selector: &Selector, nested: bool) -> StrResult<()> {
|
||||||
match selector {
|
match selector {
|
||||||
Selector::Elem(_, _) => {}
|
Selector::Elem(_, _) => {}
|
||||||
Selector::Label(_) => {}
|
Selector::Label(_) => {}
|
||||||
Selector::Regex(_) => {}
|
Selector::Regex(_) if !nested => {}
|
||||||
Selector::Or(_)
|
Selector::Or(list) | Selector::And(list) => {
|
||||||
| Selector::And(_)
|
for selector in list {
|
||||||
|
validate(selector, true)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Selector::Regex(_)
|
||||||
| Selector::Location(_)
|
| Selector::Location(_)
|
||||||
| Selector::Can(_)
|
| Selector::Can(_)
|
||||||
| Selector::Before { .. }
|
| Selector::Before { .. }
|
||||||
@ -429,7 +433,7 @@ impl FromValue for ShowableSelector {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let selector = Selector::from_value(value)?;
|
let selector = Selector::from_value(value)?;
|
||||||
validate(&selector)?;
|
validate(&selector, false)?;
|
||||||
Ok(Self(selector))
|
Ok(Self(selector))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
BIN
tests/ref/compiler/show-selector-logical.png
Normal file
BIN
tests/ref/compiler/show-selector-logical.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 8.0 KiB |
21
tests/typ/compiler/show-selector-logical.typ
Normal file
21
tests/typ/compiler/show-selector-logical.typ
Normal 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)
|
@ -37,7 +37,3 @@ the ```rs &mut T``` reference.
|
|||||||
= Red
|
= Red
|
||||||
== Blue
|
== Blue
|
||||||
=== Green
|
=== Green
|
||||||
|
|
||||||
---
|
|
||||||
// Error: 7-35 this selector cannot be used with show
|
|
||||||
#show selector(heading).or(figure): none
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user