mirror of
https://github.com/typst/typst
synced 2025-05-14 17:15:28 +08:00
Fix show rules on queried elements (#4166)
This commit is contained in:
parent
2ecd0b126f
commit
836157a924
@ -198,9 +198,14 @@ fn prepare(
|
|||||||
// Generate a location for the element, which uniquely identifies it in
|
// Generate a location for the element, which uniquely identifies it in
|
||||||
// the document. This has some overhead, so we only do it for elements
|
// the document. This has some overhead, so we only do it for elements
|
||||||
// that are explicitly marked as locatable and labelled elements.
|
// that are explicitly marked as locatable and labelled elements.
|
||||||
if target.can::<dyn Locatable>() || target.label().is_some() {
|
//
|
||||||
|
// The element could already have a location even if it is not prepared
|
||||||
|
// when it stems from a query.
|
||||||
|
let mut located = target.location().is_some();
|
||||||
|
if !located && (target.can::<dyn Locatable>() || target.label().is_some()) {
|
||||||
let location = engine.locator.locate(hash128(&target));
|
let location = engine.locator.locate(hash128(&target));
|
||||||
target.set_location(location);
|
target.set_location(location);
|
||||||
|
located = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply built-in show-set rules. User-defined show-set rules are already
|
// Apply built-in show-set rules. User-defined show-set rules are already
|
||||||
@ -220,24 +225,24 @@ fn prepare(
|
|||||||
// available in rules.
|
// available in rules.
|
||||||
target.materialize(styles.chain(map));
|
target.materialize(styles.chain(map));
|
||||||
|
|
||||||
|
if located {
|
||||||
|
// Apply metadata to be able to find the element in the frames. Do this
|
||||||
|
// after synthesis and materialization, so that it includes the
|
||||||
|
// synthesized fields. Do it before marking as prepared so that show-set
|
||||||
|
// rules will apply to this element when queried. This adds a style to
|
||||||
|
// the whole element's subtree identifying it as belonging to the
|
||||||
|
// element.
|
||||||
|
map.set(MetaElem::set_data(smallvec![Meta::Elem(target.clone())]));
|
||||||
|
}
|
||||||
|
|
||||||
// Ensure that this preparation only runs once by marking the element as
|
// Ensure that this preparation only runs once by marking the element as
|
||||||
// prepared.
|
// prepared.
|
||||||
target.mark_prepared();
|
target.mark_prepared();
|
||||||
|
|
||||||
// Apply metadata be able to find the element in the frames.
|
// If the element is located, return an extra meta elem that will be
|
||||||
// Do this after synthesis, so that it includes the synthesized fields.
|
// attached so that the metadata styles are not lost in case the element's
|
||||||
if target.location().is_some() {
|
// show rule results in nothing.
|
||||||
// Add a style to the whole element's subtree identifying it as
|
Ok(located.then(|| Packed::new(MetaElem::new()).spanned(target.span())))
|
||||||
// belonging to the element.
|
|
||||||
map.set(MetaElem::set_data(smallvec![Meta::Elem(target.clone())]));
|
|
||||||
|
|
||||||
// Return an extra meta elem that will be attached so that the metadata
|
|
||||||
// styles are not lost in case the element's show rule results in
|
|
||||||
// nothing.
|
|
||||||
return Ok(Some(Packed::new(MetaElem::new()).spanned(target.span())));
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(None)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Apply a step.
|
/// Apply a step.
|
||||||
|
BIN
tests/ref/issue-3726-query-show-set.png
Normal file
BIN
tests/ref/issue-3726-query-show-set.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.2 KiB |
@ -265,3 +265,19 @@
|
|||||||
)),
|
)),
|
||||||
([Frog], [GiraffeCat], [Iguana])
|
([Frog], [GiraffeCat], [Iguana])
|
||||||
)
|
)
|
||||||
|
|
||||||
|
--- issue-3726-query-show-set ---
|
||||||
|
// Test that show rules apply to queried elements, i.e. that the content
|
||||||
|
// returned from `query` isn't yet marked as prepared.
|
||||||
|
#set heading(numbering: "1.")
|
||||||
|
#show heading: underline
|
||||||
|
= Hi
|
||||||
|
|
||||||
|
#set heading(numbering: "I.")
|
||||||
|
#show heading: set text(blue)
|
||||||
|
#show heading: highlight.with(fill: aqua.lighten(50%))
|
||||||
|
= Bye
|
||||||
|
|
||||||
|
// New show rules apply to this, but its location and the materialized fields
|
||||||
|
// from the original are retained.
|
||||||
|
#context query(heading).join()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user