Skip to content

Commit

Permalink
Auto merge of #11496 - GuillaumeGomez:range, r=nox
Browse files Browse the repository at this point in the history
Implement Range::createContextualFragment

<!-- Reviewable:start -->
This change is [<img src="https://reviewable.io/review_button.svg" height="35" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/11496)
<!-- Reviewable:end -->
  • Loading branch information
bors-servo committed Jun 3, 2016
2 parents 6c5f5d3 + 5ab7f54 commit 51d41c5
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 38 deletions.
1 change: 0 additions & 1 deletion components/script/dom/element.rs
Expand Up @@ -130,7 +130,6 @@ impl Element {
create_element(name, prefix, document, creator)
}


pub fn new_inherited(local_name: Atom,
namespace: Namespace, prefix: Option<DOMString>,
document: &Document) -> Element {
Expand Down
6 changes: 3 additions & 3 deletions components/script/dom/htmlscriptelement.rs
Expand Up @@ -532,8 +532,8 @@ impl HTMLScriptElement {
is_js
}

pub fn mark_already_started(&self) {
self.already_started.set(true);
pub fn set_already_started(&self, already_started: bool) {
self.already_started.set(already_started);
}

fn dispatch_event(&self,
Expand Down Expand Up @@ -593,7 +593,7 @@ impl VirtualMethods for HTMLScriptElement {

// https://html.spec.whatwg.org/multipage/#already-started
if self.already_started.get() {
copy.downcast::<HTMLScriptElement>().unwrap().mark_already_started();
copy.downcast::<HTMLScriptElement>().unwrap().set_already_started(true);
}
}
}
Expand Down
41 changes: 41 additions & 0 deletions components/script/dom/range.rs
Expand Up @@ -22,6 +22,9 @@ use dom::bindings::weakref::{WeakRef, WeakRefVec};
use dom::characterdata::CharacterData;
use dom::document::Document;
use dom::documentfragment::DocumentFragment;
use dom::element::Element;
use dom::htmlbodyelement::HTMLBodyElement;
use dom::htmlscriptelement::HTMLScriptElement;
use dom::node::{Node, UnbindContext};
use dom::text::Text;
use heapsize::HeapSizeOf;
Expand Down Expand Up @@ -893,6 +896,44 @@ impl RangeMethods for Range {
// Step 6.
s
}

// https://dvcs.w3.org/hg/innerhtml/raw-file/tip/index.html#extensions-to-the-range-interface
fn CreateContextualFragment(&self, fragment: DOMString) -> Fallible<Root<DocumentFragment>> {
// Step 1.
let node = self.StartContainer();
let element = match node.type_id() {
NodeTypeId::Document(_) | NodeTypeId::DocumentFragment => None,
NodeTypeId::Element(_) => Some(node),
NodeTypeId::CharacterData(CharacterDataTypeId::Comment) |
NodeTypeId::CharacterData(CharacterDataTypeId::Text) => node.GetParentNode(),
NodeTypeId::CharacterData(CharacterDataTypeId::ProcessingInstruction) |
NodeTypeId::DocumentType => unreachable!(),
};

// Step 2.
let should_create_body = element.as_ref().map_or(true, |elem| {
let elem = elem.downcast::<Element>().unwrap();
elem.local_name() == &atom!("html") && elem.html_element_in_html_document()
});
let element: Root<Node> = if should_create_body {
Root::upcast(HTMLBodyElement::new(atom!("body"), None, &self.StartContainer().owner_doc()))
} else {
Root::upcast(element.unwrap())
};

// Step 3.
let fragment_node = try!(element.parse_fragment(fragment));

// Step 4.
for node in fragment_node.upcast::<Node>().traverse_preorder() {
if let Some(script) = node.downcast::<HTMLScriptElement>() {
script.set_already_started(false);
}
}

// Step 5.
Ok(fragment_node)
}
}

#[derive(JSTraceable)]
Expand Down
6 changes: 3 additions & 3 deletions components/script/dom/webidls/Range.webidl
Expand Up @@ -76,9 +76,9 @@ interface Range {

// https://dvcs.w3.org/hg/innerhtml/raw-file/tip/index.html#extensions-to-the-range-interface
partial interface Range {
// [NewObject, Throws]
// DocumentFragment createContextualFragment(DOMString fragment);
};//
[NewObject, Throws]
DocumentFragment createContextualFragment(DOMString fragment);
};

// http://dev.w3.org/csswg/cssom-view/#extensions-to-the-range-interface
partial interface Range {
Expand Down
2 changes: 1 addition & 1 deletion components/script/parse/html.rs
Expand Up @@ -150,7 +150,7 @@ impl<'a> TreeSink for servohtmlparser::Sink {

fn mark_script_already_started(&mut self, node: JS<Node>) {
let script = node.downcast::<HTMLScriptElement>();
script.map(|script| script.mark_already_started());
script.map(|script| script.set_already_started(true));
}

fn complete_script(&mut self, node: JS<Node>) -> NextParserState {
Expand Down
2 changes: 1 addition & 1 deletion components/script/parse/xml.rs
Expand Up @@ -107,7 +107,7 @@ impl<'a> TreeSink for servoxmlparser::Sink {
fn mark_script_already_started(&mut self, node: Self::Handle) {
let script = node.downcast::<HTMLScriptElement>();
if let Some(script) = script {
script.mark_already_started();
script.set_already_started(true);
}
}

Expand Down
24 changes: 0 additions & 24 deletions tests/wpt/metadata/domparsing/createContextualFragment.html.ini
@@ -1,35 +1,11 @@
[createContextualFragment.html]
type: testharness
[Must not throw INVALID_STATE_ERR for a detached node.]
expected: FAIL

[Simple test with paragraphs]
expected: FAIL

[Don't auto-create <body> when applied to <html>]
expected: FAIL
[<script>s should be run when appended to the document (but not before)]
expected: FAIL

[<html> and <body> must work the same, 1]
expected: FAIL
[<html> and <body> must work the same, 2]
expected: FAIL
[Implicit <body> creation]
expected: FAIL
[Namespace generally shouldn't matter]
expected: FAIL
[<html> in a different namespace shouldn't be special]
expected: FAIL

[null should be stringified]
expected: FAIL
[undefined should be stringified]
expected: FAIL

This file was deleted.

0 comments on commit 51d41c5

Please sign in to comment.