Skip to content
Permalink
Browse files

[giow] (2) Change how document.write() is ignored.

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 9c2a807be3ab0630255aad3f29d766025d7d346d
Showing with 109 additions and 132 deletions.
  1. +35 −43 complete.html
  2. +35 −43 index
  3. +39 −46 source

<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>

<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>

<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

</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
<!-- 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

<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>

<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>

78 index

<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>

<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>

<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

</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
<!-- 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

<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>

<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>

Oops, something went wrong.

0 comments on commit 9c2a807

Please sign in to comment.
You can’t perform that action at this time.