diff options
| author | Sébastien d'Herbais de Thun <sebastien.d.herbais@gmail.com> | 2023-12-07 12:22:07 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-12-07 12:22:07 +0100 |
| commit | 3e96f5f75fe4a63d868365bda91723b59c138814 (patch) | |
| tree | 96c7becbc7e9cd1d27b79f87441ff5f7e49a0d3f | |
| parent | 0bdd2191c073c1ecad29d71a55f81078e9e5f6ac (diff) | |
Fix label in `.fields()` accessor (#2884)
| -rw-r--r-- | crates/typst-macros/src/elem.rs | 12 | ||||
| -rw-r--r-- | tests/typ/bugs/label-fields-dict.typ | 31 |
2 files changed, 43 insertions, 0 deletions
diff --git a/crates/typst-macros/src/elem.rs b/crates/typst-macros/src/elem.rs index b3f86117..d237ba72 100644 --- a/crates/typst-macros/src/elem.rs +++ b/crates/typst-macros/src/elem.rs @@ -922,6 +922,17 @@ fn create_native_elem_impl(element: &Elem) -> TokenStream { .unless_capability("Unlabellable", || quote! { self.label().is_some() }) .unwrap_or_else(|| quote! { false }); + let label_field_dict = element.unless_capability("Unlabellable", || { + quote! { + if let Some(label) = self.label() { + fields.insert( + ::ecow::EcoString::inline("label").into(), + #foundations::IntoValue::into_value(label) + ); + } + } + }); + let mark_prepared = element .unless_capability("Unlabellable", || quote! { self.prepared = true; }) .unwrap_or_else(|| quote! {}); @@ -1077,6 +1088,7 @@ fn create_native_elem_impl(element: &Elem) -> TokenStream { fn fields(&self) -> #foundations::Dict { let mut fields = #foundations::Dict::new(); + #label_field_dict #(#field_dict)* #(#field_opt_dict)* fields diff --git a/tests/typ/bugs/label-fields-dict.typ b/tests/typ/bugs/label-fields-dict.typ new file mode 100644 index 00000000..05c7006a --- /dev/null +++ b/tests/typ/bugs/label-fields-dict.typ @@ -0,0 +1,31 @@ +// Tests whether the label is accessible through the has, field, +// and fields accessors +// Ref: false + +--- +// Test whether the label is accessible through the has method +#show heading: it => { + assert(it.has("label")) + it +} + += Hello, world! <my_label> + +--- +// Test whether the label is accessible through the field method +#show heading: it => { + assert(str(it.label) == "my_label") + it +} + += Hello, world! <my_label> + +--- +// Test whether the label is accessible through the fields method +#show heading: it => { + assert("label" in it.fields()) + assert(str(it.fields().label) == "my_label") + it +} + += Hello, world! <my_label> |
