Title: [201858] trunk
Revision
201858
Author
[email protected]
Date
2016-06-09 02:09:50 -0700 (Thu, 09 Jun 2016)

Log Message

slotchange event should be fired at the end of microtask
https://bugs.webkit.org/show_bug.cgi?id=157374
<rdar://problem/26154024>

Reviewed by Antti Koivisto.

Source/WebCore:

Dispatch slotchange event at the end of every microtask after delivering records to mutation observers
as specified in: https://dom.spec.whatwg.org/#notify-mutation-observers

Test: fast/shadow-dom/slotchange-event.html

* dom/Document.cpp:
(WebCore::Document::enqueueSlotchangeEvent): Deleted.
* dom/Document.h:
* dom/MutationObserver.cpp:
(WebCore::signalSlotList): Added.
(WebCore::MutationObserverMicrotask::run): mutationObserverCompoundMicrotaskQueuedFlag is now unset in
notifyMutationObservers to better match the concept to "notify mutation observers".
(WebCore::MutationObserver::enqueueSlotChangeEvent): Added.
(WebCore::MutationObserver::notifyMutationObservers): Renamed from deliverAllMutations. Added the code
to dispatch slotchange events as spec'ed, and also added comments for each step.
* dom/MutationObserver.h:
* html/HTMLSlotElement.cpp:
(WebCore::HTMLSlotElement::enqueueSlotChangeEvent): Use MutationObserver::enqueueSlotChangeEvent. Don't
create an event here since that is only needed when dispatching the event, and to keep track of whether
we've already scheduled an event or not. Use a boolean flag instead for the latter.
(WebCore::HTMLSlotElement::dispatchSlotChangeEvent): Added. Creates and dispatches an event.
(WebCore::HTMLSlotElement::dispatchEvent): Deleted.
* html/HTMLSlotElement.h:
(WebCore::HTMLSlotElement::didRemoveFromSignalSlotList): Added.

LayoutTests:

Added a test case to ensure slotchange event is dispatched at the end of a microtask.

* fast/shadow-dom/slotchange-event-expected.txt:
* fast/shadow-dom/slotchange-event.html:

Modified Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (201857 => 201858)


--- trunk/LayoutTests/ChangeLog	2016-06-09 08:41:54 UTC (rev 201857)
+++ trunk/LayoutTests/ChangeLog	2016-06-09 09:09:50 UTC (rev 201858)
@@ -1,3 +1,16 @@
+2016-06-09  Ryosuke Niwa  <[email protected]>
+
+        slotchange event should be fired at the end of microtask
+        https://bugs.webkit.org/show_bug.cgi?id=157374
+        <rdar://problem/26154024>
+
+        Reviewed by Antti Koivisto.
+
+        Added a test case to ensure slotchange event is dispatched at the end of a microtask.
+
+        * fast/shadow-dom/slotchange-event-expected.txt:
+        * fast/shadow-dom/slotchange-event.html:
+
 2016-06-08  Joseph Pecoraro  <[email protected]>
 
         REGRESSION: Web Inspector: Should be able to evaluate "{a:1, b:2}" in the console

Modified: trunk/LayoutTests/fast/shadow-dom/slotchange-event-expected.txt (201857 => 201858)


--- trunk/LayoutTests/fast/shadow-dom/slotchange-event-expected.txt	2016-06-09 08:41:54 UTC (rev 201857)
+++ trunk/LayoutTests/fast/shadow-dom/slotchange-event-expected.txt	2016-06-09 09:09:50 UTC (rev 201858)
@@ -29,4 +29,8 @@
 PASS slotchange event must fire on a slot element inside a closed shadow root  in a document when nested slots's contents change 
 PASS slotchange event must fire on a slot element inside an open shadow root  not in a document when nested slots's contents change 
 PASS slotchange event must fire on a slot element inside a closed shadow root  not in a document when nested slots's contents change 
