Skip to content

Commit

Permalink
feat: add tests for get_attributes method and keep the attributes order
Browse files Browse the repository at this point in the history
  • Loading branch information
fefit committed Oct 27, 2024
1 parent e554627 commit 093f169
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
21 changes: 12 additions & 9 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 @@ -634,23 +635,25 @@ impl IElementTrait for Rc<RefCell<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
let attr_map = meta
.borrow()
.lc_name_map
.iter()
.map(|(name, index)| {
let attr = &attrs[*index];
.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.clone(),
name,
IAttrValue::Value(attr_value.iter().collect(), attr.quote),
)
} else {
(name.clone(), IAttrValue::True)
(name, IAttrValue::True)
}
})
.collect()
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 093f169

Please sign in to comment.