Auto merge of #9734 - nox:finish-event-dispatch, r=Ms2ger

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.

<!-- Reviewable:start -->
[<img src="https://reviewable.io/review_button.svg" height="40" alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/9734)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2016-02-27 23:03:25 +05:30
commit d5ce8f308f
17 changed files with 66 additions and 86 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
[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

@ -19,32 +19,21 @@
</tbody> </tbody>
</table> </table>
<script> <script>
test(function() { function targetsForDocumentChain(document) {
var event_type = "click"; return [
var target = document.getElementById("target");
var targets = [
window,
document, document,
document.documentElement, document.documentElement,
document.body, document.body,
document.getElementById("table"), document.getElementById("table"),
document.getElementById("table-body"), document.getElementById("table-body"),
document.getElementById("parent"), document.getElementById("parent")
target,
];
var expected_targets = targets.concat(target);
var phases = [
Event.CAPTURING_PHASE,
Event.CAPTURING_PHASE,
Event.CAPTURING_PHASE,
Event.CAPTURING_PHASE,
Event.CAPTURING_PHASE,
Event.CAPTURING_PHASE,
Event.CAPTURING_PHASE,
Event.AT_TARGET,
Event.AT_TARGET,
]; ];
}
function testChain(document, targetParents, phases, event_type) {
var target = document.getElementById("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) {
@ -64,5 +53,46 @@ test(function() {
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");
}, "Event.dispatchEvent with Event.bubbles set to false."); }
var phasesForDocumentChain = [
Event.CAPTURING_PHASE,
Event.CAPTURING_PHASE,
Event.CAPTURING_PHASE,
Event.CAPTURING_PHASE,
Event.CAPTURING_PHASE,
Event.CAPTURING_PHASE,
Event.AT_TARGET,
Event.AT_TARGET,
];
test(function () {
var chainWithWindow = [window].concat(targetsForDocumentChain(document));
testChain(
document, chainWithWindow, [Event.CAPTURING_PHASE].concat(phasesForDocumentChain), "click");
}, "In window.document with click event");
test(function () {
testChain(document, targetsForDocumentChain(document), phasesForDocumentChain, "load");
}, "In window.document with load event")
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>