Skip to content

Commit

Permalink
Flesh out the speculative parser spec
Browse files Browse the repository at this point in the history
  • Loading branch information
zcorpan committed Oct 29, 2020
1 parent 0c4cb34 commit 122cff2
Showing 1 changed file with 125 additions and 12 deletions.
137 changes: 125 additions & 12 deletions source
Original file line number Diff line number Diff line change
Expand Up @@ -111086,8 +111086,6 @@ document.body.appendChild(text);
invocations of the tokenizer, yielding control back to the caller. (Tokenization will resume
when the caller returns to the "outer" tree construction stage.)</p>

<p class="XXX">Speculatively parse the remainder of the document.write string here?</p>

<p class="note">The tree construction stage of this particular parser is <a
href="#nestedParsing">being called reentrantly</a>, say from a call to <code
data-x="dom-document-write">document.write()</code>.</p>
Expand Down Expand Up @@ -112665,14 +112663,6 @@ document.body.appendChild(text);

</div>

<div w-nodev>

<h4>Speculative HTML parsing</h4>

<p class="XXX"><dfn>Speculative HTML parser</dfn>, <dfn>start the speculative HTML parser</dfn>,
<dfn>stop the speculative HTML parser</dfn>...</p>

</div>

<div w-nodev>

Expand Down Expand Up @@ -112797,8 +112787,7 @@ document.body.appendChild(text);
<li><p>Throw away any pending content in the <span>input stream</span>, and discard any future
content that would have been added to it.</p></li>

<li><p>If there is an active <span>speculative HTML parser</span>, <span>stop the speculative
HTML parser</span> for this HTML parser.</p></li>
<li><p><span>Stop the speculative HTML parser</span> for this HTML parser.</p></li>

<li><p>Set the <span>current document readiness</span> to "<code data-x="">interactive</code>"<!--
this immediately fires an event -->.</p></li>
Expand All @@ -112817,6 +112806,127 @@ document.body.appendChild(text);
</div>


<div w-nodev>

<h4>Speculative HTML parsing</h4>

<p>User agents may implement an optimization, as described in this section, to speculatively fetch
resources that are declared in the HTML markup while the HTML parser is waiting for a
<span>pending parsing-blocking script</span> to be fetched and executed. While this optimization
is not defined in precise detail, there are some rules to consider for interoperability.</p>

<p>Each <span>HTML parser</span> can have an <dfn export>active speculative HTML parser</dfn>. It
is initially null.</p>

<p>The <dfn export>speculative HTML parser</dfn> must act like the normal HTML parser (e.g., the
tree builder rules apply), with some exceptions:</p>

<ul>
<li><p>Scripts must not be executed.</p></li>

<li><p>Character encoding declarations must be ignored.</p></li>

<li>
<p>The state of the normal HTML parser and the document itself must not be affected.</p>

<p class="example">For example, the <span>next input character</span> or the <span>stack of open
elements</span> for the normal HTML parser is not affected by the <span>speculative HTML
parser</span>.</p>
</li>

<li>
<p>The result of the speculative parsing is primarily a series of <span>speculative
fetches</span>. Which kinds of resources to speculatively fetch is
<span>implementation-defined</span>, but user agents must not speculatively fetch resources that
would not be fetched with the normal HTML parser, under the assumption that the script that is
blocking the HTML parser does nothing.</p>

<p class="note">It is possible that the same markup is seen multiple times from the
<span>speculative HTML parser</span> and then the normal HTML parser. It is expected that
duplicated fetches will be prevented by normal caching rules.</p>
</li>
</ul>

<p>The <dfn export>speculative fetches</dfn> must follow these rules:</p>

<p class="XXX">Should some of these things be applied to the document "for real", even
though they are found speculatively?</p>

<ul>
<li><p>Let the HTML parser's invocation of the <span data-x="concept-appcache-init">application
cache selection algorithm</span> complete before initiating any speculative fetches, if
applicable.</p></li>

