mirror of
https://github.com/typst/typst
synced 2025-05-14 04:56:26 +08:00
Use std::ops::ControlFlow
in Content::traverse
(#6053)
Co-authored-by: Max Mynter <maxmynter@me.com>
This commit is contained in:
parent
1b2714e1a7
commit
91956d1f03
@ -3,7 +3,7 @@ use std::fmt::{self, Debug, Formatter};
|
|||||||
use std::hash::{Hash, Hasher};
|
use std::hash::{Hash, Hasher};
|
||||||
use std::iter::{self, Sum};
|
use std::iter::{self, Sum};
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
use std::ops::{Add, AddAssign, Deref, DerefMut};
|
use std::ops::{Add, AddAssign, ControlFlow, Deref, DerefMut};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use comemo::Tracked;
|
use comemo::Tracked;
|
||||||
@ -414,10 +414,11 @@ impl Content {
|
|||||||
/// 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| -> ControlFlow<()> {
|
||||||
if selector.matches(&element, None) {
|
if selector.matches(&element, None) {
|
||||||
results.push(element);
|
results.push(element);
|
||||||
}
|
}
|
||||||
|
ControlFlow::Continue(())
|
||||||
});
|
});
|
||||||
results
|
results
|
||||||
}
|
}
|
||||||
@ -427,54 +428,58 @@ impl Content {
|
|||||||
///
|
///
|
||||||
/// 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_first(&self, selector: &Selector) -> Option<Content> {
|
pub fn query_first(&self, selector: &Selector) -> Option<Content> {
|
||||||
let mut result = None;
|
self.traverse(&mut |element| -> ControlFlow<Content> {
|
||||||
self.traverse(&mut |element| {
|
if selector.matches(&element, None) {
|
||||||
if result.is_none() && selector.matches(&element, None) {
|
ControlFlow::Break(element)
|
||||||
result = Some(element);
|
} else {
|
||||||
|
ControlFlow::Continue(())
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
result
|
.break_value()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Extracts the plain text of this content.
|
/// Extracts the plain text of this content.
|
||||||
pub fn plain_text(&self) -> EcoString {
|
pub fn plain_text(&self) -> EcoString {
|
||||||
let mut text = EcoString::new();
|
let mut text = EcoString::new();
|
||||||
self.traverse(&mut |element| {
|
self.traverse(&mut |element| -> ControlFlow<()> {
|
||||||
if let Some(textable) = element.with::<dyn PlainText>() {
|
if let Some(textable) = element.with::<dyn PlainText>() {
|
||||||
textable.plain_text(&mut text);
|
textable.plain_text(&mut text);
|
||||||
}
|
}
|
||||||
|
ControlFlow::Continue(())
|
||||||
});
|
});
|
||||||
text
|
text
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Traverse this content.
|
/// Traverse this content.
|
||||||
fn traverse<F>(&self, f: &mut F)
|
fn traverse<F, B>(&self, f: &mut F) -> ControlFlow<B>
|
||||||
where
|
where
|
||||||
F: FnMut(Content),
|
F: FnMut(Content) -> ControlFlow<B>,
|
||||||
{
|
{
|
||||||
f(self.clone());
|
|
||||||
|
|
||||||
self.inner
|
|
||||||
.elem
|
|
||||||
.fields()
|
|
||||||
.into_iter()
|
|
||||||
.for_each(|(_, value)| walk_value(value, f));
|
|
||||||
|
|
||||||
/// Walks a given value to find any content that matches the selector.
|
/// Walks a given value to find any content that matches the selector.
|
||||||
fn walk_value<F>(value: Value, f: &mut F)
|
///
|
||||||
|
/// Returns early if the function gives `ControlFlow::Break`.
|
||||||
|
fn walk_value<F, B>(value: Value, f: &mut F) -> ControlFlow<B>
|
||||||
where
|
where
|
||||||
F: FnMut(Content),
|
F: FnMut(Content) -> ControlFlow<B>,
|
||||||
{
|
{
|
||||||
match value {
|
match value {
|
||||||
Value::Content(content) => content.traverse(f),
|
Value::Content(content) => content.traverse(f),
|
||||||
Value::Array(array) => {
|
Value::Array(array) => {
|
||||||
for value in array {
|
for value in array {
|
||||||
walk_value(value, f);
|
walk_value(value, f)?;
|
||||||
}
|
}
|
||||||
|
ControlFlow::Continue(())
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => ControlFlow::Continue(()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Call f on the element itself before recursively iterating its fields.
|
||||||
|
f(self.clone())?;
|
||||||
|
for (_, value) in self.inner.elem.fields() {
|
||||||
|
walk_value(value, f)?;
|
||||||
|
}
|
||||||
|
ControlFlow::Continue(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user