HTMLAllCollection

document.all (DOM)

[15] document.all は、文書中のすべての要素を含むコレクションです。 このオブジェクトIE4 DOM の一部であり、現在では廃止状態とされています >>14 が、 Webブラウザーが実装しなければならない >>14 機能の一つとされており、 すべての Webブラウザーが対応しています。

仕様書

Document オブジェクト all 属性 (DOM)

[16] Document インターフェイスall 属性は、 HTMLAllCollection オブジェクトを返す読み取り専用IDL属性です。 このオブジェクトは当該 Documentとし、 すべての要素一致するフィルターを持つものでなければなりません>>14

[37] この属性は、毎回同じオブジェクトを返します (SameObject)。 つまり文書にはちょうど1つだけ all オブジェクトが存在します。

HTMLAllCollection インターフェイス (DOM)

[23] HTMLAllCollection オブジェクトは、 document.all IDL属性で用いられるコレクションです。

[58] HTML StandardHTMLAllCollectionコレクションだと (なぜか) 明記はしていませんが、 HTMLCollection と似ている >>13 とし、コレクションの用語を使って規定されています。

[27] HTMLAllCollection インターフェイス文書環境JavaScriptに晒されています >>13

[38] このオブジェクト生きており、DOM木に対する変更が即時反映されます。

[45] かつては HTMLCollection継承していましたが、 現在は独立しています >>44

メンバー

[24] HTMLAllCollection には、継承するものも含めて次のメンバーがあります。

[57] length IDL特性以外は、すべて、 索引名前要素を取得するものです。 つまり、次のような方法で要素にアクセスできます。

JavaScript 束縛

[94] HTMLAllCollection インターフェイス実装するオブジェクトは、 legacy platform object です HTML Standard

[59] HTMLAllCollection オブジェクトには、 JavaScript にて他のプラットフォームオブジェクトとは異なる特別な取り扱いが規定されています。 すなわち、 HTMLAllCollection オブジェクトは、 IsHTMLDDA 内部スロットを持ちます HTML Standard

[17] document.all オブジェクトJavaScript ToBoolean() 演算を適用すると、を返さなければなりません>>14

[18] document.all オブジェクトJavaScript抽象等値性比較 (==!=演算子) を適用すると、undefinednull と同じように振る舞わなければなりません>>14

[19] ===!== に関しては、普通のオブジェクトと同じように振る舞います。

[20] document.all オブジェクトJavaScripttypeof 演算子を適用すると、文字列 undefined を返さなければなりません>>14

[21] これらの要件は、通常のオブジェクトの動作とは異なりますが、歴史的経緯のために必要 >>14 とされています。

[22] かつて document.allIE のみが実装していたため、 IE であるかどうかの判別に用いられることがありました。すなわち、

if (document.all) {
  // for IE
} else {
  // for non-IE
}
... で IE 以外の近代的ブラウザーは後者が実行される必要がありました。一方で、 IE のみに対応していて document.all が実装されていなければ動作しない Webサイトも存在していました。この両方のパターンに対応するため、 document.all オブジェクトは条件式においては undefined と同じように動作する実装がなされました。 現在では IE近代的ブラウザーHTML Standard に従っています。

索引による要素へのアクセス

[61] HTMLAllCollectionsupported property indices は、 HTMLCollection の場合と同じです >>13。 つまり、 [ 0, 当該コレクションにより表現される要素の数 - 1 ] の範囲整数です >>60

[62] 従って、 n を 0 以上文書中の要素数 - 1 の整数とするとき、 document.all [ n ] が存在し、それ以外の整数 n については存在しません。

[78] HTMLAllCollectionindexed getter は、 次のようにしなければなりません >>13

  1. [79] get the "all"-indexed element を実行した結果を返します。 ただしコレクション文脈オブジェクト索引は与えられた索引とします。

[67] HTMLAllCollection コレクション索引索引について get the "all"-indexed element とは、次のようにすることです >>13

  1. [68] コレクションに含まれる要素索引番目のものがあれば、
    1. [69] コレクションに含まれる要素索引番目のものを返します。
  2. [70] それ以外なら、
    1. [71] null を返します。

