use xot::Xot;
#[test]
fn test_deduplicate_namespace() {
let mut xot = Xot::new();
let root = xot
.parse(r#"Hello!"#)
.unwrap();
xot.deduplicate_namespaces(root);
assert_eq!(
xot.to_string(root).unwrap(),
r#"Hello!"#
);
}
#[test]
fn test_deduplicate_named_namespace() {
let mut xot = Xot::new();
let root = xot
.parse(r#"Hello!"#)
.unwrap();
xot.deduplicate_namespaces(root);
assert_eq!(
xot.to_string(root).unwrap(),
r#"Hello!"#
);
}
#[test]
fn test_deduplicate_named_namespace_again() {
let mut xot = Xot::new();
let root = xot
.parse(r#"
Title
Para first old Before emphasis emphasis After emphasis
Para second old
I am new
Warning here
"#)
.unwrap();
xot.deduplicate_namespaces(root);
assert_eq!(
xot.to_string(root).unwrap(),
r#"
Title
Para first old Before emphasis emphasis After emphasis
Para second old
I am new
Warning here
"#
);
}
#[test]
fn test_deduplicate_overlapping_namespace_attribute() {
let mut xot = Xot::new();
// we have a default namespace and an attribute in that namespace
// deduplicate should not clean up the overlapping n prefix in this
// case as it's in use by the attribute.
let doc = xot
.parse(r#""#)
.unwrap();
xot.deduplicate_namespaces(doc);
// now serialize the doc; we haven't removed prefix n as it's in use by an
// attribute
assert_eq!(
xot.to_string(doc).unwrap(),
r#""#
);
}
#[test]
fn test_deduplicate_overlapping_namespace_element() {
let mut xot = Xot::new();
let doc = xot
.parse(r#""#)
.unwrap();
xot.deduplicate_namespaces(doc);
// now serialize the doc; we have removed prefix n as it's in use by
// an element only
assert_eq!(
xot.to_string(doc).unwrap(),
r#""#
);
}
#[test]
fn test_deduplicate_overlapping_namespace_deeper() {
let mut xot = Xot::new();
// we have a default namespace and an attribute in that namespace
// deduplicate should not clean up the overlapping n prefix in this
// case as it's in use by the attribute. Here we test the case
// where this attribute is deeper down the tree, away from where the
// prefix is initialized.
let doc = xot
.parse(r#""#)
.unwrap();
xot.deduplicate_namespaces(doc);
// now serialize the doc
// we haven't removed prefix n as it's in use by an attribute
assert_eq!(
xot.to_string(doc).unwrap(),
r#""#
);
}
#[test]
fn test_name_ns_str_no_namespace() {
let mut xot = Xot::new();
let doc = xot.parse(r#""#).unwrap();
let doc_el = xot.document_element(doc).unwrap();
let name = xot.element(doc_el).unwrap().name();
assert_eq!(xot.name_ns_str(name), ("a", ""));
}
#[test]
fn test_name_ns_str_namespace() {
let mut xot = Xot::new();
let doc = xot.parse(r#""#).unwrap();
let doc_el = xot.document_element(doc).unwrap();
let name = xot.element(doc_el).unwrap().name();
assert_eq!(xot.name_ns_str(name), ("a", "http://example.com"));
}
#[test]
fn test_create_missing_prefixes() {
let mut xot = Xot::new();
let doc = xot.parse(r#""#).unwrap();
let root_id = xot.document_element(doc).unwrap();
let ns_id = xot.add_namespace("http://example.com");
let name_id = xot.add_name_ns("a", ns_id);
xot.append_element(root_id, name_id).unwrap();
xot.create_missing_prefixes(root_id).unwrap();
assert_eq!(
xot.to_string(doc).unwrap(),
r#""#
);
}
#[test]
fn test_unresolved_namespaces() {
let mut xot = Xot::new();
let doc = xot
.parse(r#""#)
.unwrap();
let root_id = xot.document_element(doc).unwrap();
let p_el = xot.first_child(root_id).unwrap();
let a_ns = xot.add_namespace("http://example.com/a");
assert_eq!(xot.unresolved_namespaces(p_el), [a_ns]);
}
#[test]
fn test_unresolved_namespaces_resolved() {
let mut xot = Xot::new();
let doc = xot
.parse(r#""#)
.unwrap();
let root_id = xot.document_element(doc).unwrap();
let p_el = xot.first_child(root_id).unwrap();
assert_eq!(xot.unresolved_namespaces(p_el), []);
}
#[test]
fn test_unresolved_namespaces_resolved_deeper() {
let mut xot = Xot::new();
let doc = xot
.parse(r#""#)
.unwrap();
let root_id = xot.document_element(doc).unwrap();
let p_el = xot.first_child(root_id).unwrap();
let a_ns = xot.add_namespace("http://example.com/a");
assert_eq!(xot.unresolved_namespaces(p_el), [a_ns]);
}
#[test]
fn test_is_prefix_defined() {
let mut xot = Xot::new();
let doc = xot
.parse(r#""#)
.unwrap();
let root_id = xot.document_element(doc).unwrap();
let a_prefix = xot.add_prefix("a");
let b_prefix = xot.add_prefix("b");
let xml_prefix = xot.add_prefix("xml");
let p0 = xot.first_child(root_id).unwrap();
// let b_ns = xot.add_namespace("http://example.com/b");
// let p1_name = xot.add_name_ns("p", b_ns);
// xot.append_element(root_id, p1_name).unwrap();
assert!(xot.is_prefix_defined(root_id, a_prefix));
assert!(xot.is_prefix_defined(p0, a_prefix));
assert!(!xot.is_prefix_defined(p0, b_prefix));
assert!(xot.is_prefix_defined(root_id, xml_prefix));
}
#[test]
fn test_prefix_for_namespace() {
let mut xot = Xot::new();
let doc = xot
.parse(r#""#)
.unwrap();
let root_id = xot.document_element(doc).unwrap();
let a_prefix = xot.add_prefix("a");
let a_ns = xot.add_namespace("http://example.com/a");
let b_ns = xot.add_namespace("http://example.com/b");
let xml_prefix = xot.add_prefix("xml");
let xml_ns = xot.add_namespace("http://www.w3.org/XML/1998/namespace");
let p0 = xot.first_child(root_id).unwrap();
// add a p1 that doesn't have a prefix
let p1_name = xot.add_name_ns("p", b_ns);
xot.append_element(root_id, p1_name).unwrap();
let p1 = xot.next_sibling(p0).unwrap();
assert_eq!(xot.prefix_for_namespace(root_id, a_ns), Some(a_prefix));
assert_eq!(xot.prefix_for_namespace(p0, a_ns), Some(a_prefix));
assert_eq!(xot.prefix_for_namespace(p0, b_ns), None);
assert_eq!(xot.prefix_for_namespace(p1, b_ns), None);
assert_eq!(xot.prefix_for_namespace(p0, xml_ns), Some(xml_prefix));
}
#[test]
fn test_prefix_for_namespace_overridden() {
let mut xot = Xot::new();
let doc = xot
.parse(r#""#)
.unwrap();
let root_id = xot.document_element(doc).unwrap();
let a_prefix = xot.add_prefix("a");
let a_ns = xot.add_namespace("http://example.com/a");
let b_ns = xot.add_namespace("http://example.com/b");
let p = xot.first_child(root_id).unwrap();
assert_eq!(xot.prefix_for_namespace(root_id, a_ns), Some(a_prefix));
assert_eq!(xot.prefix_for_namespace(root_id, b_ns), None);
assert_eq!(xot.prefix_for_namespace(p, b_ns), Some(a_prefix));
assert_eq!(xot.prefix_for_namespace(p, a_ns), None);
}
#[test]
fn test_prefix_for_namespace_default_and_undeclared() {
let mut xot = Xot::new();
let doc = xot
.parse(
r#"
"#,
)
.unwrap();
let root_id = xot.document_element(doc).unwrap();
let a_ns = xot.add_namespace("http://example.com/a");
let p = xot.first_child(root_id).unwrap();
let inner = xot.first_child(p).unwrap();
let inside = xot.first_child(inner).unwrap();
assert_eq!(
xot.prefix_for_namespace(root_id, a_ns),
Some(xot.empty_prefix())
);
assert_eq!(xot.prefix_for_namespace(p, a_ns), Some(xot.empty_prefix()));
assert_eq!(xot.prefix_for_namespace(inner, a_ns), None);
assert_eq!(xot.prefix_for_namespace(inside, a_ns), None);
}
#[test]
fn test_namespace_for_prefix() {
let mut xot = Xot::new();
let doc = xot
.parse(r#""#)
.unwrap();
let root_id = xot.document_element(doc).unwrap();
let a_prefix = xot.add_prefix("a");
let a_ns = xot.add_namespace("http://example.com/a");
let b_ns = xot.add_namespace("http://example.com/b");
let b_prefix = xot.add_prefix("b");
let xml_prefix = xot.add_prefix("xml");
let xml_ns = xot.add_namespace("http://www.w3.org/XML/1998/namespace");
let p0 = xot.first_child(root_id).unwrap();
// add a p1 that doesn't have a prefix
let p1_name = xot.add_name_ns("p", b_ns);
xot.append_element(root_id, p1_name).unwrap();
let p1 = xot.next_sibling(p0).unwrap();
assert_eq!(xot.namespace_for_prefix(root_id, a_prefix), Some(a_ns));
assert_eq!(xot.namespace_for_prefix(p0, a_prefix), Some(a_ns));
assert_eq!(xot.namespace_for_prefix(p0, b_prefix), None);
assert_eq!(xot.namespace_for_prefix(p1, b_prefix), None);
assert_eq!(xot.namespace_for_prefix(p0, xml_prefix), Some(xml_ns));
}
#[test]
fn test_namespace_for_prefix_default_and_undeclared() {
let mut xot = Xot::new();
let doc = xot
.parse(
r#"
"#,
)
.unwrap();
let root_id = xot.document_element(doc).unwrap();
let a_ns = xot.add_namespace("http://example.com/a");
let p = xot.first_child(root_id).unwrap();
let inner = xot.first_child(p).unwrap();
let inside = xot.first_child(inner).unwrap();
assert_eq!(
xot.namespace_for_prefix(root_id, xot.empty_prefix()),
Some(a_ns)
);
assert_eq!(xot.namespace_for_prefix(p, xot.empty_prefix()), Some(a_ns));
assert_eq!(xot.namespace_for_prefix(inner, xot.empty_prefix()), None);
assert_eq!(xot.namespace_for_prefix(inside, xot.empty_prefix()), None);
}
#[test]
fn test_namespaces() {
let mut xot = Xot::new();
let doc = xot
.parse(r#""#)
.unwrap();
let root_id = xot.document_element(doc).unwrap();
let a_prefix = xot.add_prefix("a");
let a_ns = xot.add_namespace("http://example.com/a");
let b_ns = xot.add_namespace("http://example.com/b");
let xml_prefix = xot.add_prefix("xml");
let xml_ns = xot.add_namespace("http://www.w3.org/XML/1998/namespace");
let p0 = xot.first_child(root_id).unwrap();
// add a p1 that doesn't have a prefix
let p1_name = xot.add_name_ns("p", b_ns);
xot.append_element(root_id, p1_name).unwrap();
let p1 = xot.next_sibling(p0).unwrap();
assert_eq!(
xot.namespaces_in_scope(p0).collect::>(),
[(a_prefix, a_ns), (xml_prefix, xml_ns)]
);
assert_eq!(
xot.namespaces_in_scope(root_id).collect::>(),
[(a_prefix, a_ns), (xml_prefix, xml_ns)]
);
assert_eq!(
xot.namespaces_in_scope(p1).collect::>(),
[(a_prefix, a_ns), (xml_prefix, xml_ns)]
);
}
#[test]
fn test_namespaces2() {
let mut xot = Xot::new();
let doc = xot
.parse(r#""#)
.unwrap();
let root_id = xot.document_element(doc).unwrap();
let empty_prefix = xot.empty_prefix();
let a_ns = xot.add_namespace("http://example.com/a");
let b_ns = xot.add_namespace("http://example.com/b");
let xml_prefix = xot.add_prefix("xml");
let xml_ns = xot.add_namespace("http://www.w3.org/XML/1998/namespace");
let p0 = xot.first_child(root_id).unwrap();
// add a p1 that doesn't have a prefix
let p1_name = xot.add_name_ns("p", b_ns);
xot.append_element(root_id, p1_name).unwrap();
let p1 = xot.next_sibling(p0).unwrap();
assert_eq!(
xot.namespaces_in_scope(p0).collect::>(),
[(empty_prefix, a_ns), (xml_prefix, xml_ns)]
);
assert_eq!(
xot.namespaces_in_scope(root_id).collect::>(),
[(empty_prefix, a_ns), (xml_prefix, xml_ns)]
);
assert_eq!(
xot.namespaces_in_scope(p1).collect::>(),
[(empty_prefix, a_ns), (xml_prefix, xml_ns)]
);
}
#[test]
fn test_namespaces_overrides() {
let mut xot = Xot::new();
let doc = xot
.parse(r#""#)
.unwrap();
let root_id = xot.document_element(doc).unwrap();
let a_prefix = xot.add_prefix("a");
let a_ns = xot.add_namespace("http://example.com/a");
let b_ns = xot.add_namespace("http://example.com/b");
let xml_prefix = xot.add_prefix("xml");
let xml_ns = xot.add_namespace("http://www.w3.org/XML/1998/namespace");
let p0 = xot.first_child(root_id).unwrap();
// add a p1 that doesn't have a prefix
let p1_name = xot.add_name_ns("p", b_ns);
xot.append_element(root_id, p1_name).unwrap();
let p1 = xot.next_sibling(p0).unwrap();
assert_eq!(
xot.namespaces_in_scope(p0).collect::>(),
[(a_prefix, b_ns), (xml_prefix, xml_ns)]
);
assert_eq!(
xot.namespaces_in_scope(root_id).collect::>(),
[(a_prefix, a_ns), (xml_prefix, xml_ns)]
);
assert_eq!(
xot.namespaces_in_scope(p1).collect::>(),
[(a_prefix, a_ns), (xml_prefix, xml_ns)]
);
}
#[test]
fn test_namespaces_overrides_xml_prefix() {
let mut xot = Xot::new();
let doc = xot
.parse(r#""#)
.unwrap();
let root_id = xot.document_element(doc).unwrap();
let a_ns = xot.add_namespace("http://example.com/a");
let xml_prefix = xot.add_prefix("xml");
let xml_ns = xot.add_namespace("http://www.w3.org/XML/1998/namespace");
let p0 = xot.first_child(root_id).unwrap();
assert_eq!(
xot.namespaces_in_scope(p0).collect::>(),
[(xml_prefix, a_ns)]
);
assert_eq!(
xot.namespaces_in_scope(root_id).collect::>(),
[(xml_prefix, xml_ns)]
);
}
#[test]
fn test_default_namespace_undeclared() {
let mut xot = Xot::new();
let doc = xot
.parse(r#"
"#)
.unwrap();
let xml_prefix = xot.add_prefix("xml");
let xml_ns = xot.add_namespace("http://www.w3.org/XML/1998/namespace");
let root_id = xot.document_element(doc).unwrap();
let p = xot.first_child(root_id).unwrap();
let inner = xot.first_child(p).unwrap();
let a = xot.first_child(inner).unwrap();
assert_eq!(
xot.namespaces_in_scope(a).collect::>(),
[(xml_prefix, xml_ns)]
);
assert_eq!(
xot.namespaces_in_scope(inner).collect::>(),
[(xml_prefix, xml_ns)]
);
}