Options
All
  • Public
  • Public/Protected
  • All
Menu

External module "wed/domutil"

Index

Type aliases

Caret

Caret: [Node, number]

A caret position in the form of a pair of values. The caret we are talking about here roughly corresponds to the caret that a "contenteditable" element would present to the user. It can index in text nodes and element nodes but not in attributes.

CutResult

CutResult: [Caret, Node[]]

ElementPair

ElementPair: [Element, Element]

InsertionBoundaries

InsertionBoundaries: [Caret, Caret]

RangeInfo

RangeInfo: object

A range and a flag indicating whether it is a reversed range or not. Range objects themselves do not record how they were created. If the range was created from a starting point which is greater than the end point (in document order), then the range is "reversed".

Type declaration

  • range: Range
  • reversed: boolean

SplitResult

SplitResult: [Node, Node]

Variables

Const separatorRe

separatorRe: RegExp = new RegExp(`([${separators}]+)`)

Const separators

separators: ",>+~ " = ",>+~ "

Let textToHTMLSpan

textToHTMLSpan: HTMLSpanElement

Functions

Private _genericInsertIntoText

  • 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 textNode is not a text node.

    Parameters

    • this: GenericInsertIntoTextContext
    • textNode: Text

      The text node that will be cut in two by the new element.

    • index: number

      The offset into the text node where the new element is to be inserted.

    • Optional node: Node

      The node to insert. If undefined, then this function effectively splits the text node into two parts.

    • Default value clean: boolean = true

    Returns InsertionBoundaries

    A pair containing a caret position marking the boundary between what comes before the material inserted and the material inserted, and a caret position marking 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 caret would mark the boundary between "ab" and "foo" and the second caret the boundary between "foo" and "cd".

Private _insertIntoText

  • _insertIntoText(textNode: Text, index: number, node?: Node, clean?: boolean): InsertionBoundaries

childByClass

  • childByClass(node: Node | null, cl: string): Element | null
  • Find child matching the class.

    Parameters

    • node: Node | null

      The element whose child we are looking for.

    • cl: string

      The class to use for matches.

    Returns Element | null

    The first child (in document order) that matches the class, or null if nothing matches.

childrenByClass

  • childrenByClass(node: Node | null, cl: string): Element[]
  • Find children matching the class.

    Parameters

    • node: Node | null

      The element whose children we are looking for.

    • cl: string

      The class to use for matches.

    Returns Element[]

    The children (in document order) that match the class.

closest

  • closest(node: Node | undefined | null, selector: string, limit?: Element | Document): Element | null
  • Starting with the node passed, and walking up the node's parents, returns the first node that matches the selector.

    Parameters

    • node: Node | undefined | null

      The node to start with.

    • selector: string

      The selector to use for matches.

    • Optional limit: Element | Document

      The algorithm will search up to this limit, inclusively.

    Returns Element | null

    The first element that matches the selector, or null if nothing matches.

closestByClass

  • closestByClass(node: Node | undefined | null, cl: string, limit?: Element | Document): Element | null
  • Starting with the node passed, and walking up the node's parents, returns the first element that matches the class.

    Parameters

    • node: Node | undefined | null

      The node to start with.

    • cl: string

      The class to use for matches.

    • Optional limit: Element | Document

      The algorithm will search up to this limit, inclusively.

    Returns Element | null

    The first element that matches the class, or null if nothing matches.

comparePositions

  • comparePositions(firstNode: Node, firstOffset: number, secondNode: Node, secondOffset: number): 1 | 0 | -1
  • Compare two positions in document order.

    This function relies on DOM's compareDocumentPosition function. Remember that calling that function with attributes can be problematic. (For instance, two attributes on the same element are not ordered.)

    Parameters

    • firstNode: Node

      Node of the first position.

    • firstOffset: number

      Offset of the first position.

    • secondNode: Node

      Node of the second position.

    • secondOffset: number

      Offset of the second position.

    Returns 1 | 0 | -1

    -1 if the first position comes before the second. 1 if the first position comes after the other. 0 if the two positions are equal.

