Skip to content

Commit

Permalink
add get_attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
stuarth committed Oct 26, 2024
1 parent 0d91bac commit e554627
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,34 @@ impl IElementTrait for Rc<RefCell<Node>> {
None
}

fn get_attributes(&self) -> Vec<(String, IAttrValue)> {
let node = &self.borrow();
let meta = node
.meta
.as_ref()
.expect("Element node must have a meta field.");

let attrs = &meta.borrow().attrs;

let attr_map = &meta.borrow().lc_name_map;

attr_map
.iter()
.map(|(name, index)| {
let attr = &attrs[*index];
if let Some(value) = &attr.value {
let attr_value = value.content.clone();
(
name.clone(),
IAttrValue::Value(attr_value.iter().collect(), attr.quote),
)
} else {
(name.clone(), IAttrValue::True)
}
})
.collect()
}

/// impl `set_attribute`
fn set_attribute(&mut self, name: &str, value: Option<&str>) {
let mut need_quote = false;
Expand Down
1 change: 1 addition & 0 deletions src/mesdoc/interface/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ pub trait IElementTrait: INodeTrait {
fn children_by<'a>(&'a self, matcher: Box<dyn FnMut(&dyn IElementTrait) + 'a>);
// attribute
fn get_attribute(&self, name: &str) -> Option<IAttrValue>;
fn get_attributes(&self) -> Vec<(String, IAttrValue)>;
fn set_attribute(&mut self, name: &str, value: Option<&str>);
fn remove_attribute(&mut self, name: &str);
fn has_attribute(&self, name: &str) -> bool {
Expand Down

0 comments on commit e554627

Please sign in to comment.