diff --git a/crates/typst/src/foundations/content.rs b/crates/typst/src/foundations/content.rs index 6f912086e..0d764f115 100644 --- a/crates/typst/src/foundations/content.rs +++ b/crates/typst/src/foundations/content.rs @@ -14,8 +14,8 @@ use smallvec::smallvec; use crate::diag::{SourceResult, StrResult}; use crate::engine::Engine; use crate::foundations::{ - elem, func, scope, ty, Dict, Element, Fields, Guard, IntoValue, Label, NativeElement, - Recipe, Repr, Selector, Str, Style, StyleChain, Styles, Value, + elem, func, scope, ty, Dict, Element, Fields, IntoValue, Label, NativeElement, + Recipe, RecipeIndex, Repr, Selector, Str, Style, StyleChain, Styles, Value, }; use crate::introspection::{Location, Meta, MetaElem}; use crate::layout::{AlignElem, Alignment, Axes, Length, MoveElem, PadElem, Rel, Sides}; @@ -142,8 +142,8 @@ impl Content { } /// Check whether a show rule recipe is disabled. - pub fn is_guarded(&self, guard: Guard) -> bool { - self.inner.lifecycle.contains(guard.0) + pub fn is_guarded(&self, index: RecipeIndex) -> bool { + self.inner.lifecycle.contains(index.0) } /// Whether this content has already been prepared. @@ -157,8 +157,8 @@ impl Content { } /// Disable a show rule recipe. - pub fn guarded(mut self, guard: Guard) -> Self { - self.make_mut().lifecycle.insert(guard.0); + pub fn guarded(mut self, index: RecipeIndex) -> Self { + self.make_mut().lifecycle.insert(index.0); self } diff --git a/crates/typst/src/foundations/element.rs b/crates/typst/src/foundations/element.rs index 6d73a896d..412e30892 100644 --- a/crates/typst/src/foundations/element.rs +++ b/crates/typst/src/foundations/element.rs @@ -336,7 +336,3 @@ pub enum Behaviour { /// An element that does not have a visual representation. Invisible, } - -/// Guards content against being affected by the same show rule multiple times. -#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] -pub struct Guard(pub usize); diff --git a/crates/typst/src/foundations/styles.rs b/crates/typst/src/foundations/styles.rs index 12ba28763..c6ee3c5e2 100644 --- a/crates/typst/src/foundations/styles.rs +++ b/crates/typst/src/foundations/styles.rs @@ -133,6 +133,7 @@ impl Styles { self.0.iter().find_map(|entry| match &**entry { Style::Property(property) => property.is_of(elem).then_some(property.span), Style::Recipe(recipe) => recipe.is_of(elem).then_some(Some(recipe.span)), + Style::Revocation(_) => None, }) } @@ -179,6 +180,8 @@ pub enum Style { Property(Property), /// A show rule recipe. Recipe(Recipe), + /// Disables a specific show rule recipe. + Revocation(RecipeIndex), } impl Style { @@ -204,6 +207,7 @@ impl Debug for Style { match self { Self::Property(property) => property.fmt(f), Self::Recipe(recipe) => recipe.fmt(f), + Self::Revocation(guard) => guard.fmt(f), } } } @@ -413,6 +417,10 @@ impl Debug for Recipe { } } +/// Identifies a show rule recipe from the top of the chain. +#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] +pub struct RecipeIndex(pub usize); + /// A show rule transformation that can be applied to a match. #[derive(Clone, PartialEq, Hash)] pub enum Transformation { @@ -522,13 +530,8 @@ impl<'a> StyleChain<'a> { next(self.properties::(func, id, inherent).cloned(), &default) } - /// Iterate over all style recipes in the chain. - pub fn recipes(self) -> impl Iterator { - self.entries().filter_map(Style::recipe) - } - /// Iterate over all values for the given property in the chain. - pub fn properties( + fn properties( self, func: Element, id: u8, @@ -562,7 +565,7 @@ impl<'a> StyleChain<'a> { } /// Iterate over the entries of the chain. - fn entries(self) -> Entries<'a> { + pub fn entries(self) -> Entries<'a> { Entries { inner: [].as_slice().iter(), links: self.links() } } @@ -646,7 +649,7 @@ impl Chainable for Styles { } /// An iterator over the entries in a style chain. -struct Entries<'a> { +pub struct Entries<'a> { inner: std::slice::Iter<'a, Prehashed