+PASS slotchange event must fire at the end of current microtask after mutation observers are invoked inside an open shadow root  in a document when slots's contents change 
+PASS slotchange event must fire at the end of current microtask after mutation observers are invoked inside a closed shadow root  in a document when slots's contents change 
+PASS slotchange event must fire at the end of current microtask after mutation observers are invoked inside an open shadow root  not in a document when slots's contents change 
+PASS slotchange event must fire at the end of current microtask after mutation observers are invoked inside a closed shadow root  not in a document when slots's contents change 
 

Modified: trunk/LayoutTests/fast/shadow-dom/slotchange-event.html (201857 => 201858)


--- trunk/LayoutTests/fast/shadow-dom/slotchange-event.html	2016-06-09 08:41:54 UTC (rev 201857)
+++ trunk/LayoutTests/fast/shadow-dom/slotchange-event.html	2016-06-09 09:09:50 UTC (rev 201858)
@@ -534,6 +534,86 @@
 testSlotchangeFiresWhenNestedSlotChange('open', false);
 testSlotchangeFiresWhenNestedSlotChange('closed', false);
 
+function testSlotchangeFiresAtEndOfMicroTask(mode, connectedToDocument)
+{
+    var test = async_test('slotchange event must fire at the end of current microtask after mutation observers are invoked inside '
+        + treeName(mode, connectedToDocument) + ' when slots\'s contents change');
+
+    var outerHost;
+    var innerHost;
+    var outerSlot;
+    var innerSlot;
+    var slotchangeEvents = [];
+
+    test.step(function () {
+        outerHost = document.createElement('div');
+        if (connectedToDocument)
+            document.body.appendChild(outerHost);
+
+        var outerShadow = outerHost.attachShadow({'mode': mode});
+        outerShadow.appendChild(document.createElement('span'));
+        outerSlot = document.createElement('slot');
+        outerSlot.addEventListener('slotchange', function (event) {
+            event.stopPropagation();
+            test.step(function () {
+                assert_equals(event.target, outerSlot, 'slotchange event\'s target must be the slot element');
+            });
+            slotchangeEvents.push('outer');
+        });
+
+        innerHost = document.createElement('div');
+        innerHost.appendChild(outerSlot);
+        outerShadow.appendChild(innerHost);
+
+        var innerShadow = innerHost.attachShadow({'mode': mode});
+        innerShadow.appendChild(document.createElement('span'));
+        innerSlot = document.createElement('slot');
+        innerSlot.addEventListener('slotchange', function (event) {
+            event.stopPropagation();
+            test.step(function () {
+                assert_equals(event.target, innerSlot, 'slotchange event\'s target must be the slot element');
+            });
+            slotchangeEvents.push('inner');
+        });
+        innerShadow.appendChild(innerSlot);
+
+        outerHost.appendChild(document.createElement('span'));
+
+        assert_equals(slotchangeEvents.length, 0, 'slotchange event must not be fired synchronously');
+    });
+
+    var element = document.createElement('div');
+
+    new MutationObserver(function () {
+        test.step(function () {
+            assert_equals(slotchangeEvents.length, 0, 'slotchange event must not be fired before mutation records are delivered');
+        });
+        element.setAttribute('title', 'bar');
+        innerHost.appendChild(document.createElement('span'));
+    }).observe(element, {attributes: true, attributeFilter: ['id']});
+
+    new MutationObserver(function () {
+        test.step(function () {
+            assert_array_equals(slotchangeEvents, ['outer', 'inner'], 'slotchange event must be fired during a single compound microtask');
+        });
+    }).observe(element, {attributes: true, attributeFilter: ['title']});
+
+    element.setAttribute('id', 'foo');
+
+    setTimeout(function () {
+        test.step(function () {
+            assert_array_equals(slotchangeEvents, ['outer', 'inner', 'inner'],
+                'a distinct slotchange event must be enqueued for changes made during a mutation observer delivery');
+        });
+        test.done();
+    }, 0);
+}
+
+testSlotchangeFiresAtEndOfMicroTask('open', true);
+testSlotchangeFiresAtEndOfMicroTask('closed', true);
+testSlotchangeFiresAtEndOfMicroTask('open', false);
+testSlotchangeFiresAtEndOfMicroTask('closed', false);
+
 </script>
 </body>
 </html>

Modified: trunk/Source/WebCore/ChangeLog (201857 => 201858)


