Skip to content

Commit

Permalink
Understand the "xml" namespace prefix when parsing into the DOM
Browse files Browse the repository at this point in the history
This does not yet *do* anything with the attributes; it only stops
them from preventing a successful parse.

Related to #46
  • Loading branch information
shepmaster committed Aug 10, 2017
1 parent 86111a8 commit 88bed66
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ pub mod writer;

pub use str::XmlChar;

static XML_NS_PREFIX: &'static str = "xml";
static XML_NS_URI: &'static str = "http://www.w3.org/XML/1998/namespace";

/// A prefixed name. This represents what is found in the string form
/// of an XML document, and does not apply any namespace mapping.
#[derive(Debug,Copy,Clone,PartialEq,Eq,PartialOrd,Ord)]
Expand Down
22 changes: 22 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,7 @@ struct DomBuilder<'d> {
elements: Vec<dom::Element<'d>>,
element_names: Vec<Span<PrefixedName<'d>>>,
attributes: Vec<DeferredAttribute<'d>>,
seen_top_element: bool,
}

impl<'d> DomBuilder<'d> {
Expand All @@ -807,6 +808,7 @@ impl<'d> DomBuilder<'d> {
elements: vec![],
element_names: Vec::new(),
attributes: Vec::new(),
seen_top_element: false,
}
}

Expand Down Expand Up @@ -879,6 +881,11 @@ impl<'d> DomBuilder<'d> {
element.register_prefix(*prefix, ns_uri);
}

if !self.seen_top_element {
self.seen_top_element = true;
element.register_prefix(::XML_NS_PREFIX, ::XML_NS_URI);
}

self.append_to_either(element);
self.elements.push(element);

Expand Down Expand Up @@ -1326,6 +1333,21 @@ mod test {
assert_eq!(attr.value(), "b");
}

#[test]
fn an_attribute_with_xml_space_preserve() {
let package = quick_parse("<hello xml:space='preserve'> <a/> </hello>");
let doc = package.as_document();
let top = top(&doc);

assert_eq!(top.attribute((::XML_NS_URI, "space")).unwrap().value(), "preserve");

let children = top.children();
assert_eq!(children.len(), 3);
assert_eq!(children[0].text().unwrap().text(), " ");
assert_qname_eq!(children[1].element().unwrap().name(), "a");
assert_eq!(children[2].text().unwrap().text(), " ");
}

#[test]
fn an_attribute_with_references() {
let package = quick_parse("<log msg='I &lt;3 math' />");
Expand Down
5 changes: 1 addition & 4 deletions src/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ use string_pool::{StringPool,InternedString};
use std::marker::PhantomData;
use std::slice;

static XML_NS_PREFIX: &'static str = "xml";
static XML_NS_URI: &'static str = "http://www.w3.org/XML/1998/namespace";

struct InternedQName {
namespace_uri: Option<InternedString>,
local_part: InternedString,
Expand Down Expand Up @@ -644,7 +641,7 @@ impl Connections {
{
let mut namespaces = Vec::new();

namespaces.push((XML_NS_PREFIX, XML_NS_URI));
namespaces.push((::XML_NS_PREFIX, ::XML_NS_URI));

let all_namespaces =
self.element_parents(element)
Expand Down

0 comments on commit 88bed66

Please sign in to comment.