8.4. 動的~markup挿入

注記: ~markupを文書の中へ動的に挿入するための~APIは、構文解析器と相互作用するので,それらの挙動は[ `~HTML文書$(および`~HTML構文解析器$), `~XML文書$(および`~XML構文解析器$) ]のどちらで利用されるかに依存して変わる。 ◎ 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).

各 `文書$には `動的~markup挿入-時には投出する~counter@ があり、初期~時には 0 に設定され~MUST。 この~counterは、`~tokenから要素を作成-$する~algoと併用され,[ `~custom要素~構築子$が構文解析器から呼出されたときに,`文書$上の[ `open()$m / `close()$m / `write()$m ]が利用-可能になる ]のを防ぐために利用される。 ◎ 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(type, replace), document.close(), and document.write() when they are invoked by the parser. Initially, the counter must be set to zero.

8.4.1. 入力~streamの~open法

%document = %document . `open( [ type [, replace ] ] )$m
`文書$をその場で置換する — それまでの~objを再利用しつつ,それが新たな`文書$であったかのように。 その~objが返される。 ◎ 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.
結果の`文書$には、~HTML構文解析器が結付けられる — それには `document.write()$m を利用して構文解析する~dataを与えれる。 ◎ The resulting Document has an HTML parser associated with it, which can be given data to parse using document.write().\
%type 引数は無視される。 ◎ (The type argument is ignored.)
%replace 引数に値 `replace^l を与えた場合、~session履歴~内に在する[ `文書$用の既存の~entryたち ]は除去される。 ◎ If the replace argument is present and has the value "replace", the existing entries in the session history for the Document object are removed.
`文書$が依然として構文解析-中にある場合、この~methodの効果は無い。 ◎ The method has no effect if the Document is still being parsed.
次の場合、 `InvalidStateError$E 例外を投出する ⇒# `文書$は`~XML文書$である / `~custom要素~構築子$を現在~実行している ◎ 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.
%window = %document . `~openW(url, name, features)$m
`window.open()$m ~methodと同様に働く。 ◎ Works like the window.open() method.

各 `文書$には `~unload中の~openは無視する~counter@ があり、初期~時には 0 に設定され~MUST。 この~counterは、[ `文書を~unloadする$間に,~scriptが `document.open(type, replace)$m ~methodを呼出す ]のを(直接間接問わず)防ぐために利用される。 ◎ Document objects have an ignore-opens-during-unload counter, which is used to prevent scripts from invoking the document.open(type, replace) method (directly or indirectly) while the document is being unloaded. Initially, the counter must be set to zero.

`document.open 手続き@ は, 所与の ( %文書, %入力を置換する ) に対し,次を走らす: ◎ The document open steps, given a document and replaceInput, are as follows:

  1. ~IF[ %文書 は`~XML文書$である ] ⇒ ~THROW `InvalidStateError$E ◎ If document is an XML document, then throw an "InvalidStateError" DOMException exception.
  2. ~IF[ %文書 の`動的~markup挿入-時には投出する~counter$ ~GT 0 ] ⇒ ~THROW `InvalidStateError$E ◎ If document's throw-on-dynamic-markup-insertion counter is greater than 0, then throw an "InvalidStateError" DOMException.
  3. ~IF[ %文書 は`作動中の文書$でない ] ⇒ ~RET ◎ If document is not an active document, then return document.
  4. ~IF[ ( %文書 の`生成元$, `~entry設定群~obj$により指定される`担当の文書$の`生成元$ ) は`同一生成元$でない ] ⇒ ~THROW `SecurityError$E ◎ If document's origin is not same origin to the origin of the responsible document specified by the entry settings object, then throw a "SecurityError" DOMException.
  5. ~IF[ %文書 には`作動中の構文解析器$がある ]~AND[ その`~script入子~level$ ~GT 0 ] ⇒ ~RET %文書 ◎ If document has an active parser whose script nesting level is greater than 0, then return document.

    注記: これは、 `document.open(type, replace)$m が[ 構文解析-中に見出された~inline~script内で~callされたとき ]には,基本的に無視させる — ~timer~callbackや~event~handlerなどの,構文解析器によらない~taskから~callされたときには、依然として効果があるようにしつつ。 ◎ This basically causes document.open(type, replace) 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.

  6. ~IF[ %文書 の`~unload中の~openは無視する~counter$ ~GT 0 ] ⇒ ~RET %文書 ◎ Similarly, if document's ignore-opens-during-unload counter is greater than 0, then return document.

    注記: これは、 `document.open(type, replace)$m が[ %文書 が~unloadされている間に[ `beforeunload$et / `pagehide$et / `unload$et ]~event~handlerから~callされたとき ]には,基本的に無視させる。 ◎ This basically causes document.open(type, replace) to be ignored when it's called from a beforeunload, pagehide, or unload event handler while the Document is being unloaded.

  7. %B ~LET %文書 が属する`閲覧文脈$ ◎ ↓
  8. %置換する ~LET [ 次のいずれかが満たされるならば ~T / ~ELSE_ ~F ]: ◎ Let replace be false.

    • %入力を置換する は`~ASCII大小無視$で `replace^l に合致する ◎ If replaceInput is an ASCII case-insensitive match for "replace", then set replace to true.
    • [ %B の`~session履歴$が包含する`文書$は[ %B が`作成され$bcたときに作成された `about_blank$sc `文書$ %D ]のみからなる ]~AND[ %D に対し`文書を~unloadする$ ~algoが(例えば以前の `document.open(type, replace)$m の~callにより)呼出されたことは一度もない ] ◎ Otherwise, if document's browsing context's session history contains only one Document object, and that was the about:blank Document created when document's browsing context was created, and that Document object has never had the unload a document algorithm invoked on it (e.g., by a previous call to document.open(type, replace)), then set replace to true.
  9. %文書 の`回復-可能~状態$ ~SET ~F ◎ Set document's salvageable state to false.
  10. %文書 の`~unloadを~promptする$ ⇒ ~IF[ 利用者は %文書 の`~unloadを拒否した$ ] ⇒ ~RET %文書 ◎ Prompt to unload document. If the user refused to allow the document to be unloaded, then return document.
  11. `文書を~unloadする$( %文書, `使回す^i ) ◎ Unload document, with the recycle parameter set to true.
  12. `文書を中止する$( %文書 ) ◎ Abort document.
  13. %文書 の ~EACH( `~shadowも含む広義子孫$ %~node ) に対し ⇒ `~event~listenerをすべて除去する$( %~node ) ◎ For each shadow-including inclusive descendant node of document, remove all event listeners with node.
  14. %文書 に結付けられている`~task$は — その`~task源$は問わず — すべて除去する ◎ Remove any tasks associated with document in any task source.
  15. %文書 の`全~内容を~nodeで置換する$( ~NULL ) — 変異~eventは発火することなく ◎ Replace all with null within document, without firing any mutation events.
  16. %~realm実行~文脈 ~LET 次のように~custom化する下で,`新たな~JS~realmを作成する$ ⇒# 大域~obj用に 新たな `Window$I ~obj %~window を作成する, 大域 `this^jv 束縛には %B に結付けられている `WindowProxy$I を利用する ◎ Let realm execution context be the result of creating a new JavaScript realm with the following customizations: • For the global object, create a new Window object window. • For the global this binding, use document's browsing context's associated WindowProxy.

    これは、まだ普遍的に実装されておらず,おそらく除去される — issue#1698 を見よ。 ◎ This is not universally implemented and can perhaps be removed; see issue #1698.

  17. `~window用に環境~設定群~objを設定しておく$( %~realm実行~文脈 ) ◎ Set up a window environment settings object with realm execution context.
  18. `作動中の文書を設定する$( %B, %文書, %~window ) ◎ Set the active document of document's browsing context to document with window.
  19. `文書$を成す各~singleton~objを[ %~window の`~Realm$内に作成される,その~objの新たな~instance ]に置換する ◎ Replace document's singleton objects with new instances of those objects, created in window's Realm.\

    (特に、次に挙げる~objが含まれる: `History$I, `ApplicationCache$I, `Navigator$I, 各種 `BarProp$I, 2 種の `Storage$I, 各種 `HTMLCollection$I, `Selection$I の様な他の仕様により定義される~obj。 それはまた、 %文書 の~prototypeも含め,~JS言語束縛におけるすべての~Web~IDL~prototypeも含む)。 ◎ (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.)

  20. ~IF[ %文書 の`読込後~taskは準備済み$である ] ⇒ %文書 の ⇒# `~reload上書き~flag$ ~SET ~ON, `~reload上書き~buffer$ ~SET 空~文字列 ◎ If document is ready for post-load tasks, then set document's reload override flag and set document's reload override buffer to the empty string.
  21. %文書 の`回復-可能~状態$ ~SET ~T ◎ Set document's salvageable state back to true.
  22. %文書 の`~URL$doc ~SET `~entry設定群~obj$により指定される`担当の文書$の`~URL$doc ◎ Change document's URL to the URL of the responsible document specified by the entry settings object.
  23. ~IF[ %文書 の`~iframe読込中~flag$ ~EQ ~ON ] ⇒ %文書 の`~iframe読込黙秘中~flag$ ~SET ~ON ◎ If document's iframe load in progress flag is set, then set document's mute iframe load flag.
  24. 次のようにされた新たな `~HTML構文解析器$を %文書 に結付ける ⇒ 符号化法の`確度$ ~SET `無関係^i

    これは、 `~scriptにより作成された構文解析器@ とされる。

    それは、次の 2 つを意味する:

    • `document.open(type, replace)$m / `document.close()$m ~methodにより~closeできる。
    • その~tokenizerは、~end-of-file~tokenを発する前に,明示的な `document.close()$m の~callを待機する。
    ◎ 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(type, replace) 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.
  25. %文書 の`現在の準備度$ ~SET `loading^l ◎ Set the current document readiness of document to "loading".
  26. 次を満たす`~task$はすべて除去する ⇒ [ `履歴~走査~task源$から~queueされた ]~AND[[ `~top-level閲覧文脈$の`文書~族$ ]内のある`文書$ ]に結付けられている ] ◎ Remove any tasks queued by the history traversal task source that are associated with any Document objects in the top-level browsing context's document family.
  27. %B の`~session履歴$から,次に該当する~entryすべてを除去する:

    • `現在の~entry$より後にある ◎ Remove all the entries in the browsing context's session history after the current entry. If the current entry is the last entry in the session history, then no entries are removed.

      注記: これは、~UAの~UIに影響するとは限らない。 ◎ This doesn't necessarily have to affect the user agent's user interface.

    • `現在の~entry$より前にある, かつ その`文書$ ~EQ %文書 ◎ Remove any earlier entries whose Document object is document.
  28. ~IF[ %置換する ~EQ ~F ] ⇒ 最後の~entryの直前に 次が結付けられた新たな~entryを追加する ⇒# [ 以前に %文書 に結付けられていた構文解析器]により構文解析された~text, この手続きの開始-時の %文書 の状態 ◎ If replace is false, then add a new entry, just before the last entry, and associate with the new entry the text that was parsed by the previous parser associated with document, as well as the state of document at the start of these steps.\

    これにより,利用者は、~session履歴~内を 1 つ後戻りして, `document.open(type, replace)$m ~callにより吹飛ばされた前の頁を見れるようになる。 この新たな~entryは,`文書$を持たないので、~session履歴がその~entryへ走査されたときには,新たな`文書$が作成されることになる。 ◎ This allows the user to step backwards in the session history to see the page before it was blown away by the document.open(type, replace) call. This new entry does not have a Document object, so a new one will be created if the session history is traversed to that entry.

  29. %文書 の`~unload発火-済み$ ~flag ~SET ~F (この~flagは、上の`文書を~unloadする$段の間に ~T にされ得る) ◎ Set document's fired unload flag to false. (It could have been set to true during the unload step above.)
  30. `挿入~地点$ ~SET `入力~stream$の終端の直前 (この時点では、入力~streamは空になる。) ◎ Finally, set the insertion point to point at just before the end of the input stream (which at this point will be empty).
  31. ~RET %文書 ◎ Return document.

