From 32d51ae6d114dd3ac8bf8939422dad339fb0c946 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Tue, 15 Jul 2025 10:45:13 +0200 Subject: [PATCH] Add `HtmlElem::with_optional_attr` --- crates/typst-html/src/lib.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/crates/typst-html/src/lib.rs b/crates/typst-html/src/lib.rs index d7b29dbbc..e3d33bf5a 100644 --- a/crates/typst-html/src/lib.rs +++ b/crates/typst-html/src/lib.rs @@ -79,6 +79,19 @@ impl HtmlElem { self } + /// Adds the attribute to the element if value is not `None`. + pub fn with_optional_attr( + self, + attr: HtmlAttr, + value: Option>, + ) -> Self { + if let Some(value) = value { + self.with_attr(attr, value) + } else { + self + } + } + /// Adds CSS styles to an element. fn with_styles(self, properties: css::Properties) -> Self { if let Some(value) = properties.into_inline_styles() {