2.2. Interface Event

An Event object is simply named an event. It allows for signaling that something has occurred, e.g., that an image has completed downloading.

Each event has the following associated flags that are all initially unset:

The stopPropagation() method steps are to set this’s stop propagation flag.

The cancelBubble getter steps are to return true if this’s stop propagation flag is set; otherwise false.

The cancelBubble setter steps are to set this’s stop propagation flag if the given value is true; otherwise do nothing.

The stopImmediatePropagation() method steps are to set this’s stop propagation flag and this’s stop immediate propagation flag.

To initialize an event, with type, bubbles, and cancelable, run these steps:

  1. Set event’s initialized flag.

  2. Unset event’s stop propagation flag, stop immediate propagation flag, and canceled flag.

  3. Set event’s isTrusted attribute to false.

  4. Set event’s target to null.

  5. Set event’s type attribute to type.

  6. Set event’s bubbles attribute to bubbles.

  7. Set event’s cancelable attribute to cancelable.

To dispatch an event event to an EventTarget object target, with an optional legacy target override flag and an optional legacyOutputDidListenersThrowFlag:

  1. Set event’s dispatch flag.

  2. Let targetOverride be target, if legacy target override flag is not given, and target’s associated Document otherwise. [HTML]

    legacy target override flag is only used by HTML and only when target is a Window object.

  3. Let activationTarget be null.

  4. Let relatedTarget be the result of retargeting event’s relatedTarget against target.

  5. Let clearTargets be false.

  6. If target is not relatedTarget or target is event’s relatedTarget:

    1. Let touchTargets be a new list.

    2. For each touchTarget of event’s touch target list: append the result of retargeting touchTarget against target to touchTargets.

    3. Append to an event path with event, target, targetOverride, relatedTarget, touchTargets, and false.

    4. Let isActivationEvent be true, if event is a MouseEvent object and event’s type attribute is "click"; otherwise false.

    5. If isActivationEvent is true and target has activation behavior, then set activationTarget to target.

    6. Let slottable be target, if target is a slottable and is assigned, and null otherwise.

    7. Let slotInClosedTree be false.

    8. Let parent be the result of invoking target’s get the parent with event.

    9. While parent is non-null:

      1. If slottable is non-null:

        1. Assert: parent is a slot.

        2. Set slottable to null.

        3. If parent’s root is a shadow root whose mode is "closed", then set slotInClosedTree to true.

      2. If parent is a slottable and is assigned, then set slottable to parent.

      3. Let relatedTarget be the result of retargeting event’s relatedTarget against parent.

      4. Let touchTargets be a new list.

      5. For each touchTarget of event’s touch target list: append the result of retargeting touchTarget against parent to touchTargets.

      6. If parent is a Window object, or parent is a node and target’s root is a shadow-including inclusive ancestor of parent:

        1. If isActivationEvent is true, event’s bubbles attribute is true, activationTarget is null, and parent has activation behavior, then set activationTarget to parent.

        2. Append to an event path with event, parent, null, relatedTarget, touchTargets, and slotInClosedTree.

      7. Otherwise, if parent is relatedTarget, then set parent to null.

      8. Otherwise:

        1. Set target to parent.

        2. If isActivationEvent is true, activationTarget is null, and target has activation behavior, then set activationTarget to target.

        3. Append to an event path with event, parent, target, relatedTarget, touchTargets, and slotInClosedTree.

      9. If parent is non-null, then set parent to the result of invoking parent’s get the parent with event.

      10. Set slotInClosedTree to false.

    10. Let clearTargetsItem be the last event path item in event’s path whose shadow-adjusted target is non-null.

    11. If clearTargetsItem’s shadow-adjusted target, clearTargetsItem’s relatedTarget, or an EventTarget object in clearTargetsItem’s touch target list is a node whose root is a shadow root: set clearTargets to true.

    12. If activationTarget is non-null and activationTarget has legacy-pre-activation behavior, then run activationTarget’s legacy-pre-activation behavior.

    13. For each item of event’s path, in reverse order:

      1. If item’s shadow-adjusted target is non-null, then set event’s eventPhase attribute to AT_TARGET.

      2. Otherwise, set event’s eventPhase attribute to CAPTURING_PHASE.

      3. Invoke with item, event, "capturing", and legacyOutputDidListenersThrowFlag if given.

    14. For each item of event’s path:

      1. If item’s shadow-adjusted target is non-null, then set event’s eventPhase attribute to AT_TARGET.

      2. Otherwise:

        1. If event’s bubbles attribute is false, then continue.

        2. Set event’s eventPhase attribute to BUBBLING_PHASE.

      3. Invoke with item, event, "bubbling", and legacyOutputDidListenersThrowFlag if given.

  7. Set event’s eventPhase attribute to NONE.

  8. Set event’s currentTarget attribute to null.

  9. Set event’s path to the empty list.

  10. Unset event’s dispatch flag, stop propagation flag, and stop immediate propagation flag.

  11. If clearTargets is true:

    1. Set event’s target to null.

    2. Set event’s relatedTarget to null.

    3. Set event’s touch target list to the empty list.

  12. If activationTarget is non-null:

    1. If event’s canceled flag is unset, then run activationTarget’s activation behavior with event.

    2. Otherwise, if activationTarget has legacy-canceled-activation behavior, then run activationTarget’s legacy-canceled-activation behavior.

  13. Return false if event’s canceled flag is set; otherwise true.

