Fix the "get the parent" loop when dispatching event (fixes #6733)

The DOM specification says:

A document's get the parent algorithm, given an event, returns null
if event's type attribute value is "load" or document does not have
a browsing context, and the document's associated Window object
otherwise.
This commit is contained in:
Anthony Ramine 2016-02-24 01:05:37 +01:00
parent 0455b0b301
commit e70b520c1f
17 changed files with 62 additions and 95 deletions

View file

@ -10,6 +10,7 @@ use dom::bindings::inheritance::Castable;
use dom::bindings::js::{JS, Root, RootedReference}; use dom::bindings::js::{JS, Root, RootedReference};
use dom::bindings::reflector::Reflectable; use dom::bindings::reflector::Reflectable;
use dom::bindings::trace::RootedVec; use dom::bindings::trace::RootedVec;
use dom::document::Document;
use dom::event::{Event, EventPhase}; use dom::event::{Event, EventPhase};
use dom::eventtarget::{CompiledEventListener, EventTarget, ListenerPhase}; use dom::eventtarget::{CompiledEventListener, EventTarget, ListenerPhase};
use dom::node::Node; use dom::node::Node;
@ -153,6 +154,13 @@ pub fn dispatch_event(target: &EventTarget, pseudo_target: Option<&EventTarget>,
for ancestor in target_node.ancestors() { for ancestor in target_node.ancestors() {
chain.push(JS::from_ref(ancestor.upcast())); chain.push(JS::from_ref(ancestor.upcast()));
} }
let top_most_ancestor_or_target =
Root::from_ref(chain.r().last().cloned().unwrap_or(target));
if let Some(document) = Root::downcast::<Document>(top_most_ancestor_or_target) {
if event.type_() != atom!("load") && document.browsing_context().is_some() {
chain.push(JS::from_ref(document.window().upcast()));
}
}
} }
dispatch_to_listeners(event, target, chain.r()); dispatch_to_listeners(event, target, chain.r());

View file

@ -1,3 +1,5 @@
[before-load-001.htm] [before-load-001.htm]
type: testharness type: testharness
expected: TIMEOUT [transition height from 10px to 100px / events]
expected: FAIL

View file

@ -1,5 +0,0 @@
[DOM.event.flow.html]
type: testharness
[Test Description: Dispatch an event in a DOM tree using the DOM event flow.]
expected: FAIL

View file

@ -1,5 +0,0 @@
[stopImmediatePropagation.effect.html]
type: testharness
[Test Description: stopImmediatePropagation() prevents other event listeners from being triggered and, unlike Event.stopPropagation(), its effect must be immediate. Once it has been called, further calls to this method have no additional effect.]
expected: FAIL

View file

@ -1,5 +0,0 @@
[stopPropagation.deferred.effect.html]
type: testharness
[Test Description: stopPropagation() prevents other event listeners from being triggered but its effect must be deferred until all event listeners attached on the Event.currentTarget have been triggered.]
expected: FAIL

View file

@ -1,5 +0,0 @@
[DOM.event.flow.html]
type: testharness
[Test Description: Dispatch an event in a DOM tree using the DOM event flow.]
expected: FAIL

View file

@ -1,5 +0,0 @@
[EventListener.dispatch.new.event.html]
type: testharness
[Test Description: Implementations of the DOM event model must be reentrant. Event listeners may perform actions that cause additional events to be dispatched. Such events are handled in a synchronous manner, the event propagation that causes the event listener to be triggered must resume only after the event dispatch of the new event is completed.]
expected: FAIL

View file

@ -1,5 +0,0 @@
[stopImmediatePropagation.effect.html]
type: testharness
[Test Description: stopImmediatePropagation() prevents other event listeners from being triggered and, unlike Event.stopPropagation(), its effect must be immediate. Once it has been called, further calls to this method have no additional effect.]
expected: FAIL

View file

@ -1,5 +0,0 @@
[stopPropagation.deferred.effect.html]
type: testharness
[Test Description: stopPropagation() prevents other event listeners from being triggered but its effect must be deferred until all event listeners attached on the Event.currentTarget have been triggered.]
expected: FAIL

View file

@ -1,5 +0,0 @@
[Event-dispatch-bubbles-false.html]
type: testharness
[In document with a browsing context: Event.dispatchEvent with Event.bubbles set to false.]
expected: FAIL

View file