contains

  • contains(container: Node, contained: Node): boolean
  • A contains function that handles attributes. Attributes are not part of the node tree and performing a contains test on them is always false.

    Yet it makes sense to say that an element A contains its own attributes and thus by transitivity if element A is contained by element B, then all attributes of A are contained by B. This function supports the contention just described.

    Usage note: this function is typically not needed when doing tests in the GUI tree because we do not address attributes in that tree. There is, however, no harm in using it where it is not strictly needed. In the data tree, however, we do address attributes. Code that works with either tree (e.g. the "wed/dloc" module) should use this function as a general rule so that it can work with either tree.

    Parameters

    • container: Node

      The thing which should contain in the test.

    • contained: Node

      The thing which should be contained in the test.

    Returns boolean

    Whether container contains contained.

copy

  • copy(startCaret: Caret, endCaret: Caret): Node[]
  • Copies a well formed region of the DOM tree.

    throws

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

    Parameters

    • startCaret: Caret

      Start caret position.

    • endCaret: Caret

      Ending caret position.

    Returns Node[]

    A copy of the contents.

correspondingNode

  • correspondingNode(treeA: Node, treeB: Node, nodeInA: Node): Node
  • Given two trees A and B of DOM nodes, this function finds the node in tree B which corresponds to a node in tree A. The two trees must be structurally identical. If tree B is cloned from tree A, it will satisfy this requirement. This function does not work with attribute nodes.

    throws

    {Error} If nodeInA is not treeA or a child of treeA.

    Parameters

    • treeA: Node

      The root of the first tree.

    • treeB: Node

      The root of the second tree.

    • nodeInA: Node

      A node in the first tree.

    Returns Node

    The node which corresponds to nodeInA in treeB.

dataFind

  • dataFind(node: Element, selector: string, namespaces: Record<string, string>): Element | null
  • Allows applying simple CSS selectors on the data tree as if it were an HTML tree. This is necessary because the current browsers are unable to handle tag prefixes or namespaces in selectors passed to matches, querySelector and related functions.

    The steps are:

    1. Convert selector with toGUISelector into a selector that can be applied to the GUI tree.

    2. Convert node to a GUI node.

    3. Apply the converted selector to the GUI node.

    4. Convert the resulting node to a data node.

    Parameters

    • node: Element

      The element to use as the starting point of the query.

    • selector: string

      The selector to use.

    • namespaces: Record<string, string>

      The namespaces that are known. This is used to convert element name prefixes to namespace URIs.

    Returns Element | null

    The resulting data node.

dataFindAll

  • dataFindAll(node: Element, selector: string, namespaces: Record<string, string>): Element[]
  • Allows applying simple CSS selectors on the data tree as if it were an HTML tree. Operates like dataFind but returns an array of nodes.

    Parameters

    • node: Element

      The data node to use as the starting point of the query.

    • selector: string

      The selector to use.

    • namespaces: Record<string, string>

      The namespaces that are known. This is used to convert element name prefixes to namespace URIs.

    Returns Element[]

    The resulting data nodes.

deleteNode

  • deleteNode(node: Node): void
  • Removes the node. Mainly for use with the generic functions defined here.

    Parameters

    • node: Node

      The node to remove.

    Returns void

deleteText

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

    throws

    {Error} If node is not a text Node type.

    Parameters

    • node: Text

      The text node from which to delete text.

    • index: number

      The index at which to delete text.

    • length: number

      The length of text to delete.

    Returns void

dumpCurrentSelection

  • dumpCurrentSelection(msg: string, win: Window): void
  • Dumps the current selection to the console.

    Parameters

    • msg: string

      A message to output in front of the range information.

    • win: Window

      The window for which to dump selection information.

    Returns void

dumpRange

  • dumpRange(msg: string, range?: RangeLike): void
  • Dumps a range to the console.

    Parameters

    • msg: string

      A message to output in front of the range information.

    • Optional range: RangeLike

      The range.

    Returns void

dumpRangeToString

  • dumpRangeToString(msg: string, range?: RangeLike): string
  • Dumps a range to a string.

    Parameters

    • msg: string

      A message to output in front of the range information.

    • Optional range: RangeLike

      The range.

    Returns string

firstDescendantOrSelf

  • firstDescendantOrSelf(node: Node | null | undefined): Node | null
  • Returns the first descendant or the node passed to the function if the node happens to not have a descendant. The function searches in document order.

    When passed <p><b>A</b><b><q>B</q></b></p> this code would return the text node "A" because it has no children and is first.

    Parameters

    • node: Node | null | undefined

      The node to search.

    Returns Node | null

    The first node which is both first in its parent and has no children.

