Implement Range#surroundContents

This commit is contained in:
David Zbarsky 2015-08-27 21:15:10 -07:00
parent 0bc7ad9b08
commit 3bec4d37dd
4 changed files with 38 additions and 3816 deletions

View file

@ -10,7 +10,7 @@ use dom::bindings::codegen::Bindings::RangeBinding::RangeMethods;
use dom::bindings::codegen::Bindings::RangeBinding::{self, RangeConstants};
use dom::bindings::codegen::Bindings::TextBinding::TextMethods;
use dom::bindings::codegen::Bindings::WindowBinding::WindowMethods;
use dom::bindings::codegen::InheritTypes::{CharacterDataCast, NodeCast, TextCast};
use dom::bindings::codegen::InheritTypes::{CharacterDataCast, NodeCast, TextCast, TextDerived};
use dom::bindings::error::Error::HierarchyRequest;
use dom::bindings::error::{Error, ErrorResult, Fallible};
use dom::bindings::global::GlobalRef;
@ -668,6 +668,41 @@ impl RangeMethods for Range {
Ok(())
}
// https://dom.spec.whatwg.org/#dom-range-surroundcontents
fn SurroundContents(&self, new_parent: &Node) -> ErrorResult {
// Step 1.
let start = self.StartContainer();
let end = self.EndContainer();
if start.inclusive_ancestors().any(|n| !n.is_inclusive_ancestor_of(end.r()) && !n.is_text()) ||
end.inclusive_ancestors().any(|n| !n.is_inclusive_ancestor_of(start.r()) && !n.is_text()) {
return Err(Error::InvalidState);
}
// Step 2.
match new_parent.type_id() {
NodeTypeId::Document |
NodeTypeId::DocumentType |
NodeTypeId::DocumentFragment => return Err(Error::InvalidNodeType),
_ => ()
}
// Step 3.
let fragment = try!(self.ExtractContents());
// Step 4.
Node::replace_all(None, new_parent);
// Step 5.
try!(self.InsertNode(new_parent));
// Step 6.
let _ = try!(new_parent.AppendChild(NodeCast::from_ref(fragment.r())));
// Step 7.
self.SelectNode(new_parent)
}
}
#[derive(JSTraceable)]

View file

@ -50,8 +50,8 @@ interface Range {
DocumentFragment cloneContents();
[Throws]
void insertNode(Node node);
// [Throws]
// void surroundContents(Node newParent);
[Throws]
void surroundContents(Node newParent);
[NewObject]
Range cloneRange();

View file

@ -246,30 +246,15 @@
[Range interface: operation deleteContents()]
expected: FAIL
[Range interface: operation surroundContents(Node)]
expected: FAIL
[Range interface: stringifier]
expected: FAIL
[Range interface: document.createRange() must inherit property "deleteContents" with the proper type (20)]
expected: FAIL
[Range interface: document.createRange() must inherit property "surroundContents" with the proper type (24)]
expected: FAIL
[Range interface: calling surroundContents(Node) on document.createRange() with too few arguments must throw TypeError]
expected: FAIL
[Range interface: detachedRange must inherit property "deleteContents" with the proper type (20)]
expected: FAIL
[Range interface: detachedRange must inherit property "surroundContents" with the proper type (24)]
expected: FAIL
[Range interface: calling surroundContents(Node) on detachedRange with too few arguments must throw TypeError]
expected: FAIL
[NodeFilter interface: existence and properties of interface object]
expected: FAIL

File diff suppressed because it is too large Load diff