--- trunk/Source/WebCore/ChangeLog	2016-06-09 08:41:54 UTC (rev 201857)
+++ trunk/Source/WebCore/ChangeLog	2016-06-09 09:09:50 UTC (rev 201858)
@@ -1,3 +1,36 @@
+2016-06-09  Ryosuke Niwa  <[email protected]>
+
+        slotchange event should be fired at the end of microtask
+        https://bugs.webkit.org/show_bug.cgi?id=157374
+        <rdar://problem/26154024>
+
+        Reviewed by Antti Koivisto.
+
+        Dispatch slotchange event at the end of every microtask after delivering records to mutation observers
+        as specified in: https://dom.spec.whatwg.org/#notify-mutation-observers
+
+        Test: fast/shadow-dom/slotchange-event.html
+
+        * dom/Document.cpp:
+        (WebCore::Document::enqueueSlotchangeEvent): Deleted.
+        * dom/Document.h:
+        * dom/MutationObserver.cpp:
+        (WebCore::signalSlotList): Added.
+        (WebCore::MutationObserverMicrotask::run): mutationObserverCompoundMicrotaskQueuedFlag is now unset in
+        notifyMutationObservers to better match the concept to "notify mutation observers".
+        (WebCore::MutationObserver::enqueueSlotChangeEvent): Added.
+        (WebCore::MutationObserver::notifyMutationObservers): Renamed from deliverAllMutations. Added the code
+        to dispatch slotchange events as spec'ed, and also added comments for each step.
+        * dom/MutationObserver.h:
+        * html/HTMLSlotElement.cpp:
+        (WebCore::HTMLSlotElement::enqueueSlotChangeEvent): Use MutationObserver::enqueueSlotChangeEvent. Don't
+        create an event here since that is only needed when dispatching the event, and to keep track of whether
+        we've already scheduled an event or not. Use a boolean flag instead for the latter. 
+        (WebCore::HTMLSlotElement::dispatchSlotChangeEvent): Added. Creates and dispatches an event.
+        (WebCore::HTMLSlotElement::dispatchEvent): Deleted.
+        * html/HTMLSlotElement.h:
+        (WebCore::HTMLSlotElement::didRemoveFromSignalSlotList): Added.
+
 2016-06-08  Youenn Fablet  <[email protected]>
 
         Introduce ResourceErrorBase::type

Modified: trunk/Source/WebCore/dom/Document.cpp (201857 => 201858)


--- trunk/Source/WebCore/dom/Document.cpp	2016-06-09 08:41:54 UTC (rev 201857)
+++ trunk/Source/WebCore/dom/Document.cpp	2016-06-09 09:09:50 UTC (rev 201858)
@@ -4250,11 +4250,6 @@
     m_eventQueue.enqueueEvent(WTFMove(event));
 }
 
-void Document::enqueueSlotchangeEvent(Ref<Event>&& event)
-{
-    m_eventQueue.enqueueEvent(WTFMove(event));
-}
-
 RefPtr<Event> Document::createEvent(const String& type, ExceptionCode& ec)
 {
     // Please do *not* add new event classes to this function unless they are

Modified: trunk/Source/WebCore/dom/Document.h (201857 => 201858)


--- trunk/Source/WebCore/dom/Document.h	2016-06-09 08:41:54 UTC (rev 201857)
+++ trunk/Source/WebCore/dom/Document.h	2016-06-09 09:09:50 UTC (rev 201858)
@@ -1089,7 +1089,6 @@
     void enqueueWindowEvent(Ref<Event>&&);
     void enqueueDocumentEvent(Ref<Event>&&);
     void enqueueOverflowEvent(Ref<Event>&&);
-    void enqueueSlotchangeEvent(Ref<Event>&&);
     void enqueuePageshowEvent(PageshowEventPersistence);
     void enqueueHashchangeEvent(const String& oldURL, const String& newURL);
     void enqueuePopstateEvent(RefPtr<SerializedScriptValue>&& stateObject);

Modified: trunk/Source/WebCore/dom/MutationObserver.cpp (201857 => 201858)


--- trunk/Source/WebCore/dom/MutationObserver.cpp	2016-06-09 08:41:54 UTC (rev 201857)
+++ trunk/Source/WebCore/dom/MutationObserver.cpp	2016-06-09 09:09:50 UTC (rev 201858)
@@ -35,6 +35,7 @@
 #include "Dictionary.h"
 #include "Document.h"
 #include "ExceptionCode.h"
+#include "HTMLSlotElement.h"
 #include "Microtasks.h"
 #include "MutationCallback.h"
 #include "MutationObserverRegistration.h"
@@ -146,6 +147,13 @@
     return suspendedObservers;
 }
 