@ -1,5 +0,0 @@
[Event-dispatch-handlers-changed.html]
type: testharness
[ Dispatch additional events inside an event listener ]
expected: FAIL

View file

@ -1,5 +0,0 @@
[Event-dispatch-omitted-capture.html]
type: testharness
[EventTarget.addEventListener with the capture argument omitted]
expected: FAIL

View file

@ -1,5 +0,0 @@
[Event-dispatch-reenter.html]
type: testharness
[ Dispatch additional events inside an event listener ]
expected: FAIL

View file

@ -1,5 +0,0 @@
[Event-dispatch-target-moved.html]
type: testharness
[Event propagation path when an element in it is moved within the DOM]
expected: FAIL

View file

@ -1,5 +0,0 @@
[Event-dispatch-target-removed.html]
type: testharness
[Event propagation path when an element in it is removed from the DOM]
expected: FAIL

View file

@ -0,0 +1,5 @@
[112.html]
type: testharness
[ scheduler: removing async attribute at runtime, script also has defer attribute]
expected: FAIL

View file

@ -30,32 +30,29 @@ function targetsForDocumentChain(document) {
]; ];
} }
function testChain(document, targetParents, phases, label) { function testChain(document, targetParents, phases, event_type) {
test(function() { var target = document.getElementById("target");
var event_type = "click"; var targets = targetParents.concat(target);
var target = document.getElementById("target"); var expected_targets = targets.concat(target);
var targets = targetParents.concat(target);
var expected_targets = targets.concat(target);
var actual_targets = [], actual_phases = []; var actual_targets = [], actual_phases = [];
var test_event = function(evt) { var test_event = function(evt) {
actual_targets.push(evt.currentTarget); actual_targets.push(evt.currentTarget);
actual_phases.push(evt.eventPhase); actual_phases.push(evt.eventPhase);
} }
for (var i = 0; i < targets.length; i++) { for (var i = 0; i < targets.length; i++) {
targets[i].addEventListener(event_type, test_event, true); targets[i].addEventListener(event_type, test_event, true);
targets[i].addEventListener(event_type, test_event, false); targets[i].addEventListener(event_type, test_event, false);
} }
var evt = document.createEvent("Event"); var evt = document.createEvent("Event");
evt.initEvent(event_type, false, true); evt.initEvent(event_type, false, true);
target.dispatchEvent(evt); target.dispatchEvent(evt);
assert_array_equals(actual_targets, expected_targets, "targets"); assert_array_equals(actual_targets, expected_targets, "targets");
assert_array_equals(actual_phases, phases, "phases"); assert_array_equals(actual_phases, phases, "phases");
}, label + ": Event.dispatchEvent with Event.bubbles set to false.");
} }
var phasesForDocumentChain = [ var phasesForDocumentChain = [
@ -69,13 +66,33 @@ var phasesForDocumentChain = [
Event.AT_TARGET, Event.AT_TARGET,
]; ];
chainWithWindow = [window].concat(targetsForDocumentChain(document)); test(function () {
testChain( var chainWithWindow = [window].concat(targetsForDocumentChain(document));
document, chainWithWindow, [Event.CAPTURING_PHASE].concat(phasesForDocumentChain), testChain(
"In document with a browsing context"); document, chainWithWindow, [Event.CAPTURING_PHASE].concat(phasesForDocumentChain), "click");
}, "In window.document with click event");
var documentClone = document.cloneNode(true); test(function () {
testChain( testChain(document, targetsForDocumentChain(document), phasesForDocumentChain, "load");
documentClone, targetsForDocumentChain(documentClone), phasesForDocumentChain, }, "In window.document with load event")
"In document without a browsing context");
test(function () {
var documentClone = document.cloneNode(true);
testChain(
documentClone, targetsForDocumentChain(documentClone), phasesForDocumentChain, "click");
}, "In window.document.cloneNode(true)");
test(function () {
var newDocument = new Document();
newDocument.appendChild(document.documentElement.cloneNode(true));
testChain(
newDocument, targetsForDocumentChain(newDocument), phasesForDocumentChain, "click");
}, "In new Document()");
test(function () {
var HTMLDocument = document.implementation.createHTMLDocument();
HTMLDocument.body.appendChild(document.getElementById("table").cloneNode(true));
testChain(
HTMLDocument, targetsForDocumentChain(HTMLDocument), phasesForDocumentChain, "click");
}, "In DOMImplementation.createHTMLDocument()");
</script> </script>