Skip to content

MutationObserver invocation order #713

@pmdartus

Description

@pmdartus

Based on: jsdom/jsdom#2398 (comment)

While implementing MutationObserver in jsdom, I wasn't able to make the following test pass while following the spec: https://github.com/web-platform-tests/wpt/blob/master/shadow-dom/slotchange-event.html#L540-L594

The test run the following steps:

  1. Adds 2 mutation observers (one on id attribute and another one on title) and a slot with a slotchange event handler.
  2. Update the id attribute and the append a child in the light dom of the host. We then enqueue a microtask to notify the listeners. At this point the id attribute mutation observer contains 1 mutation record, and the signal slot list contains the slot.
  3. The notifiy mutation observer algorithm is executed later on:
    1. The mutation observer list is copied, it contains the 2 mutation observers, and it iterates over the 2 mutation observers
    2. The callback for the id mutation observer is executed and in it's callback it updates title attribute and append a new element the light dom of the host. By updating the title attribute a new mutation record is enqueued but this time for the title mutation observer.
    3. The callback for the title mutation observer is invoked because it has a mutation record. And the test throws here since it expects that the slotchange event is invoked first.

Chrome and Webkit don't run into the same issue since they keep track of an active mutation observer list instead of iterating over all the mutation observers.

Metadata

Metadata

Assignees

No one assigned

    Labels

    compatStandard is not web compatible or proprietary feature needs standardizingtopic: shadowRelates to shadow trees (as defined in DOM)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions