Skip to content

Commit

Permalink
Do not assume UTF-8 for document encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingun committed Sep 3, 2023
1 parent 983f219 commit a76d401
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 25 deletions.
14 changes: 9 additions & 5 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ pub enum Error {
/// Error for when a reserved namespace is set incorrectly
ReservedNamespaceError {
/// The prefix that is tried to be bound
prefix: String,
prefix: Vec<u8>,
/// Namespace to which prefix tried to be bound
name: String,
name: Vec<u8>,
},
/// Specified namespace prefix is unknown, cannot resolve namespace for it
UnknownPrefix(Vec<u8>),
Expand Down Expand Up @@ -128,9 +128,13 @@ impl fmt::Display for Error {
write_byte_string(f, prefix)?;
f.write_str("'")
},
Error::ReservedNamespaceError{ prefix, name } => write!(
f, "The namespace prefix `{}` with name `{}` is invalid.", prefix, name
),
Error::ReservedNamespaceError{ prefix, name } => {
f.write_str("The namespace prefix '")?;
write_byte_string(f, prefix)?;
f.write_str("' cannot be bound to '")?;
write_byte_string(f, name)?;
f.write_str("'")
},
}
}
}
Expand Down
40 changes: 20 additions & 20 deletions src/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,17 +478,17 @@ impl NamespaceResolver {
if Namespace(&v) != RESERVED_NAMESPACE_XML.1 {
// error, `xml` prefix explicitly set to different value
return Err(Error::ReservedNamespaceError {
prefix: "xml".to_string(),
name: String::from_utf8(v.to_vec()).unwrap(),
prefix: b"xml".to_vec(),
name: v.to_vec(),
});
}
// don't add another NamespaceEntry for the `xml` namespace prefix
}
Some(PrefixDeclaration::Named(b"xmlns")) => {
// error, `xmlns` prefix explicitly set
return Err(Error::ReservedNamespaceError {
prefix: "xmlns".to_string(),
name: String::from_utf8(v.to_vec()).unwrap(),
prefix: b"xmlns".to_vec(),
name: v.to_vec(),
});
}
Some(PrefixDeclaration::Named(prefix)) => {
Expand All @@ -499,8 +499,8 @@ impl NamespaceResolver {
// error, non-`xml` prefix set to xml uri
// error, non-`xmlns` prefix set to xmlns uri
return Err(Error::ReservedNamespaceError {
prefix: String::from_utf8(prefix.to_vec()).unwrap(),
name: String::from_utf8(v.to_vec()).unwrap(),
prefix: prefix.to_vec(),
name: v.to_vec(),
});
}

Expand Down Expand Up @@ -927,8 +927,8 @@ mod namespaces {
&mut buffer,
) {
Err(Error::ReservedNamespaceError { prefix, name }) => {
assert_eq!(prefix, "xml");
assert_eq!(name, "not_correct_namespace");
assert_eq!(prefix, b"xml");
assert_eq!(name, b"not_correct_namespace");
}
x => panic!(
"Expected `Error::ReservedNamespaceError`, but found {:?}",
Expand All @@ -943,8 +943,8 @@ mod namespaces {
let (mut resolver, mut buffer) = NamespaceResolver::new_root_resolver_and_buf();
match resolver.push(&BytesStart::from_content(" xmlns:xml=''", 0), &mut buffer) {
Err(Error::ReservedNamespaceError { prefix, name }) => {
assert_eq!(prefix, "xml");
assert_eq!(name, "");
assert_eq!(prefix, b"xml");
assert_eq!(name, b"");
}
x => panic!(
"Expected `Error::ReservedNamespaceError`, but found {:?}",
Expand All @@ -965,8 +965,8 @@ mod namespaces {
&mut buffer,
) {
Err(Error::ReservedNamespaceError { prefix, name }) => {
assert_eq!(prefix, "not_xml");
assert_eq!(name, "http://www.w3.org/XML/1998/namespace");
assert_eq!(prefix, b"not_xml");
assert_eq!(name, b"http://www.w3.org/XML/1998/namespace");
}
x => panic!(
"Expected `Error::ReservedNamespaceError`, but found {:?}",
Expand Down Expand Up @@ -1009,8 +1009,8 @@ mod namespaces {
&mut buffer,
) {
Err(Error::ReservedNamespaceError { prefix, name }) => {
assert_eq!(prefix, "xmlns");
assert_eq!(name, "http://www.w3.org/2000/xmlns/");
assert_eq!(prefix, b"xmlns");
assert_eq!(name, b"http://www.w3.org/2000/xmlns/");
}
x => panic!(
"Expected `Error::ReservedNamespaceError`, but found {:?}",
Expand All @@ -1028,8 +1028,8 @@ mod namespaces {
&mut buffer,
) {
Err(Error::ReservedNamespaceError { prefix, name }) => {
assert_eq!(prefix, "xmlns");
assert_eq!(name, "not_correct_namespace");
assert_eq!(prefix, b"xmlns");
assert_eq!(name, b"not_correct_namespace");
}
x => panic!(
"Expected `Error::ReservedNamespaceError`, but found {:?}",
Expand All @@ -1044,8 +1044,8 @@ mod namespaces {
let (mut resolver, mut buffer) = NamespaceResolver::new_root_resolver_and_buf();
match resolver.push(&BytesStart::from_content(" xmlns:xmlns=''", 0), &mut buffer) {
Err(Error::ReservedNamespaceError { prefix, name }) => {
assert_eq!(prefix, "xmlns");
assert_eq!(name, "");
assert_eq!(prefix, b"xmlns");
assert_eq!(name, b"");
}
x => panic!(
"Expected `Error::ReservedNamespaceError`, but found {:?}",
Expand All @@ -1066,8 +1066,8 @@ mod namespaces {
&mut buffer,
) {
Err(Error::ReservedNamespaceError { prefix, name }) => {
assert_eq!(prefix, "not_xmlns");
assert_eq!(name, "http://www.w3.org/2000/xmlns/");
assert_eq!(prefix, b"not_xmlns");
assert_eq!(name, b"http://www.w3.org/2000/xmlns/");
}
x => panic!(
"Expected `Error::ReservedNamespaceError`, but found {:?}",
Expand Down

0 comments on commit a76d401

Please sign in to comment.