[66] HTMLAllCollection インターフェイスlength IDL属性は、 取得時文脈オブジェクトであるコレクションにより表現される要素の数を返さなければなりません >>13

名前による要素へのアクセス

[35] HTMLAllCollectionsupported property names は、 コレクションにより表現される要素の名前 (あれば) のリストです >>13

[65] 従って、 s を要素の名前とするとき、 document.all [ s ] が存在します。

[33] HTMLAllCollection における要素名前とは、 name 属性値ID のことをいいます。

[34] ただし name 属性が用いられるのは 「all」名前付き要素 ("all"-named elements) に限られます。 「all」名前付き要素とは、 HTML

... です >>13

[40] このリストはブラウザーによって相違がありますが、 HTML Standard は広めにとっているようです。 コメントに「keygen?」ともあります >>39

[36] ただし空文字列は名前とはみなされません。

[63] 順序は木順で、重複する場合は最初の方が採用されます >>13

[64] idname の両方がある要素があれば、 id が先に現れたと考えます >>13

[29] HTMLAllCollectionnamedItem メソッドnamed getter は、 次のようにしなければなりません >>13

  1. [80] 名前を、第1引数を文字列として解釈した結果に設定します。
  2. [30] 名前について get the "all"-named element(s) を実行した結果を返します。 コレクションは、文脈オブジェクトとします。

[72] HTMLAllCollection コレクションと名前名前について get the "all"-named element(s) とは、次のようにすることです >>13

  1. [73] 名前空文字列なら、 null を返してここで停止します。
  2. [74] 部分コレクションを、新しい HTMLCollection に設定します。
    コレクション (文書)
    フィルター
    当該節点"all"-named elementsname 属性名前と等しいか、 要素ID名前と等しいなら、
  3. [75] 部分コレクションに含まれる節点の数により、
    [81] 0個の場合
    null を返します。
    [76] 1個の場合
    部分コレクションに含まれる要素を返します。
    [77] それ以外の場合
    部分コレクションを返します。

[31] つまり、同じ名前の要素の個数により、要素が返されたり、 HTMLCollection が返されたりします。 (ここで HTMLAllCollection ではなく HTMLCollection であることにも注意。)
[32] なお、同じ条件の HTMLCollection を返すことになっても、 毎回同じオブジェクトを返すことは求められていないようです。

item メソッドと legacycaller

[25] HTMLAllCollectionitem メソッドは、次のようにしなければなりません >>13

  1. [26] 名前を、第1引数を省略可能文字列と解釈した結果に設定します。
  2. [97] 名前が省略されている場合、
    1. [98] null を返します。
  3. [99] それ以外の場合、
    1. [95] 文脈オブジェクト名前について get the "all"-indexed or named element(s) した結果を返します。

[83] HTMLAllCollection オブジェクトCall は、this引数リストについて、次のようにします >>13

  1. [100] 引数リストサイズ0 か、 引数リスト [ 0 ]] が undefined の場合、
    1. [101] null を返します。
  2. [105] それ以外の場合、
    1. [102] 名前を、 引数リスト [ 0 ] を DOMString変換した結果に設定します。
    2. [103] 結果を、文脈オブジェクト名前について get the "all"-indexed or named element(s) した結果に設定します。
    3. [104] 結果JavaScript値変換した結果を返します。
[106] 文脈オブジェクトの結果が返され、 this は無視されます。

[96] HTMLAllCollection コレクション名前について get the "all"-indexed or named element(s) するには、 次のようにします >>13

  1. [86] 名前配列索引特性名の場合、
    1. [84] 索引を、名前ToUint32 を適用した結果に設定します。
    2. [85] コレクション索引について get the "all"-indexed element を実行した結果を返します。
  2. [87] それ以外の場合、
    1. [82] コレクション名前について get the "all"-named element(s) を実行した結果を返します。

[88] つまり item メソッド引数によって indexed getter のように動作したり、 named getter のように動作したりします。

歴史

IE4

[49] document.allIE4 DOM で追加されました。 Netscape DOM0document.linksdocument.forms などに対して、すべての種別の要素、という意味で「all」と命名されたものと思われます。

[51] その後標準化された DOM1document.all を含んでいませんでした。名前によって要素にアクセスする機能は、 getElementByNamegetElementById という (Java の洗礼を受けたのか) 異様に長い名前のメソッドになりました。

