Options
All
  • Public
  • Public/Protected
  • All
Menu

A TreeUpdater is meant to serve as the sole point of modification for a DOM tree. As methods are invoked on the TreeUpdater to modify the tree, events are issued synchronously, which allows a listener to know what is happening on the tree.

Methods are divided into primitive and complex methods. Primitive methods perform one and only one modification and issue an event of the same name as their own name. Complex methods use primitive methods to perform a series of modifications on the tree. Or they delegate the actual modification work to the primitive methods. They may emit one or more events of a name different from their own name. Events are emitted after their corresponding operation is performed on the tree.

For primitive methods, the list of events which they are documented to be firing is exhaustive. For complex methods, the list is not exhaustive.

Many events have a name identical to a corresponding method. Such events are accompanied by event objects which have the same properties as the parameters of the corresponding method, with the same meaning. Therefore, their properties are not further documented.

There is a generic ChangedEvent that is emitted with every other event. This event does not carry information about what changed exactly.

The TreeUpdater.deleteNode operation is the one major exception to the basic rules given above:

  • BeforeDeleteNodeEvent is emitted before the deletion is performed. This allows performing operations based on the node's location before it is removed. For instance, calling the DOM method matches on a node that has been removed from its DOM tree is generally going to fail to perform the intended check.

  • DeleteNodeEvent has the additional formerParent property.

DocumentFragment have special handling. Although the methods below that insert new content into the tree accept DocumentFragment nodes, the tree updater inserts the contents of the fragment rather than the fragment itself. Or to put it differently, inserting a fragment is equivalent to iterating through the fragment's children and inserting each one in order. A fragment is essentially equivalent to an array.

Hierarchy

Index

Constructors

constructor

  • new TreeUpdater(tree: Element | Document): TreeUpdater

Properties

Protected _events

_events: Subject<TreeUpdaterEvents>

Protected dlocRoot

dlocRoot: DLocRoot

events

events: Observable<TreeUpdaterEvents>

Protected tree

tree: Element | Document

The node which contains the tree to update.

Methods

Protected _emit

Protected _splitAt

  • _splitAt(top: Node, node: Node, index: number): SplitResult
  • Splits a DOM tree into two halves.

    Parameters

    • top: Node

      The node at which the splitting operation should end. This node will be split but the function won't split anything above this node.

    • node: Node

      The node at which to start.

    • index: number

      The index at which to start in the node.

    Returns SplitResult

    An array containing in order the first and second half of the split.

cut

  • A complex method. Removes the contents between the start and end carets from the DOM tree. If two text nodes become adjacent, they are merged.

    throws

    {Error} If Nodes in the range are not in the same element.

    Parameters

    • start: DLoc

      The start position.

    • end: DLoc

      The end position.

    Returns [DLoc, Node[]]

    A pair of items. The first item is a DLoc object indicating the position where the cut happened. The second item is a list of nodes, the cut contents.

deleteNode

  • deleteNode(node: Node): void
  • A primitive method. Removes a node from the DOM tree. This method must not be called directly by code that performs changes of the DOM tree at a high level, because it does not prevent two text nodes from being contiguous after deletion of the node. Call removeNode instead. This method is meant to be used by other complex methods of TreeUpdater and by some low-level facilities of wed.

    emits

    DeleteNodeEvent

    emits

    BeforeDeleteNodeEvent

    emits

    ChangedEvent

    Parameters

    • node: Node

      The node to remove

    Returns void

deleteText

  • deleteText(loc: DLoc, length: number): void
  • deleteText(node: Text, index: number, length: number): void
  • A complex method. Deletes text from a text node. If the text node becomes empty, it is deleted.

    Parameters

    • loc: DLoc

      Where to delete.

    • length: number

      The length of text to delete.

    Returns void

  • Parameters

    • node: Text
    • index: number
    • length: number

    Returns void