+// https://dom.spec.whatwg.org/#signal-slot-list
+static Vector<RefPtr<HTMLSlotElement>>& signalSlotList()
+{
+    static NeverDestroyed<Vector<RefPtr<HTMLSlotElement>>> list;
+    return list;
+}
+
 static bool mutationObserverCompoundMicrotaskQueuedFlag;
 
 class MutationObserverMicrotask final : public Microtask {
@@ -153,8 +161,7 @@
 private:
     Result run() final
     {
-        mutationObserverCompoundMicrotaskQueuedFlag = false;
-        MutationObserver::deliverAllMutations();
+        MutationObserver::notifyMutationObservers();
         return Result::Done;
     }
 };
@@ -176,6 +183,15 @@
     queueMutationObserverCompoundMicrotask();
 }
 
+void MutationObserver::enqueueSlotChangeEvent(HTMLSlotElement& slot)
+{
+    ASSERT(isMainThread());
+    ASSERT(!signalSlotList().contains(&slot));
+    signalSlotList().append(&slot);
+
+    queueMutationObserverCompoundMicrotask();
+}
+
 void MutationObserver::setHasTransientRegistration()
 {
     ASSERT(isMainThread());
@@ -220,8 +236,12 @@
     m_callback->call(records, this);
 }
 
-void MutationObserver::deliverAllMutations()
+void MutationObserver::notifyMutationObservers()
 {
+    // https://dom.spec.whatwg.org/#notify-mutation-observers
+    // 1. Unset mutation observer compound microtask queued flag.
+    mutationObserverCompoundMicrotaskQueuedFlag = false;
+
     ASSERT(isMainThread());
     static bool deliveryInProgress = false;
     if (deliveryInProgress)
@@ -240,20 +260,35 @@
         }
     }
 