00年代

[52] document.allIE しか実装しませんでしたが、 短く使いやすかったので、少なくない Webページがこれを使っていました。

[53] W3C はずっとこの現実を無視していましたが、 Webブラウザーは現実の Webサイトとの互換性の要求から、 非標準でありながらも document.all を実装せざるを得なくなりました。

[54] Firefox標準モードではこれを実装しないことで最後まで抵抗を続けましたが、 奇癖モードでは実装せざるを得ませんでした。

[55] 一方で、IE のみが実装していた時代に、 document.all の有無で IE とそれ以外のWebブラウザーを分岐する悪習が広まっていました。 document.all を実装しなければならないが、 if (document.all) { ... } を実行してはならない、 という難題でありましたが、 document.all オブジェクト比較時に評価される、 との特例で解決されることとなりました。

[56] このような動作は本来 JavaScript では認められておらず、 document.all の他はかつて styleFloat メソッドで行われたくらいです。
[1] DOMNodeInserted と DOMNodeRemoved に似た仕組みを IE でも使えるようにして、セレクタの実行結果をキャッシュする - uupaaの開発日記 ( 版) http://d.hatena.ne.jp/uupaa/20081127/1227726901
  • DOMNodeInserted, DOMNodeRemoved
    • document.all.length でノード数を調べることで検出します。事前に作成されているコレクションを参照するため、かなり高速です。
    • このやり方では、挿入, 削除されたタイミングを動的に知ることはできません。
    • Firefoxの標準準拠モード以外なら、ほぼ全てのモダンブラウザで使えます。

[2] DSiブラウザdocument.allになります・・・。

HTML5

[50] 結局ほとんどすべての Webブラウザーdocument.all を実装したことで、 HTML5 も「廃止機能」 (Webブラウザーは実装しなければならないが、 著者が使うべきではない機能) として document.all を規定することになりました。

[3] IRC logs: freenode / #whatwg / 20101115 ( ( 版)) http://krijnhoetmer.nl/irc-logs/whatwg/20101115#l-625

[4] Web Applications 1.0 r6900 Update HTMLAllCollection.item(DOMString) to act like namedItem. ( ( 版)) http://html5.org/tools/web-apps-tracker?from=6899&to=6900

[5] RE: Make Microsoft follow the spec. ( (Chris Wilson 著, 版)) http://lists.w3.org/Archives/Public/www-html/2001Mar/0026.html

[6] Web Applications 1.0 r7624 Clarify the document.all black magic. ( ( 版)) http://html5.org/tools/web-apps-tracker?from=7623&to=7624

[7] Web Applications 1.0 r7798 Fix document.all since DOM changed out from under us... also, ack for previous checkin ( ( 版)) http://html5.org/tools/web-apps-tracker?from=7797&to=7798

[8] Web Applications 1.0 r8469 Remove some parts of document.all that data shows have virtually no usage. ( ( 版)) http://html5.org/tools/web-apps-tracker?from=8468&to=8469

[9] Re: twitter, document.all ( (Ms2ger 著, 版)) http://lists.w3.org/Archives/Public/www-archive/2014Feb/0012.html

[10] Web Applications 1.0 r8502 Restore document.all() legacycaller, for compat ( ( 版)) http://html5.org/tools/web-apps-tracker?from=8501&to=8502

[11] Web Applications 1.0 r8469 Remove some parts of document.all that data shows have virtually no usage. ( ( 版)) http://html5.org/tools/web-apps-tracker?from=8468&to=8469

[12] Web Applications 1.0 r8010 Make the empty string no longer be a possible index for most objects that can be indexed by name. ( ( 版)) http://html5.org/tools/web-apps-tracker?from=8009&to=8010

[39] Web Applications 1.0 r8501 Update the list of elements that support name='' attributes for the purposes of document.all (mostly using the IE/Firefox lists, which are nearly identical, and a superset of Safari/Chrome) ( 版) http://html5.org/tools/web-apps-tracker?from=8500&to=8501

[41] HTML Standard Tracker ( ( 版)) http://html5.org/tools/web-apps-tracker?from=8644&to=8645