focusNode

  • focusNode(node: Node): void
  • Focuses the node itself or if the node is a text node, focuses the parent.

    throws

    {Error} If the node is neither a text node nor an element. Trying to focus something other than these is almost certainly an algorithmic bug.

    Parameters

    • node: Node

      The node to focus.

    Returns void

genericCutFunction

  • 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

    Returns CutResult

    The first item is the caret position indicating where the cut happened. The second item is a list of nodes, the cut contents.

genericInsertIntoText

  • 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

    • this: GenericInsertIntoTextContext
    • textNode: Text

      The text node that will be cut in two by the new element.

    • index: number

      The offset into the text node where the new element is to be inserted.

    • Optional node: Node

      The node to insert.

    Returns InsertionBoundaries

    A pair containing a caret position marking the boundary between what comes before the material inserted and the material inserted, and a caret position marking 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 caret would mark the boundary between "ab" and "foo" and the second caret the boundary between "foo" and "cd".

genericInsertText

  • 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

    • this: GenericInsertTextContext
    • node: Node

      The node where the text is to be inserted.

    • index: number

      The location in the node where the text is to be inserted.

    • text: string

      The text to insert.

    • Default value caretAtEnd: boolean = true

      Whether the caret position returned should be placed at the end of the inserted text.

    Returns TextInsertionResult

    The result of inserting the text.

getCharacterImmediatelyAt

  • getCharacterImmediatelyAt(caret: Caret): string | undefined
  • Gets the character immediately at the caret. The word "immediately" here means that this function does not walk the DOM. If the caret is pointing into an element node, it will check whether the node at the offset is a text node and use it. That's the extent to which it walks the DOM.

    Parameters

    • caret: Caret

      The caret position.

    Returns string | undefined

    The character, if it exists.

getCharacterImmediatelyBefore

  • getCharacterImmediatelyBefore(caret: Caret): string | undefined
  • Gets the character immediately before the caret. The word "immediately" here means that this function does not walk the DOM. If the caret is pointing into an element node, it will check whether the node before the offset is a text node and use it. That's the extent to which it walks the DOM.

    Parameters

    • caret: Caret

      The caret position.

    Returns string | undefined

    The character, if it exists.

getSelectionRange

  • getSelectionRange(win: Window): Range | undefined
  • Gets the first range in the selection.

    Parameters

    • win: Window

      The window for which we want the selection.

    Returns Range | undefined

    The first range in the selection. Undefined if there is no selection or no range.

htmlToElements

  • htmlToElements(html: string, document?: Document): Node[]
  • Converts an HTML string to an array of DOM nodes. This function is not responsible for checking the HTML for security holes it is the responsibility of the calling code to ensure the HTML passed is clean.

    Parameters

    • html: string

      The HTML to convert.

    • Optional document: Document

      The document for which to create the nodes. If not specified, the document will be the global document.

    Returns Node[]

    The resulting nodes.

indexOf

  • indexOf(a: NodeList, target: Node): number
  • indexOf<T>(a: T[], target: T): number
  • Search an array.

    Parameters

    • a: NodeList

      The array to search.

    • target: Node

      The target to find.

    Returns number

    -1 if the target is not found, or its index.

  • Type parameters

    • T

    Parameters

    • a: T[]
    • target: T

    Returns number

insertIntoText

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

    Parameters

    • textNode: Text

      The text node that will be cut in two by the new element.

    • index: number

      The offset into the text node where the new element is to be inserted.

    • node: Node

      The node to insert.

    Returns InsertionBoundaries

    A pair containing a caret position marking the boundary between what comes before the material inserted and the material inserted, and a caret position marking 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 caret would mark the boundary between "ab" and "foo" and the second caret the boundary between "foo" and "cd".

insertNodeAt

  • insertNodeAt(parent: Element, index: number, node: Node): void
  • Inserts a node at the position specified. Mainly for use with the generic functions defined here.

    Parameters

    • parent: Element

      The node which will become the parent of the inserted node.

    • index: number

      The position at which to insert the node into the parent.

    • node: Node

      The node to insert.

    Returns void

