diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e9634c..dbb642a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ All notable changes to the `dom_query` crate will be documented in this file. - Implemented `NodeRef::is_match` and `NodeRef::is` methods, which allow checking if a node matches a given matcher (`&Matcher`) or selector (`&str`) without creating a `Selection` object. - +- Implemented `Tree::base_uri`, a quick method that returns the base URI of the document based on the `href` attribute of the `` element. `Document::base_uri` and `NodeRef::base_uri` provide the same functionality. Inspired by [Node: baseURI property]( https://developer.mozilla.org/en-US/docs/Web/API/Node/baseURI). ### Changed diff --git a/tests/node-traversal.rs b/tests/node-traversal.rs index 567e9c4..3a9d17a 100644 --- a/tests/node-traversal.rs +++ b/tests/node-traversal.rs @@ -261,8 +261,21 @@ fn test_node_base_uri() { "#; let doc = Document::from(contents); + // during first call of .base_uri, the result will be cached with OnceCell + let base_uri = doc.base_uri().unwrap(); + assert_eq!(base_uri.as_ref(), "https://www.example.com/"); + let sel = doc.select_single("#main"); let node = sel.nodes().first().unwrap(); + // using cached result. Access at any node of the tree. let base_uri = node.base_uri().unwrap(); assert_eq!(base_uri.as_ref(), "https://www.example.com/"); } + + +#[cfg_attr(not(target_arch = "wasm32"), test)] +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] +fn test_node_base_uri_none() { + let doc = Document::from(ANCESTORS_CONTENTS); + assert!(doc.base_uri().is_none()); +} \ No newline at end of file