domutils

  • Version 3.1.0
  • Published
  • 162 kB
  • 3 dependencies
  • BSD-2-Clause license

Install

npm i domutils
yarn add domutils
pnpm add domutils

Overview

Utilities for working with htmlparser2's dom

Index

Functions

function append

append: (elem: ChildNode, next: ChildNode) => void;
  • Append an element after another.

    Manipulation

    Parameter elem

    The element to append after.

    Parameter next

    The element be added.

function appendChild

appendChild: (parent: ParentNode, child: ChildNode) => void;
  • Append a child to an element.

    Manipulation

    Parameter parent

    The element to append to.

    Parameter child

    The element to be added as a child.

function compareDocumentPosition

compareDocumentPosition: (nodeA: AnyNode, nodeB: AnyNode) => number;
  • Compare the position of one node against another node in any other document, returning a bitmask with the values from DocumentPosition.

    Document order: > There is an ordering, document order, defined on all the nodes in the > document corresponding to the order in which the first character of the > XML representation of each node occurs in the XML representation of the > document after expansion of general entities. Thus, the document element > node will be the first node. Element nodes occur before their children. > Thus, document order orders element nodes in order of the occurrence of > their start-tag in the XML (after expansion of entities). The attribute > nodes of an element occur after the element and before its children. The > relative order of attribute nodes is implementation-dependent.

    Source: http://www.w3.org/TR/DOM-Level-3-Core/glossary.html#dt-document-order

    Helpers

    Parameter nodeA

    The first node to use in the comparison

    Parameter nodeB

    The second node to use in the comparison

    Returns

    A bitmask describing the input nodes' relative position.

    See http://dom.spec.whatwg.org/#dom-node-comparedocumentposition for a description of these values.

function existsOne

existsOne: (test: (elem: Element) => boolean, nodes: AnyNode[]) => boolean;
  • Checks if a tree of nodes contains at least one node passing a test.

    Querying

    Parameter test

    Function to test nodes on.

    Parameter nodes

    Array of nodes to search.

    Returns

    Whether a tree of nodes contains at least one node passing the test.

function filter

filter: (
test: (elem: AnyNode) => boolean,
node: AnyNode | AnyNode[],
recurse?: boolean,
limit?: number
) => AnyNode[];
  • Search a node and its children for nodes passing a test function. If node is not an array, it will be wrapped in one.

    Querying

    Parameter test

    Function to test nodes on.

    Parameter node

    Node to search. Will be included in the result set if it matches.

    Parameter recurse

    Also consider child nodes.

    Parameter limit

    Maximum number of nodes to return.

    Returns

    All nodes passing test.

function find

find: (
test: (elem: AnyNode) => boolean,
nodes: AnyNode[],
recurse: boolean,
limit: number
) => AnyNode[];
  • Search an array of nodes and their children for nodes passing a test function.

    Querying

    Parameter test

    Function to test nodes on.

    Parameter nodes

    Array of nodes to search.

    Parameter recurse

    Also consider child nodes.

    Parameter limit

    Maximum number of nodes to return.

    Returns

    All nodes passing test.

function findAll

findAll: (test: (elem: Element) => boolean, nodes: AnyNode[]) => Element[];
  • Search an array of nodes and their children for elements passing a test function.

    Same as find, but limited to elements and with less options, leading to reduced complexity.

    Querying

    Parameter test

    Function to test nodes on.

    Parameter nodes

    Array of nodes to search.

    Returns

    All nodes passing test.

function findOne

findOne: (
test: (elem: Element) => boolean,
nodes: AnyNode[],
recurse?: boolean
) => Element | null;
  • Finds one element in a tree that passes a test.

    Querying

    Parameter test

    Function to test nodes on.

    Parameter nodes

    Node or array of nodes to search.

    Parameter recurse

    Also consider child nodes.

    Returns

    The first node that passes test.

function findOneChild

findOneChild: <T>(test: (elem: T) => boolean, nodes: T[]) => T | undefined;
  • Finds the first element inside of an array that matches a test function. This is an alias for Array.prototype.find.

    Querying

    Parameter test

    Function to test nodes on.

    Parameter nodes

    Array of nodes to search.

    Returns

    The first node in the array that passes test.

    Deprecated

    Use Array.prototype.find directly.

function getAttributeValue

getAttributeValue: (elem: Element, name: string) => string | undefined;
  • Gets an attribute from an element.

    Traversal

    Parameter elem

    Element to check.

    Parameter name

    Attribute name to retrieve.

    Returns

    The element's attribute value, or undefined.

function getChildren

getChildren: (elem: AnyNode) => ChildNode[];
  • Get a node's children.

    Traversal

    Parameter elem

    Node to get the children of.

    Returns

    elem's children, or an empty array.

function getElementById

getElementById: (
id: string | ((id: string) => boolean),
nodes: AnyNode | AnyNode[],
recurse?: boolean
) => Element | null;
  • Returns the node with the supplied ID.

    Legacy Query Functions

    Parameter id

    The unique ID attribute value to look for.

    Parameter nodes

    Nodes to search through.

    Parameter recurse

    Also consider child nodes.

    Returns

    The node with the supplied ID.

