From 1edeab2b7b7d039fd9dd83bc14255bddff47618c Mon Sep 17 00:00:00 2001 From: Martin Fischer Date: Wed, 16 Aug 2023 18:39:05 +0200 Subject: fix: report value offset/span as None for the empty attr syntax --- src/attr.rs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/attr.rs b/src/attr.rs index d062a84..48ae5b0 100644 --- a/src/attr.rs +++ b/src/attr.rs @@ -33,6 +33,8 @@ pub(crate) struct AttrInternal { /// The start offset of the attribute name. pub name_offset: O, /// The start offset of the attribute value. + /// For the empty attribute syntax this is just `S::Offset::default()`. + /// We intentionally don't use `Option` here to spare us a byte per attribute. pub value_offset: O, pub value_syntax: Option, } @@ -65,7 +67,8 @@ pub struct AttributeOwned { /// The start offset of the attribute name. pub name_offset: O, /// The start offset of the attribute value. - pub value_offset: O, // TODO: wrap this in an Option once we can recognize the empty attribute syntax + /// `None` in case of the empty attribute syntax (e.g. `disabled` in ``). + pub value_offset: Option, /// The syntax of the attribute value. /// `None` indicates the empty attribute syntax (e.g. `disabled` in ``). pub value_syntax: Option, @@ -96,9 +99,14 @@ impl<'a, O: Offset> Attribute<'a, O> { self.map_val.name_offset..self.map_val.name_offset + self.name.len() } - /// Calculates the span of the attribute value and returns it. - pub fn value_span(&self) -> Range { - self.map_val.value_offset..self.map_val.value_offset + self.map_val.value.len() + /// For explicitly defined values calculates the span of the attribute value and returns it. + /// + /// Returns `None` for attributes using the empty attribute syntax (e.g. `disabled` in ``). + pub fn value_span(&self) -> Option> { + if self.map_val.value_syntax.is_none() { + return None; + } + Some(self.map_val.value_offset..self.map_val.value_offset + self.map_val.value.len()) } /// Returns the attribute value syntax in case the value is explicitly defined. @@ -141,7 +149,10 @@ impl Iterator for AttrIntoIter { name, value: map_val.value, name_offset: map_val.name_offset, - value_offset: map_val.value_offset, + value_offset: map_val + .value_syntax + .is_some() + .then_some(map_val.value_offset), value_syntax: map_val.value_syntax, }) } -- cgit v1.2.3