insertAt

  • A complex method. This is a convenience method that will call primitive methods to insert the specified item at the specified location. Note that this method returns nothing even if the primitives it uses return some information.

    throws

    {Error} If loc is not in an element or text node or if what is not an element or text node.

    Parameters

    • loc: DLoc

      The location where to insert.

    • what: Insertable

      The data to insert.

    Returns void

  • Parameters

    Returns void

insertBefore

  • insertBefore(parent: Element, toInsert: Element | Text, beforeThis: Node | null): void
  • A complex method. Inserts the specified item before another one. Note that the order of operands is the same as for the insertBefore DOM method.

    throws

    {Error} If beforeThis is not a child of parent.

    Parameters

    • parent: Element

      The node that contains the two other parameters.

    • toInsert: Element | Text

      The node to insert.

    • beforeThis: Node | null

      The node in front of which to insert. A value of null results in appending to the parent node.

    Returns void

insertIntoText

  • A complex method. Inserts an element into text, effectively splitting the text node in two. This function takes care to modify the DOM tree only once.

    throws

    {Error} If the node to insert is undefined or null.

    Parameters

    • loc: DLoc

      The location at which to cut.

    • node: Node

      The node to insert.

    Returns InsertionBoundaries

    The first element of the array is a DLoc at the boundary between what comes before the material inserted and the material inserted. The second element of the array is a DLoc at the boundary between the material inserted and what comes after. If I insert "foo" at position 2 in "abcd", then the final result would be "abfoocd" and the first location would be the boundary between "ab" and "foo" and the second location the boundary between "foo" and "cd".

  • Parameters

    • parent: Text
    • index: number
    • node: Node

    Returns InsertionBoundaries

insertNodeAt

  • insertNodeAt(loc: DLoc, node: Node): void
  • insertNodeAt(parent: Node, index: number, node: Node): void
  • A primitive method. Inserts a node at the specified position.

    emits

    InsertNodeAtEvent

    emits

    ChangedEvent

    Parameters

    • loc: DLoc

      The location at which to insert.

    • node: Node

      The node to insert.

    Returns void

  • Parameters

    • parent: Node
    • index: number
    • node: Node

    Returns void

insertText

  • A complex method. Inserts text into a node. This function will use already existing text nodes whenever possible rather than create a new text node.

    throws

    {Error} If node is not an element or text Node type.

    Parameters

    • loc: DLoc

      The location at which to insert the text.

    • text: string

      The text to insert.

    • Optional caretAtEnd: undefined | true | false

      Whether the returned caret should be at the end of the inserted text or the start. If not specified, the default is true.

    Returns TextInsertionResult

    The result of inserting text.

  • Parameters

    • node: Node
    • index: number
    • text: string
    • Optional caretAtEnd: undefined | true | false

    Returns TextInsertionResult

mergeTextNodes

  • mergeTextNodes(node: Node): DLoc
  • A complex method. If the node is a text node and followed by a text node, this method will combine them.

    Parameters

    • node: Node

      The node to check. This method will fail with an exception if this parameter is undefined or null. Use mergeTextNodesNF if you want a method that will silently do nothing if undefined or null are expected values.

    Returns DLoc

    A position between the two parts that were merged, or between the two nodes that were not merged (because they were not both text).

mergeTextNodesNF

  • mergeTextNodesNF(node: Node | null | undefined): DLoc | undefined
  • A complex method. If the node is a text node and followed by a text node, this method will combine them.

    Parameters

    • node: Node | null | undefined

      The node to check. This method will do nothing if the node to remove is undefined or null.

    Returns DLoc | undefined

    A position between the two parts that were merged, or between the two nodes that were not merged (because they were not both text). This will be undefined if there was no node to remove.

nodeToPath

  • nodeToPath(node: Node): string
  • Converts a node to a path.

    Parameters

    • node: Node

      The node for which to return a path.

    Returns string

    The path of the node relative to the root of the tree we are updating.

pathToNode

  • pathToNode(path: string): Node | null
  • Converts a path to a node.

    Parameters

    • path: string

      The path to convert.

    Returns Node | null

    The node corresponding to the path passed.

