Closed Bug 789315 Opened 10 years ago Closed 7 years ago

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


(Core :: DOM: Core & HTML, defect)

Not set



Tracking Status
firefox45 --- fixed


(Reporter: sicking, Assigned: smaug)




(4 files, 3 obsolete files)

Attached file 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.
(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.
Looks like spec's event loop handling is rather odd, but ok, we can do this.
Er, we're doing the right thing per spec, so the spec needs to be changed too.
(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 :)
Attached patch WIP (obsolete) — Splinter 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.
(and looks like the xmlcontensink stuff is wrong)
Spec fixed.
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?
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 ...
Duplicate of this bug: 1180927
I'll deal with this in a bit different way.
Attached patch v1Splinter 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)
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+
Attached patch -extra space (obsolete) — Splinter Review
Attached patch fix for a buggy wpt test (obsolete) — Splinter Review
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+
Attachment #8692595 - Attachment is obsolete: true
Attachment #8692627 - Attachment is obsolete: true
Closed: 7 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla45
Component: DOM → DOM: Core & HTML
You need to log in before you can comment on or make changes to this bug.