Skip to content

Commit

Permalink
Implement Document.currentScript
Browse files Browse the repository at this point in the history
  • Loading branch information
luniv committed Feb 27, 2015
1 parent 8ad3c5a commit 5f5d124
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 31 deletions.
14 changes: 14 additions & 0 deletions components/script/dom/document.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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> {
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -601,6 +609,7 @@ impl Document {
ready_state: Cell::new(ready_state),
possibly_focused: Default::default(),
focused: Default::default(),
current_script: Default::default(),
}
}

Expand Down Expand Up @@ -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| {
Expand Down
19 changes: 9 additions & 10 deletions components/script/dom/htmlscriptelement.rs
Expand Up @@ -7,22 +7,23 @@ 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;
use dom::bindings::codegen::InheritTypes::{HTMLScriptElementDerived, HTMLScriptElementCast};
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};
Expand Down Expand Up @@ -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...
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions components/script/dom/webidls/Document.webidl
Expand Up @@ -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;
Expand Down

This file was deleted.

7 changes: 0 additions & 7 deletions tests/wpt/metadata/html/dom/interfaces.html.ini
Expand Up @@ -15,9 +15,6 @@
[Document interface: attribute cssElementMap]
expected: FAIL

[Document interface: attribute currentScript]
expected: FAIL

[Document interface: operation open(DOMString,DOMString)]
expected: FAIL

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -9821,4 +9815,3 @@
[Window interface: existence and properties of interface prototype object]
expected: FAIL

5 comments on commit 5f5d124

@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 jdm
at luniv@5f5d124

@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 luniv/servo/document-currentscript = 5f5d124 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.

luniv/servo/document-currentscript = 5f5d124 merged ok, testing candidate = 26567ef

@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 = 26567ef

Please sign in to comment.