Interface TreeAdapter<T>

Tree adapter is a set of utility functions that provides minimal required abstraction layer beetween parser and a specific AST format. Note that TreeAdapter is not designed to be a general purpose AST manipulation library. You can build such library on top of existing TreeAdapter or use one of the existing libraries from npm.

Have a look at the default tree adapter for reference.

interface TreeAdapter<T> {
    onItemPop?: ((item: T["element"], newTop: T["parentNode"]) => void);
    onItemPush?: ((item: T["element"]) => void);
    adoptAttributes(recipient: T["element"], attrs: Attribute[]): void;
    appendChild(parentNode: T["parentNode"], newNode: T["childNode"]): void;
    createCommentNode(data: string): T["commentNode"];
    createDocument(): T["document"];
    createDocumentFragment(): T["documentFragment"];
    createElement(tagName: string, namespaceURI: NS, attrs: Attribute[]): T["element"];
    createTextNode(value: string): T["textNode"];
    detachNode(node: T["childNode"]): void;
    getAttrList(element: T["element"]): Attribute[];
    getChildNodes(node: T["parentNode"]): T["childNode"][];
    getCommentNodeContent(commentNode: T["commentNode"]): string;
    getDocumentMode(document: T["document"]): DOCUMENT_MODE;
    getDocumentTypeNodeName(doctypeNode: T["documentType"]): string;
    getDocumentTypeNodePublicId(doctypeNode: T["documentType"]): string;
    getDocumentTypeNodeSystemId(doctypeNode: T["documentType"]): string;
    getFirstChild(node: T["parentNode"]): null | T["childNode"];
    getNamespaceURI(element: T["element"]): NS;
    getNodeSourceCodeLocation(node: T["node"]): undefined | null | ElementLocation;
    getParentNode(node: T["node"]): null | T["parentNode"];
    getTagName(element: T["element"]): string;
    getTemplateContent(templateElement: T["template"]): T["documentFragment"];
    getTextNodeContent(textNode: T["textNode"]): string;
    insertBefore(parentNode: T["parentNode"], newNode: T["childNode"], referenceNode: T["childNode"]): void;
    insertText(parentNode: T["parentNode"], text: string): void;
    insertTextBefore(parentNode: T["parentNode"], text: string, referenceNode: T["childNode"]): void;
    isCommentNode(node: T["node"]): node is T["commentNode"];
    isDocumentTypeNode(node: T["node"]): node is T["documentType"];
    isElementNode(node: T["node"]): node is T["element"];
    isTextNode(node: T["node"]): node is T["textNode"];
    setDocumentMode(document: T["document"], mode: DOCUMENT_MODE): void;
    setDocumentType(document: T["document"], name: string, publicId: string, systemId: string): void;
    setNodeSourceCodeLocation(node: T["node"], location: null | ElementLocation): void;
    setTemplateContent(templateElement: T["template"], contentElement: T["documentFragment"]): void;
    updateNodeSourceCodeLocation(node: T["node"], location: Partial<ElementLocation>): void;
}

Type Parameters

Properties

onItemPop?: ((item: T["element"], newTop: T["parentNode"]) => void)

Optional callback for elements being popped from the stack of open elements.

Type declaration

    • (item, newTop): void
    • Parameters

      • item: T["element"]

        The element being popped.

      • newTop: T["parentNode"]

      Returns void

onItemPush?: ((item: T["element"]) => void)

Optional callback for elements being pushed to the stack of open elements.

