[18] いくつかの [[HTML要素]]には、 [DFN[[CODE(DOMa)@en[[[text]]]]]]
[[IDL属性]]があります。これは[[要素の内容]]から[[テキスト]]を抽出するものです。

;; [19] [CODE(DOMa)@en[[[textContent]]]] [[IDL属性]]と似ていますが、
動作が微妙に異なっていますし、すべての[[要素]]に存在するわけでもありません。

* 仕様書

[REFS[
- [32] '''[CITE@en[DOM Standard]] ([TIME[2017-06-29 02:44:54 +09:00]]) <https://dom.spec.whatwg.org/#concept-child-text-content>'''
- [9] [CITE@en-GB-x-hixie[HTML Standard]] ([TIME[2016-02-21 07:50:23 +09:00]] 版) <https://html.spec.whatwg.org/#dom-script-text>
]REFS]

* 子供テキスト内容

[23] [[要素]]の[DFN[[F[[RUBYB[子供テキスト内容]@en[child text content]]]]]]は、
当該[[要素]]の[[子供]]である [CODE(DOMi)@en[Text]] [[節点]]すべての[F[データ]]を、
[[木順]]に連結したものです [SRC[>>32, >>26]]。

[28] [[子供テキスト内容]]は、次の場面で使われます。

[FIG(list middle)[
- [CODE(HTMLe)@en[title]] [[要素]]
-- [CODE(JS)@en[document.title]]
-- [CODE(DOMa)@en[text][Node.text]] [[IDL属性]]
-- [[文書の題名]]
- [CODE(HTMLe)@en[style]] [[要素]]
-- [CODE(DOMa)@en[text][Node.text]] [[IDL属性]]
-- [[CSS]] [[ソーステキスト]]
- [CODE(HTMLe)@en[script]] [[要素]]
-- [CODE(DOMa)@en[text][Node.text]] [[IDL属性]]
--- [[JavaScript]] [[ソーステキスト]]
- [CODE(HTMLe)@en[time]] [[要素]]の[[日時値]]
- [CODE(HTMLe)@en[noscript]] [[要素]]の[[内容モデル]]
- [CODE(HTMLe)@en[textarea]]
-- [[再設定アルゴリズム]]
-- [CODE(DOMa)@en[defaultValue]] [[IDL属性]]の[[取得器]]

[HISTORY[
- [CODE(HTMLe)@en[menuitem]] [[要素]]の[[ラベル][ラベル (menuitem)]]
]HISTORY]
]FIG]

[25] このうち [CODE(HTMLe)@en[title]], [CODE(HTMLe)@en[style]], [CODE(HTMLe)@en[script]]
については、[[HTML構文解析器]]は[[内容]]部分で[[タグ]]を認識しないので、
[[子要素]]を持つことはありません。 ([[DOM]] 操作により[[子要素]]を[[挿入]]することはできますが。)

[24] 似たものに [CODE(DOMa)@en[textContent]] がありますが、
[[子孫]]すべての[[テキスト]]を[[連結]]するものですから、
常時最新値を使う処理の場合に、
[[子孫]]のあらゆる[[変異]]を観測して適宜更新する必要があります。
それは必ずしも低コストな処理ではないため、[[子要素]]が特に必要なければ、
[[子テキストの連結]]が使われることがあります [SRC[>>21]]。

[29] [CODE(HTMLe)@en[option]] [[要素]]は[[子供テキスト内容]]と [CODE(DOMa)@en[textContent]]
の中間のようなものを使っています。

* [CODE(DOMi)@en[HTMLScriptElement]] インターフェイス

[10] [CODE(DOMi)@en[[[HTMLScriptElement]]]] [[インターフェイス]]の
[DFN[[CODE(DOMa)@en[[[text]]]]]] [[IDL属性]]は、
[[取得時]]、次のようにしなければ[['''なりません''']] [SRC[>>9]]。

[FIG(steps)[
= [12] [VAR[テキスト]]を、[[空文字列]]に設定します。
= [11] [[文脈オブジェクト]]の[F[[[子供]]]]リストに含まれる[[節点]][VAR[節点]]について[[木順]]に、
== [13] [VAR[節点]]が [CODE(DOMi)@en[[[Text]]]] なら、
=== [14] [VAR[テキスト]]の末尾に[VAR[節点]]の[F[[[データ]]]]を追加します。
= [15] [VAR[テキスト]]を返します。
]FIG]

