domutils

  • Version 2.8.0
  • Published
  • 56.6 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: any, next: any) => void;
  • Append an element after another.

    Parameter elem

    The element to append after.

    Parameter next

    The element be added.

function appendChild

appendChild: (elem: any, child: any) => void;
  • Append a child to an element.

    Parameter elem

    The element to append to.

    Parameter child

    The element to be added as a child.

function compareDocumentPosition

compareDocumentPosition: (nodeA: any, nodeB: any) => number;
  • Compare the position of one node against another node in any other document. The return value is a bitmask with the following values:

    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

    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: any) => boolean, nodes: Node[]) => boolean;
  • 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 a test.

function filter

filter: (
test: (elem: any) => boolean,
node: Node | Node[],
recurse?: boolean,
limit?: number
) => Node[];
  • Search a node and its children for nodes passing a test function.

    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: any) => boolean,
nodes: Node[],
recurse: boolean,
limit: number
) => Node[];
  • Search an array of node and its children for nodes passing a test function.

    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: any) => boolean, nodes: Node[]) => Element[];
  • Search and array of nodes and its children for nodes passing a test function.

    Same as find, only with less options, leading to reduced complexity.

    Parameter test

    Function to test nodes on.

    Parameter nodes

    Array of nodes to search.

    Returns

    All nodes passing test.

function findOne

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

    Parameter test

    Function to test nodes on.

    Parameter nodes

    Array of nodes to search.

    Parameter recurse

    Also consider child nodes.

    Returns

    The first child node that passes test.

function findOneChild

findOneChild: (test: (elem: any) => boolean, nodes: Node[]) => Node | undefined;
  • Finds the first element inside of an array that matches a test function.

    Parameter test

    Function to test nodes on.

    Parameter nodes

    Array of nodes to search.

    Returns

    The first node in the array that passes test.

function getAttributeValue

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

    Parameter elem

    Element to check.

    Parameter name

    Attribute name to retrieve.

    Returns

    The element's attribute value, or undefined.

function getChildren

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

    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: Node | Node[],
recurse?: boolean
) => Element | null;
  • 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: Node | Node[],
recurse: boolean,
limit?: number
) => Node[];
  • 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: Node | Node[],
recurse?: boolean,
limit?: number
) => Element[];
  • 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: Node | Node[],
recurse?: boolean,
limit?: number
) => Node[];
  • 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: Node[]) => Feed | null;
  • Get the feed object from the root of a DOM tree.

    Parameter doc

    The DOM to to extract the feed from.

    Returns

    The feed.

function getInnerHTML

getInnerHTML: (node: any, options?: any) => string;
  • 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: any) => string;
  • Get the tag name of an element.

    Parameter elem

    The element to get the name for.

    Returns

    The tag name of elem.

function getOuterHTML

getOuterHTML: (node: Node | Node[], options?: any) => string;
  • 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: any): Element | null; (elem: any): any };

    function getSiblings

    getSiblings: (elem: any) => Node[];
    • 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.

      Parameter elem

      Element to get the siblings of.

      Returns

      elem's siblings.

    function getText

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

      Parameter node

      Node to get the inner text of.

      Returns

      node's inner text.

      Deprecated

      Use textContent instead.

    function hasAttrib

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

      Parameter elem

      Element to check.

      Parameter name

      Attribute name to look for.

      Returns

      Returns whether elem has the attribute name.

    function innerText

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

    function nextElementSibling

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

      Parameter elem

      The element to get the next sibling of.

      Returns

      elem's next sibling that is a tag.

    function prepend

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

      Parameter elem

      The element to prepend before.

      Parameter prev

      The element be added.

    function prependChild

    prependChild: (elem: any, child: any) => void;
    • Prepend a child to an element.

      Parameter elem

      The element to prepend before.

      Parameter child

      The element to be added as a child.

    function prevElementSibling

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

      Parameter elem

      The element to get the previous sibling of.

      Returns

      elem's previous sibling that is a tag.

    function removeElement

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

      Parameter elem

      The element to be removed

    function removeSubsets

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

      Parameter nodes

      Nodes to filter.

      Returns

      Remaining nodes that aren't subtrees of each other.

    function replaceElement

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

      Parameter elem

      The element to be replaced

      Parameter replacement

      The element to be added

    function testElement

    testElement: (options: TestElementOpts, node: any) => boolean;
    • 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: Node | Node[]) => string;

    function uniqueSort

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

      Parameter nodes

      Array of DOM nodes.

      Returns

      Collection of unique nodes, sorted in document order.

    Interfaces

    interface Feed

    interface Feed {}

      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 {}

                        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 {}

                                      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;

                                                                  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';

                                                                                type FeedItemMediaMedium

                                                                                type FeedItemMediaMedium = 'image' | 'audio' | 'video' | 'document' | 'executable';

                                                                                  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>