<li>
<p>If the <span>speculative HTML parser</span> encounters one of the following elements, then
act as if that element is processed for the purpose of its effect of speculative fetches for
resources after the element.</p>

<ul class="brief">
<li>A <code>base</code> element.</li>

<li>A <code>meta</code> element whose <code data-x="attr-meta-http-equiv">http-equiv</code>
attribute is in the <span data-x="attr-meta-http-equiv-content-security-policy">Content
security policy</span> state.</li>

<li>A <code>meta</code> element whose <code data-x="attr-meta-name">name</code> attribute is an
<span>ASCII case-insensitive</span> match for "<code
data-x="meta-referrer">referrer</code>".</li>

<li>A <code>meta</code> element whose <code data-x="attr-meta-name">name</code> attribute is an
<span>ASCII case-insensitive</span> match for "<code data-x="">viewport</code>". (This can
affect whether a media query list <span>matches the environment</span>.) <ref
spec=CSSDEVICEADAPT></li>
</ul>
</li>
</ul>

<p>To <dfn>start the speculative HTML parser</dfn> for an instance of an HTML parser
<var>parser</var>:</p>

<ol>
<li><p>Optionally, return. (This allows user agents to opt out of speculative HTML
parsing.)</p></li>

<li>
<p>If <var>parser</var> already has an <span>active speculative HTML parser</span>, then
<span>stop the speculative HTML parser</span> for <var>parser</var>.</p>

<p class="note">This can happen when <code data-x="">document.write()</code> writes another
parser-blocking script. For simplicity, this specification always restarts speculative parsing,
but user agents can implement a more efficient strategy, so long as the end result is
equivalent.</p>
</li>

<li><p>Let <var>speculativeParser</var> be a new <span>speculative HTML parser</span>, with the
same state as <var>parser</var>.</p></li>

<li><p>Set <var>parser</var>'s <span>active speculative HTML parser</span> to
<var>speculativeParser</var>.</p></li>

<li><p><span>In parallel</span>, run <var>speculativeParser</var> until it is stopped or until it
reaches the end of its <span>input stream</span>.</p></li>
</ol>


<p>To <dfn>stop the speculative HTML parser</dfn> for an instance of an HTML parser
<var>parser</var>:</p>

<ol>
<li><p>Let <var>speculativeParser</var> be <var>parser</var>'s <span>active speculative HTML
parser</span>.</p></li>

<li><p>If <var>speculativeParser</var> is null, then return.</p></li>

<li><p>Throw away any pending content in <var>speculativeParser</var>'s <span>input
stream</span>, and discard any future content that would have been added to it.</p></li>

<li><p>Set <var>parser</var>'s <span>active speculative HTML parser</span> to null.</p></li>
</ol>

</div>


<div w-nodev>

<h4>Coercing an HTML DOM into an infoset</h4>
Expand Down Expand Up @@ -123986,6 +124096,9 @@ INSERT INTERFACES HERE
<dt id="refsCSSCOLORADJUST">[CSSCOLORADJUST]</dt>
<dd><cite><a href="https://drafts.csswg.org/css-color-adjust/">CSS Color Adjustment Module</a></cite>, E. Etemad, R. Atanassov, R. Lillesveen, T. Atkins. W3C.</dd>

<dt id="refsCSSDEVICEADAPT">[CSSDEVICEADAPT]</dt>
<dd><cite><a href="https://drafts.csswg.org/ccss-device-adapt/">CSS Device Adaption</a></cite>, F. Rivoal, M. Rakow. W3C.</dd>

<dt id="refsCSSDISPLAY">[CSSDISPLAY]</dt>
<dd><cite><a href="https://drafts.csswg.org/css-display/">CSS Display</a></cite>, T. Atkins, E. Etemad. W3C.</dd>

Expand Down

0 comments on commit 122cff2

Please sign in to comment.