;; [17] これは [CODE(DOMa)@en[[[textContent]]]] とは違って[[子要素]]を無視します。
[[HTML構文]]では [CODE(HTMLe)@en[[[script]]]] [[要素]]に[[子要素]]を含めることはできませんが、
[[XHTML]] や[[スクリプト]]を使えば可能です。しかし[[スクリプト]]の実行時には
[CODE(DOMa)@en[[[textContent]]]] ではなく [CODE(DOMa)@en[[[text]]]]
の値が使われ、[[子要素]]の[[内容]]は[[スクリプト]]の[[ソースコード]]の一部とはみなされません。

[16] [[設定時]]には、 [CODE(DOMa)@en[[[textContent]]]]
[[IDL属性]]の[[設定器]]と同じようにしなければ[['''なりません''']] [SRC[>>9]]。

* 変更手順群

[34] 
[CODE[text]] が変化するとき、特定の処理が行われることがあります。
[SEE[ [[子供テキスト内容変更手順群]] ]]

* 歴史

[20] [CODE(DOMa)@en[[[text]]]] [[IDL属性]]は [[IE4 DOM]] に由来します。

** [CODE(DOMi)@en[Node]] インターフェイス

[8] [[MSXML]] では [CODE(DOMi)@en[[[Node]]]] [[インターフェイス]]相当に [CODE(DOMa)@en[[[text]]]]
[[属性]]があるようです。

[REFS[
- [6] [CITE@en-us[text Property ''''''[''''''DOM'''''']'''''']]
( ([TIME[2013-05-12 05:15:17 +09:00]] 版))
<http://msdn.microsoft.com/en-us/library/ms762687(v=vs.85).aspx>
]REFS]

[7] >>6 によると標準の [[DOM]] の [CODE(DOMa)@en[[[textContent]]]] に近いですが、
[CODE(XMLa)@en[[[xml:space]]]] [[属性]]などによって挙動が変わるようです。

** HTML5

[REFS[
- [1] [CITE@en[(X)HTML5 Tracking]]
([TIME[2010-01-11 13:57:26 +09:00]] 版)
<http://html5.org/tools/web-apps-tracker?from=4561&to=4562>
- [3] [CITE['''['''whatwg''']''' a element and .text]]
([TIME[2010-08-10 09:24:47 +09:00]] 版)
<http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2010-August/027781.html>
]REFS]

[2] >>1 で [CODE(DOMi)@en[[[HTMLAnchorElement]]]] の [CODE(DOMa)@en[[[text]]]] [[属性]]が定義されました。

[REFS[
- [4] [CITE@en[Web Applications 1.0 r7763     Match reality, and factor out an algorithm.]]
( ([TIME[2013-03-22 03:47:00 +09:00]] 版))
<http://html5.org/tools/web-apps-tracker?from=7762&to=7763>
]REFS]

[5] >>4 で [CODE(DOMi)@en[[[HTMLOptionElement]]]] の [CODE(DOMa)@en[[[text]]]] [[属性]]の定義が改められています。

** 共通化

[33] 
[TIME[2016年][year:2016]]、従来個別に定義されていた本概念に[[子供テキスト内容]]という名前が与えられました。