Nodes are objects that implement Node. Nodes participate in a tree, which is known as the node tree.

4.4. Interface Node

Each node has an associated node document, set upon creation, that is a document.

To create a node that implements an interface interface, given a document document:

  1. Assert: interface is neither Document nor an interface that inherits from Document. To create a document, use create a document instead.

  2. Return a new node that implements interface, in document’s relevant realm, with its node document set to document.

To clone a single node given a node node, document document, and null or a CustomElementRegistry object fallbackRegistry:

  1. Let copy be null.

  2. If node is an element:

    1. Let registry be node’s custom element registry.

    2. If registry is null, then set registry to fallbackRegistry.

    3. If registry is a global custom element registry, then set registry to document’s custom element registry’s effective global custom element registry.

    4. Set copy to the result of creating an element, given document, node’s local name, node’s namespace, node’s namespace prefix, node’s is value, false, and registry.

    5. For each attribute of node’s attribute list:

      1. Let copyAttribute be the result of cloning a single node given attribute, document, and null.

      2. Append copyAttribute to copy.

  3. Otherwise, if node is a document, set copy to the result of creating a document that implements the same interfaces as node, given document’s relevant realm.

  4. Otherwise, set copy to the result of creating a node that implements the same interfaces as node, given document.

  5. Fulfill these additional requirements, switching on the interface node implements:

    Document
    1. Set copy’s encoding, content type, URL, origin, type, mode, and allow declarative shadow roots, to those of node.

    2. If node’s custom element registry’s is scoped is true, then set copy’s custom element registry to node’s custom element registry.

    DocumentType

    Set copy’s name, public ID, and system ID to those of node.

    Attr

    Set copy’s namespace, namespace prefix, local name, and value to those of node.

    Text
    Comment

    Set copy’s data to that of node.

    ProcessingInstruction

    Set copy’s target and data to those of node.

    Otherwise

    Do nothing.

  6. Assert: copy is a node.

  7. If node is a document, then set document to copy.

  8. Set copy’s node document to document.

  9. Return copy.

4.5. Interface Document

Document nodes are simply known as documents.

To adopt a node node into a document document:

  1. Let oldDocument be node’s node document.

  2. If node’s parent is non-null, then remove node.

  3. If document is not oldDocument, then for each inclusiveDescendant of node’s shadow-including inclusive descendants, in shadow-including tree order:

    1. Set inclusiveDescendant’s node document to document.

    2. If inclusiveDescendant is a shadow root and if any of the following are true:

      then set inclusiveDescendant’s custom element registry to document’s custom element registry’s effective global custom element registry.

    3. Otherwise, if inclusiveDescendant is an element:

      1. Set the node document of each attribute in inclusiveDescendant’s attribute list to document.

      2. If inclusiveDescendant’s custom element registry is null or inclusiveDescendant’s custom element registry’s is scoped is false:

        1. Let registry be null.

        2. If inclusiveDescendant’s custom element registry is non-null, inclusiveDescendant’s parent is null, or inclusiveDescendant’s parent is an exclusive DocumentFragment node, then set registry to document’s custom element registry.

        3. Otherwise, set registry to the result of looking up a custom element registry given inclusiveDescendant’s parent.

        4. Set inclusiveDescendant’s custom element registry to registry’s effective global custom element registry.

      3. If inclusiveDescendant is custom, then enqueue a custom element callback reaction with inclusiveDescendant, callback name "adoptedCallback", and « oldDocument, document ».

    4. Run the adopting steps with inclusiveDescendant and oldDocument.

The new DocumentFragment() constructor steps are to set this’s node document to current global object’s associated Document.

4.9. Interface Element

Element nodes are simply known as elements.

To append an attribute attribute to an element element:

  1. Append attribute to element’s attribute list.

  2. Set attribute’s element to element.

  3. Set attribute’s node document to element’s node document.

  4. Handle attribute changes for attribute with element, null, and attribute’s value.

To replace an attribute oldAttribute with an attribute newAttribute:

  1. Let element be oldAttribute’s element.

  2. Replace oldAttribute by newAttribute in element’s attribute list.

  3. Set newAttribute’s element to element.

  4. Set newAttribute’s node document to element’s node document.

  5. Set oldAttribute’s element to null.

  6. Handle attribute changes for oldAttribute with element, oldAttribute’s value, and newAttribute’s value.

The new Text(data) constructor steps are to set this’s data to data and this’s node document to current global object’s associated Document.

The new ProcessingInstruction(target, data) constructor steps are:

  1. Set this’s node document to current global object’s associated Document.

  2. Initialize this with target and data.

The new Comment(data) constructor steps are to set this’s data to data and this’s node document to current global object’s associated Document.