-    while (!activeMutationObservers().isEmpty()) {
-        Vector<RefPtr<MutationObserver>> observers;
-        copyToVector(activeMutationObservers(), observers);
+    while (!activeMutationObservers().isEmpty() || !signalSlotList().isEmpty()) {
+        // 2. Let notify list be a copy of unit of related similar-origin browsing contexts' list of MutationObserver objects.
+        Vector<RefPtr<MutationObserver>> notifyList;
+        copyToVector(activeMutationObservers(), notifyList);
         activeMutationObservers().clear();
-        std::sort(observers.begin(), observers.end(), [](auto& lhs, auto& rhs) {
+        std::sort(notifyList.begin(), notifyList.end(), [](auto& lhs, auto& rhs) {
             return lhs->m_priority < rhs->m_priority;
         });
 
-        for (auto& observer : observers) {
+        // 3. Let signalList be a copy of unit of related similar-origin browsing contexts' signal slot list.
+        // 4. Empty unit of related similar-origin browsing contexts' signal slot list.
+        Vector<RefPtr<HTMLSlotElement>> slotList;
+        if (!signalSlotList().isEmpty()) {
+            slotList.swap(signalSlotList());
+            for (auto& slot : slotList)
+                slot->didRemoveFromSignalSlotList();
+        }
+
+        // 5. For each MutationObserver object mo in notify list, execute a compound microtask subtask
+        for (auto& observer : notifyList) {
             if (observer->canDeliver())
                 observer->deliver();
             else
                 suspendedMutationObservers().add(observer);
         }
+
+        // 6. For each slot slot in signalList, in order, fire an event named slotchange, with its bubbles attribute set to true, at slot.
+        for (auto& slot : slotList)
+            slot->dispatchSlotChangeEvent();
     }
 
     deliveryInProgress = false;

Modified: trunk/Source/WebCore/dom/MutationObserver.h (201857 => 201858)


--- trunk/Source/WebCore/dom/MutationObserver.h	2016-06-09 08:41:54 UTC (rev 201857)
+++ trunk/Source/WebCore/dom/MutationObserver.h	2016-06-09 09:09:50 UTC (rev 201858)
@@ -39,6 +39,7 @@
 
 namespace WebCore {
 
+class HTMLSlotElement;
 class MutationCallback;
 class MutationObserverRegistration;
 class MutationRecord;
@@ -96,11 +97,13 @@
 
     HashSet<Node*> observedNodes() const;
 
+    static void enqueueSlotChangeEvent(HTMLSlotElement&);
+
 private:
     explicit MutationObserver(Ref<MutationCallback>&&);
     void deliver();
 
-    static void deliverAllMutations();
+    static void notifyMutationObservers();
     static bool validateOptions(MutationObserverOptions);
 
     Ref<MutationCallback> m_callback;

Modified: trunk/Source/WebCore/html/HTMLSlotElement.cpp (201857 => 201858)


--- trunk/Source/WebCore/html/HTMLSlotElement.cpp	2016-06-09 08:41:54 UTC (rev 201857)
+++ trunk/Source/WebCore/html/HTMLSlotElement.cpp	2016-06-09 09:09:50 UTC (rev 201858)
@@ -31,6 +31,7 @@
 #include "Event.h"
 #include "EventNames.h"
 #include "HTMLNames.h"
+#include "MutationObserver.h"
 #include "ShadowRoot.h"
 
 namespace WebCore {
@@ -126,24 +127,24 @@
 
 void HTMLSlotElement::enqueueSlotChangeEvent()
 {
-    if (m_enqueuedSlotChangeEvent)
+    // https://dom.spec.whatwg.org/#signal-a-slot-change
+    if (m_inSignalSlotList)
         return;
+    m_inSignalSlotList = true;
+    MutationObserver::enqueueSlotChangeEvent(*this);
+}
 
+void HTMLSlotElement::dispatchSlotChangeEvent()
+{
+    m_inSignalSlotList = false;
+
     bool bubbles = false;
     bool cancelable = false;
-    auto event = Event::create(eventNames().slotchangeEvent, bubbles, cancelable);
+    Ref<Event> event = Event::create(eventNames().slotchangeEvent, bubbles, cancelable);
     event->setTarget(this);
-    m_enqueuedSlotChangeEvent = event.ptr();
-    document().enqueueSlotchangeEvent(WTFMove(event));
+    dispatchEvent(event);
 }
 
-bool HTMLSlotElement::dispatchEvent(Event& event)
-{
-    if (&event == m_enqueuedSlotChangeEvent)
-        m_enqueuedSlotChangeEvent = nullptr;
-    return HTMLElement::dispatchEvent(event);
 }
 
-}
-
 #endif

Modified: trunk/Source/WebCore/html/HTMLSlotElement.h (201857 => 201858)


--- trunk/Source/WebCore/html/HTMLSlotElement.h	2016-06-09 08:41:54 UTC (rev 201857)
+++ trunk/Source/WebCore/html/HTMLSlotElement.h	2016-06-09 09:09:50 UTC (rev 201858)
@@ -42,16 +42,18 @@
     Vector<Node*> assignedNodes(const AssignedNodesOptions&) const;
 
     void enqueueSlotChangeEvent();
+    void didRemoveFromSignalSlotList() { m_inSignalSlotList = false; }
 
+    void dispatchSlotChangeEvent();
+
 private:
     HTMLSlotElement(const QualifiedName&, Document&);
 
     InsertionNotificationRequest insertedInto(ContainerNode&) final;
     void removedFrom(ContainerNode&) final;
     void attributeChanged(const QualifiedName&, const AtomicString& oldValue, const AtomicString& newValue, AttributeModificationReason) final;
-    bool dispatchEvent(Event&) final;
 
-    Event* m_enqueuedSlotChangeEvent { nullptr };
+    bool m_inSignalSlotList { false };
 };
 
 }
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to