What is the relevant global object for the newly created document
What is the issue with the HTML Standard?
If you look at the create a new browsing context and document algorithm, you'll notice that it creates a document object in step 15, but doesn't associate it with the window object in any way.
Let document be a new Document, with:
type "html" content type "text/html" mode "quirks" origin origin browsing context browsingContext permissions policy permissionsPolicy active sandboxing flag set sandboxFlags load timing info loadTimingInfo is initial about:blank true about base URL creatorBaseURL allow declarative shadow roots true custom element registry a new CustomElementRegistry object
And then the problem arises in step 20, which calls the make active algorithm and passes the created document. The first line that appears in it is:
- Let window be document's relevant global object.
And here it is not entirely clear what window will be equal to? I understand what relevant global object is. What will be [[Realm]] for the specified document?
By the way, why not take window for make active as it happens in step 2 of the set up a window environment settings object algorithm?
- Let realm be the value of execution context's Realm component.
- Let window be realm's global object.
How then does the document get associated to the window?
@annevk @domfarolino
We are pretty sloppy at the moment with respect to defining the global objects are created in. This isn't unique unfortunately. The global that is used here is created in step 10.
Step 10 creates a new realm execution context. This calls into ECMAScript's https://tc39.es/ecma262/#sec-initializehostdefinedrealm which creates a new execution context, and a new realm, and set's the execution context's [[Realm]] to the newly-created realm. It also sets up the realm's global object to Window, per the customization steps passed into step 10.
Step 15 creates a "new" document, and any "new" platform object gets created in a realm, but I guess one oddity here is that "creating a new realm" takes the currently running (aka newly-created) execution off the stack, so maybe the "new" Document doesn't automatically get associated with the newly-created realm as it technically should? It is a little indirect, and as @annevk mentions, a little sloppy. (However I think the intention is clearly implementable).
See also https://github.com/whatwg/webidl/issues/135#issuecomment-772716243.
@domfarolino what you wrote completely describes my understanding. As far as I know, the IDL specification describes creation of platform objects And when creating a platform object, the Realm must be explicitly passed. But the HTML specification does not explicitly specify that the window object must use the new definition from IDL.
@annevk it's a pity that such an important part of the HTML specification has such a problem.
@domfarolino @annevk I remembered one more thing:
The Window object has an associated Document, which is a Document object. It is set when the Window object is created, and only ever changed during navigation from the initial about:blank Document.
The create a new browsing context and document algorithm does not have an explicit setting of the associated Document for window, due to the fact that window is not defined in the algorithm at all. As I understand it, this case is the same as window and it is implicitly set after document is created?
Fwiw, in ladybird we do the following:
- a WindowProxy is allocated for and associated with the new browsing context as part of the global object customization steps in step 10. This is because the WindowProxy cannot be allocated without a realm in our implementation.
- the document is set as the associated document of the window created as the global object for step 10 as part of the substeps for step 15.
I don't think there's any other way to interpret or implement the steps that makes sense, as far as setting the initial associated document of the window. Funnily enough we don't create a DOM Document, but instead an HTML document, linking to #4792.
As far as "what should the [[Realm]] slot of the document be", it must be the realm created in step 10. Since a document is-a platform object, it must be associated with a realm at creation. And the only one that makes sense is the one we just created to serve as the realm for that document (and its browsing context).
So I definitely agree with statement that "the intention[of the spec text] should be clearly implementable."
@ADKaster thanks for your comment, it was really helpful to know what the browser developer does in this case.