From 3e96f5f75fe4a63d868365bda91723b59c138814 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20d=27Herbais=20de=20Thun?= Date: Thu, 7 Dec 2023 12:22:07 +0100 Subject: [PATCH] Fix label in `.fields()` accessor (#2884) --- crates/typst-macros/src/elem.rs | 12 +++++++++++ tests/typ/bugs/label-fields-dict.typ | 31 ++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 tests/typ/bugs/label-fields-dict.typ diff --git a/crates/typst-macros/src/elem.rs b/crates/typst-macros/src/elem.rs index b3f861172..d237ba72b 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 000000000..05c7006ad --- /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! + +--- +// Test whether the label is accessible through the field method +#show heading: it => { + assert(str(it.label) == "my_label") + it +} + += Hello, world! + +--- +// 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!