Skip to content

Commit

Permalink
Merge branch 'stuarth-main'
Browse files Browse the repository at this point in the history
  • Loading branch information
fefit committed Oct 27, 2024
2 parents 0d91bac + 093f169 commit c8931a1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
31 changes: 31 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use rphtml::{
},
};
use std::borrow::Cow;
use std::collections::BTreeMap;
use std::rc::Rc;
use std::{any::Any, cell::RefCell};
// re export `IAttrValue` `IEnumTyped` `INodeType`
Expand Down Expand Up @@ -628,6 +629,36 @@ 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
.iter()
.map(|(name, index)| (*index, name.clone()))
.collect::<BTreeMap<usize, String>>();
attr_map
.into_iter()
.map(|(index, name)| {
let attr = &attrs[index];
if let Some(value) = &attr.value {
let attr_value = value.content.clone();
(
name,
IAttrValue::Value(attr_value.iter().collect(), attr.quote),
)
} else {
(name, 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
25 changes: 22 additions & 3 deletions tests/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ type Result = StdResult<(), BoxDynError>;
#[test]
fn test_normal_attr() -> Result {
const ATTR_NAME: &str = "contenteditable";
const HTML: &str = r#"<div contenteditable><input type="text" type="file" /></div>"#;
const HTML: &str = r#"<div class='first' contenteditable><input type="text" type="file" /></div><div class='test-attrs' draggable data-type='link' name='cool' data-type='override'></div>"#;
let root = Vis::load(HTML)?;
let mut div = root.children("div");
let mut div = root.children("div.first");
// has attribute
assert!(div.has_attr(ATTR_NAME));
assert!(!div.has_attr("content"));
Expand All @@ -29,12 +29,31 @@ fn test_normal_attr() -> Result {
assert!(value.as_ref().unwrap().to_string() == "");
assert!(value.as_ref().unwrap().to_list().is_empty());
// always get the first appeared attribute
let input = div.children("input");
let mut input = div.children("input");
let value = input.attr("type");
assert!(value.is_some());
assert!(value.as_ref().unwrap().is_str("text"));
assert!(value.as_ref().unwrap().to_string() == "text");
assert_eq!(value.as_ref().unwrap().to_list(), vec!["text"]);
input.set_attr("type", Some("file"));
assert!(input.attr("type").unwrap().is_str("file"));
// attributes
let attrs_div = root.children("div.test-attrs");
let div = attrs_div.get(0).unwrap();
let attrs = div.get_attributes();
assert_eq!(attrs.len(), 4);
let attr_1 = &attrs[0];
assert_eq!(attr_1.0, String::from("class"));
assert!(attr_1.1.is_str("test-attrs"));
let attr_2 = &attrs[1];
assert_eq!(attr_2.0, String::from("draggable"));
assert!(attr_2.1.is_true());
let attr_3 = &attrs[2];
assert_eq!(attr_3.0, String::from("data-type"));
assert!(attr_3.1.is_str("link"));
let attr_4 = &attrs[3];
assert_eq!(attr_4.0, String::from("name"));
assert!(attr_4.1.is_str("cool"));
// ignore attribute cases: issue #2
let html: &str = r#"<input type="text" READONly /></div>"#;
let root = Vis::load(html)?;
Expand Down

0 comments on commit c8931a1

Please sign in to comment.