function getElements

getElements: (
options: TestElementOpts,
nodes: AnyNode | AnyNode[],
recurse: boolean,
limit?: number
) => AnyNode[];
  • Returns all nodes that match options.

    Legacy Query Functions

    Parameter options

    An object describing nodes to look for.

    Parameter nodes

    Nodes to search through.

    Parameter recurse

    Also consider child nodes.

    Parameter limit

    Maximum number of nodes to return.

    Returns

    All nodes that match options.

function getElementsByTagName

getElementsByTagName: (
tagName: string | ((name: string) => boolean),
nodes: AnyNode | AnyNode[],
recurse?: boolean,
limit?: number
) => Element[];
  • Returns all nodes with the supplied tagName.

    Legacy Query Functions

    Parameter tagName

    Tag name to search for.

    Parameter nodes

    Nodes to search through.

    Parameter recurse

    Also consider child nodes.

    Parameter limit

    Maximum number of nodes to return.

    Returns

    All nodes with the supplied tagName.

function getElementsByTagType

getElementsByTagType: (
type: any,
nodes: AnyNode | AnyNode[],
recurse?: boolean,
limit?: number
) => AnyNode[];
  • Returns all nodes with the supplied type.

    Legacy Query Functions

    Parameter type

    Element type to look for.

    Parameter nodes

    Nodes to search through.

    Parameter recurse

    Also consider child nodes.

    Parameter limit

    Maximum number of nodes to return.

    Returns

    All nodes with the supplied type.

function getFeed

getFeed: (doc: AnyNode[]) => Feed | null;
  • Get the feed object from the root of a DOM tree.

    Feeds

    Parameter doc

    The DOM to to extract the feed from.

    Returns

    The feed.

function getInnerHTML

getInnerHTML: (node: AnyNode, options?: DomSerializerOptions) => string;
  • Stringify

    Parameter node

    Node to get the inner HTML of.

    Parameter options

    Options for serialization.

    Returns

    node's inner HTML.

    Deprecated

    Use the dom-serializer module directly.

function getName

getName: (elem: Element) => string;
  • Get the tag name of an element.

    Traversal

    Parameter elem

    The element to get the name for.

    Returns

    The tag name of elem.

function getOuterHTML

getOuterHTML: (
node: AnyNode | ArrayLike<AnyNode>,
options?: DomSerializerOptions
) => string;
  • Stringify

    Parameter node

    Node to get the outer HTML of.

    Parameter options

    Options for serialization.

    Returns

    node's outer HTML.

    Deprecated

    Use the dom-serializer module directly.

function getParent

