Skip to content

Commit

Permalink
[giow] (2) Change how document.write() is ignored.
Browse files Browse the repository at this point in the history
Fixing http://www.w3.org/Bugs/Public/show_bug.cgi?id=9767

git-svn-id: http://svn.whatwg.org/webapps@5616 340c8d12-0b0e-0410-8428-c7bf67bfef74
  • Loading branch information
Hixie committed Oct 12, 2010
1 parent 45e46cf commit 9c2a807
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 132 deletions.
78 changes: 35 additions & 43 deletions complete.html
Expand Up @@ -11661,23 +11661,35 @@ <h4 id=document.write()><span class=secno>3.5.3 </span><code title=dom-document-

<dd>

<p>Adds the given string(s) to the <code><a href=#document>Document</a></code>'s input
stream. If necessary, calls the <code title=dom-document-open><a href=#dom-document-open>open()</a></code> method implicitly
first.</p>
<p>In general, adds the given string(s) to the
<code><a href=#document>Document</a></code>'s input stream.</p>

<p class=warning>This method has very idiosyncratic behavior. In
some cases, this method can affect the state of the <a href=#html-parser>HTML
parser</a> while the parser is running, resulting in a DOM that
does not correspond to the source of the document. In other cases,
the call can clear the current page first, as if <code title=dom-document-open><a href=#dom-document-open>document.open()</a></code> had been called.
In yet more cases, the method is simply ignored, or throws an
exception. To make matters worse, the exact behavior of this
method can in some cases be dependent on network latency, which
can lead to failures that are very hard to debug. <strong>For all
these reasons, use of this method is strongly
discouraged.</strong></p>

<p>This method throws an <code><a href=#invalid_state_err>INVALID_STATE_ERR</a></code> exception
when invoked on <a href=#xml-documents>XML documents</a>.</p>

<p>Unless called from the body of a <code><a href=#script>script</a></code> element
while the document is being parsed, or called on a script-created
document, calling this method will clear the current page first,
as if <code title=dom-document-open><a href=#dom-document-open>document.open()</a></code> had
been called.</p>

</dd>

</dl><div class=impl>

<p><code><a href=#document>Document</a></code> objects have an
<dfn id=ignore-destructive-writes-counter>ignore-destructive-writes counter</dfn>, which is used in
conjunction with the processing of <code><a href=#script>script</a></code> elements to
prevent external scripts from being able to use <code title=dom-document-write><a href=#dom-document-write>document.write()</a></code> to blow away the
document by implicitly calling <code title=dom-document-open><a href=#dom-document-open>document.open()</a></code>. Initially, the
counter must be set to zero.</p>

<p>The <dfn id=dom-document-write title=dom-document-write><code>document.write(...)</code></dfn>
method must act as follows:</p>

Expand All @@ -11697,8 +11709,8 @@ <h4 id=document.write()><span class=secno>3.5.3 </span><code title=dom-document-
<li>

<p>If the <a href=#insertion-point>insertion point</a> is undefined and the
<code><a href=#document>Document</a></code> has the <a href=#ignore-destructive-writes>"ignore-destructive-writes"</a>
flag set, then abort these steps.</p>
<code><a href=#document>Document</a></code>'s <a href=#ignore-destructive-writes-counter>ignore-destructive-writes
counter</a> is greater than zero, then abort these steps.</p>

</li>

Expand Down Expand Up @@ -14308,15 +14320,6 @@ <h4 id=script><span class=secno>4.3.1 </span>The <dfn><code>script</code></dfn>
<a href=#parser-inserted>"parser-inserted"</a>, to let the parser know when to
execute the script.</p>

<p>The fourth is a flag is the <dfn id=ignore-destructive-writes>"ignore-destructive-writes"</dfn> flag.
Initially, <code><a href=#script>script</a></code> elements must have this flag unset
(script blocks, when created, do not have a "ignore-destructive-writes"
flag). This flag is used to ensure that scripts <em>not</em>
inserted by the parser but inserted while a parser is active do not
blow away the document if they use the <code title=dom-document-write><a href=#dom-document-write>document.write()</a></code> API.
<code><a href=#document>Document</a></code> objects can also have this flag set; it's
propagated to the <code><a href=#document>Document</a></code> when the script runs.</p>

<p>The last few pieces of state are <dfn id="the-script-block's-type"><var>the script block's
type</var></dfn>, <dfn id="the-script-block's-character-encoding"><var>the script block's character
encoding</var></dfn>, and <dfn id="the-script-block's-fallback-character-encoding"><var>the script block's fallback
Expand Down Expand Up @@ -14472,16 +14475,6 @@ <h4 id=script><span class=secno>4.3.1 </span>The <dfn><code>script</code></dfn>

</ol></li>

<li>

<p>If the element's <code><a href=#document>Document</a></code> has an <a href=#active-parser>active
parser</a>, and the parser's <a href=#script-nesting-level>script nesting level</a>
is non-zero, but this <code><a href=#script>script</a></code> element does not have
the <a href=#parser-inserted>"parser-inserted"</a> flag set, the user agent must
set the element's <a href=#ignore-destructive-writes>"ignore-destructive-writes"</a> flag.</p>

