APIs for dynamically inserting markup into the document interact with the parser, and thus their behavior varies depending on whether they are used with HTML documents (and the HTML parser ) or XML documents (and the XML parser ).
Document
objects
have
a
throw-on-dynamic-markup-insertion
counter
,
which
is
used
in
conjunction
with
the
create
an
element
for
the
token
algorithm
to
prevent
custom
element
constructors
from
being
able
to
use
document.open()
,
document.close()
,
and
document.write()
when
they
are
invoked
by
the
parser.
Initially,
the
counter
must
be
set
to
zero.
open
(
Causes
the
Document
to
be
replaced
in-place,
as
if
it
was
a
new
Document
object,
but
reusing
the
previous
object,
which
is
then
returned.
If
the
type
argument
is
omitted
or
has
the
value
"
text/html
",
then
the
The
resulting
Document
has
an
HTML
parser
associated
with
it,
which
can
be
given
data
to
parse
using
document.write()
.
Otherwise,
all
content
passed
to
document.write()
will
be
parsed
as
plain
text.
If
the
replace
argument
is
present
and
has
the
value
"
replace
",
the
existing
entries
in
the
session
history
for
the
Document
object
are
removed.
The
method
has
no
effect
if
the
Document
is
still
being
parsed.
Throws
an
"
InvalidStateError
"
DOMException
if
the
Document
is
an
XML
document
.
Throws
an
"
InvalidStateError
"
DOMException
if
the
parser
is
currently
executing
a
custom
element
constructor
.
open
(
url
,
name
,
features
)
Works
like
the
window.open()
method.
Document
objects
have
an
ignore-opens-during-unload
counter
,
which
is
used
to
prevent
scripts
from
invoking
the
document.open()
method
(directly
or
indirectly)
while
the
document
is
being
unloaded
.
Initially,
the
counter
must
be
set
to
zero.
The
document
open
steps
,
given
a
document
,
type
,
and
replaceInput
,
are
as
follows:
If
document
is
an
XML
document
,
then
throw
an
"
InvalidStateError
"
DOMException
exception.
If
document
's
throw-on-dynamic-markup-insertion
counter
is
greater
than
0,
then
throw
an
"
InvalidStateError
"
DOMException
.
If
Let
document
entryDocument
is
not
an
active
document
,
then
return
be
the
responsible
document
.
specified
by
the
entry
settings
object
.
If
document
's
origin
is
not
same
origin
to
the
entryDocument
's
origin
of
the
responsible
document
specified
by
the
entry
settings
object
,
then
throw
a
"
SecurityError
"
DOMException
.
If document has an active parser whose script nesting level is greater than 0, then return document .
This
basically
causes
document.open()
to
be
ignored
when
it's
called
in
an
inline
script
found
during
parsing,
while
still
letting
it
have
an
effect
when
called
from
a
non-parser
task
such
as
a
timer
callback
or
event
handler.
Similarly, if document 's ignore-opens-during-unload counter is greater than 0, then return document .
This
basically
causes
document.open()
to
be
ignored
when
it's
called
from
a
beforeunload
,
pagehide
,
or
unload
event
handler
while
the
Document
is
being
unloaded.
If
replaceInput
document
has
a
browsing
context
and
there
is
an
ASCII
case-insensitive
match
for
"
replace
",
then
set
replace
existing
attempt
to
true.
Otherwise,
if
navigate
document
's
browsing
context
,
then
stop
document
loading
's
session
history
contains
only
one
Document
given
document
.
Issue
#3447
object,
and
that
was
looks
into
the
about:blank
Document
distinction
between
an
ongoing
instance
of
the
navigate
created
when
document
's
browsing
context
algorithm
versus
tasks
to
navigate
was
created
,
and
that
Document
object
has
never
had
are
still
queued.
For
the
unload
a
document
purpose
of
implementing
this
step,
both
an
ongoing
instance
of
the
navigate
algorithm
invoked
on
it
(e.g.,
by
a
previous
call
and
tasks
queued
to
document.open()
navigate
),
then
set
replace
should
be
counted
towards
"an
existing
attempt
to
true.
navigate
,"
before
that
issue
is
resolved.
Set
For
each
shadow-including
inclusive
descendant
document
node
's
salvageable
state
to
false.
Prompt
to
unload
of
document
.
If
the
user
refused
to
allow
the
document
to
be
unloaded
,
then
return
,
erase
all
event
listeners
and
handlers
given
document
node
.
Unload
If
document
,
with
the
recycle
parameter
set
to
true.
is
the
associated
Document
Abort
of
document
.
Unregister
's
relevant
global
object
,
then
erase
all
event
listeners
registered
on
document
and
its
descendants.
Remove
any
tasks
handlers
associated
with
given
document
in
any
task
source
's
relevant
global
object
.
Remove
Replace
all
child
nodes
of
with
null
within
document
,
without
firing
any
mutation
events.
Call
the
JavaScript
InitializeHostDefinedRealm()
abstract
operation
with
the
following
customizations:
For
the
global
object,
create
a
new
Window
object
window
.
For
the
global
this
value,
use
If
document
's
browsing
context
's
associated
WindowProxy
.
This
is
not
universally
implemented
and
can
perhaps
be
removed;
see
issue
#1698
.
fully
active
,
then:
Let
realm
execution
context
newURL
be
the
running
JavaScript
execution
context
.
This
is
the
JavaScript
execution
context
created
in
the
previous
step.
Set
up
a
window
environment
settings
object
with
realm
execution
context
.
Set
the
active
document
of
document
's
browsing
context
to
document
with
window
.
Replace
document
's
singleton
objects
with
new
instances
copy
of
those
objects,
created
in
window
's
Realm
.
(This
includes
in
particular
the
History
,
ApplicationCache
,
and
Navigator
,
objects,
the
various
BarProp
objects,
the
two
Storage
objects,
the
various
HTMLCollection
objects,
and
objects
defined
by
other
specifications,
like
Selection
.
It
also
includes
all
the
Web
IDL
prototypes
in
the
JavaScript
binding,
including
document
's
prototype.)
Change
document
entryDocument
's
character
encoding
to
UTF-8
URL
.
If
document
entryDocument
is
ready
for
post-load
tasks
,
then
set
not
document
's
reload
override
flag
and
,
then
set
document
's
reload
override
buffer
to
the
empty
string.
Set
document
newURL
's
salvageable
fragment
state
back
to
true.
null.
Change
document
's
URL
to
Run
the
URL
and
history
update
steps
of
the
responsible
with
document
specified
by
the
entry
settings
object
.
and
newURL
.
If document 's iframe load in progress flag is set, then set document 's mute iframe load flag.
Set document to no-quirks mode .
Create
a
new
HTML
parser
and
associate
it
with
document
.
This
is
a
script-created
parser
(meaning
that
it
can
be
closed
by
the
document.open()
and
document.close()
methods,
and
that
the
tokenizer
will
wait
for
an
explicit
call
to
document.close()
before
emitting
an
end-of-file
token).
The
encoding
confidence
is
irrelevant
.
Set
the
current
document
readiness
of
document
to
"
loading
".
Finally, set the insertion point to point at just before the end of the input stream (which at this point will be empty).
Return document .
The
document
open
steps
do
not
affect
whether
a
Document
is
ready
for
post-load
tasks
or
fewer,
the
completely
loaded
.
The
document.open(
open(
method
must
return
the
result
of
running
the
document
open
steps
with
this
type
unused1
,
replace
unused2
)
Document
object,
object.
The
type
,
unused1
and
replace
.
The
document.open()
unused2
arguments
are
ignored,
but
kept
in
the
IDL
to
allow
code
that
calls
the
function
with
one
or
two
arguments
to
continue
working.
They
are
necessary
due
to
Web
IDL
overload
resolution
algorithm
method
does
not
affect
whether
rules,
which
would
throw
a
Document
TypeError
is
ready
exception
for
post-load
tasks
such
calls
had
the
arguments
not
been
there.
heycam/webidl#581
or
completely
loaded
.
investigates
changing
the
algorithm
to
allow
for
their
removal.
[WEBIDL]
When
invoked
with
three
arguments,
the
The
open(
url
,
name
,
features
)
method
must
run
these
steps:
If
this
Document
object
is
not
an
active
document
,
then
throw
an
"
InvalidStateError
"
DOMException
exception.
Return the result of running the window open steps with url , name , and features .
close
()
Closes
the
input
stream
that
was
opened
by
the
document.open()
method.
Throws
an
"
InvalidStateError
"
DOMException
if
the
Document
is
an
XML
document
.
Throws
an
"
InvalidStateError
"
DOMException
if
the
parser
is
currently
executing
a
custom
element
constructor
.
The
close()
method
must
run
the
following
steps:
If
the
Document
object
is
an
XML
document
,
then
throw
an
"
InvalidStateError
"
DOMException
.
If
the
Document
object's
throw-on-dynamic-markup-insertion
counter
is
greater
than
zero,
then
throw
an
"
InvalidStateError
"
DOMException
.
If there is no script-created parser associated with the document, then return.
Insert an explicit "EOF" character at the end of the parser's input stream .
If there is a pending parsing-blocking script , then return.
Run the tokenizer, processing resulting tokens as they are emitted, and stopping when the tokenizer reaches the explicit "EOF" character or spins the event loop .
document.write()
write
(
text
...)
In
general,
adds
the
given
string(s)
to
the
Document
's
input
stream.
This
method
has
very
idiosyncratic
behavior.
In
some
cases,
this
method
can
affect
the
state
of
the
HTML
parser
while
the
parser
is
running,
resulting
in
a
DOM
that
does
not
correspond
to
the
source
of
the
document
(e.g.
if
the
string
written
is
the
string
"
<plaintext>
"
or
"
<!--
").
In
other
cases,
the
call
can
clear
the
current
page
first,
as
if
document.open()
had
been
called.
In
yet
more
cases,
the
method
is
simply
ignored,
or
throws
an
exception.
Users
agents
are
explicitly
allowed
to
avoid
executing
script
elements
inserted
via
this
method
.
And
to
make
matters
even
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.
For
all
these
reasons,
use
of
this
method
is
strongly
discouraged.
Throws
an
"
InvalidStateError
"
DOMException
when
invoked
on
XML
documents
.
Throws
an
"
InvalidStateError
"
DOMException
if
the
parser
is
currently
executing
a
custom
element
constructor
.
Document
objects
have
an
ignore-destructive-writes
counter
,
which
is
used
in
conjunction
with
the
processing
of
script
elements
to
prevent
external
scripts
from
being
able
to
use
document.write()
to
blow
away
the
document
by
implicitly
calling
document.open()
.
Initially,
the
counter
must
be
set
to
zero.
The
document
write
steps
,
given
a
Document
object
document
and
a
string
input
,
are
as
follows:
If
document
is
an
XML
document
,
then
throw
an
"
InvalidStateError
"
DOMException
.
If
document
's
throw-on-dynamic-markup-insertion
counter
is
greater
than
0,
then
throw
an
"
InvalidStateError
"
DOMException
.
If the insertion point is undefined, then:
If document 's ignore-opens-during-unload counter is greater than 0 or document 's ignore-destructive-writes counter is greater than 0, then return.
Run
the
document
open
steps
with
document
,
"
text/html
",
and
the
empty
string.
If
the
user
refused
to
allow
the
document
to
be
unloaded
,
then
return.
Otherwise,
the
insertion
point
will
point
at
just
before
the
end
of
the
(empty)
input
stream
.
.
Insert
input
into
the
input
stream
just
before
the
insertion
point
.
If
document
's
reload
override
flag
is
set,
then
append
input
to
document
's
reload
override
buffer
.
If
there
is
no
pending
parsing-blocking
script
,
have
the
HTML
parser
process
input
,
one
code
point
at
a
time,
processing
resulting
tokens
as
they
are
emitted,
and
stopping
when
the
tokenizer
reaches
the
insertion
point
or
when
the
processing
of
the
tokenizer
is
aborted
by
the
tree
construction
stage
(this
can
happen
if
a
script
end
tag
token
is
emitted
by
the
tokenizer).
If
the
document.write()
method
was
called
from
script
executing
inline
(i.e.
executing
because
the
parser
parsed
a
set
of
script
tags),
then
this
is
a
reentrant
invocation
of
the
parser
.
If
the
parser
pause
flag
is
set,
the
tokenizer
will
abort
immediately
and
no
HTML
will
be
parsed,
per
the
tokenizer's
parser
pause
flag
check
.
The
document.write(...)
method,
when
invoked,
must
run
the
document
write
steps
with
this
Document
object
and
a
string
that
is
the
concatanation
of
all
arguments
passed.
document.writeln()
writeln
(
text
...)
Adds
the
given
string(s)
to
the
Document
's
input
stream,
followed
by
a
newline
character.
If
necessary,
calls
the
open()
method
implicitly
first.
Throws
an
"
InvalidStateError
"
DOMException
when
invoked
on
XML
documents
.
Throws
an
"
InvalidStateError
"
DOMException
if
the
parser
is
currently
executing
a
custom
element
constructor
.
The
document.writeln(...)
method,
when
invoked,
must
run
the
document
write
steps
with
this
Document
object
and
a
string
that
is
the
concatanation
of
all
arguments
passed
and
U+000A
LINE
FEED.