getParent: (elem: AnyNode) => ParentNode | null;

    function getSiblings

    getSiblings: (elem: AnyNode) => AnyNode[];
    • Gets an elements siblings, including the element itself.

      Attempts to get the children through the element's parent first. If we don't have a parent (the element is a root node), we walk the element's prev & next to get all remaining nodes.

      Traversal

      Parameter elem

      Element to get the siblings of.

      Returns

      elem's siblings, including elem.

    function getText

    getText: (node: AnyNode | AnyNode[]) => string;
    • Get a node's inner text. Same as textContent, but inserts newlines for <br> tags. Ignores comments.

      Stringify

      Parameter node

      Node to get the inner text of.

      Returns

      node's inner text.

      Deprecated

      Use textContent instead.

    function hasAttrib

    hasAttrib: (elem: Element, name: string) => boolean;
    • Checks whether an element has an attribute.

      Traversal

      Parameter elem

      Element to check.

      Parameter name

      Attribute name to look for.

      Returns

      Returns whether elem has the attribute name.

    function innerText

    innerText: (node: AnyNode | AnyNode[]) => string;

    function nextElementSibling

    nextElementSibling: (elem: AnyNode) => Element | null;
    • Returns the next element sibling of a node.

      Traversal

      Parameter elem

      The element to get the next sibling of.

      Returns

      elem's next sibling that is a tag, or null if there is no next sibling.

    function prepend

    prepend: (elem: ChildNode, prev: ChildNode) => void;
    • Prepend an element before another.

      Manipulation

      Parameter elem

      The element to prepend before.

      Parameter prev

      The element be added.

    function prependChild

    prependChild: (parent: ParentNode, child: ChildNode) => void;
    • Prepend a child to an element.

      Manipulation

      Parameter parent

      The element to prepend before.

      Parameter child

      The element to be added as a child.

    function prevElementSibling

    prevElementSibling: (elem: AnyNode) => Element | null;
    • Returns the previous element sibling of a node.

      Traversal

      Parameter elem

      The element to get the previous sibling of.

      Returns

      elem's previous sibling that is a tag, or null if there is no previous sibling.

    function removeElement

    removeElement: (elem: ChildNode) => void;
    • Remove an element from the dom

      Manipulation

      Parameter elem

      The element to be removed

    function removeSubsets

    removeSubsets: (nodes: AnyNode[]) => AnyNode[];
    • Given an array of nodes, remove any member that is contained by another member.

      Helpers

      Parameter nodes

      Nodes to filter.

      Returns

      Remaining nodes that aren't contained by other nodes.

    function replaceElement

    replaceElement: (elem: ChildNode, replacement: ChildNode) => void;
    • Replace an element in the dom

      Manipulation

      Parameter elem

      The element to be replaced

      Parameter replacement

      The element to be added

    function testElement

    testElement: (options: TestElementOpts, node: AnyNode) => boolean;
    • Checks whether a node matches the description in options.

      Legacy Query Functions

      Parameter options

      An object describing nodes to look for.

      Parameter node

      The element to test.

      Returns

      Whether the element matches the description in options.

    function textContent

    textContent: (node: AnyNode | AnyNode[]) => string;

    function uniqueSort

    uniqueSort: <T extends AnyNode>(nodes: T[]) => T[];
    • Sort an array of nodes based on their relative position in the document, removing any duplicate nodes. If the array contains nodes that do not belong to the same document, sort order is unspecified.

      Helpers

      Parameter nodes

      Array of DOM nodes.

      Returns

      Collection of unique nodes, sorted in document order.

    Interfaces

    interface Feed

    interface Feed {}
    • The root of a feed.

      Feeds

    property author

    author?: string;

      property description

      description?: string;

        property id

        id?: string;

          property items

          items: FeedItem[];
            link?: string;

              property title

              title?: string;

                property type

                type: string;

                  property updated

                  updated?: Date;

                    interface FeedItem

                    interface FeedItem {}
                    • An entry of a feed.

                      Feeds

                    property description

                    description?: string;

                      property id

                      id?: string;
                        link?: string;

                          property media

                          media: FeedItemMedia[];

                            property pubDate

                            pubDate?: Date;

                              property title

                              title?: string;

                                interface FeedItemMedia

                                interface FeedItemMedia {}
                                • A media item of a feed entry.

                                  Feeds

                                property bitrate

                                bitrate?: number;

                                  property channels

                                  channels?: number;

                                    property duration

                                    duration?: number;

                                      property expression

                                      expression?: FeedItemMediaExpression;

                                        property fileSize

                                        fileSize?: number;

                                          property framerate

                                          framerate?: number;

                                            property height

                                            height?: number;

                                              property isDefault

                                              isDefault: boolean;

                                                property lang

                                                lang?: string;

                                                  property medium

                                                  medium: FeedItemMediaMedium | undefined;

                                                    property samplingrate

                                                    samplingrate?: number;

                                                      property type

                                                      type?: string;

                                                        property url

                                                        url?: string;

                                                          property width

                                                          width?: number;

                                                            interface TestElementOpts

                                                            interface TestElementOpts {}
                                                            • An object with keys to check elements against. If a key is tag_name, tag_type or tag_contains, it will check the value against that specific value. Otherwise, it will check an attribute with the key's name.

                                                              Legacy Query Functions

                                                            property tag_contains

                                                            tag_contains?: string | ((data?: string) => boolean);

                                                              property tag_name

                                                              tag_name?: string | ((name: string) => boolean);

                                                                property tag_type

                                                                tag_type?: string | ((name: string) => boolean);

                                                                  index signature

                                                                  [attributeName: string]:
                                                                  | undefined
                                                                  | string
                                                                  | ((attributeValue: string) => boolean);

                                                                    Enums

                                                                    enum DocumentPosition

                                                                    const enum DocumentPosition {
                                                                    DISCONNECTED = 1,
                                                                    PRECEDING = 2,
                                                                    FOLLOWING = 4,
                                                                    CONTAINS = 8,
                                                                    CONTAINED_BY = 16,
                                                                    }

                                                                    member CONTAINED_BY

                                                                    CONTAINED_BY = 16

                                                                      member CONTAINS

                                                                      CONTAINS = 8

                                                                        member DISCONNECTED

                                                                        DISCONNECTED = 1

                                                                          member FOLLOWING

                                                                          FOLLOWING = 4

                                                                            member PRECEDING

                                                                            PRECEDING = 2

                                                                              Type Aliases

                                                                              type FeedItemMediaExpression

                                                                              type FeedItemMediaExpression = 'sample' | 'full' | 'nonstop';
                                                                              • The type of a media item.

                                                                                Feeds

                                                                              type FeedItemMediaMedium

                                                                              type FeedItemMediaMedium = 'image' | 'audio' | 'video' | 'document' | 'executable';
                                                                              • The medium of a media item.

                                                                                Feeds

                                                                              Package Files (8)

                                                                              Dependencies (3)

                                                                              Dev Dependencies (13)

                                                                              Peer Dependencies (0)

                                                                              No peer dependencies.

                                                                              Badge

                                                                              To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

                                                                              You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/domutils.

                                                                              • Markdown
                                                                                [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/domutils)
                                                                              • HTML
                                                                                <a href="https://www.jsdocs.io/package/domutils"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>