[42] Intent to Deprecate and Remove: document.all. - Google グループ ( 版) https://groups.google.com/a/chromium.org/forum/#!topic/blink-dev/urMtTTPpUro

[43] ブラウザ JavaScript ( 版) http://www.opera.com/docs/browserjs/

[44] Fix #210: give HTMLAllCollection its own class · whatwg/html@4bade08 ( 版) https://github.com/whatwg/html/commit/4bade08fff14d95011e2709cb2ba47e859c0a690

[46] Update ECMAScript and IDL integration · whatwg/html@550c57f ( 版) https://github.com/whatwg/html/commit/550c57f186dd73784a8e4660440db2f642e37c0e

[47] Revert "Update ECMAScript and IDL integration" · whatwg/html@3a23548 ( 版) https://github.com/whatwg/html/commit/3a235480f1979fcddc65e07fe5a3be7bed428d17

[48] Fix #775: update HTMLAllCollection to match reality better · whatwg/html@24b8c4f ( 版) https://github.com/whatwg/html/commit/24b8c4f7fcd89596eaf5335d813a559bddbb65cd

[89] document.all willful violation · Issue #668 · tc39/ecma262 () https://github.com/tc39/ecma262/issues/668

[90] Implement undefined-like exotic objects by bterlson · Pull Request #673 · tc39/ecma262 () https://github.com/tc39/ecma262/pull/673

[91] Remove <applet> (zcorpan著, ) https://github.com/whatwg/html/commit/b9b9d60f9451c05f8e8d542e4094f987ae1a5fb8

[92] Remove <applet> (except for parsing rules) · Issue #454 · whatwg/html () https://github.com/whatwg/html/issues/454

[93] Editorial: switch HTMLAllCollection legacycaller to custom [[Call]] (tobie著, ) https://github.com/whatwg/html/commit/0679448c59cb4cd69019be1e1f35fee1bcbc9c66

[107] Add HTMLAllCollection [[Call]], remove legacycaller special operation by tobie · Pull Request #2932 · whatwg/html () https://github.com/whatwg/html/pull/2932

[108] Remove legacy callers (TimothyGu著, ) https://github.com/heycam/webidl/commit/2b1a990df64deb0b3d3d91ebd83f8efe59e2809b

[109] [[Construct]] for legacy platform objects? · Issue #407 · heycam/webidl () https://github.com/heycam/webidl/issues/407

[110] Remove legacy callers by TimothyGu · Pull Request #412 · heycam/webidl () https://github.com/heycam/webidl/pull/412

[111] Define document.all as having [[IsHTMLDDA]] (TimothyGu著, ) https://github.com/whatwg/html/commit/542be55f0a627460b55a08b7a1914ea05e9b3a3e

[28] Add a [[IsHTMLDDA]] internal slot to document.all · Issue #3015 · whatwg/html () https://github.com/whatwg/html/issues/3015

[112] Define document.all as having [[IsHTMLDDA]] by TimothyGu · Pull Request #3087 · whatwg/html () https://github.com/whatwg/html/pull/3087

[113] Regression fix for document.all(undefined) (foolip著, ) https://github.com/whatwg/html/commit/8c42aa6164d34f60228268f0fccfb23fad2ade5d

[114] Treat undefined as no arguments for document.all(undefined) by foolip · Pull Request #3392 · whatwg/html () https://github.com/whatwg/html/pull/3392

[115] Disallow -0 in algorithm for checking array index by TimothyGu · Pull Request #517 · heycam/webidl () https://github.com/heycam/webidl/pull/517

[116] Deprecations and removals in Chrome 65  |  Web  |  Google Developers () https://developers.google.com/web/updates/2018/02/chrome-65-deprecations

[117] Editorial: improve HTMLAllCollection IDL (annevk著, ) https://github.com/whatwg/html/commit/be2f5df15c86c1847d85514602904751ec2e2b70

[118] Improve HTMLAllCollection IDL by annevk · Pull Request #3562 · whatwg/html () https://github.com/whatwg/html/pull/3562

[119] typeof operator and platform objects · Issue #512 · heycam/webidl () https://github.com/heycam/webidl/issues/512

[120] Implementation-defined typeof still necessary? · Issue #1440 · tc39/ecma262 () https://github.com/tc39/ecma262/issues/1440