Skip to content

Commit

Permalink
add remove_attribute_ns
Browse files Browse the repository at this point in the history
  • Loading branch information
RavuAlHemio authored and dginev committed Nov 15, 2020
1 parent e83a893 commit 7459130
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/tree/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,34 @@ impl Node {
}
}

/// Removes the property of given `name` and namespace (`ns`)
pub fn remove_property_ns(&mut self, name: &str, ns: &str) -> Result<(), Box<dyn Error>> {
let c_name = CString::new(name).unwrap();
let c_ns = CString::new(ns).unwrap();
unsafe {
let attr_node = xmlHasNsProp(
self.node_ptr_mut()?,
c_name.as_bytes().as_ptr(),
c_ns.as_bytes().as_ptr(),
);
if !attr_node.is_null() {
let remove_prop_status = xmlRemoveProp(attr_node);
if remove_prop_status == 0 {
Ok(())
} else {
// Propagate libxml2 failure to remove
Err(From::from(format!(
"libxml2 failed to remove property with status: {:?}",
remove_prop_status
)))
}
} else {
// silently no-op if asked to remove a property which is not present
Ok(())
}
}
}

/// Alias for get_property
pub fn get_attribute(&self, name: &str) -> Option<String> {
self.get_property(name)
Expand Down Expand Up @@ -484,6 +512,11 @@ impl Node {
self.remove_property(name)
}

/// Alias for remove_property_ns
pub fn remove_attribute_ns(&mut self, name: &str, ns: &str) -> Result<(), Box<dyn Error>> {
self.remove_property_ns(name, ns)
}

/// Get a copy of the attributes of this node
pub fn get_properties(&self) -> HashMap<String, String> {
let mut attributes = HashMap::new();
Expand Down
3 changes: 3 additions & 0 deletions tests/tree_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ fn attribute_namespace_accessors() {
assert!(fb_ns_result.is_ok());
let fb_ns = fb_ns_result.unwrap();
assert!(element.set_attribute_ns("fb", "fb", &fb_ns).is_ok());
assert_eq!(element.get_attribute_ns("fb", "http://www.foobar.org"), Some("fb".to_string()));
assert!(element.remove_attribute_ns("fb", "http://www.foobar.org").is_ok());
assert_eq!(element.get_attribute_ns("fb", "http://www.foobar.org"), None);

let ns_prefix = element.lookup_namespace_prefix("http://www.w3.org/XML/1998/namespace");
assert_eq!(ns_prefix, Some("xml".to_string())); // system ns has the global prefix when doing global lookup
Expand Down

0 comments on commit 7459130

Please sign in to comment.