MutationObservers are notified about parsed content after <script> runs, not before

RESOLVED FIXED in Firefox 45



5 years ago
2 years ago


(Reporter: sicking, Assigned: smaug)


Mac OS X

Firefox Tracking Flags

(firefox45 fixed)



(4 attachments, 3 obsolete attachments)

Created attachment 659028 [details]
simple testcase

When parsing and we hit a <script> element, we should flush all MutationObserver notifications before executing the contents of the script.

Note that we should do this after inserting the <script> though. I.e. the MutationObservers should have been notified about the <script> element before the contents of the element begins to run.

Comment 1

5 years ago
(In reply to Jonas Sicking (:sicking) from comment #0)
> When parsing and we hit a <script> element, we should flush all
> MutationObserver notifications before executing the contents of the script.

Comment 2

5 years ago
Looks like spec's event loop handling is rather odd, but ok, we can do this.

Comment 3

5 years ago
Er, we're doing the right thing per spec, so the spec needs to be changed too.

Comment 4

5 years ago
(Spec just requires stable state, but that doesn't mean end of microtask or end of task,
nor is there any special case for mutation observers.)
Yeah, sorry, I'm not sure what the spec requires here. Technically we definitely don't follow the spec right now since the spec calls for no notifications to be fired due to parsing.

It simply seemed to me that the behavior in comment #0 made the most sense.

My thinking was that running a script is very similar to a microtask. And generally when the outermost microtask starts we have flushed all mutationobserver notifications. The spec defines that notifications are flushed at the end of each outermost microtask, but the effect is that they are flushed by the time the next microtask starts.

The advantage over current behavior is script can count on application logic being in a consistent state by the time the script runs. I.e. if you have a MutationObserver which implements a template library, you can know that all the template logic has been updated to the page's current state by the time the script runs.

Right now you can't really know which part of the DOM has been notified about since that depends on when the parser last decided to return to the event loop.

But I'm very open to other proposals. I mostly filed this because that's how I had assumed it would work. No deeper thinking behind it :)

Comment 6

5 years ago
Created attachment 663583 [details] [diff] [review]

Not tested properly yet.
HTML5 parsing part should be ok (it is actually very tiny change with diff -w),
but I need to think about the old content sink stuff still, and actually test them.

Comment 7

5 years ago
(and looks like the xmlcontensink stuff is wrong)
Spec fixed.

Comment 9

5 years ago
I've noticed that you can get the requested behaviour if you add a 'beforescriptexecute' handler. For example, if I add this line to the testcase:

window.addEventListener("beforescriptexecute", function(){console.log("At beforescriptexecute")}, false);

I get the output:

[01:22:09.334] At beforescriptexecute
[01:22:09.335] added #text
[01:22:09.335] added B
[01:22:09.335] added #text
[01:22:09.335] added SCRIPT
[01:22:09.335] I'm now in script tag
[01:22:09.336] At beforescriptexecute
[01:22:09.336] added #text
[01:22:09.336] added A
[01:22:09.337] added #text
[01:22:09.337] added SCRIPT
[01:22:09.338] I'm now in second script tag
[01:22:09.338] added #text
[01:22:09.339] added DIV
[01:22:09.339] added #text

Can that be relied on as a workaround?

Comment 10

5 years ago
Yes. If there are listeners for beforescriptexecute, the MutationObserver callbacks will be
called at the end of microtask, which is end of event listener.

But I'll try to get back to this bug ASAP, and then we'll need to change the specs a bit ...


2 years ago
Duplicate of this bug: 1180927

Comment 12

2 years ago
I'll deal with this in a bit different way.

Comment 13

2 years ago
Created attachment 8669869 [details] [diff] [review]

I'm sure khuey likes microtasks these days :)
Anyhow, the spec effectively requires a checkpoint before executing a script.
The spec is just super convoluted.
Assignee: nobody → bugs
Attachment #663583 - Attachment is obsolete: true
Attachment #8669869 - Flags: review?(khuey)


2 years ago
Attachment #8669869 - Flags: review?(amarchesini)
Comment on attachment 8669869 [details] [diff] [review]

Review of attachment 8669869 [details] [diff] [review]:

Sorry but I cannot review this patch. I don't know all the implications of performing a microtask checkpoint.

::: dom/base/test/test_bug789315.html
@@ +25,5 @@
> +              addedNode.mutationObserverHasNotified = true;
> +            }
> +          }
> +        }
> +      

extra spaces.
Attachment #8669869 - Flags: review?(amarchesini) → feedback+
Attachment #8669869 - Flags: review?(khuey) → review+

Comment 15

2 years ago
Created attachment 8692595 [details] [diff] [review]
-extra space

Comment 16

2 years ago
Created attachment 8692627 [details] [diff] [review]
fix for a buggy wpt test

Comment 17

2 years ago
Created attachment 8692628 [details] [diff] [review]
remove wpt .ini now that we pass the tests
Comment on attachment 8692627 [details] [diff] [review]
fix for a buggy wpt test

Review of attachment 8692627 [details] [diff] [review]:

::: testing/web-platform/tests/dom/nodes/MutationObserver-document.html
@@ +53,5 @@
> +                         },
> +                         target: function () {
> +                          return document.getElementById("n00");
> +                         }},
> +                         {type: "childList",

nit: unindent this by 1 space.
Attachment #8692627 - Flags: review+

Comment 19

2 years ago
Created attachment 8692640 [details] [diff] [review]
fix for a buggy wpt test
Attachment #8692595 - Attachment is obsolete: true
Attachment #8692627 - Attachment is obsolete: true

Comment 20

2 years ago

Comment 21

2 years ago
Last Resolved: 2 years ago
status-firefox45: --- → fixed
Resolution: --- → FIXED
Target Milestone: --- → mozilla45
You need to log in before you can comment on or make changes to this bug.