Skip to content

Commit

Permalink
Implement Document.createElementNS.
Browse files Browse the repository at this point in the history
  • Loading branch information
therealglazou authored and Ms2ger committed Apr 7, 2014
1 parent 86c83f7 commit 4b0da08
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/components/script/dom/bindings/codegen/Bindings.conf
Expand Up @@ -33,6 +33,7 @@ DOMInterfaces = {
'createComment',
'createDocumentFragment',
'createElement',
'createElementNS',
'createProcessingInstruction',
'createTextNode',
'embeds',
Expand Down Expand Up @@ -95,6 +96,7 @@ DOMInterfaces = {
'contains',
'insertBefore',
'isEqualNode',
'namespaceURI',
'nodeName',
'nodeValue',
'normalize',
Expand Down
55 changes: 51 additions & 4 deletions src/components/script/dom/document.rs
Expand Up @@ -9,13 +9,13 @@ use dom::bindings::codegen::InheritTypes::{DocumentTypeCast, HTMLHtmlElementCast
use dom::bindings::codegen::DocumentBinding;
use dom::bindings::js::JS;
use dom::bindings::utils::{Reflectable, Reflector, reflect_dom_object};
use dom::bindings::error::{ErrorResult, Fallible, NotSupported, InvalidCharacter, HierarchyRequest};
use dom::bindings::utils::{xml_name_type, InvalidXMLName};
use dom::bindings::error::{ErrorResult, Fallible, NotSupported, InvalidCharacter, HierarchyRequest, NamespaceError};
use dom::bindings::utils::{xml_name_type, InvalidXMLName, Name, QName};
use dom::comment::Comment;
use dom::documentfragment::DocumentFragment;
use dom::documenttype::DocumentType;
use dom::domimplementation::DOMImplementation;
use dom::element::{Element, AttributeHandlers};
use dom::element::{Element, AttributeHandlers, get_attribute_parts};
use dom::element::{HTMLHtmlElementTypeId, HTMLHeadElementTypeId, HTMLTitleElementTypeId};
use dom::element::{HTMLBodyElementTypeId, HTMLFrameSetElementTypeId};
use dom::event::Event;
Expand All @@ -37,8 +37,9 @@ use dom::location::Location;
use html::hubbub_html_parser::build_element_from_tag;
use hubbub::hubbub::{QuirksMode, NoQuirks, LimitedQuirks, FullQuirks};
use layout_interface::{DocumentDamageLevel, ContentChangedDocumentDamage};
use servo_util::namespace;
use servo_util::namespace::{Namespace, Null};
use servo_util::str::DOMString;
use servo_util::str::{DOMString, null_str_as_empty_ref};

use collections::hashmap::HashMap;
use js::jsapi::JSContext;
Expand Down Expand Up @@ -263,6 +264,52 @@ impl Document {
Ok(build_element_from_tag(local_name, abstract_self))
}

// http://dom.spec.whatwg.org/#dom-document-createelementns
pub fn CreateElementNS(&self, abstract_self: &JS<Document>,
namespace: Option<DOMString>,
qualified_name: DOMString) -> Fallible<JS<Element>> {
let ns = Namespace::from_str(null_str_as_empty_ref(&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(NamespaceError);
},
QName => {}
}

let (prefix_from_qname, local_name_from_qname) = get_attribute_parts(qualified_name);
match (&ns, prefix_from_qname, local_name_from_qname.as_slice()) {
// throw if prefix is not null and namespace is null
(&namespace::Null, Some(_), _) => {
debug!("Namespace can't be null with a non-null prefix");
return Err(NamespaceError);
},
// throw if prefix is "xml" and namespace is not the XML namespace
(_, Some(ref prefix), _) if "xml" == *prefix && ns != namespace::XML => {
debug!("Namespace must be the xml namespace if the prefix is 'xml'");
return Err(NamespaceError);
},
// throw if namespace is the XMLNS namespace and neither qualifiedName nor prefix is "xmlns"
(&namespace::XMLNS, Some(ref prefix), _) if "xmlns" == *prefix => {},
(&namespace::XMLNS, _, "xmlns") => {},
(&namespace::XMLNS, _, _) => {
debug!("The prefix or the qualified name must be 'xmlns' if namespace is the XMLNS namespace ");
return Err(NamespaceError);
},
_ => {}
}

if ns == namespace::HTML {
Ok(build_element_from_tag(local_name_from_qname, abstract_self))
} else {
Ok(Element::new(local_name_from_qname, ns, abstract_self))
}
}

// http://dom.spec.whatwg.org/#dom-document-createdocumentfragment
pub fn CreateDocumentFragment(&self, abstract_self: &JS<Document>) -> JS<DocumentFragment> {
DocumentFragment::new(abstract_self)
Expand Down
12 changes: 10 additions & 2 deletions src/components/script/dom/element.rs
Expand Up @@ -6,6 +6,7 @@

use dom::attr::Attr;
use dom::attrlist::AttrList;
use dom::bindings::codegen::ElementBinding;
use dom::bindings::codegen::InheritTypes::{ElementDerived, HTMLImageElementCast};
use dom::bindings::codegen::InheritTypes::{HTMLIFrameElementCast, NodeCast};
use dom::bindings::codegen::InheritTypes::HTMLObjectElementCast;
Expand Down Expand Up @@ -133,14 +134,16 @@ pub enum ElementTypeId {
HTMLUListElementTypeId,
HTMLVideoElementTypeId,
HTMLUnknownElementTypeId,

ElementTypeId,
}

//
// Element methods
//

impl Element {
pub fn new_inherited(type_id: ElementTypeId, tag_name: ~str, namespace: Namespace, document: JS<Document>) -> Element {
pub fn new_inherited(type_id: ElementTypeId, tag_name: DOMString, namespace: Namespace, document: JS<Document>) -> Element {
Element {
node: Node::new_inherited(ElementNodeTypeId(type_id), document),
tag_name: tag_name,
Expand All @@ -151,6 +154,11 @@ impl Element {
}
}

pub fn new(tag_name: DOMString, namespace: Namespace, document: &JS<Document>) -> JS<Element> {
let element = Element::new_inherited(ElementTypeId, tag_name, namespace, document.clone());
Node::reflect_node(~element, document, ElementBinding::Wrap)
}

pub fn html_element_in_html_document(&self) -> bool {
self.namespace == namespace::HTML &&
self.node.owner_doc().get().is_html_document
Expand Down Expand Up @@ -647,7 +655,7 @@ impl IElement for JS<Element> {
}
}

fn get_attribute_parts(name: DOMString) -> (Option<~str>, ~str) {
pub fn get_attribute_parts(name: DOMString) -> (Option<~str>, ~str) {
//FIXME: Throw for XML-invalid names
//FIXME: Throw for XMLNS-invalid names
let (prefix, local_name) = if name.contains(":") {
Expand Down
5 changes: 3 additions & 2 deletions src/components/script/dom/node.rs
Expand Up @@ -1734,8 +1734,9 @@ impl Node {
}

// http://dom.spec.whatwg.org/#dom-node-namespaceuri
pub fn GetNamespaceURI(&self) -> Option<DOMString> {
None
pub fn GetNamespaceURI(&self, abstract_self: &JS<Node>) -> Option<DOMString> {
let element: Option<JS<Element>> = ElementCast::to(abstract_self);
element.map(|element| element.get().namespace.to_str().to_owned())
}

// http://dom.spec.whatwg.org/#dom-node-prefix
Expand Down
2 changes: 2 additions & 0 deletions src/components/script/dom/webidls/Document.webidl
Expand Up @@ -28,6 +28,8 @@ interface Document : Node {

[Creator, Throws]
Element createElement(DOMString localName);
[Creator, Throws]
Element createElementNS(DOMString? namespace, DOMString qualifiedName);
[Creator]
DocumentFragment createDocumentFragment();
[Creator]
Expand Down
33 changes: 33 additions & 0 deletions src/test/content/test_document_createElementNS.html
@@ -0,0 +1,33 @@
<!DOCTYPE html>
<html>
<head>
<script src="harness.js"></script>
<script id="script">
// test1: createElementNS
{
var htmlElt = document.createElementNS("http://www.w3.org/1999/xhtml", "p");
is_not(htmlElt, null, "test1-0: createElementNS");
is_a(htmlElt, Element, "test1-1: createElementNS");
is_a(htmlElt, HTMLElement, "test1-2: createElementNS");
is_a(htmlElt, HTMLParagraphElement, "test1-3: createElementNS");
is(htmlElt.namespaceURI, "http://www.w3.org/1999/xhtml", "test1-4: createElementNS");

var otherNsElt = document.createElementNS("http://www.example.org/dummy", "p");
is_not(otherNsElt, null, "test1-5: createElementNS");
is_a(otherNsElt, Element, "test1-6: createElementNS");
is_not_a(otherNsElt, HTMLElement, "test1-7: createElementNS");
is(otherNsElt.namespaceURI, "http://www.example.org/dummy", "test1-8: createElementNS");
}
// test2: Node.namespaceURI
{
is(document.namespaceURI, null, "test2.0: createElementNS");
is(document.documentElement.namespaceURI, "http://www.w3.org/1999/xhtml", "test2.1: createElementNS");
var scriptElt = document.getElementById("script");
is(scriptElt.firstChild.namespaceURI, null, "test2.2: createElementNS");
is(scriptElt.nextSibling.namespaceURI, null, "test2.3: createElementNS");
}

finish();
</script><!-- this is a comment -->
</head>
</html>

5 comments on commit 4b0da08

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from jdm
at Ms2ger@4b0da08

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging Ms2ger/servo/createElementNS = 4b0da08 into auto

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ms2ger/servo/createElementNS = 4b0da08 merged ok, testing candidate = f9c9e62

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = f9c9e62

Please sign in to comment.