Skip to content

Commit

Permalink
wrapper for xmlXPathCompile binding
Browse files Browse the repository at this point in the history
  • Loading branch information
bcpeinhardt authored and dginev committed Mar 26, 2022
1 parent 95f9286 commit 6617a19
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/xpath.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,14 @@ impl fmt::Display for Object {
}
}
}

/// Calls the binding to http://xmlsoft.org/html/libxml-xpath.html#xmlXPathCompile and return true if
/// a non-null pointer is returned. The idea is to use this to validate an xpath independent of context.
/// Tests describing what this validates in tests/xpath_tests.rs
pub fn xml_xpath_compiles(xpath: &str) -> bool {
let c_xpath = CString::new(xpath).unwrap();
let xml_xpath_comp_expr_ptr = unsafe { xmlXPathCompile(c_xpath.as_bytes().as_ptr()) };
let result = !xml_xpath_comp_expr_ptr.is_null();
unsafe { libc::free(xml_xpath_comp_expr_ptr as *mut c_void); }
result
}
17 changes: 17 additions & 0 deletions tests/xpath_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,20 @@ fn cleanup_safely_unlinked_xpath_nodes() {
drop(doc);
assert!(true, "Drops went OK.");
}

/// Tests for the fn xml_xpath_compiles
mod compile_tests {
use libxml::xpath::xml_xpath_compiles;

#[test]
fn can_compile_an_xpath() {
let compiles = xml_xpath_compiles("//a");
assert_eq!(compiles, true);
}

#[test]
fn invalid_xpath_does_not_compile() {
let compiles = xml_xpath_compiles("//a[but invalid]");
assert_eq!(compiles, false);
}
}

0 comments on commit 6617a19

Please sign in to comment.