Add script::dom::utils::validate_qualified_name()

This commit is contained in:
Anthony Ramine 2015-04-07 23:20:56 +02:00
parent 7e07dcc7ee
commit abc01d598a
4 changed files with 29 additions and 39 deletions

View file

@ -7,7 +7,7 @@
use dom::bindings::codegen::PrototypeList;
use dom::bindings::codegen::PrototypeList::MAX_PROTO_CHAIN_LENGTH;
use dom::bindings::conversions::{native_from_reflector_jsmanaged, is_dom_class};
use dom::bindings::error::throw_type_error;
use dom::bindings::error::{Error, ErrorResult, throw_type_error};
use dom::bindings::global::GlobalRef;
use dom::bindings::js::{Temporary, Root};
use dom::browsercontext;
@ -604,6 +604,21 @@ pub unsafe fn delete_property_by_id(cx: *mut JSContext, object: *mut JSObject,
return true;
}
/// Validate a qualified name. See https://dom.spec.whatwg.org/#validate for details.
pub fn validate_qualified_name(qualified_name: &str) -> ErrorResult {
match xml_name_type(qualified_name) {
XMLName::InvalidXMLName => {
// Step 1.
return Err(Error::InvalidCharacter);
},
XMLName::Name => {
// Step 2.
return Err(Error::Namespace);
},
XMLName::QName => Ok(())
}
}
/// Results of `xml_name_type`.
#[derive(PartialEq)]
#[allow(missing_docs)]

View file

@ -27,8 +27,8 @@ use dom::bindings::js::{MutNullableJS, JS, JSRef, LayoutJS, Temporary, Temporary
use dom::bindings::js::{OptionalRootable, RootedReference};
use dom::bindings::refcounted::Trusted;
use dom::bindings::utils::reflect_dom_object;
use dom::bindings::utils::xml_name_type;
use dom::bindings::utils::XMLName::{QName, Name, InvalidXMLName};
use dom::bindings::utils::{xml_name_type, validate_qualified_name};
use dom::bindings::utils::XMLName::InvalidXMLName;
use dom::comment::Comment;
use dom::customevent::CustomEvent;
use dom::documentfragment::DocumentFragment;
@ -976,17 +976,7 @@ impl<'a> DocumentMethods for JSRef<'a, Document> {
namespace: Option<DOMString>,
qualified_name: DOMString) -> Fallible<Temporary<Element>> {
let ns = namespace::from_domstring(namespace);
match xml_name_type(&qualified_name) {
InvalidXMLName => {
debug!("Not a valid element name");
return Err(InvalidCharacter);
},
Name => {
debug!("Not a valid qualified element name");
return Err(Namespace);
},
QName => {}
}
try!(validate_qualified_name(&qualified_name));
let (prefix_from_qname, local_name_from_qname) = get_attribute_parts(&qualified_name);
match (&ns, prefix_from_qname, local_name_from_qname) {

View file

@ -8,12 +8,10 @@ use dom::bindings::codegen::Bindings::DOMImplementationBinding::DOMImplementatio
use dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
use dom::bindings::codegen::InheritTypes::NodeCast;
use dom::bindings::error::Fallible;
use dom::bindings::error::Error::{InvalidCharacter, Namespace};
use dom::bindings::global::GlobalRef;
use dom::bindings::js::{JS, JSRef, Root, Temporary, OptionalRootable};
use dom::bindings::utils::{Reflector, reflect_dom_object};
use dom::bindings::utils::xml_name_type;
use dom::bindings::utils::XMLName::{QName, Name, InvalidXMLName};
use dom::bindings::utils::validate_qualified_name;
use dom::document::{Document, DocumentHelpers, IsHTMLDocument};
use dom::document::DocumentSource;
use dom::documenttype::DocumentType;
@ -52,18 +50,11 @@ impl DOMImplementation {
// http://dom.spec.whatwg.org/#domimplementation
impl<'a> DOMImplementationMethods for JSRef<'a, DOMImplementation> {
// http://dom.spec.whatwg.org/#dom-domimplementation-createdocumenttype
fn CreateDocumentType(self, qname: DOMString, pubid: DOMString, sysid: DOMString) -> Fallible<Temporary<DocumentType>> {
match xml_name_type(&qname) {
// Step 1.
InvalidXMLName => Err(InvalidCharacter),
// Step 2.
Name => Err(Namespace),
// Step 3.
QName => {
fn CreateDocumentType(self, qualified_name: DOMString, pubid: DOMString, sysid: DOMString)
-> Fallible<Temporary<DocumentType>> {
try!(validate_qualified_name(&qualified_name));
let document = self.document.root();
Ok(DocumentType::new(qname, Some(pubid), Some(sysid), document.r()))
}
}
Ok(DocumentType::new(qualified_name, Some(pubid), Some(sysid), document.r()))
}
// http://dom.spec.whatwg.org/#dom-domimplementation-createdocument

View file

@ -30,8 +30,8 @@ use dom::bindings::error::Error::NoModificationAllowed;
use dom::bindings::js::{MutNullableJS, JS, JSRef, LayoutJS, Temporary, TemporaryPushable};
use dom::bindings::js::{OptionalRootable, RootedReference};
use dom::bindings::trace::RootedVec;
use dom::bindings::utils::xml_name_type;
use dom::bindings::utils::XMLName::{QName, Name, InvalidXMLName};
use dom::bindings::utils::{xml_name_type, validate_qualified_name};
use dom::bindings::utils::XMLName::InvalidXMLName;
use dom::create::create_element;
use dom::domrect::DOMRect;
use dom::domrectlist::DOMRectList;
@ -1037,14 +1037,8 @@ impl<'a> ElementMethods for JSRef<'a, Element> {
// Step 1.
let namespace = namespace::from_domstring(namespace_url);
let name_type = xml_name_type(&name);
match name_type {
// Step 2.
InvalidXMLName => return Err(InvalidCharacter),
// Step 3.
Name => return Err(Error::Namespace),
QName => {}
}
// Steps 2-3.
try!(validate_qualified_name(&name));
// Step 4.
let (prefix, local_name) = get_attribute_parts(&name);