removeNode

  • removeNode(node: Node | undefined | null): DLoc
  • A complex method. Removes a node from the DOM tree. If two text nodes become adjacent, they are merged.

    Parameters

    • node: Node | undefined | null

      The node to remove. This method will fail with an exception if this parameter is undefined or null. Use removeNodeNF if you want a method that will silently do nothing if undefined or null are expected values.

    Returns DLoc

    A location between the two parts that were merged, or between the two nodes that were not merged (because they were not both text).

removeNodeNF

  • removeNodeNF(node: Node | undefined | null): DLoc | undefined
  • A complex method. Removes a node from the DOM tree. If two text nodes become adjacent, they are merged.

    Parameters

    • node: Node | undefined | null

      The node to remove. This method will do nothing if the node to remove is undefined or null.

    Returns DLoc | undefined

    A location between the two parts that were merged, or between the two nodes that were not merged (because they were not both text). This will be undefined if there was no node to remove.

removeNodes

  • removeNodes(nodes: Node[]): DLoc | undefined
  • A complex method. Removes a list of nodes from the DOM tree. If two text nodes become adjacent, they are merged.

    throws

    {Error} If nodes are not contiguous siblings.

    Parameters

    • nodes: Node[]

      These nodes must be immediately contiguous siblings in document order.

    Returns DLoc | undefined

    The location between the two parts that were merged, or between the two nodes that were not merged (because they were not both text). Undefined if the list of nodes is empty.

setAttribute

  • setAttribute(node: Element, attribute: string, value: string | null | undefined): void
  • A complex method. Sets an attribute to a value. Setting to the value null or undefined deletes the attribute. This method sets attributes outside of any namespace.

    emits

    SetAttributeNSEvent

    emits

    ChangedEvent

    Parameters

    • node: Element

      The node to modify.

    • attribute: string

      The name of the attribute to modify.

    • value: string | null | undefined

      The value to give to the attribute.

    Returns void

setAttributeNS

  • setAttributeNS(node: Element, ns: string, attribute: string, value: string | null | undefined): void
  • A primitive method. Sets an attribute to a value. Setting to the value null or undefined deletes the attribute.

    emits

    SetAttributeNSEvent

    emits

    ChangedEvent

    Parameters

    • node: Element

      The node to modify.

    • ns: string

      The URI of the namespace of the attribute.

    • attribute: string

      The name of the attribute to modify.

    • value: string | null | undefined

      The value to give to the attribute.

    Returns void

setTextNode

  • setTextNode(node: Text, value: string): void
  • A complex method. Sets a text node to a specified value.

    throws

    {Error} If called on a non-text Node type.

    Parameters

    • node: Text

      The node to modify.

    • value: string

      The new value of the node.

    Returns void

setTextNodeValue

  • setTextNodeValue(node: Text, value: string): void
  • A primitive method. Sets a text node to a specified value. This method must not be called directly by code that performs changes of the DOM tree at a high level, because it does not prevent a text node from becoming empty. Call TreeUpdater.setTextNode instead. This method is meant to be used by other complex methods of TreeUpdater and by some low-level facilities of wed.

    emits

    SetTextNodeValueEvent

    emits

    ChangedEvent

    throws

    {Error} If called on a non-text Node type.

    Parameters

    • node: Text

      The node to modify. Must be a text node.

    • value: string

      The new value of the node.

    Returns void

splitAt

  • A complex method. Splits a DOM tree into two halves.

    throws

    {Error} If the split location is not inside the top node or if the call would merely split a text node in two.

    Parameters

    • top: Node

      The node at which the splitting operation should end. This node will be split but the function won't split anything above this node.

    • loc: DLoc

      The location where to start the split.

    Returns SplitResult

    An array containing in order the first and second half of the split.

  • Parameters

    • top: Node
    • node: Node
    • index: number

    Returns SplitResult

Generated using TypeDoc