</li>

<li id=script-processing-encoding>

<p>If the <code><a href=#script>script</a></code> element has a <code title=attr-script-charset><a href=#attr-script-charset>charset</a></code> attribute, then let
Expand Down Expand Up @@ -14674,6 +14667,12 @@ <h4 id=script><span class=secno>4.3.1 </span>The <dfn><code>script</code></dfn>
<!-- SCRIPT EXEC -->
<dd>

<p>Executing the script block must consists of running the
following steps. For the purposes of these steps, the script is
considered to be from an <i>external file</i> if, while the
<a href=#running-a-script>running a script</a> algorithm above was running for this
script, the <code><a href=#script>script</a></code> element had a <code title=attr-script-src><a href=#attr-script-src>src</a></code> attribute specified.</p>

<ol><li>

<p>Initialize <dfn id="the-script-block's-source"><var>the script block's source</var></dfn> as
Expand Down Expand Up @@ -14779,19 +14778,11 @@ <h4 id=script><span class=secno>4.3.1 </span>The <dfn><code>script</code></dfn>

<li>

<p>If the <code><a href=#script>script</a></code> element's
<a href=#ignore-destructive-writes>"ignore-destructive-writes"</a> flag is set, then flag the
<code><a href=#document>Document</a></code> the <code><a href=#script>script</a></code> element was in
when the <a href=#ignore-destructive-writes>"ignore-destructive-writes"</a> flag was set as being
itself <a href=#ignore-destructive-writes>"ignore-destructive-writes"</a>. Let <var title="">neutralized doc</var> be that
<p>If the script is from an external file, then increment the
<a href=#ignore-destructive-writes-counter>ignore-destructive-writes counter</a> of the
<code><a href=#script>script</a></code> element's <code><a href=#document>Document</a></code>. Let <var title="">neutralized doc</var> be that
<code><a href=#document>Document</a></code>.</p>

<!-- theorem: this can only happen when the parser nesting
levels of all parsers on this thread are zero, so any scripts
inserted while this is true will not themselves have the flag
said, so we don't have to worry about this being a counter
instead of just a boolean. -->

</li>

<li>
Expand All @@ -14808,8 +14799,9 @@ <h4 id=script><span class=secno>4.3.1 </span>The <dfn><code>script</code></dfn>

<li>

<p>Remove the <a href=#ignore-destructive-writes>"ignore-destructive-writes"</a> flag from <var title="">neutralized doc</var>, if it was set in the earlier
step.</p>
<p>Decrement the <a href=#ignore-destructive-writes-counter>ignore-destructive-writes counter</a>
of <var title="">neutralized doc</var>, if it was incremented in
the earlier step.</p>

</li>

Expand Down
78 changes: 35 additions & 43 deletions index
Expand Up @@ -11638,23 +11638,35 @@ http://software.hixie.ch/utilities/js/live-dom-viewer/?%3C%21DOCTYPE%20HTML%3E%0

<dd>

<p>Adds the given string(s) to the <code><a href=#document>Document</a></code>'s input
stream. If necessary, calls the <code title=dom-document-open><a href=#dom-document-open>open()</a></code> method implicitly
first.</p>
<p>In general, adds the given string(s) to the
<code><a href=#document>Document</a></code>'s input stream.</p>

<p class=warning>This method has very idiosyncratic behavior. In
some cases, this method can affect the state of the <a href=#html-parser>HTML
parser</a> while the parser is running, resulting in a DOM that
does not correspond to the source of the document. In other cases,
the call can clear the current page first, as if <code title=dom-document-open><a href=#dom-document-open>document.open()</a></code> had been called.
In yet more cases, the method is simply ignored, or throws an
exception. To make matters worse, the exact behavior of this
method can in some cases be dependent on network latency, which
can lead to failures that are very hard to debug. <strong>For all
these reasons, use of this method is strongly
discouraged.</strong></p>

<p>This method throws an <code><a href=#invalid_state_err>INVALID_STATE_ERR</a></code> exception
when invoked on <a href=#xml-documents>XML documents</a>.</p>

<p>Unless called from the body of a <code><a href=#script>script</a></code> element
while the document is being parsed, or called on a script-created
document, calling this method will clear the current page first,
as if <code title=dom-document-open><a href=#dom-document-open>document.open()</a></code> had
been called.</p>

</dd>

</dl><div class=impl>

<p><code><a href=#document>Document</a></code> objects have an
<dfn id=ignore-destructive-writes-counter>ignore-destructive-writes counter</dfn>, which is used in
conjunction with the processing of <code><a href=#script>script</a></code> elements to
prevent external scripts from being able to use <code title=dom-document-write><a href=#dom-document-write>document.write()</a></code> to blow away the
document by implicitly calling <code title=dom-document-open><a href=#dom-document-open>document.open()</a></code>. Initially, the
counter must be set to zero.</p>

