mirror of
https://github.com/servo/servo.git
synced 2025-08-03 04:30:10 +01:00
implement Document#createAttribute
This commit is contained in:
parent
b4c3aec383
commit
4b754bd457
8 changed files with 43 additions and 66 deletions
|
@ -82,7 +82,7 @@ pub struct Attr {
|
||||||
prefix: Option<DOMString>,
|
prefix: Option<DOMString>,
|
||||||
|
|
||||||
/// the element that owns this attribute.
|
/// the element that owns this attribute.
|
||||||
owner: JS<Element>,
|
owner: Option<JS<Element>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Reflectable for Attr {
|
impl Reflectable for Attr {
|
||||||
|
@ -94,7 +94,7 @@ impl Reflectable for Attr {
|
||||||
impl Attr {
|
impl Attr {
|
||||||
fn new_inherited(local_name: Atom, value: AttrValue,
|
fn new_inherited(local_name: Atom, value: AttrValue,
|
||||||
name: Atom, namespace: Namespace,
|
name: Atom, namespace: Namespace,
|
||||||
prefix: Option<DOMString>, owner: JSRef<Element>) -> Attr {
|
prefix: Option<DOMString>, owner: Option<JSRef<Element>>) -> Attr {
|
||||||
Attr {
|
Attr {
|
||||||
reflector_: Reflector::new(),
|
reflector_: Reflector::new(),
|
||||||
local_name: local_name,
|
local_name: local_name,
|
||||||
|
@ -102,13 +102,13 @@ impl Attr {
|
||||||
name: name,
|
name: name,
|
||||||
namespace: namespace,
|
namespace: namespace,
|
||||||
prefix: prefix,
|
prefix: prefix,
|
||||||
owner: JS::from_rooted(owner),
|
owner: owner.map(|o| JS::from_rooted(o)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new(window: JSRef<Window>, local_name: Atom, value: AttrValue,
|
pub fn new(window: JSRef<Window>, local_name: Atom, value: AttrValue,
|
||||||
name: Atom, namespace: Namespace,
|
name: Atom, namespace: Namespace,
|
||||||
prefix: Option<DOMString>, owner: JSRef<Element>) -> Temporary<Attr> {
|
prefix: Option<DOMString>, owner: Option<JSRef<Element>>) -> Temporary<Attr> {
|
||||||
reflect_dom_object(box Attr::new_inherited(local_name, value, name, namespace, prefix, owner),
|
reflect_dom_object(box Attr::new_inherited(local_name, value, name, namespace, prefix, owner),
|
||||||
&global::Window(window), AttrBinding::Wrap)
|
&global::Window(window), AttrBinding::Wrap)
|
||||||
}
|
}
|
||||||
|
@ -139,9 +139,16 @@ impl<'a> AttrMethods for JSRef<'a, Attr> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn SetValue(self, value: DOMString) {
|
fn SetValue(self, value: DOMString) {
|
||||||
let owner = self.owner.root();
|
match self.owner {
|
||||||
|
None => {
|
||||||
|
*self.value.borrow_mut() = StringAttrValue(value)
|
||||||
|
}
|
||||||
|
Some(o) => {
|
||||||
|
let owner = o.root();
|
||||||
let value = owner.parse_attribute(&self.namespace, self.local_name(), value);
|
let value = owner.parse_attribute(&self.namespace, self.local_name(), value);
|
||||||
self.set_value(ReplacedAttr, value);
|
self.set_value(ReplacedAttr, value, *owner);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn TextContent(self) -> DOMString {
|
fn TextContent(self) -> DOMString {
|
||||||
|
@ -177,7 +184,7 @@ impl<'a> AttrMethods for JSRef<'a, Attr> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn GetOwnerElement(self) -> Option<Temporary<Element>> {
|
fn GetOwnerElement(self) -> Option<Temporary<Element>> {
|
||||||
Some(Temporary::new(self.owner))
|
self.owner.map(|o| Temporary::new(o))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn Specified(self) -> bool {
|
fn Specified(self) -> bool {
|
||||||
|
@ -186,16 +193,17 @@ impl<'a> AttrMethods for JSRef<'a, Attr> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait AttrHelpers<'a> {
|
pub trait AttrHelpers<'a> {
|
||||||
fn set_value(self, set_type: AttrSettingType, value: AttrValue);
|
fn set_value(self, set_type: AttrSettingType, value: AttrValue, owner: JSRef<Element>);
|
||||||
fn value(self) -> Ref<'a, AttrValue>;
|
fn value(self) -> Ref<'a, AttrValue>;
|
||||||
fn local_name(self) -> &'a Atom;
|
fn local_name(self) -> &'a Atom;
|
||||||
fn summarize(self) -> AttrInfo;
|
fn summarize(self) -> AttrInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> AttrHelpers<'a> for JSRef<'a, Attr> {
|
impl<'a> AttrHelpers<'a> for JSRef<'a, Attr> {
|
||||||
fn set_value(self, set_type: AttrSettingType, value: AttrValue) {
|
fn set_value(self, set_type: AttrSettingType, value: AttrValue, owner: JSRef<Element>) {
|
||||||
let owner = self.owner.root();
|
assert!(Some(owner) == self.owner.map(|o| *o.root()));
|
||||||
let node: JSRef<Node> = NodeCast::from_ref(*owner);
|
|
||||||
|
let node: JSRef<Node> = NodeCast::from_ref(owner);
|
||||||
let namespace_is_null = self.namespace == ns!("");
|
let namespace_is_null = self.namespace == ns!("");
|
||||||
|
|
||||||
match set_type {
|
match set_type {
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
use dom::attr::AttrHelpers;
|
use dom::attr::{Attr, AttrHelpers, StringAttrValue};
|
||||||
use dom::bindings::cell::DOMRefCell;
|
use dom::bindings::cell::DOMRefCell;
|
||||||
use dom::bindings::codegen::Bindings::DocumentBinding;
|
use dom::bindings::codegen::Bindings::DocumentBinding;
|
||||||
use dom::bindings::codegen::Bindings::DocumentBinding::{DocumentMethods, DocumentReadyState};
|
use dom::bindings::codegen::Bindings::DocumentBinding::{DocumentMethods, DocumentReadyState};
|
||||||
|
@ -622,6 +622,22 @@ impl<'a> DocumentMethods for JSRef<'a, Document> {
|
||||||
ScriptCreated))
|
ScriptCreated))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// http://dom.spec.whatwg.org/#dom-document-createattribute
|
||||||
|
fn CreateAttribute(self, local_name: DOMString) -> Fallible<Temporary<Attr>> {
|
||||||
|
if xml_name_type(local_name.as_slice()) == InvalidXMLName {
|
||||||
|
debug!("Not a valid element name");
|
||||||
|
return Err(InvalidCharacter);
|
||||||
|
}
|
||||||
|
|
||||||
|
let window = self.window.root();
|
||||||
|
let name = Atom::from_slice(local_name.as_slice());
|
||||||
|
// repetition used because string_cache::atom::Atom is non-copyable
|
||||||
|
let l_name = Atom::from_slice(local_name.as_slice());
|
||||||
|
let value = StringAttrValue("".to_string());
|
||||||
|
|
||||||
|
Ok(Attr::new(*window, name, value, l_name, ns!(""), None, None))
|
||||||
|
}
|
||||||
|
|
||||||
// http://dom.spec.whatwg.org/#dom-document-createdocumentfragment
|
// http://dom.spec.whatwg.org/#dom-document-createdocumentfragment
|
||||||
fn CreateDocumentFragment(self) -> Temporary<DocumentFragment> {
|
fn CreateDocumentFragment(self) -> Temporary<DocumentFragment> {
|
||||||
DocumentFragment::new(self)
|
DocumentFragment::new(self)
|
||||||
|
|
|
@ -509,13 +509,13 @@ impl<'a> AttributeHandlers for JSRef<'a, Element> {
|
||||||
None => {
|
None => {
|
||||||
let window = window_from_node(self).root();
|
let window = window_from_node(self).root();
|
||||||
let attr = Attr::new(*window, local_name, value.clone(),
|
let attr = Attr::new(*window, local_name, value.clone(),
|
||||||
name, namespace.clone(), prefix, self);
|
name, namespace.clone(), prefix, Some(self));
|
||||||
self.attrs.borrow_mut().push_unrooted(&attr);
|
self.attrs.borrow_mut().push_unrooted(&attr);
|
||||||
(self.attrs.borrow().len() - 1, FirstSetAttr)
|
(self.attrs.borrow().len() - 1, FirstSetAttr)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
(*self.attrs.borrow())[idx].root().set_value(set_type, value);
|
(*self.attrs.borrow())[idx].root().set_value(set_type, value, self);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_attribute(self, namespace: &Namespace, local_name: &Atom,
|
fn parse_attribute(self, namespace: &Namespace, local_name: &Atom,
|
||||||
|
|
|
@ -1558,7 +1558,7 @@ impl Node {
|
||||||
&Attr::new(*window,
|
&Attr::new(*window,
|
||||||
attr.local_name().clone(), attr.value().clone(),
|
attr.local_name().clone(), attr.value().clone(),
|
||||||
attr.name().clone(), attr.namespace().clone(),
|
attr.name().clone(), attr.namespace().clone(),
|
||||||
attr.prefix().clone(), copy_elem));
|
attr.prefix().clone(), Some(copy_elem)));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
_ => ()
|
_ => ()
|
||||||
|
|
|
@ -35,6 +35,9 @@ interface Document : Node {
|
||||||
[Throws]
|
[Throws]
|
||||||
ProcessingInstruction createProcessingInstruction(DOMString target, DOMString data);
|
ProcessingInstruction createProcessingInstruction(DOMString target, DOMString data);
|
||||||
|
|
||||||
|
[Throws]
|
||||||
|
Attr createAttribute(DOMString localName);
|
||||||
|
|
||||||
[Throws]
|
[Throws]
|
||||||
Node importNode(Node node, optional boolean deep = false);
|
Node importNode(Node node, optional boolean deep = false);
|
||||||
[Throws]
|
[Throws]
|
||||||
|
|
|
@ -126,9 +126,6 @@
|
||||||
[Document interface: operation importNode(Node,boolean)]
|
[Document interface: operation importNode(Node,boolean)]
|
||||||
expected: FAIL
|
expected: FAIL
|
||||||
|
|
||||||
[Document interface: operation createAttribute(DOMString)]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[Document interface: operation createAttributeNS(DOMString,DOMString)]
|
[Document interface: operation createAttributeNS(DOMString,DOMString)]
|
||||||
expected: FAIL
|
expected: FAIL
|
||||||
|
|
||||||
|
@ -180,12 +177,6 @@
|
||||||
[Document interface: xmlDoc must inherit property "origin" with the proper type (3)]
|
[Document interface: xmlDoc must inherit property "origin" with the proper type (3)]
|
||||||
expected: FAIL
|
expected: FAIL
|
||||||
|
|
||||||
[Document interface: xmlDoc must inherit property "createAttribute" with the proper type (20)]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[Document interface: calling createAttribute(DOMString) on xmlDoc with too few arguments must throw TypeError]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[Document interface: xmlDoc must inherit property "createAttributeNS" with the proper type (21)]
|
[Document interface: xmlDoc must inherit property "createAttributeNS" with the proper type (21)]
|
||||||
expected: FAIL
|
expected: FAIL
|
||||||
|
|
||||||
|
|
|
@ -1,35 +0,0 @@
|
||||||
[Document-createAttribute.html]
|
|
||||||
type: testharness
|
|
||||||
[createAttribute("")]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[createAttribute("invalid^Name")]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[createAttribute("\\\\")]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[createAttribute("\'")]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[createAttribute("\\"")]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[createAttribute("0")]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[createAttribute("0:a")]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[createAttribute("title")]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[createAttribute("TITLE")]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[createAttribute(null)]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[createAttribute(undefined)]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
|
@ -1053,12 +1053,6 @@
|
||||||
[Document interface: document.implementation.createDocument(null, "", null) must inherit property "origin" with the proper type (3)]
|
[Document interface: document.implementation.createDocument(null, "", null) must inherit property "origin" with the proper type (3)]
|
||||||
expected: FAIL
|
expected: FAIL
|
||||||
|
|
||||||
[Document interface: document.implementation.createDocument(null, "", null) must inherit property "createAttribute" with the proper type (20)]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[Document interface: calling createAttribute(DOMString) on document.implementation.createDocument(null, "", null) with too few arguments must throw TypeError]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[Document interface: document.implementation.createDocument(null, "", null) must inherit property "createAttributeNS" with the proper type (21)]
|
[Document interface: document.implementation.createDocument(null, "", null) must inherit property "createAttributeNS" with the proper type (21)]
|
||||||
expected: FAIL
|
expected: FAIL
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue