<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://www.w3.org/Bugs/Public/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4"
          urlbase="https://www.w3.org/Bugs/Public/"
          
          maintainer="sysbot+bugzilla@w3.org"
>

    <bug>
          <bug_id>27320</bug_id>
          
          <creation_ts>2014-11-13 21:03:45 +0000</creation_ts>
          <short_desc>Need some clarification around MutationObserver stuff.</short_desc>
          <delta_ts>2014-11-14 11:45:25 +0000</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebAppsWG</product>
          <component>DOM</component>
          <version>unspecified</version>
          <rep_platform>PC</rep_platform>
          <op_sys>Windows NT</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>INVALID</resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>P2</priority>
          <bug_severity>normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Arkadiusz Michalski (Spirit)">crimsteam</reporter>
          <assigned_to name="Anne">annevk</assigned_to>
          <cc>bugs</cc>
    
    <cc>mike</cc>
    
    <cc>www-dom</cc>
          
          <qa_contact>public-webapps-bugzilla</qa_contact>

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>114898</commentid>
    <comment_count>0</comment_count>
    <who name="Arkadiusz Michalski (Spirit)">crimsteam</who>
    <bug_when>2014-11-13 21:03:45 +0000</bug_when>
    <thetext>I tried analyze dependencies across all algorithms and other prose for MutationObserver oposite to how browsers works, but every time something is wrong. Here are some inaccuracies:

=== 

callback MutationCallback = void (sequence&lt;MutationRecord&gt; mutations, MutationObserver observer);

In practice mutations and observer are optional (Firefox/Chrome/IE), so maybe add &quot;optional&quot; to IDL? And maybe cover this somehow in prose (step 3.4 in &quot;notify mutation observers&quot;).

&lt;script&gt;
	var new_MO = new MutationObserver(function(){});
	alert(new_MO); // create without any problem
&lt;/script&gt;

===

Next is more complicated, but I&apos;m not sure whether it is browsers problem or DOM. This affects mainly to &quot;notify mutation observers&quot; alghoritm and proper support takeRecords() method. Important thing is that if we operete on orginal or copy of something (like list or other object).

Look this:
2. Let notify list be a copy of unit of related similar-origin browsing contexts&apos;s list of MutationObserver objects. 

Per spec. callback get only copy of MO (as second argument) but all browsers operate on original.

&lt;script&gt;

	var new_MO = new MutationObserver(function(records, observer){

		alert(new_MO === observer);
		alert(new_MO === this);
		alert(this === observer);
		
	});
	
	var new_P = document.createElement(&quot;P&quot;);
	new_MO.observe(new_P, {attributes: true});
	
	new_P.id = &quot;setID&quot;;
	
&lt;/script&gt;

Above is important for takeRecords() method because we can use this method for original (new_MO in our example) and for copy (observer in our example as second argument for callback or &quot;this&quot; value) directly in callback. takeRecords() method should clear &quot;record queue&quot;, but if we have different object we have different &quot;record queue&quot; too.

&lt;script&gt;

	var new_MO = new MutationObserver(function(records, observer){

		// All browsers trate new_MO and observer as the same object (instance) and clear its record queue before invoke callback
		// its correct per step 3.2, but in generaly not per step 2. (algo &quot;notify mutation observers&quot;) because its operate on copy

		var takeFrom_MO = new_MO.takeRecords().length; //  record queue is empty
		var takeFrom_observer = observer.takeRecords().length; //  record queue is empty
		
		document.documentElement.innerHTML += &quot;new_MO.takeRecords().length: &quot; + takeFrom_MO
			+ &quot;&lt;br&gt;&quot; + &quot;observer.takeRecords().length: &quot; + takeFrom_observer
			
			// This was only copy so we still get 1
			+ &quot;&lt;br&gt;&quot; + &quot;records.length: &quot; + records.length
	});
	
	var new_P = document.createElement(&quot;P&quot;);
	new_MO.observe(new_P, {attributes: true});
	
	new_P.id = &quot;setID&quot;;

&lt;/script&gt;

What do you think?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>114899</commentid>
    <comment_count>1</comment_count>
    <who name="Olli Pettay">bugs</who>
    <bug_when>2014-11-13 21:22:13 +0000</bug_when>
    <thetext>(In reply to Arkadiusz Michalski (Spirit) from comment #0)
&gt; I tried analyze dependencies across all algorithms and other prose for
&gt; MutationObserver oposite to how browsers works, but every time something is
&gt; wrong. Here are some inaccuracies:
&gt; 
&gt; === 
&gt; 
&gt; callback MutationCallback = void (sequence&lt;MutationRecord&gt; mutations,
&gt; MutationObserver observer);
&gt; 
&gt; In practice mutations and observer are optional (Firefox/Chrome/IE), so
&gt; maybe add &quot;optional&quot; to IDL? And maybe cover this somehow in prose (step 3.4
&gt; in &quot;notify mutation observers&quot;).

No need for optional there. That is just how callbacks work. 
Similar to event listeners.
callback interface EventListener {
  void handleEvent(Event event);
};
yet you don&apos;t have to define the parameter if you&apos;re not going to use it anywhere.


&gt; Per spec. callback get only copy of MO (as second argument) but all browsers
&gt; operate on original.
Nothing says observer should be copy.
In fact, there is the following as a note:
&quot;the constructed MutationObserver object as second argument. &quot;
and there is also
&quot;If queue is non-empty, call mo&apos;s callback with queue as first argument, and mo (itself) as second argument and callback this value. &quot;


&gt; 
&gt; 	var new_MO = new MutationObserver(function(records, observer){
&gt; 
&gt; 		// All browsers trate new_MO and observer as the same object (instance)
&gt; and clear its record queue before invoke callback
&gt; 		// its correct per step 3.2, but in generaly not per step 2. (algo &quot;notify
&gt; mutation observers&quot;) because its operate on copy
Step 2 says to take a copy of the list. Not copy the MutationObserver objects.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>114908</commentid>
    <comment_count>2</comment_count>
    <who name="Arkadiusz Michalski (Spirit)">crimsteam</who>
    <bug_when>2014-11-13 23:54:26 +0000</bug_when>
    <thetext>Ok, callback case its clear.

Ehh... around &quot;copy&quot; I waste some time:), didn&apos;t realize that &quot;copy list&quot; just copy list (where content of list is still original). But still don&apos;t understand why list must be copy. In this alghoritm we don&apos;t modificate list but touch only its content (MutationObserver objects), so contents of list and copy of list looks/are the same.

To thank you for your explanation, this can be correct in &quot;queue a mutation record&quot;:

4.7 If observer has a paired string, set record&apos;s oldValue to observer&apos;s paired string. &lt;&lt; for /oldValue/ you can use reference (as we have in previous steps)</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>114929</commentid>
    <comment_count>3</comment_count>
    <who name="Anne">annevk</who>
    <bug_when>2014-11-14 11:45:25 +0000</bug_when>
    <thetext>The previous steps are different. Paired string is already set to oldValue earlier on. I have linked oldValue now though...</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>