Implement Document.currentScript

This commit is contained in:
James Gilbertson 2015-02-26 06:43:49 -07:00
parent 8ad3c5aeb6
commit 5f5d1246ef
5 changed files with 24 additions and 31 deletions

View file

@ -45,6 +45,7 @@ use dom::htmlelement::{HTMLElement, HTMLElementTypeId};
use dom::htmlheadelement::HTMLHeadElement;
use dom::htmlhtmlelement::HTMLHtmlElement;
use dom::htmltitleelement::HTMLTitleElement;
use dom::htmlscriptelement::HTMLScriptElement;
use dom::location::Location;
use dom::mouseevent::MouseEvent;
use dom::keyboardevent::KeyboardEvent;
@ -116,6 +117,8 @@ pub struct Document {
possibly_focused: MutNullableJS<Element>,
/// The element that currently has the document focus context.
focused: MutNullableJS<Element>,
/// The script element that is currently executing.
current_script: MutNullableJS<HTMLScriptElement>,
}
impl DocumentDerived for EventTarget {
@ -206,6 +209,7 @@ pub trait DocumentHelpers<'a> {
fn handle_click_event(self, js_runtime: *mut JSRuntime, _button: uint, point: Point2D<f32>);
fn dispatch_key_event(self, key: Key, state: KeyState,
modifiers: KeyModifiers, compositor: &mut Box<ScriptListener+'static>);
fn set_current_script(self, script: Option<JSRef<HTMLScriptElement>>);
}
impl<'a> DocumentHelpers<'a> for JSRef<'a, Document> {
@ -535,6 +539,10 @@ impl<'a> DocumentHelpers<'a> for JSRef<'a, Document> {
window.r().flush_layout(ReflowGoal::ForDisplay, ReflowQueryType::NoQuery);
}
fn set_current_script(self, script: Option<JSRef<HTMLScriptElement>>) {
self.current_script.assign(script);
}
}
#[derive(PartialEq)]
@ -601,6 +609,7 @@ impl Document {
ready_state: Cell::new(ready_state),
possibly_focused: Default::default(),
focused: Default::default(),
current_script: Default::default(),
}
}
@ -1002,6 +1011,11 @@ impl<'a> DocumentMethods for JSRef<'a, Document> {
})
}
// http://www.whatwg.org/specs/web-apps/current-work/#dom-document-currentscript
fn GetCurrentScript(self) -> Option<Temporary<HTMLScriptElement>> {
self.current_script.get()
}
// http://www.whatwg.org/specs/web-apps/current-work/#dom-document-body
fn GetBody(self) -> Option<Temporary<HTMLElement>> {
self.get_html_element().and_then(|root| {

View file

@ -7,6 +7,7 @@ use std::ascii::AsciiExt;
use dom::attr::Attr;
use dom::attr::AttrHelpers;
use dom::bindings::codegen::Bindings::AttrBinding::AttrMethods;
use dom::bindings::codegen::Bindings::DocumentBinding::DocumentMethods;
use dom::bindings::codegen::Bindings::HTMLScriptElementBinding;
use dom::bindings::codegen::Bindings::HTMLScriptElementBinding::HTMLScriptElementMethods;
use dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
@ -14,15 +15,15 @@ use dom::bindings::codegen::InheritTypes::{HTMLScriptElementDerived, HTMLScriptE
use dom::bindings::codegen::InheritTypes::{ElementCast, HTMLElementCast, NodeCast};
use dom::bindings::codegen::InheritTypes::EventTargetCast;
use dom::bindings::global::GlobalRef;
use dom::bindings::js::{JSRef, Temporary, OptionalRootable};
use dom::bindings::js::{JSRef, Temporary, OptionalRootable, RootedReference};
use dom::bindings::refcounted::Trusted;
use dom::document::Document;
use dom::document::{Document, DocumentHelpers};
use dom::element::{Element, AttributeHandlers, ElementCreator};
use dom::eventtarget::{EventTarget, EventTargetTypeId};
use dom::event::{Event, EventBubbles, EventCancelable, EventHelpers};
use dom::element::ElementTypeId;
use dom::htmlelement::{HTMLElement, HTMLElementTypeId};
use dom::node::{Node, NodeHelpers, NodeTypeId, window_from_node, CloneChildrenFlag};
use dom::node::{Node, NodeHelpers, NodeTypeId, document_from_node, window_from_node, CloneChildrenFlag};
use dom::virtualmethods::VirtualMethods;
use dom::window::ScriptHelpers;
use script_task::{ScriptMsg, Runnable};
@ -313,13 +314,12 @@ impl<'a> HTMLScriptElementHelpers for JSRef<'a, HTMLScriptElement> {
// document. Let neutralised doc be that Document.
// Step 2.b.4.
// TODO: Let old script element be the value to which the script
// element's node document's currentScript object was most recently
// initialised.
let document = document_from_node(self).root();
let document = document.r();
let old_script = document.GetCurrentScript().root();
// Step 2.b.5.
// TODO: Initialise the script element's node document's currentScript
// object to the script element.
document.set_current_script(Some(self));
// Step 2.b.6.
// TODO: Create a script...
@ -328,8 +328,7 @@ impl<'a> HTMLScriptElementHelpers for JSRef<'a, HTMLScriptElement> {
&*url.serialize());
// Step 2.b.7.
// TODO: Initialise the script element's node document's currentScript
// object to old script element.
document.set_current_script(old_script.r());
// Step 2.b.8.
// TODO: Decrement the ignore-destructive-writes counter of neutralised

View file

@ -83,6 +83,7 @@ partial interface Document {
readonly attribute HTMLCollection anchors;
readonly attribute HTMLCollection applets;
NodeList getElementsByName(DOMString elementName);
readonly attribute HTMLScriptElement? currentScript;
// special event handler IDL attributes that only apply to Document objects
[LenientThis] attribute EventHandler onreadystatechange;

View file

@ -1,14 +0,0 @@
[Document.currentScript.html]
type: testharness
[Script parse-inline]
expected: FAIL
[Script parse-ext]
expected: FAIL
[Script dom-inline]
expected: FAIL
[Script dom-ext]
expected: FAIL

View file

@ -15,9 +15,6 @@
[Document interface: attribute cssElementMap]
expected: FAIL
[Document interface: attribute currentScript]
expected: FAIL
[Document interface: operation open(DOMString,DOMString)]
expected: FAIL
@ -1098,9 +1095,6 @@
[Document interface: document.implementation.createDocument(null, "", null) must inherit property "cssElementMap" with the proper type (52)]
expected: FAIL
[Document interface: document.implementation.createDocument(null, "", null) must inherit property "currentScript" with the proper type (53)]
expected: FAIL
[Document interface: document.implementation.createDocument(null, "", null) must inherit property "open" with the proper type (54)]
expected: FAIL
@ -9821,4 +9815,3 @@
[Window interface: existence and properties of interface prototype object]
expected: FAIL