Options
All
  • Public
  • Public/Protected
  • All
Menu

This class models a listener designed to listen to changes to a DOM tree and fire events on the basis of the changes that it detects.

An included-element event is fired when an element appears in the observed tree whether it is directly added or added because its parent was added. The opposite events are excluding-element and excluded-element. The event excluding-element is generated before the tree fragment is removed, and excluded-element after*.

An added-element event is fired when an element is directly added to the observed tree. The opposite events are excluding-element and removed-element.

A children-changing and children-changed event are fired when an element's children are being changed.

A text-changed event is fired when a text node has changed.

An attribute-changed is fired when an attribute has changed.

A trigger event with name [name] is fired when trigger([name]) is called. Trigger events are meant to be triggered by event handlers called by the listener, not by other code.

Example

Consider the following HTML fragment:

<ul>
 <li>foo</li>
</ul>

If the fragment is added to a <div> element, an included-element event will be generated for <ul> and <li> but an added-element event will be generated only for <ul>. A changed-children event will be generated for the parent of <ul>.

If the fragment is removed, an excluding-element and excluded-element event will be generated for <ul> and <li> but a removing-element and remove-element event will be generated only for <ul>. A children-changing and children-changed event will be generated for the parent of <ul>.

The order in which handlers are added matters. The listener provides the following guarantee: for any given type of event, the handlers will be called in the order that they were added to the listener.

Warnings:

  • Keep in mind that the children-changed, excluded-element and removed-element events are generated after the DOM operation that triggers them. This has some consequences. In particular, a selector that will work perfectly with removing-element or excluding-element may not work with removed-element and excluded-element. This would happen if the selector tests for ancestors of the element removed or excluded. By the time the -ed events are generated, the element is gone from the DOM tree and such selectors will fail.

    The -ed version of these events are still useful. For instance, a wed mode in use for editing scholarly articles listens for excluded-element with a selector that is a tag name so that it can remove references to these elements when they are removed. Since it does not need anything more complex then excluded-element works perfectly.

  • A listener does not verify whether the parameters passed to handlers are part of the DOM tree. For instance, handler A could operate on element X so that it is removed from the DOM tree. If there is already another mutation on X in the pipeline by the time A is called and handler B is called to deal with it, then by the time B is run X will no longer be part of the tree.

    To put it differently, even if when an event is generated element X was part of the DOM tree, it is possible that by the time the handlers that must be run for that mutation are run, X is no longer part of the DOM tree.

    Handlers that care about whether they are operating on elements that are in the DOM tree should perform a test themselves to check whether what is passed to them is still in the tree.

    The handlers fired on removed-elements events work on nodes that have been removed from the DOM tree. To know what was before and after these nodes before they were removed use events that have previous_sibling and next_sibling parameters, because it is likely that the nodes themselves will have both their previousSibling and nextSibling set to null.

  • Handlers that are fired on children-changed events, and which modify the DOM tree can easily result in infinite loops. Care should be taken early in any such handler to verify that the kind of elements added or removed should result in a change to the DOM tree, and ignore those changes that are not relevant.

Hierarchy

  • DOMListener

Index

Constructors

constructor

Properties

Private root

root: Node

The root of the DOM tree about which the listener should listen to changes.

Private scheduledProcessTriggers

scheduledProcessTriggers: number | undefined

Private stopped

stopped: boolean = true

Private triggerHandlers

triggerHandlers: object = Object.create(null)

Type declaration

Private triggersToFire

triggersToFire: object = Object.create(null)

Type declaration

  • [key: string]: number

Private updater

updater: TreeUpdater

Methods

Private _addRemCalls

  • _addRemCalls<T>(name: T, node: Element, target: Element): CallSpec<T>[]
  • Produces the calls for the added/removed family of events.

    Type parameters

    Parameters

    • name: T

      The event name.

    • node: Element

      The node added or removed.

    • target: Element

      The parent of this node.

    Returns CallSpec<T>[]

    A list of call specs.

Private _beforeDeleteNodeHandler

Protected _callHandler

  • _callHandler(handler: Function, ...rest: any[]): void
  • Utility function for calling event handlers.

    Parameters

    • handler: Function

      The handler.

    • Rest ...rest: any[]

      The arguments to pass to the handler.

    Returns void

Private _childrenCalls

  • _childrenCalls<T>(call: T, parent: Element, added: Node[], removed: Node[], prev: Node | null, next: Node | null): CallSpec<T>[]
  • Produces the calls for children-... events.

    Type parameters

    Parameters

    • call: T

      The type of call to produce.

    • parent: Element

      The parent of the children that have changed.

    • added: Node[]

      The children that were added.

    • removed: Node[]

      The children that were removed.

    • prev: Node | null

      Node preceding the children.

    • next: Node | null

      Node following the children.

    Returns CallSpec<T>[]

    A list of call specs.

Private _deleteNodeHandler

Private _incExcCalls

  • _incExcCalls<T>(name: T, node: Element, target: Element): CallSpec<T>[]
  • Produces the calls for included/excluded family of events.

    Type parameters

    Parameters

    • name: T

      The event name.

    • node: Element

      The node which was included or excluded and for which we must issue the events.

    • target: Element

      The parent of this node.

    Returns CallSpec<T>[]

    A list of call specs.

Private _insertNodeAtHandler

Protected _processTriggers

  • _processTriggers(): void

Private _scheduleProcessTriggers

  • _scheduleProcessTriggers(): void

Private _setAttributeNSHandler

Private _setTextNodeValueHandler

addHandler

  • addHandler(eventType: "trigger", selector: string, handler: TriggerHandler): void
  • addHandler<T>(eventType: T, selector: string, handler: EventHandlers[T]): void
  • Adds an event handler or a trigger handler. Note that if you want to add a trigger handler, the first argument must be a single string, due to how the 2nd argument is interpreted.

    throws

    {Error} If an event is unrecognized.

    Parameters

    • eventType: "trigger"
    • selector: string

      When adding an event handler, this argument is a CSS selector. When adding a trigger handler, this argument is a trigger name.

      Note that the meaning of the selector parameter for text-changed events is different than the usual. Whereas for all other handlers, the selector matches the element parameter passed to the handlers, in the case of a text-changed event the selector matches the parent of the node parameter.

    • handler: TriggerHandler

      The handler to be called by this listener when the events specified in eventTypes occur.

    Returns void

  • Type parameters

    Parameters

    • eventType: T
    • selector: string
    • handler: EventHandlers[T]

    Returns void

clearPending

  • clearPending(): void
  • Clear anything that is pending. Some implementations may have triggers delivered asynchronously.

    Returns void

processImmediately

  • processImmediately(): void

startListening

  • startListening(): void
  • Start listening to changes on the root passed when the object was constructed.

    Returns void

stopListening

  • stopListening(): void

trigger

  • trigger(name: string): void
  • Tells the listener to fire the named trigger as soon as possible.

    Parameters

    • name: string

      The name of the trigger to fire.

    Returns void

Object literals

Private eventHandlers

eventHandlers: object

added-element

added-element: never[] = []

attribute-changed

attribute-changed: never[] = []

children-changed

children-changed: never[] = []

children-changing

children-changing: never[] = []

excluded-element

excluded-element: never[] = []

excluding-element

excluding-element: never[] = []

included-element

included-element: never[] = []

removed-element

removed-element: never[] = []

removing-element

removing-element: never[] = []

text-changed

text-changed: never[] = []

Generated using TypeDoc