insertText

  • insertText(node: Node, index: number, text: string, caretAtEnd?: undefined | true | false): TextInsertionResult
  • Inserts text into a node. This function will use already existing text nodes whenever possible rather than create a new text node.

    function
    throws

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

    Parameters

    • node: Node

      The node where the text is to be inserted.

    • index: number

      The location in the node where the text is to be inserted.

    • text: string

      The text to insert.

    • Optional caretAtEnd: undefined | true | false

      Whether to return the caret position at the end of the inserted text or at the beginning. Default to true.

    Returns TextInsertionResult

    The result of inserting the text.

isNotDisplayed

  • isNotDisplayed(el: HTMLElement, root: HTMLElement | HTMLDocument): boolean
  • Determine whether an element is displayed. This function is designed to handle checks in wed's GUI tree, and not as a general purpose solution. It only checks whether the element or its parents have display set to "none".

    Parameters

    • el: HTMLElement

      The DOM element for which we want to check whether it is displayed or not.

    • root: HTMLElement | HTMLDocument

      The parent of el beyond which we do not search.

    Returns boolean

    true if the element or any of its parents is not displayed. false otherwise. If the search up the DOM tree hits root, then the value returned is false.

isWellFormedRange

  • isWellFormedRange(range: RangeLike): boolean
  • Determines whether a range is well-formed. A well-formed range is one which starts and ends in the same element.

    Parameters

    • range: RangeLike

      An object which has the startContainer, startOffset, endContainer, endOffset attributes set. The interpretation of these values is the same as for DOM Range objects. Therefore, the object passed can be a DOM range.

    Returns boolean

    true if the range is well-formed. false if not.

lastDescendantOrSelf

  • lastDescendantOrSelf(node: Node | null | undefined): Node | null
  • Returns the last descendant or the node passed to the function if the node happens to not have a descendant. The function searches in reverse document order.

    When passed <p><b>A</b><b><q>B</q></b></p> this code would return the text node "B" because it has no children and is last.

    Parameters

    • node: Node | null | undefined

      The node to search.

    Returns Node | null

    The last node which is both last in its parent and has no children.

linkTrees

  • linkTrees(rootA: Element, rootB: Element): void
  • This function recursively links two DOM trees through the jQuery .data() method. For an element in the first tree the data item named "wed_mirror_node" points to the corresponding element in the second tree, and vice-versa. It is presumed that the two DOM trees are perfect mirrors of each other, although no test is performed to confirm this.

    Parameters

    • rootA: Element
    • rootB: Element

    Returns void

makePlaceholder

  • makePlaceholder(text?: undefined | string): HTMLElement
  • Makes a placeholder element

    Parameters

    • Optional text: undefined | string

      The text to put in the placeholder.

    Returns HTMLElement

    A node.

mergeTextNodes

  • mergeTextNodes(node: Node): Caret
  • Merges a text node with the next text node, if present. When called on something which is not a text node or if the next node is not text, does nothing. Mainly for use with the generic functions defined here.

    Parameters

    • node: Node

      The node to merge with the next node.

    Returns Caret

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

nextCaretPosition

  • nextCaretPosition(caret: Caret, container: Node, noText: boolean): Caret | null
  • This function determines the caret position if the caret was moved forward.

    This function does not fully emulate how a browser moves the caret. The sole emulation it performs is to check whether whitespace matters or not. It skips whitespace that does not matter.

    Parameters

    • caret: Caret

      A caret position where the search starts. This should be an array of length two that has in first position the node where the caret is and in second position the offset in that node. This pair is to be interpreted in the same way node, offset pairs are interpreted in selection or range objects.

    • container: Node

      A DOM node which indicates the container within which caret movements must be contained.

    • noText: boolean

      If true, and a text node would be returned, the function will instead return the parent of the text node.

    Returns Caret | null

    The next caret position, or null if such position does not exist. The container parameter constrains movements to positions inside it.

Private nodePairFromRange

  • Returns the element nodes that contain the start and the end of the range. If an end of the range happens to be in a text node, the element node will be that node's parent.

    throws

    {Error} If a node in range is not of element or text Node types.

    Parameters

    • range: RangeLike

      An object which has the startContainer, startOffset, endContainer, endOffset attributes set. The interpretation of these values is the same as for DOM Range objects. Therefore, the object passed can be a DOM range.

    Returns ElementPair

    A pair of nodes.

