Bug 40047 - document.write is not synchronous when using the HTML5 parser
Summary: document.write is not synchronous when using the HTML5 parser
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: DOM (show other bugs)
Version: 528+ (Nightly build)
Hardware: All All
: P2 Normal
Assignee: Nobody
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-06-02 00:03 PDT by Eric Seidel (no email)
Modified: 2010-06-02 22:25 PDT (History)
6 users (show)

See Also:


Attachments
Patch (3.29 KB, patch)
2010-06-02 01:09 PDT, Eric Seidel (no email)
no flags Details | Formatted Diff | Diff
Patch for landing (3.22 KB, patch)
2010-06-02 01:25 PDT, Eric Seidel (no email)
no flags Details | Formatted Diff | Diff
Patch for landing (3.28 KB, patch)
2010-06-02 14:07 PDT, Eric Seidel (no email)
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Eric Seidel (no email) 2010-06-02 00:03:07 PDT
REGRESSION(60409): document.write is not synchronous when using the HTML5 parser

It's unclear from the spec:
http://www.whatwg.org/specs/web-apps/current-work/multipage/apis-in-html-documents.html#document.write()

But it seems to me that document.write() should wait until all parsing is "done" for the write before returning.

In the old parser this was not an issue, as it was not as aggressive about yielding.  The new HTML5 parser yields when waiting for script loads or for stylesheet loads.

http://www.whatwg.org/specs/web-apps/current-work/multipage/scripting-1.html#running-a-script
talks about queuing tasks on the task queue, but it doesn't really talk about the parser yielding.

The old parser never seems to wait for <script src=""> tags during parsing (in violation of HTML5) nor does it block inline <script> tags ever (which HTML5 says it should for style-sheets), so I think that's how it produces a synchronous document.write behavior.
Comment 1 Eric Seidel (no email) 2010-06-02 00:05:53 PDT
http://www.whatwg.org/specs/web-apps/current-work/multipage/tokenization.html#parsing-main-incdata

Talks about:

Spin the event loop until there is no style sheet blocking scripts and the script's "ready to be parser-executed" flag is set.

Which may be what we need to do when the parser is supposed to be synchronous (like for document.write).
Comment 2 Eric Seidel (no email) 2010-06-02 00:30:38 PDT
I've tested and confirmed:  The old parser does not block document.write() for <script src> loads, however it won't actually process any contents of the write (or future writes) after the <script src> until that script has loaded.

I think the easiest way out of this is to remove the blocking of inline <script> tags on stylesheet loads.  To do so, we'd probably have to spin the event loop inside the parser like recommend by the HTML5 spec.  I don't really want to write that code (I am not even sure it's possible to spin the event loop from WebCore).  I think that's something we can deal with after we turn the HTML5 parser on as the default parser.
Comment 3 Eric Seidel (no email) 2010-06-02 01:09:38 PDT
Created attachment 57632 [details]
Patch
Comment 4 Adam Barth 2010-06-02 01:21:07 PDT
Comment on attachment 57632 [details]
Patch

WebCore/html/HTML5ScriptRunner.cpp:244
 +      //     m_parsingBlockingScript.element = script;
Commented out code!  :(
Comment 5 Eric Seidel (no email) 2010-06-02 01:25:36 PDT
Created attachment 57633 [details]
Patch for landing
Comment 6 WebKit Commit Bot 2010-06-02 03:58:43 PDT
Comment on attachment 57633 [details]
Patch for landing

Rejecting patch 57633 from commit-queue.

Failed to run "[u'/Users/eseidel/Projects/CommitQueue/WebKitTools/Scripts/svn-apply', u'--force']" exit_code: 1
Parsed 2 diffs from patch file(s).
patching file WebCore/ChangeLog
Hunk #1 succeeded at 1 with fuzz 3.
patching file WebCore/html/HTML5ScriptRunner.cpp
Hunk #2 FAILED at 238.
1 out of 2 hunks FAILED -- saving rejects to file WebCore/html/HTML5ScriptRunner.cpp.rej

Full output: http://webkit-commit-queue.appspot.com/results/2812104
Comment 7 Henri Sivonen 2010-06-02 04:54:01 PDT
(In reply to comment #2)
> I think the easiest way out of this is to remove the blocking of inline <script> tags on stylesheet loads.

It seems to me that it would be sufficient to make document.written inline scripts not wait on network loads of any kind.

By inspection, it seems to me that Gecko isn't spec-compliant here. I intend to file a spec bug.
Comment 8 Alexey Proskuryakov 2010-06-02 13:51:41 PDT
Since this isn't an issue in any production (or nightly) configuration, I've removed the REGRESSION prefix. Please let me know if this is a problem for your workflow.
Comment 9 Eric Seidel (no email) 2010-06-02 14:07:18 PDT
Created attachment 57701 [details]
Patch for landing
Comment 10 WebKit Commit Bot 2010-06-02 22:25:24 PDT
Comment on attachment 57701 [details]
Patch for landing

Clearing flags on attachment: 57701

Committed r60606: <http://trac.webkit.org/changeset/60606>
Comment 11 WebKit Commit Bot 2010-06-02 22:25:32 PDT
All reviewed patches have been landed.  Closing bug.