diff options
| author | Laurenz <laurmaedje@gmail.com> | 2022-11-26 15:08:23 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2022-11-26 15:09:03 +0100 |
| commit | 36490f7f7bbe450b12faa24eba500909ccda3f85 (patch) | |
| tree | f4d81df927f36c69e2b0ede1926d01044851a2fc /src | |
| parent | 7af46fc025ee08eb78ae7f6898300083c886bf6f (diff) | |
Make text and space nodes unselectable
Diffstat (limited to 'src')
| -rw-r--r-- | src/model/cast.rs | 2 | ||||
| -rw-r--r-- | src/model/eval.rs | 5 | ||||
| -rw-r--r-- | src/model/func.rs | 51 |
3 files changed, 30 insertions, 28 deletions
diff --git a/src/model/cast.rs b/src/model/cast.rs index 0e7739ac..d0a4650a 100644 --- a/src/model/cast.rs +++ b/src/model/cast.rs @@ -185,7 +185,7 @@ dynamic! { Selector: "selector", Value::Str(text) => Self::text(&text), Value::Label(label) => Self::Label(label), - Value::Func(func) => Self::Node(func.node()?, None), + Value::Func(func) => func.select(None)?, @regex: Regex => Self::Regex(regex.clone()), } diff --git a/src/model/eval.rs b/src/model/eval.rs index 6c21a666..da7036b7 100644 --- a/src/model/eval.rs +++ b/src/model/eval.rs @@ -932,10 +932,9 @@ impl Eval for ast::SetRule { } let target = self.target(); - let span = target.span(); - let target = target.eval(vm)?.cast::<Func>().at(span)?; + let target = target.eval(vm)?.cast::<Func>().at(target.span())?; let args = self.args().eval(vm)?; - target.set(args, span) + target.set(args) } } diff --git a/src/model/func.rs b/src/model/func.rs index 4a2a162b..dcb82027 100644 --- a/src/model/func.rs +++ b/src/model/func.rs @@ -5,11 +5,12 @@ use std::sync::Arc; use comemo::{Track, Tracked}; use super::{ - Args, Eval, Flow, Node, NodeId, Route, Scope, Scopes, Selector, StyleMap, Value, Vm, + Args, Dict, Eval, Flow, Node, NodeId, Route, Scope, Scopes, Selector, StyleMap, + Value, Vm, }; use crate::diag::{bail, SourceResult, StrResult}; use crate::syntax::ast::{self, AstNode, Expr}; -use crate::syntax::{SourceId, Span, SyntaxNode}; +use crate::syntax::{SourceId, SyntaxNode}; use crate::util::EcoString; use crate::World; @@ -108,33 +109,35 @@ impl Func { Self(Arc::new(Repr::With(self, args))) } - /// Execute the function's set rule and return the resulting style map. - pub fn set(&self, mut args: Args, span: Span) -> SourceResult<StyleMap> { - let Repr::Native(Native { set: Some(set), .. }) = self.0.as_ref() else { - bail!(span, "this function cannot be customized with set"); - }; - - let styles = set(&mut args)?; - args.finish()?; - Ok(styles) + /// Create a selector for this function's node type, filtering by node's + /// whose [fields](super::Content::field) match the given arguments. + pub fn where_(self, args: &mut Args) -> StrResult<Selector> { + let fields = args.to_named(); + args.items.retain(|arg| arg.name.is_none()); + self.select(Some(fields)) } - /// The id of the node to customize with this function's show rule. - pub fn node(&self) -> StrResult<NodeId> { - match self.0.as_ref() { - Repr::Native(Native { node: Some(id), .. }) => Ok(*id), - _ => Err("this function cannot be customized with show")?, - } + /// Execute the function's set rule and return the resulting style map. + pub fn set(&self, mut args: Args) -> SourceResult<StyleMap> { + Ok(match self.0.as_ref() { + Repr::Native(Native { set: Some(set), .. }) => { + let styles = set(&mut args)?; + args.finish()?; + styles + } + _ => StyleMap::new(), + }) } - /// Create a selector for this function's node type, filtering by node's - /// whose [fields](super::Content::field) match the given arguments. - pub fn where_(self, args: &mut Args) -> StrResult<Selector> { + /// Create a selector for this function's node type. + pub fn select(&self, fields: Option<Dict>) -> StrResult<Selector> { match self.0.as_ref() { - Repr::Native(Native { node: Some(id), .. }) => { - let named = args.to_named(); - args.items.retain(|arg| arg.name.is_none()); - Ok(Selector::Node(*id, Some(named))) + &Repr::Native(Native { node: Some(id), .. }) => { + if id == item!(text_id) { + Err("to select text, please use a string or regex instead")?; + } + + Ok(Selector::Node(id, fields)) } _ => Err("this function is not selectable")?, } |
