Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend document.open/write to non-active documents #2827

Closed
dvoytenko opened this issue Jul 11, 2017 · 15 comments
Closed

Extend document.open/write to non-active documents #2827

dvoytenko opened this issue Jul 11, 2017 · 15 comments
Labels
interop Implementations are not interoperable with each other topic: document.open() topic: parser

Comments

@dvoytenko
Copy link

As it stands, the spec defines that document.open and document.write should be a no-op for a non-active document. See http://www.whatwg.org/specs/web-apps/current-work/multipage/elements.html#dom-document-write:

If document is not an active document, then return.

However, document.open/write is a convenient (and essentially the only) way to incrementally stream-parse DOM. For instance:

var parser = document.implementation.createHTMLDocument('');
parser.open();
parser.write('<html><body>...');
parser.close();

Despite the spec, all major browsers except Firefox support this. See https://bugzilla.mozilla.org/show_bug.cgi?id=867102

Alternatives to using this API would be either parse the whole DOM in one go using DOMParser or, what most often done on Firefox as a workaround, to parse via a hidden iframe. IMHO both alternatives are significantly worse than expanding the spec to what most of browsers already do.

@bzbarsky
Copy link
Contributor

Need tests to see what various browsers do for non-active documents in browsing contexts, at the very least.

Also, need to figure out what open should really do for documents with no window.

@annevk
Copy link
Member

annevk commented Jul 11, 2017

cc @whatwg/html-parser

@RReverser
Copy link
Member

Also, need to figure out what open should really do for documents with no window.

Not sure how window matters for document.open?

@inikulin
Copy link
Member

I'm not a big fan of this streaming parsing hack via document.write. Maybe we can work on implementing streaming API for DOMParser instead?

@jdm
Copy link
Member

jdm commented Jul 11, 2017

@RReverser
Copy link
Member

@jdm Ah. It creates a new Window object, so steps should be same. Doesn't really matter that there is no "physical" window in UI.

@wanderview
Copy link
Member

Note, the spec creates a new window global, but not all browsers implement that. We implement it in firefox, but I believe other browsers just reset some state on the current global.

@annevk
Copy link
Member

annevk commented Jul 11, 2017

Yeah, that is #1698.

@bzbarsky
Copy link
Contributor

It creates a new Window object, so steps should be same.

Almost certainly not, for documents that don't have a Window to start with. Like I said, needs some careful thought.

But yes, it really would be good to have a sane streaming parsing API; people keep asking for that. That might be somewhat orthogonal to whether we support open/write on non-browser-context docs for compat reasons.

@domenic
Copy link
Member

domenic commented Jul 11, 2017

I'd strongly prefer if other browsers fixed their document.open/document.write, and we created a reasonable streaming parser API instead.

@dvoytenko
Copy link
Author

@domenic For continuity, I'd prefer the other browsers to keep the existing document.open/write until they make the streaming API available.

That being said, I suspect it might be hard to get the consensus on what streaming DOM parsing would be and document.open/write are already there and I just don't see a huge conceptual difference between current per-spec document.open/write and extending it to detached docs like most of browsers do. I'm concerned that a straightforward and tangible improvement would be swallowed by a much bigger spec work.

@RByers
Copy link

RByers commented Jul 11, 2017

Screenshots confirming @dvoytenko's claim that all major browsers except Firefox support this based on a trivial test.

Filed a chromium bug to track the deviation from the spec here. I don't see an existing UseCounter quantifying the web compat risk of trying to change this, but with (at least) AMP depending on this behavior any measure is likely to be substantial.

Since 3 browsers already implement this and there's some not-easily-quantified compat risk to changing it, perhaps we should update the spec / tests to match reality in parallel to whatever people want to do with a parser API? If we want more web compat data confirming this isn't easy to break we could add a UseCounter to chromium then check the HTTP Archive for (non-AMP) sites hitting it during page load, but that'll take a few months to get results for.

@domenic domenic added the interop Implementations are not interoperable with each other label Jul 13, 2017
@dvoytenko
Copy link
Author

Hi All! Wanted to circle back and follow up on this issue. Is it possible to achieve a consensus and standardize this behavior? Or create an alternative?

@domenic
Copy link
Member

domenic commented Aug 28, 2017

I don't have the resources at the moment to work on either a good streaming parser API or an interoperable spec and comprehensive set of tests that covers all the permutations @bzbarsky mentions (plus the known interop issue of #1698 that may block this one).

@dominiccooney
Copy link

dominiccooney commented Sep 1, 2017

I filed Issue 2993 to take a stab at what asynchronous parsing might look like.

TimothyGu added a commit to TimothyGu/html that referenced this issue Aug 30, 2018
There is no longer anything fundamental that prevents document.open()
from being useful on non-active documents. This also aligns with Chrome,
Edge, and Safari. In fact, several projects like AMP already utilize
this property as a streaming HTML parser to desirable effects.

Fixes whatwg#2827.

Tests: web-platform-tests/wpt#12636
Tests: …
domenic pushed a commit that referenced this issue Sep 4, 2018
There is no longer anything fundamental that prevents document.open()
from being useful on non-active documents. This also aligns with Chrome,
Edge, and Safari. In fact, some developers already utilize this property
as a streaming HTML parser to desirable effect (see #2827).

Additionally, use a more appropriate guard for erasing event listeners
and handlers on the Window object, as revealed by the tests.

Fixes #2827.

Tests: web-platform-tests/wpt#12636
Tests: web-platform-tests/wpt#12770
mustaqahmed pushed a commit to mustaqahmed/html that referenced this issue Feb 15, 2019
There is no longer anything fundamental that prevents document.open()
from being useful on non-active documents. This also aligns with Chrome,
Edge, and Safari. In fact, some developers already utilize this property
as a streaming HTML parser to desirable effect (see whatwg#2827).

Additionally, use a more appropriate guard for erasing event listeners
and handlers on the Window object, as revealed by the tests.

Fixes whatwg#2827.

Tests: web-platform-tests/wpt#12636
Tests: web-platform-tests/wpt#12770
mustaqahmed pushed a commit to mustaqahmed/html that referenced this issue Feb 15, 2019
There is no longer anything fundamental that prevents document.open()
from being useful on non-active documents. This also aligns with Chrome,
Edge, and Safari. In fact, some developers already utilize this property
as a streaming HTML parser to desirable effect (see whatwg#2827).

Additionally, use a more appropriate guard for erasing event listeners
and handlers on the Window object, as revealed by the tests.

Fixes whatwg#2827.

Tests: web-platform-tests/wpt#12636
Tests: web-platform-tests/wpt#12770
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interop Implementations are not interoperable with each other topic: document.open() topic: parser
Development

No branches or pull requests

12 participants