<p>The <dfn id=dom-document-write title=dom-document-write><code>document.write(...)</code></dfn>
method must act as follows:</p>

Expand All @@ -11674,8 +11686,8 @@ http://software.hixie.ch/utilities/js/live-dom-viewer/?%3C%21DOCTYPE%20HTML%3E%0
<li>

<p>If the <a href=#insertion-point>insertion point</a> is undefined and the
<code><a href=#document>Document</a></code> has the <a href=#ignore-destructive-writes>"ignore-destructive-writes"</a>
flag set, then abort these steps.</p>
<code><a href=#document>Document</a></code>'s <a href=#ignore-destructive-writes-counter>ignore-destructive-writes
counter</a> is greater than zero, then abort these steps.</p>

</li>

Expand Down Expand Up @@ -14285,15 +14297,6 @@ c-end = "--&gt;"</pre>
<a href=#parser-inserted>"parser-inserted"</a>, to let the parser know when to
execute the script.</p>

<p>The fourth is a flag is the <dfn id=ignore-destructive-writes>"ignore-destructive-writes"</dfn> flag.
Initially, <code><a href=#script>script</a></code> elements must have this flag unset
(script blocks, when created, do not have a "ignore-destructive-writes"
flag). This flag is used to ensure that scripts <em>not</em>
inserted by the parser but inserted while a parser is active do not
blow away the document if they use the <code title=dom-document-write><a href=#dom-document-write>document.write()</a></code> API.
<code><a href=#document>Document</a></code> objects can also have this flag set; it's
propagated to the <code><a href=#document>Document</a></code> when the script runs.</p>

<p>The last few pieces of state are <dfn id="the-script-block's-type"><var>the script block's
type</var></dfn>, <dfn id="the-script-block's-character-encoding"><var>the script block's character
encoding</var></dfn>, and <dfn id="the-script-block's-fallback-character-encoding"><var>the script block's fallback
Expand Down Expand Up @@ -14449,16 +14452,6 @@ c-end = "--&gt;"</pre>

</ol></li>

<li>

<p>If the element's <code><a href=#document>Document</a></code> has an <a href=#active-parser>active
parser</a>, and the parser's <a href=#script-nesting-level>script nesting level</a>
is non-zero, but this <code><a href=#script>script</a></code> element does not have
the <a href=#parser-inserted>"parser-inserted"</a> flag set, the user agent must
set the element's <a href=#ignore-destructive-writes>"ignore-destructive-writes"</a> flag.</p>

</li>

<li id=script-processing-encoding>

<p>If the <code><a href=#script>script</a></code> element has a <code title=attr-script-charset><a href=#attr-script-charset>charset</a></code> attribute, then let
Expand Down Expand Up @@ -14651,6 +14644,12 @@ c-end = "--&gt;"</pre>
<!-- SCRIPT EXEC -->
<dd>

<p>Executing the script block must consists of running the
following steps. For the purposes of these steps, the script is
considered to be from an <i>external file</i> if, while the
<a href=#running-a-script>running a script</a> algorithm above was running for this
script, the <code><a href=#script>script</a></code> element had a <code title=attr-script-src><a href=#attr-script-src>src</a></code> attribute specified.</p>

<ol><li>

<p>Initialize <dfn id="the-script-block's-source"><var>the script block's source</var></dfn> as
Expand Down Expand Up @@ -14756,19 +14755,11 @@ c-end = "--&gt;"</pre>

<li>

<p>If the <code><a href=#script>script</a></code> element's
<a href=#ignore-destructive-writes>"ignore-destructive-writes"</a> flag is set, then flag the
<code><a href=#document>Document</a></code> the <code><a href=#script>script</a></code> element was in
when the <a href=#ignore-destructive-writes>"ignore-destructive-writes"</a> flag was set as being
itself <a href=#ignore-destructive-writes>"ignore-destructive-writes"</a>. Let <var title="">neutralized doc</var> be that
<p>If the script is from an external file, then increment the
<a href=#ignore-destructive-writes-counter>ignore-destructive-writes counter</a> of the
<code><a href=#script>script</a></code> element's <code><a href=#document>Document</a></code>. Let <var title="">neutralized doc</var> be that
<code><a href=#document>Document</a></code>.</p>

<!-- theorem: this can only happen when the parser nesting
levels of all parsers on this thread are zero, so any scripts
inserted while this is true will not themselves have the flag
said, so we don't have to worry about this being a counter
instead of just a boolean. -->

</li>

<li>
Expand All @@ -14785,8 +14776,9 @@ c-end = "--&gt;"</pre>

<li>

<p>Remove the <a href=#ignore-destructive-writes>"ignore-destructive-writes"</a> flag from <var title="">neutralized doc</var>, if it was set in the earlier
step.</p>
<p>Decrement the <a href=#ignore-destructive-writes-counter>ignore-destructive-writes counter</a>
of <var title="">neutralized doc</var>, if it was incremented in
the earlier step.</p>

</li>

Expand Down

0 comments on commit 9c2a807

Please sign in to comment.