diff options
-rw-r--r-- | components/script/dom/bindings/utils.rs | 17 | ||||
-rw-r--r-- | components/script/dom/document.rs | 16 | ||||
-rw-r--r-- | components/script/dom/domimplementation.rs | 21 | ||||
-rw-r--r-- | components/script/dom/element.rs | 14 |
4 files changed, 29 insertions, 39 deletions
diff --git a/components/script/dom/bindings/utils.rs b/components/script/dom/bindings/utils.rs index 7faca76b6c6..97f6eaf9392 100644 --- a/components/script/dom/bindings/utils.rs +++ b/components/script/dom/bindings/utils.rs @@ -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)] diff --git a/components/script/dom/document.rs b/components/script/dom/document.rs index 62fdfbeb109..a280507d4ff 100644 --- a/components/script/dom/document.rs +++ b/components/script/dom/document.rs @@ -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) { diff --git a/components/script/dom/domimplementation.rs b/components/script/dom/domimplementation.rs index 7e2a3dba03a..e5cac62f4e7 100644 --- a/components/script/dom/domimplementation.rs +++ b/components/script/dom/domimplementation.rs @@ -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 => { - let document = self.document.root(); - Ok(DocumentType::new(qname, Some(pubid), Some(sysid), document.r())) - } - } + 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(qualified_name, Some(pubid), Some(sysid), document.r())) } // http://dom.spec.whatwg.org/#dom-domimplementation-createdocument diff --git a/components/script/dom/element.rs b/components/script/dom/element.rs index c3bf678a48d..0ebf4d02bea 100644 --- a/components/script/dom/element.rs +++ b/components/script/dom/element.rs @@ -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); |