CSC/ECE 517 Spring 2017/M1702 Implement the Mutation Observer API Design: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
 
(39 intermediate revisions by 3 users not shown)
Line 5: Line 5:
=='''Introduction'''==
=='''Introduction'''==
===Servo===
===Servo===
[https://en.wikipedia.org/wiki/Servo_(layout_engine) Servo] is an open source web browser engine designed for application and embedded use. It is a high performance browser engine created by Mozilla Research and is written in the Rust language. The main idea behind the Servo is to create a parallel environment where the components are handle by isolated and fine grained tasks. These components can be rendering, HTML parsing etc.
[https://en.wikipedia.org/wiki/Servo_(layout_engine) Servo] is an open source web browser engine designed for application and embedded use. It is a high performance browser engine created by Mozilla Research and is written in the Rust language. The main idea behind the Servo is to create a parallel environment where the components are handled by isolated and fine grained tasks. These components can be rendering, HTML parsing etc.


===Rust===
===Rust===
Line 16: Line 16:
The purpose of this project is to implement the basic functionality required to implement a <span class="plainlinks">[https://dom.spec.whatwg.org/#mutation-observers Mutation Observer API]</span> as defined in the <span class="plainlinks">[https://dom.spec.whatwg.org DOM]</span> Specifications.
The purpose of this project is to implement the basic functionality required to implement a <span class="plainlinks">[https://dom.spec.whatwg.org/#mutation-observers Mutation Observer API]</span> as defined in the <span class="plainlinks">[https://dom.spec.whatwg.org DOM]</span> Specifications.
In the <span class="plainlinks">[http://wiki.expertiza.ncsu.edu/index.php/CSC/ECE_517_Spring_2017/M1702_Implement_the_Mutation_Observer_API First Phase of the Project]</span>, we implemented the initial Steps as described in the <span class="plainlinks">[https://github.com/servo/servo/wiki/Mutation-observer-project Project Specification]</span>. The initial implementation is merged in the original Servo Repository and the code can be found <span class="plainlinks">[https://github.com/servo/servo/commit/107ac9ab56c1a4cee3e5f9828f91ab394e3e8eee here]</span>.
In the <span class="plainlinks">[http://wiki.expertiza.ncsu.edu/index.php/CSC/ECE_517_Spring_2017/M1702_Implement_the_Mutation_Observer_API First Phase of the Project]</span>, we implemented the initial Steps as described in the <span class="plainlinks">[https://github.com/servo/servo/wiki/Mutation-observer-project Project Specification]</span>. The initial implementation is merged in the original Servo Repository and the code can be found <span class="plainlinks">[https://github.com/servo/servo/commit/107ac9ab56c1a4cee3e5f9828f91ab394e3e8eee here]</span>.
For the next phase of the project, we have to implement the functionality for supporting Mutation Observers. We plan on acheiving that in the following steps:
For the next phase of the project, we have to implement the functionality for supporting Mutation Observers.
Based on the Project description, we have made a Project Design, which is described in detail below.


===Add support for mutation observer microtasks===
=='''Project Design'''==
*We will add a <code>mutation_observer_compound_microtask_queued</code> member to <code>ScriptThread</code>. This will be used later to implement the <span class="plainlinks">[https://dom.spec.whatwg.org/#queue-a-mutation-observer-compound-microtask queue a mutation observer compound microtask algorithm]</span> described below.
*Next, we are going to implement the <span class="plainlinks">[https://dom.spec.whatwg.org/#queue-a-mutation-observer-compound-microtask queue a mutation observer compound microtask algorithm]</span> by adding a new variant to the <span class="plainlinks">[https://dxr.mozilla.org/servo/rev/216a89f7766dd366c4afbeae42cf6e1fb4f67349/components/script/microtask.rs#29 Microtask]</span> enum for mutation observers.
**Initially, if mutation observer compound microtask queued flag is set, then we return.
**We have to set mutation observer compound microtask queued flag.
**The later step will be to queue a compound microtask to notify mutation observers.
*We will implement the <span class="plainlinks">[https://dom.spec.whatwg.org/#notify-mutation-observers notify mutation observers algorithm]</span> (ignoring the specific text about "execute a compound microtask") using the vector of observers previously added to <code>ScriptThread</code>. The notify mutation observers algorithm is as below:
**For this, we need to unset mutation observer compound microtask queued flag set in the previous algorithm.
**We will declare notifyList to be a copy of unit of related similar-origin browsing contexts' list of MutationObserver objects.
**Also, we will declare signalList to be a copy of unit of related similar-origin browsing contexts' signal slot list.
**We need to empty unit of related similar-origin browsing contexts' signal slot list.
**For each MutationObserver object mo in notifyList, we shall execute a compound microtask subtask to run these steps:
***Declare queue to contain mo's record queue.
***mo's record queue will be emptied.
***All transient registered observers whose observer is mo will be removed.
***If queue is non-empty, invoke mo’s callback with a list of arguments consisting of queue and mo, and mo as the callback this value. If this throws an exception, report the exception.
**For each [https://dom.spec.whatwg.org/#concept-slot slot] slot in signalList, in order, we shall fire an event named slotchange, with its bubbles attribute set to true, at slot.


===Add support for observing specific mutations===
===High Level Design===
*Firstly we will add a vector of <code>MutationObserver</code> objects to <code>Node</code>.
Our Project Implements the MutationObserver API in the Mozilla's Servo Browser Engine. The High Level Design for the same is described below:
*Then we will implement <span class="plainlinks">[https://dom.spec.whatwg.org/#dom-mutationobserver-observe observe]</span> method of MutationObserver. As per the DOM specification, the observe method has the following behaviour:
[[File:MutationObserver.png|center]]
**If either ''options''’ <code>attributeOldValue</code> or <code>attributeFilter</code> is present and ''options''’ <code>attributes</code> is omitted, then we set the ''options''’ attributes to <code>true</code>.
 
**If options’ characterDataOldValue is present and options’ characterData is omitted, then we set options’ characterData to true.
This is a high-level design of what the MutationObserver API actually does.
**If none of options’ childList, attributes, and characterData is true, then we throw a TypeError.
 
**If options’ attributeOldValue is true and options’ attributes is false, then we throw a TypeError.
*According to the <span class="plainlinks">[https://dom.spec.whatwg.org/ DOM Standard]</span>, when the contents of the page are mutated, the web content receives callback through the MutationObserver API.
**If options’ attributeFilter is present and options’ attributes is false, then we throw a TypeError.
*The changes are recorded by the <span class="plainlinks">[https://dom.spec.whatwg.org/#mutation-observers MutationObserver]</span> using <span class="plainlinks">[https://dom.spec.whatwg.org/#interface-mutationrecord MutationRecords]</span>.
**If options’ characterDataOldValue is true and options’ characterData is false, then we throw a TypeError.
*The <span class="plainlinks">[https://dom.spec.whatwg.org/#dom-mutationobserver-observe observe method]</span> of the MutationObserver is invoked when the contents of the page are mutated.
**For each observer registered in target’s list of registered observers whose observer is the context object we perform the following steps:
 
***We remove all transient registered observers whose source is registered.
The technical design describes how we plan on implementing it.
***We also replace registered’s options with options.
 
**Otherwise, we add a new registered observer to target’s list of registered observers with the context object as the observer and options as the options, and then also, we add target to context object’s list of nodes on which it is registered.  
===Technical Design===
**The last two steps above mean we replace an existing matching entry with the new options, or add a new entry if there is no match.
 
*We will implement the <span class="plainlinks">[https://dom.spec.whatwg.org/#queue-a-mutation-record queue a mutation record algorithm]</span>. The queue a mutation record algorithm has the following behavior:
====Add support for mutation observer microtasks====
**We declare the interested observers initially as an empty set of MutationObserver objects optionally paired with a string.
 
**We also declare nodes be the inclusive ancestors of target.
*We will add a <code>mutation_observer_compound_microtask_queued</code> member to the <code>ScriptThread</code> class. This will be used later to implement the algorithm for ''queuing a mutation observer compound microtask'' and ''notifying a mutation observer'' as described below.
**For each node in nodes, and then for each registered observer (with registered observer’s options as options) in node’s list of registered observers we decide:
 
***If none of the following are true
*Then, we will implement the <span class="plainlinks">[https://dom.spec.whatwg.org/#queue-a-mutation-observer-compound-microtask queue a mutation observer compound microtask algorithm]</span> by adding a new variant to the <span class="plainlinks">[https://dxr.mozilla.org/servo/rev/216a89f7766dd366c4afbeae42cf6e1fb4f67349/components/script/microtask.rs#29 Microtask]</span> enum for mutation observers. The algorithm for doing that is described below:
****node is not target and options’ subtree is false
  1) If the ''mutation observer compound microtask queued'' flag is set, then return.
****type is "attributes" and options’ attributes is not true
  2) Set the ''mutation observer compound microtask queued'' flag.
****type is "attributes", options’ attributeFilter is present, and options’ attributeFilter does not contain name or namespace is non-null
  3) <span class="plainlinks">[https://html.spec.whatwg.org/multipage/webappapis.html#queue-a-microtask Queue]</span> a <span class="plainlinks">[https://html.spec.whatwg.org/multipage/webappapis.html#compound-microtask compound microtask]</span> which will be used to notify the mutation observers.
****type is "characterData" and options’ characterData is not true
 
****type is "childList" and options’ childList is false
*Next, we will implement the <span class="plainlinks">[https://dom.spec.whatwg.org/#notify-mutation-observers notify mutation observers algorithm]</span> using the vector of observers previously added to the <code>ScriptThread</code> class. The notify mutation observers algorithm is implemented as described below:
***then,:
  1) Unset the ''mutation observer compound microtask queued'' flag, which was set in the previous algorithm.
****If registered observer’s observer is not in interested observers, we append registered observer’s observer to interested observers.
  2) Declare ''notifyList'' to be a copy of ''unit of related similar-origin browsing contexts''' list of MutationObserver objects.
****If either type is "attributes" and options’ attributeOldValue is true, or type is "characterData" and options’ characterDataOldValue is true,we set the paired string of registered observer’s observer in interested observers to oldValue.
  3) Declare ''signalList'' to be a copy of ''unit of related similar-origin browsing contexts''' <span class="plainlinks">[https://dom.spec.whatwg.org/#signal-slot-list signal slot list]</span>.
**For each observer in interested observers:
  4) Empty the ''unit of related similar-origin browsing contexts''' signal slot list.
***We declare record to be a new MutationRecord object with its type set to type and target set to target.
  5) For each MutationObserver object ''mo'' in ''notifyList'', we shall execute a ''compound microtask subtask'' to run these steps:
***If name and namespace are given, we set record’s attributeName to name, and record’s attributeNamespace to namespace.
    1) Declare ''queue'' to contain ''mo'''s <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-mo-queue record queue]</span>.
***If addedNodes is given, we set record’s addedNodes to addedNodes.
    2) Empty ''mo'''s <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-mo-queue record queue]</span>.
***If removedNodes is given, we set record’s removedNodes to removedNodes,
    3) All transient registered observers whose observer is ''mo'' will be removed.
***If previousSibling is given, we set record’s previousSibling to previousSibling.
    4) If ''queue'' is non-empty, then invoke ''mo''’s callback with a list of arguments consisting of <code>queue</code> and ''mo'', and ''mo'' as the <span class="plainlinks">[https://heycam.github.io/webidl/#dfn-callback-this-value callback this value]</span>.
***If nextSibling is given, we set record’s nextSibling to nextSibling.
        If this throws an exception, then we will report the exception.
***If observer has a paired string, we set record’s oldValue to observer’s paired string.
  6) For each ''<span class="plainlinks">[https://dom.spec.whatwg.org/#concept-slot slot]</span>'' in ''signalList'', an event named ''slotchange'' will be fired in the same order as it is stored in the <code>signalist</code>,
***We then append record to observer’s record queue.  
      with its ''<span class="plainlinks">[https://dom.spec.whatwg.org/#dom-event-bubbles bubbles]</span>'' attribute set to ''true'', at ''slot''.
**We then queue a mutation observer compound microtask.  
 
*We will make <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-element-attributes-change changing]</span>/<span class="plainlinks">[https://dom.spec.whatwg.org/#concept-element-attributes-append appending]</span>/<span class="plainlinks">[https://dom.spec.whatwg.org/#concept-element-attributes-remove removing]</span>/<span class="plainlinks">[https://dom.spec.whatwg.org/#concept-element-attributes-replace replacing]</span> an attribute queue a mutation record via <span class="plainlinks">[https://dxr.mozilla.org/servo/rev/216a89f7766dd366c4afbeae42cf6e1fb4f67349/components/script/dom/attr.rs#171 Attr::set_value]</span>, <span class="plainlinks">[https://dxr.mozilla.org/servo/rev/216a89f7766dd366c4afbeae42cf6e1fb4f67349/components/script/dom/element.rs#955 Element::push_attribute]</span>, and <span class="plainlinks">[https://dxr.mozilla.org/servo/rev/216a89f7766dd366c4afbeae42cf6e1fb4f67349/components/script/dom/element.rs#1077 Element::remove_first_matching_attribute]</span>.
====Add support for observing specific mutations====
**
*Firstly we will add a vector of <code>MutationObserver</code> objects to <code>Node</code> to store the Mutation Observers.
*Then we will implement the <span class="plainlinks">[https://dom.spec.whatwg.org/#dom-mutationobserver-observe observe]</span> method of MutationObserver. As per the DOM specification, the observe method has the following behaviour:
  1) If either ''options''’ ''attributeOldValue'' or ''attributeFilter'' is present and ''options''’ ''attributes'' is omitted, then we set the ''options''’ ''attributes''
    to true.
  2) If options’ ''characterDataOldValue'' is present and options’ ''characterData'' is omitted, then we set options’ ''characterData'' to true.
  3) If none of options’ ''childList'', attributes, and ''characterData'' is true, then we <span class="plainlinks">[https://heycam.github.io/webidl/#dfn-throw throw]</span> a ''TypeError''.
  4) If options’ ''attributeOldValue'' is true and options’ ''attributes'' is false, then we <span class="plainlinks">[https://heycam.github.io/webidl/#dfn-throw throw]</span> a ''TypeError''.
  5) If options’ ''attributeFilter'' is present and options’ ''attributes'' is false, then we <span class="plainlinks">[https://heycam.github.io/webidl/#dfn-throw throw]</span> a ''TypeError''.
  6) If options’ ''characterDataOldValue'' is true and options’ ''characterData'' is false, then we <span class="plainlinks">[https://heycam.github.io/webidl/#dfn-throw throw]</span> a ''TypeError''.
  7) For each <span class="plainlinks">[https://dom.spec.whatwg.org/#registered-observer registered observers]</span> "''registered''" in target’s list of <span class="plainlinks">[https://dom.spec.whatwg.org/#registered-observer registered observers]</span> whose '''observer''' is the context object we perform the
    following steps:
    1) We remove all <span class="plainlinks">[https://dom.spec.whatwg.org/#transient-registered-observer transient registered observers]</span> whose '''source''' is "''registered''".
    2) We also replace ''registered''’s '''options''' with options.
  8) Otherwise, we add a new <span class="plainlinks">[https://dom.spec.whatwg.org/#registered-observer registered observers]</span> to target’s list of <span class="plainlinks">[https://dom.spec.whatwg.org/#registered-observer registered observers]</span> with the <span class="plainlinks">[https://dom.spec.whatwg.org/#context-object context object]</span> as the observer and ''options''
    as the options, and then also, we add target to <span class="plainlinks">[https://dom.spec.whatwg.org/#context-object context object]</span>’s list of <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-node node]</span> on which it is registered.
'''Note''': Steps 7 and 8 in the above description means that we replace an existing matching entry with the new options, or add a new entry if there is no match.
 
*Next, we will implement the <span class="plainlinks">[https://dom.spec.whatwg.org/#queue-a-mutation-record queue a mutation record algorithm]</span>. Which is described as follows:
  1) Declare the ''interested'' observers initially as an empty set of <span class="plainlinks">[https://dom.spec.whatwg.org/#mutationobserver MutationObserver]</span> objects optionally paired with a string.
  2) Declare ''nodes'' be the <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-tree-inclusive-ancestor inclusive ancestors]</span> of target.
  3) For each ''node'' in nodes, and then for each ''registered observer'' (with ''registered observer''’s '''options''' as ''options'') in node’s list of
    <span class="plainlinks">[https://dom.spec.whatwg.org/#registered-observer registered observers]</span> we decide:
    1) If none of the following are true
      1) ''node'' is not target and ''options''’ subtree is false
      2) ''type'' is "''attributes''" and ''options''’ ''attributes'' is not true
      3) ''type'' is "''attributes''", ''options''’ ''attributeFilter'' is present, and ''options''’ ''attributeFilter'' does not contain name or namespace is non-null
      4) ''type'' is "''characterData''" and ''options''’ ''characterData'' is not true
      5) ''type'' is "''childList''" and ''options''’ ''childList'' is false
    then,:
      1) If ''registered observer''’s '''observer''' is not in ''interested'' observers, we append ''registered observer''’s '''observer''' to ''interested observers''.
      2) If either type is "''attributes''" and ''options''’ ''attributeOldValue'' is true, or type is "''characterData''" and ''options''’
        ''characterDataOldValue'' is true,we set the paired string of ''registered observer''’s '''observer''' in ''interested observers'' to ''oldValue''.
  4) For each ''observer'' in ''interested observers'':
    1) Declare ''record'' to be a new <span class="plainlinks">[https://dom.spec.whatwg.org/#mutationrecord MutationRecord]</span> object with its <span class="plainlinks">[https://dom.spec.whatwg.org/#dom-mutationrecord-type type]</span> set to ''type'' and <span class="plainlinks">[https://dom.spec.whatwg.org/#dom-mutationrecord-target target]</span> set to ''target''.
    2) If ''name'' and ''namespace'' are given, we set ''record''’s <span class="plainlinks">[https://dom.spec.whatwg.org/#dom-mutationrecord-attributename attributeName]</span> to ''name'', and ''record''’s <span class="plainlinks">[https://dom.spec.whatwg.org/#dom-mutationrecord-attributenamespace attributeNamespace]</span> to ''namespace''.
    3) If ''addedNodes'' is given, we set ''record''’s <span class="plainlinks">[https://dom.spec.whatwg.org/#dom-mutationrecord-addednodes addedNodes]</span> to ''addedNodes''.
    4) If ''removedNodes'' is given, we set ''record''’s <span class="plainlinks">[https://dom.spec.whatwg.org/#dom-mutationrecord-removednodes removedNodes]</span> to ''removedNodes'',
    5) If ''previousSibling'' is given, we set ''record''’s <span class="plainlinks">[https://dom.spec.whatwg.org/#dom-mutationrecord-previoussibling previousSibling]</span> to ''previousSibling''.
    6) If ''nextSibling'' is given, we set ''record''’s <span class="plainlinks">[https://dom.spec.whatwg.org/#dom-mutationrecord-nextsibling nextSibling]</span> to ''nextSibling''.
    7) If ''observer'' has a paired string, we set ''record''’s <span class="plainlinks">[https://dom.spec.whatwg.org/#dom-mutationrecord-oldvalue oldValue]</span> to ''observer''’s paired string.
    8) Then, append ''record'' to ''observer''’s <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-mo-queue record queue]</span>.
  5) <span class="plainlinks">[https://dom.spec.whatwg.org/#queue-a-mutation-observer-compound-microtask Queue a mutation observer compound microtask]</span>.
 
*We will make <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-element-attributes-change changing]</span>/<span class="plainlinks">[https://dom.spec.whatwg.org/#concept-element-attributes-append appending]</span>/<span class="plainlinks">[https://dom.spec.whatwg.org/#concept-element-attributes-remove removing]</span>/<span class="plainlinks">[https://dom.spec.whatwg.org/#concept-element-attributes-replace replacing]</span> of an attribute, queue a mutation record via the <span class="plainlinks">[https://dxr.mozilla.org/servo/rev/216a89f7766dd366c4afbeae42cf6e1fb4f67349/components/script/dom/attr.rs#171 Attr::set_value]</span>, <span class="plainlinks">[https://dxr.mozilla.org/servo/rev/216a89f7766dd366c4afbeae42cf6e1fb4f67349/components/script/dom/element.rs#955 Element::push_attribute]</span>, and <span class="plainlinks">[https://dxr.mozilla.org/servo/rev/216a89f7766dd366c4afbeae42cf6e1fb4f67349/components/script/dom/element.rs#1077 Element::remove_first_matching_attribute]</span> attributes. These changes are performed in the following steps:
**If we want to '''change''' an <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-attribute attribute]</span> ''attribute'' from an <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-element element]</span> ''element'' to value, we run these steps:
  1) <span class="plainlinks">[https://dom.spec.whatwg.org/#queue-a-mutation-record Queue a mutation record]</span> of "attributes" for element with name attribute’s <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-attribute-local-name local name]</span>, namespace attribute’s <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-attribute-namespace namespace]</span>, and oldValue
    attribute’s <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-attribute-value value]</span>.
  2) If ''element'' is <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-element-custom custom]</span>, we <span class="plainlinks">[https://html.spec.whatwg.org/multipage/scripting.html#enqueue-a-custom-element-callback-reaction enqueue a custom element callback reaction]</span> with ''element'', callback name "''attributeChangedCallback''", and an
    argument list containing ''attribute''’s <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-attribute-local-name local name]</span>, ''attribute''’s <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-attribute-value value]</span>, value, and ''attribute''’s <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-attribute-namespace namespace]</span>.
  3) We then run the<span class="plainlinks">[https://dom.spec.whatwg.org/#concept-element-attributes-change-ext attribute change steps]</span> with ''element'', ''attribute''’s <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-attribute-local-name local name]</span>, ''attribute''’s <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-attribute-value value]</span>, ''value'', and ''attribute''’s  <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-attribute-namespace namespace]</span>.
  4) We set ''attribute''’s <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-attribute-value value]</span> to ''value''.
 
**If we want to '''append''' an <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-attribute attribute]</span> ''attribute'' to an <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-element element]</span> ''element'', we run these steps:
  1) We<span class="plainlinks">[https://dom.spec.whatwg.org/#queue-a-mutation-record queue a mutation record]</span> of "''attributes''" for ''element'' with name ''attribute''’s<span class="plainlinks">[https://dom.spec.whatwg.org/#concept-attribute-local-name local name]</span>, namespace ''attribute''’s <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-attribute-namespace namespace]</span>, and ''oldValue''
    null.
  2) If ''element'' is <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-element-custom custom]</span>, we <span class="plainlinks">[https://html.spec.whatwg.org/multipage/scripting.html#enqueue-a-custom-element-callback-reaction enqueue a custom element callback reaction]</span> with ''element'', callback name "''attributeChangedCallback''", and an
    argument list containing ''attribute''’s <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-attribute-local-name local name]</span>, null, ''attribute''’s <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-attribute-value value]</span>, and ''attribute''’s <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-attribute-namespace namespace]</span>.
  3) We then run the attribute change steps with element, attribute’s <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-attribute-local-name local name]</span>, null, attribute’s <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-attribute-value value]</span>, and attribute’s namespace.
  4) We also <span class="plainlinks">[https://infra.spec.whatwg.org/#list-append append]</span> ''attribute'' to ''element''’s <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-element-attribute attribute list]</span>.
  5) We then set ''attribute''’s <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-attribute-element element]</span> to ''element''.
 
**If we want to '''remove''' an <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-attribute attribute]</span> ''attribute'' from an <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-element element]</span> ''element'', we run these steps:
  1) We queue a mutation record of "attributes" for element with name attribute’s local name, namespace attribute’s <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-attribute-namespace namespace]</span>, and oldValue
    attribute’s <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-attribute-value value]</span>.
  2) If element is <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-element-custom custom]</span>, we then <span class="plainlinks">[https://html.spec.whatwg.org/multipage/scripting.html#enqueue-a-custom-element-callback-reaction enqueue a custom element callback reaction]</span> with element, callback name "attributeChangedCallback", and an
    argument list containing attribute’s <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-attribute-local-name local name]</span>, attribute’s <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-attribute-value value]</span>, null, and attribute’s <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-attribute-namespace namespace]</span>.
  3) We run the attribute change steps with element, attribute’s <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-attribute-local-name local name]</span>, attribute’s <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-attribute-value value]</span>, null, and attribute’s <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-attribute-namespace namespace]</span>.
  4) We then remove attribute from element’s attribute list.
  5) We set attribute’s <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-attribute-element element]</span> to null.
 
**If we want to '''replace''' an <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-attribute attribute]</span> ''oldAttr'' by an <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-attribute attribute]</span> ''newAttr'' in an <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-element element]</span> ''element'', run these steps:
  1) We<span class="plainlinks">[https://dom.spec.whatwg.org/#queue-a-mutation-record queue a mutation record]<span> of "''attributes''" for ''element'' with name ''oldAttr''’s <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-attribute-local-name local name]</span>,<span class="plainlinks">[https://dom.spec.whatwg.org/#concept-attribute-namespace namespace]</span> ''oldAttr''’s namespace, and oldValue
    ''oldAttr''’s <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-attribute-value value]</span>.
  2) If element is <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-element-custom custom]</span>, we then <span class="plainlinks">[https://html.spec.whatwg.org/multipage/scripting.html#enqueue-a-custom-element-callback-reaction enqueue a custom element callback reaction]</span> with element, callback name "attributeChangedCallback", and an
    argument list containing ''oldAttr''’s <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-attribute-local-name local name]</span>, ''oldAttr''’s <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-attribute-value value]</span>, ''newAttr''’s <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-attribute-value value]</span>, and ''oldAttr''’s namespace.
  3) We run the attribute change steps with element, ''oldAttr''’s <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-attribute-local-name local name]</span>, ''oldAttr''’s <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-attribute-value value]</span>, ''newAttr''’s <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-attribute-value value]</span>, and ''oldAttr''’s namespace.
  4) We then <span class="plainlinks">[https://infra.spec.whatwg.org/#list-replace replace]</span> ''oldAttr'' by ''newAttr'' in element’s <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-element-attribute attribute list]</span>.
  5) We set ''oldAttr''’s <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-attribute-element element]</span> to null.
  6) We set ''newAttr''’s <span class="plainlinks">[https://dom.spec.whatwg.org/#concept-attribute-element element]</span> to ''element''.


=='''Test Plan'''==
=='''Test Plan'''==
In the <span class="plainlinks">[http://wiki.expertiza.ncsu.edu/index.php/CSC/ECE_517_Spring_2017/M1702_Implement_the_Mutation_Observer_API First Phase of the Project]</span>,, our tests were written with regard to attributes, Childlist, Character data and records of a Mutation Observer. These tests are present in web-platform-tests directory (i.e <code>.tests/wpt/web-platform-tests/dom/nodes</code>).
In the <span class="plainlinks">[http://wiki.expertiza.ncsu.edu/index.php/CSC/ECE_517_Spring_2017/M1702_Implement_the_Mutation_Observer_API First Phase of the Project]</span>, our tests were written with regard to attributes, Childlist, Character data and records of a Mutation Observer. These tests are present in web-platform-tests directory (i.e <code>.tests/wpt/web-platform-tests/dom/nodes</code>).
 
The project owner from Mozilla has <span class="plainlinks">[https://github.com/servo/servo/issues/6633#issuecomment-292680210 mentioned]</span> that our existing tests will provide enough coverage for the newly added code as well.
 
* The following are the tests written with regard to attributes of a Mutation Observer ( i.e <code>.tests/wpt/web-platform-tests/dom/nodes/MutationObserver-attributes.html</code>).
** The scenario where there is no old attribute in the document and an update happens on the attribute.
** The scenario where there is a old attribute and an update happens.
** The scenario where the old attribute is an empty string.
** The scenario where the new update is the same as the old value.
** The scenario where multiple attributes in a document are updated.
** The scenario where there is invalid character in the updated attribute.
** The scenario where multiple old attributes are removed.
** The scenario where new attribute is added.
** The scenario where there is no Childlist in the mutation.
 
* The following are the tests written with regard to Childlist of a Mutation Observer ( i.e <code>.tests/wpt/web-platform-tests/dom/nodes/MutationObserver-childList.html</code>).
** The scenario where there is no mutation in the Childlist node.
** The scenario where there is updation on the contents of Childlist node.
** The scenario where there is updation on the contents of Childlist node on a previously empty content.
** The scenario where there is removal of contents in the Childlist node.
** The scenario where there is both addition and removal of contents in the Childlist node.
** The scenario where there is addition of contents into the last Child.
** The scenario where we append a child to Childlist node.
** The scenario where a child is removed.
** The scenario where both child and data is removed.
** The scenario where a child is inserted.
** The scenario where children are inserted.
** The scenario where children are both removed and added.
 
 
* The following are the tests written with regard to Character data of a Mutation Observer ( i.e <code>.tests/wpt/web-platform-tests/dom/nodes/MutationObserver-characterData.html</code>).
** The scenario where there is a updation without old data.
** The scenario where there is a updation with an old data.
** The scenario where there is a addition to an existing data.
** The scenario where there is a addition to an empty string.
** The scenario where there is a removal of data and an empty string is created.
** The scenario when a child is removed and data is updated.


For this part of the project, our test plan is to check the following:
*Test the micro task for mutation observer.
**Check if microtask queued flag is initially unset.
**Check if the task returns on setting the microtask queued flag.
**On notifying mutation observers, check if microtask queued flag is unset.
**Check the size of record queue on notifying mutation observers.
**Check if the number of events fired is equal to number of slots in Signal list.


*Test the observation of specific mutations:
* The following are the tests written with regard to Records of a Mutation Observer ( i.e <code>.tests/wpt/web-platform-tests/dom/nodes/MutationObserver-takeRecords.html</code>).
**Whenever an observe method is invoked and attributeOldValue or attributeFilter is present and attributes is omitted, check if attributes is set to true.
** The test to check if attributes, Child list and Character Data are present or not.
**Whenever an observe method is invoked and characterDataOldValue is present and characterData is omitted, check if characterData is set to true.
===Running the Tests===
**Whenever an observe method is invoked and a TypeError is thrown, check if atleast one of the following scenarios are true.
***If none of options’ childList, attributes, and characterData is true.
***attributeOldValue is true and attributes is false.
***attributeFilter is present and attributes is false.
***characterDataOldValue is true and characterData is false


The web-platform tests can be run by giving the following command at the root directory of the project:
The web-platform tests can be run by giving the following command at the root directory of the project:

Latest revision as of 17:59, 7 May 2017

Mutation Observer API Project with SERVO & RUST

Servo is a prototype web browser engine written in the RUST language. The DOM standard defines a MutationObserver API that allows web content to receive callbacks when the page contents are mutated. In simple words, MutationObserver facilitates the developers with a technique to react to the changes in DOM. MutationObserver is designed to replace the Mutation Events defined in the DOM3 Events specification. The goal of this project is to implement the fundamental pieces required to support the Mutation Observer API.

Introduction

Servo

Servo is an open source web browser engine designed for application and embedded use. It is a high performance browser engine created by Mozilla Research and is written in the Rust language. The main idea behind the Servo is to create a parallel environment where the components are handled by isolated and fine grained tasks. These components can be rendering, HTML parsing etc.

Rust

Rust is an open source systems programming language developed by Mozilla. Servo is written in Rust. The main purpose behind it's design is to be thread safe and concurrent. The emphasis is also on speed, safety and control of memory layout.

Setting up and Building the Project

Follow the Guidelines mentioned in the read me page of the project's Wiki Page for building the Project in a Linux Environment.

Project Description

The purpose of this project is to implement the basic functionality required to implement a Mutation Observer API as defined in the DOM Specifications. In the First Phase of the Project, we implemented the initial Steps as described in the Project Specification. The initial implementation is merged in the original Servo Repository and the code can be found here. For the next phase of the project, we have to implement the functionality for supporting Mutation Observers. Based on the Project description, we have made a Project Design, which is described in detail below.

Project Design

High Level Design

Our Project Implements the MutationObserver API in the Mozilla's Servo Browser Engine. The High Level Design for the same is described below:

This is a high-level design of what the MutationObserver API actually does.

  • According to the DOM Standard, when the contents of the page are mutated, the web content receives callback through the MutationObserver API.
  • The changes are recorded by the MutationObserver using MutationRecords.
  • The observe method of the MutationObserver is invoked when the contents of the page are mutated.

The technical design describes how we plan on implementing it.

Technical Design

Add support for mutation observer microtasks

  • We will add a mutation_observer_compound_microtask_queued member to the ScriptThread class. This will be used later to implement the algorithm for queuing a mutation observer compound microtask and notifying a mutation observer as described below.
 1) If the mutation observer compound microtask queued flag is set, then return.
 2) Set the mutation observer compound microtask queued flag.
 3) Queue a compound microtask which will be used to notify the mutation observers.
  • Next, we will implement the notify mutation observers algorithm using the vector of observers previously added to the ScriptThread class. The notify mutation observers algorithm is implemented as described below:
 1) Unset the mutation observer compound microtask queued flag, which was set in the previous algorithm.
 2) Declare notifyList to be a copy of unit of related similar-origin browsing contexts' list of MutationObserver objects.
 3) Declare signalList to be a copy of unit of related similar-origin browsing contexts' signal slot list.
 4) Empty the unit of related similar-origin browsing contexts' signal slot list.
 5) For each MutationObserver object mo in notifyList, we shall execute a compound microtask subtask to run these steps:
   1) Declare queue to contain mo's record queue.
   2) Empty mo's record queue.
   3) All transient registered observers whose observer is mo will be removed.
   4) If queue is non-empty, then invoke mo’s callback with a list of arguments consisting of queue and mo, and mo as the callback this value.
       If this throws an exception, then we will report the exception.
 6) For each slot in signalList, an event named slotchange will be fired in the same order as it is stored in the signalist,
     with its bubbles attribute set to true, at slot.

Add support for observing specific mutations

  • Firstly we will add a vector of MutationObserver objects to Node to store the Mutation Observers.
  • Then we will implement the observe method of MutationObserver. As per the DOM specification, the observe method has the following behaviour:
 1) If either optionsattributeOldValue or attributeFilter is present and optionsattributes is omitted, then we set the optionsattributes
   to true.
 2) If options’ characterDataOldValue is present and options’ characterData is omitted, then we set options’ characterData to true.
 3) If none of options’ childList, attributes, and characterData is true, then we throw a TypeError.
 4) If options’ attributeOldValue is true and options’ attributes is false, then we throw a TypeError.
 5) If options’ attributeFilter is present and options’ attributes is false, then we throw a TypeError.
 6) If options’ characterDataOldValue is true and options’ characterData is false, then we throw a TypeError.
 7) For each registered observers "registered" in target’s list of registered observers whose observer is the context object we perform the
   following steps:
   1) We remove all transient registered observers whose source is "registered".
   2) We also replace registered’s options with options. 
 8) Otherwise, we add a new registered observers to target’s list of registered observers with the context object as the observer and options
   as the options, and then also, we add target to context object’s list of node on which it is registered. 

Note: Steps 7 and 8 in the above description means that we replace an existing matching entry with the new options, or add a new entry if there is no match.

 1) Declare the interested observers initially as an empty set of MutationObserver objects optionally paired with a string.
 2) Declare nodes be the inclusive ancestors of target.
 3) For each node in nodes, and then for each registered observer (with registered observer’s options as options) in node’s list of
   registered observers we decide:
   1) If none of the following are true
     1) node is not target and options’ subtree is false
     2) type is "attributes" and optionsattributes is not true
     3) type is "attributes", optionsattributeFilter is present, and optionsattributeFilter does not contain name or namespace is non-null
     4) type is "characterData" and optionscharacterData is not true
     5) type is "childList" and optionschildList is false 
   then,:
     1) If registered observer’s observer is not in interested observers, we append registered observer’s observer to interested observers.
     2) If either type is "attributes" and optionsattributeOldValue is true, or type is "characterData" and optionscharacterDataOldValue is true,we set the paired string of registered observer’s observer in interested observers to oldValue.
 4) For each observer in interested observers:
   1) Declare record to be a new MutationRecord object with its type set to type and target set to target.
   2) If name and namespace are given, we set record’s attributeName to name, and record’s attributeNamespace to namespace.
   3) If addedNodes is given, we set record’s addedNodes to addedNodes.
   4) If removedNodes is given, we set record’s removedNodes to removedNodes,
   5) If previousSibling is given, we set record’s previousSibling to previousSibling.
   6) If nextSibling is given, we set record’s nextSibling to nextSibling.
   7) If observer has a paired string, we set record’s oldValue to observer’s paired string.
   8) Then, append record to observer’s record queue.
 5) Queue a mutation observer compound microtask.
 1) Queue a mutation record of "attributes" for element with name attribute’s local name, namespace attribute’s namespace, and oldValue
    attribute’s value.
 2) If element is custom, we enqueue a custom element callback reaction with element, callback name "attributeChangedCallback", and an
    argument list containing attribute’s local name, attribute’s value, value, and attribute’s namespace.
 3) We then run theattribute change steps with element, attribute’s local name, attribute’s value, value, and attribute’s  namespace.
 4) We set attribute’s value to value.
    • If we want to append an attribute attribute to an element element, we run these steps:
 1) Wequeue a mutation record of "attributes" for element with name attribute’slocal name, namespace attribute’s namespace, and oldValue
   null.
 2) If element is custom, we enqueue a custom element callback reaction with element, callback name "attributeChangedCallback", and an
    argument list containing attribute’s local name, null, attribute’s value, and attribute’s namespace.
 3) We then run the attribute change steps with element, attribute’s local name, null, attribute’s value, and attribute’s namespace.
 4) We also append attribute to element’s attribute list.
 5) We then set attribute’s element to element.
    • If we want to remove an attribute attribute from an element element, we run these steps:
 1) We queue a mutation record of "attributes" for element with name attribute’s local name, namespace attribute’s namespace, and oldValue
    attribute’s value.
 2) If element is custom, we then enqueue a custom element callback reaction with element, callback name "attributeChangedCallback", and an
    argument list containing attribute’s local name, attribute’s value, null, and attribute’s namespace.
 3) We run the attribute change steps with element, attribute’s local name, attribute’s value, null, and attribute’s namespace.
 4) We then remove attribute from element’s attribute list.
 5) We set attribute’s element to null. 
 1) Wequeue a mutation record of "attributes" for element with name oldAttr’s local name,namespace oldAttr’s namespace, and oldValue
    oldAttr’s value.
 2) If element is custom, we then enqueue a custom element callback reaction with element, callback name "attributeChangedCallback", and an
    argument list containing oldAttr’s local name, oldAttr’s value, newAttr’s value, and oldAttr’s namespace.
 3) We run the attribute change steps with element, oldAttr’s local name, oldAttr’s value, newAttr’s value, and oldAttr’s namespace.
 4) We then replace oldAttr by newAttr in element’s attribute list.
 5) We set oldAttr’s element to null.
 6) We set newAttr’s element to element.

Test Plan

In the First Phase of the Project, our tests were written with regard to attributes, Childlist, Character data and records of a Mutation Observer. These tests are present in web-platform-tests directory (i.e .tests/wpt/web-platform-tests/dom/nodes).

The project owner from Mozilla has mentioned that our existing tests will provide enough coverage for the newly added code as well.

  • The following are the tests written with regard to attributes of a Mutation Observer ( i.e .tests/wpt/web-platform-tests/dom/nodes/MutationObserver-attributes.html).
    • The scenario where there is no old attribute in the document and an update happens on the attribute.
    • The scenario where there is a old attribute and an update happens.
    • The scenario where the old attribute is an empty string.
    • The scenario where the new update is the same as the old value.
    • The scenario where multiple attributes in a document are updated.
    • The scenario where there is invalid character in the updated attribute.
    • The scenario where multiple old attributes are removed.
    • The scenario where new attribute is added.
    • The scenario where there is no Childlist in the mutation.


  • The following are the tests written with regard to Childlist of a Mutation Observer ( i.e .tests/wpt/web-platform-tests/dom/nodes/MutationObserver-childList.html).
    • The scenario where there is no mutation in the Childlist node.
    • The scenario where there is updation on the contents of Childlist node.
    • The scenario where there is updation on the contents of Childlist node on a previously empty content.
    • The scenario where there is removal of contents in the Childlist node.
    • The scenario where there is both addition and removal of contents in the Childlist node.
    • The scenario where there is addition of contents into the last Child.
    • The scenario where we append a child to Childlist node.
    • The scenario where a child is removed.
    • The scenario where both child and data is removed.
    • The scenario where a child is inserted.
    • The scenario where children are inserted.
    • The scenario where children are both removed and added.


  • The following are the tests written with regard to Character data of a Mutation Observer ( i.e .tests/wpt/web-platform-tests/dom/nodes/MutationObserver-characterData.html).
    • The scenario where there is a updation without old data.
    • The scenario where there is a updation with an old data.
    • The scenario where there is a addition to an existing data.
    • The scenario where there is a addition to an empty string.
    • The scenario where there is a removal of data and an empty string is created.
    • The scenario when a child is removed and data is updated.


  • The following are the tests written with regard to Records of a Mutation Observer ( i.e .tests/wpt/web-platform-tests/dom/nodes/MutationObserver-takeRecords.html).
    • The test to check if attributes, Child list and Character Data are present or not.

Running the Tests

The web-platform tests can be run by giving the following command at the root directory of the project:

   ./mach test-wpt tests/wpt/web-platform-tests/dom/nodes.

We have Updated the corresponding test expectations as per the given guidelines in the tests/wpt/metadata/dom/nodes/ directory.

Design Pattern

Our Project follows the Observer Pattern, where a list of the objects dependent on the current object is maintained and the dependents are notified of any state changes. The Mutation Observer helps in achieving this functionality.

References

1. https://doc.rust-lang.org/stable/book/
2. https://en.wikipedia.org/wiki/Rust_(programming_language)
3. https://en.wikipedia.org/wiki/Servo_(layout_engine)
4. https://github.com/servo/servo
5. https://github.com/servo/servo/wiki/Mutation-observer-project
6. https://dom.spec.whatwg.org/