[21] [CITE@en[Validity and processing should be aligned for <time> · Issue #644 · whatwg/html]]
([TIME[2016-03-05 11:20:24 +09:00]] 版)
<https://github.com/whatwg/html/issues/644>

[22] [CITE@en[Fix #644: Only look at child Text nodes in <time> · whatwg/html@b437a80]]
([TIME[2016-03-05 18:11:27 +09:00]] 版)
<https://github.com/whatwg/html/commit/b437a803c2310d8fb2e05aef343ea24166afb9fd>

[27] [CITE@en[Refactor child Text nodes to a new concept · whatwg/html@0dd609f]] ([TIME[2016-03-10 21:01:01 +09:00]] 版) <https://github.com/whatwg/html/commit/0dd609fddc08dab337d24da874c787e51e851efc>

[30] [CITE@en[Formalize custom element reactions · whatwg/html@27aa7bc]]
([TIME[2016-04-26 18:53:24 +09:00]] 版)
<https://github.com/whatwg/html/commit/27aa7bc4fa6f168654a8c858f0773e611f679b39>

- [26] '''[CITE@en-GB-x-hixie[HTML Standard]] ([TIME[2016-03-10 07:13:01 +09:00]] 版) <https://html.spec.whatwg.org/#child-text-content>'''

[31] [CITE@en[Add child text content and its change steps]]
([[domenic]]著, [TIME[2017-06-28 01:07:56 +09:00]])
<https://github.com/whatwg/dom/commit/93db3408c360a958c2f1c00fbb99bf6c5d6fa5b2>

[36] [CITE@en[Add child text content and its change steps by domenic · Pull Request #466 · whatwg/dom]]
([TIME[2017-06-29 17:05:00 +09:00]])
<https://github.com/whatwg/dom/pull/466>

[38] [CITE@en[Update textarea textContent/defaultValue/value interaction]]
([[domenic]]著, [TIME[2017-06-16 05:46:10 +09:00]])
<https://github.com/whatwg/html/commit/5afbba1cf62ee01bc6af3fd220d01f3f7591a0fc>

[39] [CITE@en["When the textarea element's textContent IDL attribute changes" · Issue #2750 · whatwg/html]]
([TIME[2017-06-29 17:18:36 +09:00]])
<https://github.com/whatwg/html/issues/2750>

[40] [CITE@en[Textarea defaultValue handling doesn't really match UAs (who don't match each other) when there are child elements · Issue #2752 · whatwg/html]]
([TIME[2017-06-29 17:19:46 +09:00]])
<https://github.com/whatwg/html/issues/2752>

[42] [CITE@en[Update textarea textContent/defaultValue/value interaction by domenic · Pull Request #2766 · whatwg/html]]
([TIME[2017-06-29 17:21:00 +09:00]])
<https://github.com/whatwg/html/pull/2766>

[44] [CITE@en[Improve <style> and <script> processing and conformance]]
([[domenic]]著, [TIME[2017-09-14 18:42:48 +09:00]])
<https://github.com/whatwg/html/commit/9c612ac8641b5174849a2d3cb924fe662a8d3a09>

[45] [CITE@en[<style> should use child text content changed steps · Issue #2996 · whatwg/html]]
([TIME[2017-09-19 22:42:21 +09:00]])
<https://github.com/whatwg/html/issues/2996>

[46] [CITE@en[Improve <style> and <script> processing and conformance by domenic · Pull Request #3024 · whatwg/html]]
([TIME[2017-09-19 22:47:33 +09:00]])
<https://github.com/whatwg/html/pull/3024>

[47] [CITE@en[Editorial: use "child text content" in the <script> processing model]]
([[ankurkaushal]]著, [TIME[2018-01-29 13:53:26 +09:00]])
<https://github.com/whatwg/html/commit/58286f7078b3aceee060ed6d3635ab8749a77238>

[48] [CITE@en[<script> processing model should use child text content, not the text IDL attribute · Issue #3420 · whatwg/html]]
([TIME[2018-01-30 16:38:37 +09:00]])
<https://github.com/whatwg/html/issues/3420>

[49] [CITE@en[#3420: Editorial fix for the <script> processing model. by ankurkaushal · Pull Request #3421 · whatwg/html]]
([TIME[2018-01-30 16:39:11 +09:00]])
<https://github.com/whatwg/html/pull/3421>

[50] [CITE@en[Amend bad link to the child text content change steps]]
([[alexnoz]]著, [TIME[2018-01-29 16:13:49 +09:00]])
<https://github.com/whatwg/html/commit/8427925e026fa96cb61e7290f18a168d4ee9e183>

[51] [CITE@en[Bad link to "child text content change steps" in "The `style` element" · Issue #3384 · whatwg/html]]
([TIME[2018-01-30 16:39:48 +09:00]])
<https://github.com/whatwg/html/issues/3384>

[52] [CITE@en[Amend bad link to the child text content change steps by alexnoz · Pull Request #3422 · whatwg/html]]
([TIME[2018-01-30 16:40:10 +09:00]])
<https://github.com/whatwg/html/pull/3422>

[53] [CITE@en[Use "child text content" to check if a <script> is empty]]
([[domenic]]著, [TIME[2018-02-06 02:04:01 +09:00]])
<https://github.com/whatwg/html/commit/1d64c41e811aab5831be474355bd346c9eaf2874>

[54] [CITE@en[<script> processing model with various children · Issue #3419 · whatwg/html]]
([TIME[2018-02-13 15:20:34 +09:00]])
<https://github.com/whatwg/html/issues/3419>

[55] [CITE@en[Use "child text content" to check if a <script> is empty by domenic · Pull Request #3451 · whatwg/html]]
([TIME[2018-02-13 15:21:03 +09:00]])
<https://github.com/whatwg/html/pull/3451>

[56] [CITE@en[Stop using textContent in normative text]]
([[annevk]]著, [TIME[2019-04-04 17:23:13 +09:00]])
<https://github.com/whatwg/html/commit/ec860d872de3712277242655562a4d2a1325592a>

[57] [CITE@en[Align with DOM's children changed steps]]
([[annevk]], [TIME[2019-12-06 20:24:46 +09:00]], [TIME[2021-08-20T08:28:14.000Z]])
<https://github.com/whatwg/html/commit/928f862788e5d1d3fef905fd36bb55ea1939b0c1>