Methods

  • Copies attributes to the given element. Only attributes that are not yet present in the element are copied.

    Parameters

    • recipient: T["element"]

      Element to copy attributes into.

    • attrs: Attribute[]

      Attributes to copy.

    Returns void

  • Appends a child node to the given parent node.

    Parameters

    • parentNode: T["parentNode"]

      Parent node.

    • newNode: T["childNode"]

      Child node.

    Returns void

  • Creates a comment node.

    Parameters

    • data: string

      Comment text.

    Returns T["commentNode"]

  • Creates an element node.

    Parameters

    • tagName: string

      Tag name of the element.

    • namespaceURI: NS

      Namespace of the element.

    • attrs: Attribute[]

      Attribute name-value pair array. Foreign attributes may contain namespace and prefix fields as well.

    Returns T["element"]

  • Removes a node from its parent.

    Parameters

    • node: T["childNode"]

      Node to remove.

    Returns void

  • Returns the given element's attributes in an array, in the form of name-value pairs. Foreign attributes may contain namespace and prefix fields as well.

    Parameters

    • element: T["element"]

      Element.

    Returns Attribute[]

  • Returns the given node's children in an array.

    Parameters

    • node: T["parentNode"]

      Node.

    Returns T["childNode"][]

  • Returns the given comment node's content.

    Parameters

    • commentNode: T["commentNode"]

      Comment node.

    Returns string

  • Returns the given document type node's name.

    Parameters

    • doctypeNode: T["documentType"]

      Document type node.

    Returns string

  • Returns the given document type node's public identifier.

    Parameters

    • doctypeNode: T["documentType"]

      Document type node.

    Returns string

  • Returns the given document type node's system identifier.

    Parameters

    • doctypeNode: T["documentType"]

      Document type node.

    Returns string

  • Returns the first child of the given node.

    Parameters

    • node: T["parentNode"]

      Node.

    Returns null | T["childNode"]

  • Returns the given node's parent.

    Parameters

    • node: T["node"]

      Node.

    Returns null | T["parentNode"]

  • Returns the given element's tag name.

    Parameters

    • element: T["element"]

      Element.

    Returns string

  • Returns the <template> element content element.

    Parameters

    • templateElement: T["template"]

      <template> element.

    Returns T["documentFragment"]

  • Returns the given text node's content.

    Parameters

    • textNode: T["textNode"]

      Text node.

    Returns string

  • Inserts a child node to the given parent node before the given reference node.

    Parameters

    • parentNode: T["parentNode"]

      Parent node.

    • newNode: T["childNode"]

      Child node.

    • referenceNode: T["childNode"]

      Reference node.

    Returns void

  • Inserts text into a node. If the last child of the node is a text node, the provided text will be appended to the text node content. Otherwise, inserts a new text node with the given text.

    Parameters

    • parentNode: T["parentNode"]

      Node to insert text into.

    • text: string

      Text to insert.

    Returns void

  • Inserts text into a sibling node that goes before the reference node. If this sibling node is the text node, the provided text will be appended to the text node content. Otherwise, inserts a new sibling text node with the given text before the reference node.

    Parameters

    • parentNode: T["parentNode"]

      Node to insert text into.

    • text: string

      Text to insert.

    • referenceNode: T["childNode"]

      Node to insert text before.

    Returns void

  • Determines if the given node is a comment node.

    Parameters

    • node: T["node"]

      Node.

    Returns node is T["commentNode"]

  • Determines if the given node is a document type node.

    Parameters

    • node: T["node"]

      Node.

    Returns node is T["documentType"]

  • Determines if the given node is an element.

    Parameters

    • node: T["node"]

      Node.

    Returns node is T["element"]

  • Determines if the given node is a text node.

    Parameters

    • node: T["node"]

      Node.

    Returns node is T["textNode"]

  • Sets the document type. If the document already contains a document type node, the name, publicId and systemId properties of this node will be updated with the provided values. Otherwise, creates a new document type node with the given properties and inserts it into the document.

    Parameters

    • document: T["document"]

      Document node.

    • name: string

      Document type name.

    • publicId: string

      Document type public identifier.

    • systemId: string

      Document type system identifier.

    Returns void

  • Sets the <template> element content element.

    Parameters

    • templateElement: T["template"]

      <template> element.

    • contentElement: T["documentFragment"]

      Content element.

    Returns void