`open(type, replace)@m ~methodの被呼出時には、次を走らせ~MUST ⇒ ~RET `document.open 手続き$( 此れ, %replace ) ◎ The open(type, replace) method must return the result of running the document open steps with this Document object and replace.

注記: %type 引数は無視される。 また,この~methodは、`文書$が[ `読込後~taskは準備済み$, `完全に読込まれ$る ]のどちらになるかには影響しない。 ◎ The type argument is ignored. Also, the document.open(type, replace) method does not affect whether a Document is ready for post-load tasks or completely loaded.

`~openW(url, name, features)@m ~methodの被呼出時には、次を走らせ~MUST ◎ The open(url, name, features) method must run these steps:

  1. ~IF[ 此れは`作動中の文書$でない ] ⇒ ~THROW `InvalidStateError$E ◎ If this Document object is not an active document, then throw an "InvalidStateError" DOMException exception.
  2. ~RET `~window~open手続き$( %url, %name, %features ) ◎ Return the result of running the window open steps with url, name, and features.

8.4.2. 入力~streamの~close法

%document . `close()$m
`document.open(type, replace)$m ~methodで~openされた入力~streamを~closeする。 ◎ Closes the input stream that was opened by the document.open(type, replace) method.
次の場合、 `InvalidStateError$E 例外を投出する ⇒# `文書$は`~XML文書$である / `~custom要素~構築子$を現在~実行している ◎ 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.

`document.close()@m ~methodの被呼出時には、次を走らせ~MUST: ◎ The close() method must run the following steps:

  1. ~IF[ 此れは`~XML文書$である ] ⇒ ~THROW `InvalidStateError$E ◎ If the Document object is an XML document, then throw an "InvalidStateError" DOMException.
  2. ~IF[ 此れの`動的~markup挿入-時には投出する~counter$ ~GT 0 ] ⇒ ~THROW `InvalidStateError$E ◎ If the Document object's throw-on-dynamic-markup-insertion counter is greater than zero, then throw an "InvalidStateError" DOMException.
  3. %構文解析器 ~LET 此れの`~scriptにより作成された構文解析器$は[ 在るならば それ / 無いならば ε ] ◎ ↓
  4. ~IF[ %構文解析器 ~EQ ε ] ⇒ ~RET ◎ If there is no script-created parser associated with the document, then return.
  5. %構文解析器 の`入力~stream$の終端に`明示的な~EOF$を挿入する ◎ Insert an explicit "EOF" character at the end of the parser's input stream.
  6. ~IF[ 此れの`構文解析器を阻んでいる~script$は在る ] ⇒ ~RET ◎ If there is a pending parsing-blocking script, then return.
  7. %構文解析器 の~tokenizerを走らす ⇒ ~tokenizerから~tokenが発されるごとに,それを処理する。 [ ~tokenizerが`明示的な~EOF$に達するか,`~event-loopを回す$ ]時点で停止する。 ◎ 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.

8.4.3. `document.write()^m

%document . `write(text...)$m
一般に,所与の文字列(たち)を`文書$の入力~streamに追加する。 ◎ In general, adds the given string(s) to the Document's input stream.
この~methodの挙動は、とても特異的である。 一部の事例では、`~HTML構文解析器$が走っている間に 構文解析器の状態に影響し得る — その結果、文書の~sourceに対応しない~DOMになり得る(例:書込された文字列が `<plaintext>^l や `<!--^l であった場合など)。 別の事例では、 `document.open(type, replace)$m が~callされていたかのように,現在の頁をまず消去することもある。 さらには、単純に無視されたり, 例外を投出する事例もある。 ~UAには、この~methodを介して挿入された `script^e 要素に対しては, 実行するのを避けることが明示的に許容される。 この~methodの正確な挙動は、~network待時間に依存する事例すらあり、とても~debugし難い失敗へ導く。 これらすべての理由から、この~methodは利用しないことが強く奨励される。 ◎ 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(type, replace) 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.
次の場合、 `InvalidStateError$E 例外を投出する ⇒# `文書$は`~XML文書$である / `~custom要素~構築子$を現在~実行している ◎ Throws an "InvalidStateError" DOMException when invoked on XML documents. ◎ Throws an "InvalidStateError" DOMException if the parser is currently executing a custom element constructor.

各 `文書$には `破壊的書込は無視する~counter@ があり、初期~時には 0 に設定され~MUST。 この~counterは、 `script$e 要素の処理と併用され,[ 外部~scriptが、[ `document.write()$m を利用して,暗黙的に`document.open(type, replace)$m を~callする ]ことにより,文書を吹飛ばす ]のを防ぐために利用される。 ◎ 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(type, replace). Initially, the counter must be set to zero.

`document.write 手続き@ は、所与の ( `文書$ %文書, 文字列 %入力 ) に対し,次を走らす: ◎ The document write steps, given a Document object document and a string input, are as follows:

  1. ~IF[ %文書 は`~XML文書$である ] ⇒ ~THROW `InvalidStateError$E ◎ If document is an XML document, then throw an "InvalidStateError" DOMException.
  2. ~IF[ %文書 の`動的~markup挿入-時には投出する~counter$ ~GT 0 ] ⇒ ~THROW `InvalidStateError$E ◎ If document's throw-on-dynamic-markup-insertion counter is greater than 0, then throw an "InvalidStateError" DOMException.
  3. ~IF[ %文書 は`作動中の文書$でない ] ⇒ ~RET ◎ If document is not an active document, then return.
  4. ~IF[ `挿入~地点$ ~EQ ε ]: ◎ If the insertion point is undefined, then:

    1. ~IF[ %文書 の`~unload中の~openは無視する~counter$ ~GT 0 ]~OR[ %文書 の`破壊的書込は無視する~counter$ ~GT 0 ] ⇒ ~RET ◎ 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.
    2. `document.open 手続き$( %文書, 空~文字列 ) ◎ Run the document open steps with document and the empty string.\
    3. ~IF[ 前~段の手続きの中で,利用者は %文書 の`~unloadを拒否した$ ] ⇒ ~RET ◎ If the user refused to allow the document to be unloaded, then return.\
    4. `挿入~地点$ ~SET(空の)`入力~stream$の終端の直前 ◎ Otherwise, the insertion point will point at just before the end of the (empty) input stream.
  5. %入力 を`入力~stream$内の`挿入~地点$に挿入する ◎ Insert input into the input stream just before the insertion point.
  6. ~IF[ %文書 の`~reload上書き~flag$ ~EQ ~ON ] ⇒ %入力 を %文書 の`~reload上書き~buffer$に付加する ◎ If document's reload override flag is set, then append input to document's reload override buffer.
  7. ~IF[ `構文解析器を阻んでいる~script$は在る ] ⇒ ~RET ◎ If there is no pending parsing-blocking script,\
  8. %入力 を`~HTML構文解析器$に処理させる: ◎ have the HTML parser process input,\

    • 符号位置を 1 個ずつ与え,結果の~tokenが発される度にそれを処理する。 ◎ one code point at a time, processing resulting tokens as they are emitted, and\
    • 次のいずれかが生じたときに停止する ⇒ ~tokenizerが挿入~地点に達したとき / ~tokenizerの処理が 木~構築~段階にて中止されたとき(これは、 `script$e 終了tag~tokenが~tokenizerにより発された場合に起こり得る)。 ◎ 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).

    注記: `document.write()$m ~methodが,~inlineに実行している~scriptから~callされている場合(すなわち,構文解析器が `script$e を終了tagまで構文解析したことにより,~callされた)、これは 構文解析器の再入呼出しになる。 [ 構文解析器の`静止~flag$ ~EQ ~ON ]の場合、~tokenizerにおける`静止~flag検査$により,[ ~tokenizerは即時に中止され,~HTMLは構文解析されない ]ことになる。 ◎ 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.

`document.write(...)@m ~methodの被呼出時には、次を走らせ~MUST ⇒ `document.write 手続き$( 此れ, 次に与える文字列 ) ⇒ 渡されたすべての引数を順に連結した結果 ◎ 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.

8.4.4. `document.writeln()^m

%document . `writeln(text...)$m
所与の文字列(たち)を `文書$の入力~streamに追加した上で、改行文字も追加する。 必要とされるなら, `open(type, replace)$m ~methodも暗黙的に~callする。 ◎ Adds the given string(s) to the Document's input stream, followed by a newline character. If necessary, calls the open(type, replace) method implicitly first.
次の場合、 `InvalidStateError$E 例外を投出する ⇒# `文書$は`~XML文書$である / `~custom要素~構築子$を現在~実行している ◎ Throws an "InvalidStateError" DOMException when invoked on XML documents. ◎ Throws an "InvalidStateError" DOMException if the parser is currently executing a custom element constructor.

`document.writeln(...)@m ~methodの被呼出時には、次を走らせ~MUST ⇒ `document.write 手続き$( 此れ, 次に与える文字列 ) ⇒ 渡されたすべての引数を順に連結した結果に `000A^U LINE FEED を付加した結果 ◎ 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.