Skip to content

Commit

Permalink
Implement ChildNode.remove()
Browse files Browse the repository at this point in the history
  • Loading branch information
hmac authored and jdm committed May 5, 2014
1 parent 325a39b commit 6f310a5
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 7 deletions.
11 changes: 9 additions & 2 deletions src/components/script/dom/characterdata.rs
Expand Up @@ -4,13 +4,13 @@

//! DOM bindings for `CharacterData`.

use dom::bindings::codegen::InheritTypes::CharacterDataDerived;
use dom::bindings::codegen::InheritTypes::{CharacterDataDerived, NodeCast};
use dom::bindings::js::JSRef;
use dom::bindings::error::{Fallible, ErrorResult, IndexSize};
use dom::bindings::utils::{Reflectable, Reflector};
use dom::document::Document;
use dom::eventtarget::{EventTarget, NodeTargetTypeId};
use dom::node::{CommentNodeTypeId, Node, NodeTypeId, TextNodeTypeId, ProcessingInstructionNodeTypeId};
use dom::node::{CommentNodeTypeId, Node, NodeTypeId, TextNodeTypeId, ProcessingInstructionNodeTypeId, NodeHelpers};
use servo_util::str::DOMString;

#[deriving(Encodable)]
Expand Down Expand Up @@ -48,6 +48,7 @@ pub trait CharacterDataMethods {
fn InsertData(&mut self, _offset: u32, _arg: DOMString) -> ErrorResult;
fn DeleteData(&mut self, _offset: u32, _count: u32) -> ErrorResult;
fn ReplaceData(&mut self, _offset: u32, _count: u32, _arg: DOMString) -> ErrorResult;
fn Remove(&mut self);
}

impl<'a> CharacterDataMethods for JSRef<'a, CharacterData> {
Expand Down Expand Up @@ -98,6 +99,12 @@ impl<'a> CharacterDataMethods for JSRef<'a, CharacterData> {
// FIXME: Once we have `Range`, we should implement step7 to step11
Ok(())
}

// http://dom.spec.whatwg.org/#dom-childnode-remove
fn Remove(&mut self) {
let node: &mut JSRef<Node> = NodeCast::from_mut_ref(self);
node.remove_self();
}
}

impl Reflectable for CharacterData {
Expand Down
11 changes: 9 additions & 2 deletions src/components/script/dom/documenttype.rs
Expand Up @@ -2,12 +2,12 @@
* 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/. */

use dom::bindings::codegen::InheritTypes::DocumentTypeDerived;
use dom::bindings::codegen::InheritTypes::{DocumentTypeDerived, NodeCast};
use dom::bindings::codegen::BindingDeclarations::DocumentTypeBinding;
use dom::bindings::js::{JSRef, Temporary};
use dom::document::Document;
use dom::eventtarget::{EventTarget, NodeTargetTypeId};
use dom::node::{Node, DoctypeNodeTypeId};
use dom::node::{Node, DoctypeNodeTypeId, NodeHelpers};
use servo_util::str::DOMString;

/// The `DOCTYPE` tag.
Expand Down Expand Up @@ -59,6 +59,7 @@ pub trait DocumentTypeMethods {
fn Name(&self) -> DOMString;
fn PublicId(&self) -> DOMString;
fn SystemId(&self) -> DOMString;
fn Remove(&mut self);
}

impl<'a> DocumentTypeMethods for JSRef<'a, DocumentType> {
Expand All @@ -73,4 +74,10 @@ impl<'a> DocumentTypeMethods for JSRef<'a, DocumentType> {
fn SystemId(&self) -> DOMString {
self.system_id.clone()
}

// http://dom.spec.whatwg.org/#dom-childnode-remove
fn Remove(&mut self) {
let node: &mut JSRef<Node> = NodeCast::from_mut_ref(self);
node.remove_self();
}
}
7 changes: 7 additions & 0 deletions src/components/script/dom/element.rs
Expand Up @@ -406,6 +406,7 @@ pub trait ElementMethods {
fn GetInnerHTML(&self) -> Fallible<DOMString>;
fn GetOuterHTML(&self) -> Fallible<DOMString>;
fn Children(&self) -> Temporary<HTMLCollection>;
fn Remove(&mut self);
}

impl<'a> ElementMethods for JSRef<'a, Element> {
Expand Down Expand Up @@ -678,6 +679,12 @@ impl<'a> ElementMethods for JSRef<'a, Element> {
let window = window_from_node(self).root();
HTMLCollection::children(&*window, NodeCast::from_ref(self))
}

// http://dom.spec.whatwg.org/#dom-childnode-remove
fn Remove(&mut self) {
let node: &mut JSRef<Node> = NodeCast::from_mut_ref(self);
node.remove_self();
}
}

pub fn get_attribute_parts(name: DOMString) -> (Option<~str>, ~str) {
Expand Down
8 changes: 8 additions & 0 deletions src/components/script/dom/node.rs
Expand Up @@ -425,6 +425,8 @@ pub trait NodeHelpers {

fn get_bounding_content_box(&self) -> Rect<Au>;
fn get_content_boxes(&self) -> Vec<Rect<Au>>;

fn remove_self(&mut self);
}

impl<'a> NodeHelpers for JSRef<'a, Node> {
Expand Down Expand Up @@ -630,6 +632,12 @@ impl<'a> NodeHelpers for JSRef<'a, Node> {
document.deref().wait_until_safe_to_modify_dom();
}

fn remove_self(&mut self) {
match self.parent_node().root() {
Some(ref mut parent) => parent.remove_child(self),
None => ()
}
}
}

/// If the given untrusted node address represents a valid DOM node in the given runtime,
Expand Down
2 changes: 1 addition & 1 deletion src/components/script/dom/webidls/CharacterData.webidl
Expand Up @@ -25,4 +25,4 @@ interface CharacterData : Node {
void replaceData(unsigned long offset, unsigned long count, DOMString data);
};

//CharacterData implements ChildNode;
CharacterData implements ChildNode;
25 changes: 25 additions & 0 deletions src/components/script/dom/webidls/ChildNode.webidl
@@ -0,0 +1,25 @@
/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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/.
*
* The origin of this IDL file is:
* http://dom.spec.whatwg.org/#interface-childnode
*/

[NoInterfaceObject]
interface ChildNode {
// Not implemented yet:
// void before((Node or DOMString)... nodes);
// void after((Node or DOMString)... nodes);
// void replace((Node or DOMString)... nodes);
void remove();
};

// [NoInterfaceObject]
// interface NonDocumentTypeChildNode {
// [Pure]
// readonly attribute Element? previousElementSibling;
// [Pure]
// readonly attribute Element? nextElementSibling;
// };
2 changes: 1 addition & 1 deletion src/components/script/dom/webidls/DocumentType.webidl
Expand Up @@ -16,4 +16,4 @@ interface DocumentType : Node {
readonly attribute DOMString systemId;
};

//DocumentType implements ChildNode;
DocumentType implements ChildNode;
2 changes: 1 addition & 1 deletion src/components/script/dom/webidls/Element.webidl
Expand Up @@ -65,5 +65,5 @@ partial interface Element {
readonly attribute DOMString outerHTML;
};

//Element implements ChildNode;
Element implements ChildNode;
Element implements ParentNode;

5 comments on commit 6f310a5

@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 Ms2ger
at jdm@6f310a5

@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 jdm/servo/remove = 6f310a5 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.

jdm/servo/remove = 6f310a5 merged ok, testing candidate = 2da560e

@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 = 2da560e

Please sign in to comment.