parentChildCompare

  • parentChildCompare(parentNode: Node, parentOffset: number, childNode: Node): 1 | -1
  • Compare two locations that have already been determined to be in a parent-child relation. Important: the relationship must have been formally tested before calling this function.

    Parameters

    • parentNode: Node
    • parentOffset: number
    • childNode: Node

    Returns 1 | -1

    -1 if parent is before child, 1 otherwise.

pointInContents

  • pointInContents(element: Element, x: number, y: number): boolean
  • Checks whether a point is in the element's contents. This means inside the element and not inside one of the scrollbars that the element may have. The coordinates passed must be relative to the document. If the coordinates are taken from an event, this means passing pageX and pageY.

    Parameters

    • element: Element

      The element to check.

    • x: number

      The x coordinate relative to the document.

    • y: number

      The y coordinate relative to the document.

    Returns boolean

    true if inside, false if not.

prevCaretPosition

  • prevCaretPosition(caret: Caret, container: Node, noText: boolean): Caret | null
  • This function determines the caret position if the caret was moved backwards.

    This function does not fully emulate how a browser moves the caret. The sole emulation it performs is to check whether whitespace matters or not. It skips whitespace that does not matter.

    Parameters

    • caret: Caret

      A caret position where the search starts. This should be an array of length two that has in first position the node where the caret is and in second position the offset in that node. This pair is to be interpreted in the same way node, offset pairs are interpreted in selection or range objects.

    • container: Node

      A DOM node which indicates the container within which caret movements must be contained.

    • noText: boolean

      If true, and a text node would be returned, the function will instead return the parent of the text node.

    Returns Caret | null

    The previous caret position, or null if such position does not exist. The container parameter constrains movements to positions inside it.

rangeFromPoints

  • rangeFromPoints(startContainer: Node, startOffset: number, endContainer: Node, endOffset: number): RangeInfo
  • Creates a range from two points in a document.

    Parameters

    • startContainer: Node
    • startOffset: number
    • endContainer: Node
    • endOffset: number

    Returns RangeInfo

    The range information.

siblingByClass

  • siblingByClass(node: Node | null, cl: string): Element | null
  • Find a sibling matching the class.

    Parameters

    • node: Node | null

      The element whose sibling we are looking for.

    • cl: string

      The class to use for matches.

    Returns Element | null

    The first sibling (in document order) that matches the class, or null if nothing matches.

splitTextNode

  • splitTextNode(textNode: Text, index: number): SplitResult
  • Splits a text node into two nodes. This function takes care to modify the DOM tree only once.

    Parameters

    • textNode: Text

      The text node to split into two text nodes.

    • index: number

      The offset into the text node where to split.

    Returns SplitResult

    The first element is the node before index after split and the second element is the node after the index after split.

textToHTML

  • textToHTML(text: string): string
  • Convert a string to HTML encoding. For instance if you want to have the less-than symbol be part of the contents of a span element, it would have to be escaped to < otherwise it would be interpreted as the beginning of a tag. This function does this kind of escaping.

    Parameters

    • text: string

      The text to convert.

    Returns string

    The converted text.

toGUISelector

  • toGUISelector(selector: string, namespaces: Record<string, string>): string
  • Converts a CSS selector written as if it were run against the XML document being edited by wed into a selector that will match the corresponding items in the GUI tree. This implementation is extremely naive and likely to break on complex selectors. Some specific things it cannot do:

    • Match attributes.

    • Match pseudo-elements.

    Parameters

    • selector: string

      The selector to convert.

    • namespaces: Record<string, string>

      The namespaces that are known. This is used to convert element name prefixes to namespace URIs.

    Returns string

    The converted selector.

unlinkTree

  • unlinkTree(root: Element): void
  • This function recursively unlinks a DOM tree though the jQuery .data() method.

    Parameters

    • root: Element

      A DOM node.

    Returns void

Object literals

Const plainDOMMockup

plainDOMMockup: object

deleteNode

deleteNode: deleteNode = deleteNode

insertFragAt

insertFragAt: insertNodeAt = insertNodeAt

insertNodeAt

insertNodeAt: insertNodeAt = insertNodeAt

Generated using TypeDoc