Options
All
  • Public
  • Public/Protected
  • All
Menu

Class DLoc

DLoc objects model locations in a DOM tree. Although the current implementation does not enforce this, these objects are to be treated as immutable. These objects have node and offset properties that are to be interpreted in the same way DOM locations usually are: the node is the location of a DOM Node in a DOM tree (or an attribute), and offset is a location in that node. DLoc objects are said to have a root relative to which they are positioned.

A DLoc object can point to an offset inside an Element, inside a Text node or inside an Attr.

Use makeDLoc to make DLoc objects. Calling this constructor directly is not legal.

Hierarchy

  • DLoc

Index

Constructors

Private constructor

  • Parameters

    • root: ValidRoots

      The root of the DOM tree to which this DLoc applies.

    • node: Node | Attr

      The node of the location.

    • offset: number

      The offset of the location.

    Returns DLoc

Properties

node

node: Node | Attr

The node of the location.

offset

offset: number

The offset of the location.

root

The root of the DOM tree to which this DLoc applies.

Accessors

pointedNode

  • get pointedNode(): Node | Attr | undefined
  • This is the node to which this location points. For locations pointing to attributes and text nodes, that's the same as node. For locations pointing to an element, that's the child to which the node, offset pair points. Since this pair may point after the last child of an element, the child obtained may be undefined.

    Returns Node | Attr | undefined

Methods

clone

compare

  • compare(other: DLoc): -1 | 0 | 1
  • Compare two locations. Note that for attribute ordering, this class arbitrarily decides that the order of two attributes on the same element is the same as the order of their name fields as if they were sorted in an array with Array.prototype.sort(). This differs from how Node.compareDocumentPosition determines the order of attributes. We want something stable, which is not implementation dependent. In all other cases, the nodes are compared in the same way Node.compareDocumentPosition does.

    throws

    {Error} If the nodes are disconnected.

    Parameters

    • other: DLoc

      The other location to compare this one with.

    Returns -1 | 0 | 1

    0 if the locations are the same. -1 if this location comes first. 1 if the other location comes first.

equals

  • equals(other: DLoc | undefined | null): boolean
  • Parameters

    • other: DLoc | undefined | null

    Returns boolean

    Whether this and other are equal. They are equal if they are the same object or if they point to the same location.

getLocationAfterInParent

  • getLocationAfterInParent(): DLoc
  • Same as getLocationInParent except that the location points after the current node.

    throws

    {Error} If the current node has no parent.

    Returns DLoc

    The location in the parent, as described above.

getLocationInParent

  • getLocationInParent(): DLoc
  • Make a new location. Let's define "current node" as the node of the current location. The new location points to the current node. (The offset of the current location is effectively ignored.) That is, the new location has for node the parent node of the current node, and for offset the offset of the current node in its parent.

    throws

    {Error} If the current node has no parent.

    Returns DLoc

    The location in the parent, as described above.

isValid

  • isValid(): boolean
  • Verifies whether the DLoc object points to a valid location. The location is valid if its node is a child of its root and if its offset points inside the range of children of its node.

    Returns boolean

    Whether the object is valid.

make

  • make(caret: Caret): DLoc
  • make(node: Node | Attr, offset?: undefined | number): DLoc
  • Make a new location in the same DOM tree as the current one. This is a convenience function that enables avoid having to pass root around.

    Parameters

    • caret: Caret

      A node, offset pair.

    Returns DLoc

    The new location.

  • Parameters

    • node: Node | Attr
    • Optional offset: undefined | number

    Returns DLoc

makeDLocRange

  • Make a range from this location. If other is not specified, the range starts and ends with this location. If other is specified, the range goes from this location to the other location.

    Parameters

    • Optional other: DLoc

      The other location to use.

    Returns DLocRange | undefined

    The range.

makeRange

  • makeRange(): Range | undefined
  • makeRange(other: DLoc): RangeInfo | undefined
  • Make a range from this location. If other is not specified, the range starts and ends with this location, and the return value is a range. If other is specified, the range goes from this location to the other location. If other comes before this, then the range is "reversed". When other is specified, the return value is an object (see below). (An undefined value for other is interpreted as an unspecified other.)

    throws

    {Error} If trying to make a range from an attribute node. DOM ranges can only point into elements or text nodes.

    Returns Range | undefined

    The return value is just a range when the method is called without other. Otherwise, it is a range info object. The return value is undefined if either this or other is invalid.

  • Parameters

    Returns RangeInfo | undefined

makeWithOffset

  • makeWithOffset(offset: number): DLoc
  • Make a new location with the same node as the current location but with a new offset.

    Parameters

    • offset: number

      The offset of the new location.

    Returns DLoc

    The new location.

mustMakeDLocRange

normalizeOffset

  • normalizeOffset(): DLoc
  • Creates a new DLoc object with an offset that is valid. It does this by "normalizing" the offset, i.e. by setting the offset to its maximum possible value.

    Returns DLoc

    The normalized location. This will be this, if it so happens that this is already valid.

toArray

  • Converts the location to an array. This array contains only the node and offset of the location. The root is not included because this method is of use to pass data to functions that work with raw DOM information. These functions do not typically expect a root.

    Returns Caret

    The node and offset pair.

Static makeDLoc

  • makeDLoc(root: ValidRoots | DLocRoot, node: Node | Attr | undefined | null, offset?: undefined | number, normalize?: undefined | true | false): DLoc | undefined
  • makeDLoc(root: ValidRoots | DLocRoot, location: Caret, normalize?: undefined | true | false): DLoc | undefined
  • Makes a location.

    throws

    {Error} If node is not in root or if root has not been marked as a root.

    Parameters

    • root: ValidRoots | DLocRoot

      The root of the DOM tree to which this location belongs.

    • node: Node | Attr | undefined | null

      The node of the location.

    • Optional offset: undefined | number

      The offset of the location. If the offset is omitted, then the location will point to node rather than be a location that points to the node inside of node at offset offset.

    • Optional normalize: undefined | true | false

      Whether to normalize the offset to a valid value.

    Returns DLoc | undefined

    The location. It returns undefined if the node is "absent" because it is undefined or null. This is true irrespective of the signature used. If you use a Caret and it has an absent node, then the result is undefined.

  • Parameters

    Returns DLoc | undefined

Static mustMakeDLoc

  • mustMakeDLoc(root: ValidRoots | DLocRoot, node: Node | Attr | undefined | null, offset?: undefined | number, normalize?: undefined | true | false): DLoc
  • mustMakeDLoc(root: ValidRoots | DLocRoot, location: Caret, normalize?: undefined | true | false): DLoc

Generated using TypeDoc