[5] [DFN[[RUBYB[[[変異観測器]]]@en[mutation observer]]]] ([DFN[[[変異オブザーバー]]]]) は、 [[DOM]]
の変化の通知を[[スクリプト]]が受け取るための仕組みです。
予め所定の方法で観測器となる[[コールバック関数]]を指定することで、 
[[DOM木]]における[[子供]]や[[属性]]の変化を観測し、そのタイミングで処理を行うことができます。

* 処理

[33] [[変異観測器]]は、[[変異]]が発生した[[タスク]]の処理後、[[マイクロタスク]]として呼び出されます。

* ごみ収集

[19] [[ごみ収集]]も参照。

[21] [CODE[observe]] した[[変異観測器]]は、[[節点]]から[[強い参照]]を持たれます。
従って、 [[JavaScript]] のコードから[[変異観測器]]への参照が無くなっても、
引き続き動作を続けます。不要になった時は、 (参照を無くす前に)
[CODE[disconnect]] する必要があります。

* 歴史

** 変異イベントから変異観測器へ

[4] [[変異観測器]]は、[[変異イベント]]の代替として考案され、 [[DOM4]]
仕様に取り込まれました。

[REFS[
- [31] [CITE@en[DOM Mutation Events Replacement: The Story So Far / Existing Points  of Consensus]] ([[Rafael Weinstein]] 著, [TIME[2011-08-11 09:41:29 +09:00]] 版) <http://lists.w3.org/Archives/Public/public-webapps/2011JulSep/0779.html>
- [32] [CITE@en[DOM Mutation Events Replacement: When to deliver mutations]] ([[Rafael Weinstein]] 著, [TIME[2011-08-11 09:44:32 +09:00]] 版) <http://lists.w3.org/Archives/Public/public-webapps/2011JulSep/0780.html>
- [30] [CITE@en[Mutation Observers: a replacement for DOM Mutation Events]] ([[Adam Klein]] 著, [TIME[2011-09-24 06:16:40 +09:00]] 版) <http://lists.w3.org/Archives/Public/public-webapps/2011JulSep/1622.html>
- [1] [CITE[IRC logs: freenode / #whatwg / 20120228]]
( ([TIME[2012-03-08 00:01:17 +09:00]] 版))
<http://krijnhoetmer.nl/irc-logs/whatwg/20120228>
]REFS]

[34] [[変異観測器]]の呼び出しは[[イベントループ]]と密接に関わっているため、 [[HTML Standard]]
で規定されました。

** 構文解析器と変異観測器

[35] [[変異イベント]]は[[構文解析]]によって生じる [[DOM木]]の変化については発生しないことになっていましたが、
[[変異観測器]]はそれも観測できることと規定されました。

[REFS[
- [20] [CITE@en[Web Applications 1.0 r7484     The parser should have mutation observers.]] ([TIME[2012-10-24 07:24:00 +09:00]] 版) <http://html5.org/tools/web-apps-tracker?from=7483&to=7484>
]REFS]


[36] [CITE[Integrate with the new microtask world order. https://www.w3.org/Bugs/Pu... · d8b0057 · whatwg/dom]]
( ([TIME[2014-04-12 08:37:53 +09:00]] 版))
<https://github.com/whatwg/dom/commit/d8b0057ca0dabb961d9d8435fba5e0b5544946d8>

[37] [CITE@en[Exceptions thrown from mutation observer callbacks should be reported pe... · 2e567d3 · whatwg/dom]]
( ([TIME[2014-11-30 15:00:21 +09:00]] 版))
<https://github.com/whatwg/dom/commit/2e567d3308724b29c9787bd069332775a3dbd8cc>

[2] [CITE@en[Replacing a child within parent had the wrong previousSibling in its … · whatwg/dom@d9fbd12]]
([TIME[2015-07-24 22:54:57 +09:00]] 版)
<https://github.com/whatwg/dom/commit/d9fbd12e2115fed6e37236a90cff4dfbb959b9d0>

[3] [CITE@en[Always queue mutation records after said mutations. Fixes #60. · whatwg/dom@bc31c18]]
([TIME[2015-07-30 14:20:02 +09:00]] 版)
<https://github.com/whatwg/dom/commit/bc31c1821451cb2fc85fe91b3cb4ffbf92add4d1>

[6] [CITE@en[Close #122: fix mutation record for a removing a node · whatwg/dom@9202ada]]
([TIME[2015-11-28 12:16:46 +09:00]] 版)
<https://github.com/whatwg/dom/commit/9202ada4ae8f606b67df95ea39323316e36536c2>

[7] [CITE@en[Clarify initial state of "mutation observer compound microtask queued… · whatwg/dom@f2427d8]]
([TIME[2016-01-02 11:22:08 +09:00]] 版)
<https://github.com/whatwg/dom/commit/f2427d8b83f6922ea58dd21f7bc3633d13ffafef>

[8] [CITE@en[Reword queue a mutation record to avoid "continue" · whatwg/dom@d3653ac]]
([TIME[2016-03-15 12:11:44 +09:00]] 版)
<https://github.com/whatwg/dom/commit/d3653ac38d388cf64d88f32439cae590c1658e9d>

[9] [CITE@en[23250 – No way with mutation observers to watch mutations to an element's parent]]
([TIME[2016-04-16 13:05:52 +09:00]] 版)
<https://www.w3.org/Bugs/Public/show_bug.cgi?id=23250>

[10] [CITE@en[Do not let mutation observer callbacks influence the used signal slot…]]
( ([[annevk]]著, [TIME[2016-06-09 16:27:03 +09:00]]))
<https://github.com/whatwg/dom/commit/d4ca21707fe3eb5d60dd1de0111752504b3da185>

[11] [CITE@en[Web Applications 1.0 r7009     Add support for MutationObservers. (Also, add a minor internal note in the canvas section unrelated to this.) (Also, automated update of the CLDR which resulted in removing separate support for el-POLYTON.)]]
( ([TIME[2012-03-01 06:24:00 +09:00]] 版))
<http://html5.org/tools/web-apps-tracker?from=7008&to=7009>

[12] [CITE[IRC logs: freenode / #whatwg / 20120409]]
( ([TIME[2012-04-15 13:32:46 +09:00]] 版))
<http://krijnhoetmer.nl/irc-logs/whatwg/20120409>

[13] [CITE[IRC logs: freenode / #whatwg / 20120830]]
( ([TIME[2012-09-05 21:57:54 +09:00]] 版))
<http://krijnhoetmer.nl/irc-logs/whatwg/20120830#l-398>

[14] [CITE[IRC logs: freenode / #whatwg / 20120922]]
( ([TIME[2012-09-24 20:09:27 +09:00]] 版))
<http://krijnhoetmer.nl/irc-logs/whatwg/20120922>

[15] [CITE@en[Web Applications 1.0 r7484     The parser should have mutation observers.]]
( ([TIME[2012-10-24 07:24:00 +09:00]] 版))
<http://html5.org/tools/web-apps-tracker?from=7483&to=7484>

[16] [CITE[Bug 16142 – Spec delivery semantics for MutationObservers]]
( ([TIME[2012-03-08 00:07:58 +09:00]] 版))
<https://www.w3.org/Bugs/Public/show_bug.cgi?id=16142>

[17] [CITE@en[Require one of childList, attributes, and characterData to be true after... · 4bf29a4 · whatwg/dom]]
( ([TIME[2014-10-23 02:24:57 +09:00]] 版))
<https://github.com/whatwg/dom/commit/4bf29a4ae4a1e5e4f1a5249f5fc7aa40d37b3f51>

[18] [[構文解析器]]の動作と [CODE(DOMi)@en[[[MutationObserver]]]]
の呼び出しタイミングの関係は[[ブラウザー]]ごとに全然違っているようです。
[TIME[2016-02-22T15:39:28.500Z]]

[22] [CITE@en[Mark observe()'s dictionary argument as optional]]
([[annevk]]著, [TIME[2016-08-29 15:45:52 +09:00]])
<https://github.com/whatwg/dom/commit/e4f7faafddf5e57dd0ecfca36a0a798d82162320>

[23] [CITE@en[Clarify insert-node's record's previousSibling]]
([[bwrrp]]著, [TIME[2017-05-31 19:00:09 +09:00]])
<https://github.com/whatwg/dom/commit/d9cd30defeccb4b143913859e70c12a1da4bf55b>

[24] [CITE@en[Clarify insert-node's record's previousSibling by bwrrp · Pull Request #464 · whatwg/dom]]
([TIME[2017-06-29 16:02:06 +09:00]])
<https://github.com/whatwg/dom/pull/464>

[25] [CITE@en[Add '''['''Exposed=Window''']''' to MutationObserver]]
([[annevk]]著, [TIME[2017-09-06 17:37:07 +09:00]])
<https://github.com/whatwg/dom/commit/c9544325095195c3afdf6284c29b26e84ce572aa>

[26] [CITE@en[Add '''['''Exposed=Window''']''' to MutationObserver by annevk · Pull Request #507 · whatwg/dom]]
([TIME[2017-09-07 12:06:07 +09:00]])
<https://github.com/whatwg/dom/pull/507>

[27] [CITE@en[Hide `nonce` content attribute values. (#2369) by mikewest · Pull Request #2373 · whatwg/html]]
([TIME[2018-02-17 23:35:36 +09:00]])
<https://github.com/whatwg/html/pull/2373>

[28] [CITE@en[Editorial: use Infra to define mutation observers]]
([[annevk]]著, [TIME[2018-03-29 20:12:35 +09:00]])
<https://github.com/whatwg/dom/commit/50b3051320e9617e1b2ff899dfef6f8dc5255bdf>

[29] [CITE@en[Small correct for Mutation observers · Issue #132 · whatwg/dom]]
([TIME[2018-03-30 15:39:22 +09:00]])
<https://github.com/whatwg/dom/issues/132>

[38] [CITE@en[Use Infra to define mutation observers by annevk · Pull Request #609 · whatwg/dom]]
([TIME[2018-03-30 15:39:59 +09:00]])
<https://github.com/whatwg/dom/pull/609>

[39] [CITE@en[Use DOM's "replace all" primitive in document.open()]]
([[annevk]]著, [TIME[2018-05-01 01:44:29 +09:00]])
<https://github.com/whatwg/html/commit/423c66403522b09138dc3b366bf96111bc030a04>

[40] [CITE@en[Use DOM's "replace all" primitive in document.open() by annevk · Pull Request #3598 · whatwg/html]]
([TIME[2018-05-03 10:36:31 +09:00]])
<https://github.com/whatwg/html/pull/3598>

[41] 
[[Edge]] は
[CODE[observe]]
[[メソッド]]のオプションに何も指定されていないとき
[CODE[SyntaxError]] を[[投げ]]ます。
[TIME[2018-12-13T03:45:07.800Z]]

[42] 
[[Edge]] は
[CODE[observe]]
[[メソッド]]のオプションに [CODE[attributeFilter]] が指定されて
[CODE[attributes]] が指定されていないとき、
[CODE[SyntaxError]]
を[[投げ]]ます。
[TIME[2018-12-13T03:45:43.900Z]]

[43] [CITE@en[Align with IDL dictionary changes]]
([[annevk]]著, [TIME[2019-08-09 00:54:02 +09:00]])
<https://github.com/whatwg/dom/commit/c15e65f193ab7561485606158c72f491c67f4b11>

[44] [CITE@en[Align with IDL constructor changes]]
([[autokagami]]著, [TIME[2019-09-24 22:42:56 +09:00]])
<https://github.com/whatwg/dom/commit/57512fac17cf2f1c4c85be4aec178c8086ee5ee4>

[45] [CITE@en[Use Web IDL constructor operations by foolip · Pull Request #782 · whatwg/dom]]
([TIME[2020-05-14 14:05:50 +09:00]])
<https://github.com/whatwg/dom/pull/782>

[46] [CITE@en[Remove compound microtasks]]
([[annevk]], [TIME[2019-03-30 00:11:20 +09:00]], [TIME[2021-03-25T11:58:33.000Z]])
<https://github.com/whatwg/dom/commit/7a8406bcb0332c1370f305c77565b7a3c069541d>

[47] [CITE@en[Dispatching slotchange within a compound microtask · Issue #734 · whatwg/dom]]
([TIME[2021-03-25T12:03:27.000Z]])
<https://github.com/whatwg/dom/issues/734>

[48] [CITE@en[Remove compound microtasks by annevk · Pull Request #741 · whatwg/dom]]
([TIME[2021-03-25T12:04:41.000Z]])
<https://github.com/whatwg/dom/pull/741>

[49] [CITE@en[Editorial: embrace that attributes are objects]]
([[annevk]], [TIME[2019-12-12 02:40:19 +09:00]], [TIME[2022-01-27T11:50:04.000Z]])
<https://github.com/whatwg/dom/commit/0038d097f5cad2a7ffdbe75c234b58960aa320f6>