@types/parse5

  • Version 6.0.0
  • Published
  • 22.5 kB
  • No dependencies
  • MIT license

Install

npm i @types/parse5
yarn add @types/parse5
pnpm add @types/parse5

Overview

TypeScript definitions for parse5

Index

Functions

Interfaces

Type Aliases

Functions

function parse

parse: <T extends TreeAdapter = TypedTreeAdapter<TreeAdapterTypeMap>>(
html: string,
options?: ParserOptions<T>
) => T extends TypedTreeAdapter<infer TMap> ? TMap['document'] : Document;
  • Parses an HTML string.

    Parameter html

    Input HTML string.

    Parameter options

    Parsing options.

    Example 1

    const parse5 = require('parse5');
    const document = parse5.parse('<!DOCTYPE html><html><head></head><body>Hi there!</body></html>');
    console.log(document.childNodes[1].tagName); //> 'html'

function parseFragment

parseFragment: {
<T extends TreeAdapter = TypedTreeAdapter<TreeAdapterTypeMap>>(
html: string,
options?: ParserOptions<T>
): T extends TypedTreeAdapter<infer TMap>
? TMap['documentFragment']
: DocumentFragment;
<T extends TreeAdapter = TypedTreeAdapter<TreeAdapterTypeMap>>(
fragmentContext: Element,
html: string,
options?: ParserOptions<T>
): T extends TypedTreeAdapter<infer TMap>
? TMap['documentFragment']
: DocumentFragment;
};
  • Parses an HTML fragment.

    Parameter fragmentContext

    Parsing context element. If specified, given fragment will be parsed as if it was set to the context element's innerHTML property.

    Parameter html

    Input HTML fragment string.

    Parameter options

    Parsing options.

    Example 1

    const parse5 = require('parse5');
    const documentFragment = parse5.parseFragment('<table></table>');
    console.log(documentFragment.childNodes[0].tagName); //> 'table'
    // Parses the html fragment in the context of the parsed <table> element.
    const trFragment = parser.parseFragment(documentFragment.childNodes[0], '<tr><td>Shake it, baby</td></tr>');
    console.log(trFragment.childNodes[0].childNodes[0].tagName); //> 'td'

function serialize

serialize: <T extends TreeAdapter = TypedTreeAdapter<TreeAdapterTypeMap>>(
node: T extends TypedTreeAdapter<infer TMap> ? TMap['node'] : Node,
options?: SerializerOptions<T>
) => string;
  • Serializes an AST node to an HTML string.

    Parameter node

    Node to serialize.

    Parameter options

    Serialization options.

    Example 1

    const parse5 = require('parse5');
    const document = parse5.parse('<!DOCTYPE html><html><head></head><body>Hi there!</body></html>');
    // Serializes a document.
    const html = parse5.serialize(document);
    // Serializes the <html> element content.
    const str = parse5.serialize(document.childNodes[1]);
    console.log(str); //> '<head></head><body>Hi there!</body>'

Interfaces

interface Attribute

interface Attribute {}
  • Element attribute.

property name

name: string;
  • The name of the attribute.

property namespace

namespace?: string;
  • The namespace of the attribute.

property prefix

prefix?: string;
  • The namespace-related prefix of the attribute.

property value

value: string;
  • The value of the attribute.

interface AttributesLocation

interface AttributesLocation {}

    index signature

    [attributeName: string]: Location;

      interface CommentNode

      interface CommentNode {}
      • Default tree adapter CommentNode interface.

      property data

      data: string;
      • Comment text.

      property nodeName

      nodeName: '#comment';
      • The name of the node.

      property parentNode

      parentNode: ParentNode;
      • Parent node.

      property sourceCodeLocation

      sourceCodeLocation?: Location;
      • Comment source code location info. Available if location info is enabled via ParserOptions.

      interface Document

      interface Document {}
      • Default tree adapter Document interface.

      property childNodes

      childNodes: ChildNode[];
      • Child nodes.

      property mode

      mode: DocumentMode;
      • [Document mode](https://dom.spec.whatwg.org/#concept-document-limited-quirks).

      property nodeName

      nodeName: '#document';
      • The name of the node.

      interface DocumentFragment

      interface DocumentFragment {}
      • Default tree adapter DocumentFragment interface.

      property childNodes

      childNodes: ChildNode[];
      • Child nodes.

      property nodeName

      nodeName: '#document-fragment';
      • The name of the node.

      interface DocumentType

      interface DocumentType {}
      • Default tree adapter DocumentType interface.

      property name

      name: string;
      • Document type name.

      property nodeName

      nodeName: '#documentType';
      • The name of the node.

      property publicId

      publicId: string;
      • Document type public identifier.

      property systemId

      systemId: string;
      • Document type system identifier.

      interface Element

      interface Element {}
      • Default tree adapter Element interface.

      property attrs

      attrs: Attribute[];
      • List of element attributes.

      property childNodes

      childNodes: ChildNode[];
      • Child nodes.

      property namespaceURI

      namespaceURI: string;
      • Element namespace.

      property nodeName

      nodeName: string;
      • The name of the node. Equals to element tagName.

      property parentNode

      parentNode: ParentNode;
      • Parent node.

      property sourceCodeLocation

      sourceCodeLocation?: ElementLocation;
      • Element source code location info. Available if location info is enabled via ParserOptions.

      property tagName

      tagName: string;
      • Element tag name.

      interface ElementLocation

      interface ElementLocation extends StartTagLocation {}

        property endTag

        endTag: Location;
        • Element's end tag location info.

        property startTag

        startTag: StartTagLocation;
        • Element's start tag location info.

        interface Location

        interface Location {}

          property endCol

          endCol: number;
          • One-based column index of the last character

          property endLine

          endLine: number;
          • One-based line index of the last character

          property endOffset

          endOffset: number;
          • Zero-based last character index

          property startCol

          startCol: number;
          • One-based column index of the first character

          property startLine

          startLine: number;
          • One-based line index of the first character

          property startOffset

          startOffset: number;
          • Zero-based first character index

          interface ParserOptions

          interface ParserOptions<T extends TreeAdapter = TreeAdapter> {}

            property scriptingEnabled

            scriptingEnabled?: boolean;
            • The [scripting flag](https://html.spec.whatwg.org/multipage/parsing.html#scripting-flag). If set to true, noscript element content will be parsed as text.

              **Default:** true

            property sourceCodeLocationInfo

            sourceCodeLocationInfo?: boolean;
            • Enables source code location information. When enabled, each node (except the root node) will have a sourceCodeLocation property. If the node is not an empty element, sourceCodeLocation will be a ElementLocation object, otherwise it will be Location. If the element was implicitly created by the parser (as part of [tree correction](https://html.spec.whatwg.org/multipage/syntax.html#an-introduction-to-error-handling-and-strange-cases-in-the-parser)), its sourceCodeLocation property will be undefined.

              **Default:** false

            property treeAdapter

            treeAdapter?: T;
            • Specifies the resulting tree format.

              **Default:** treeAdapters.default

            interface SerializerOptions

            interface SerializerOptions<T extends TreeAdapter = TreeAdapter> {}

              property treeAdapter

              treeAdapter?: T;
              • * Specifies input tree format.

                **Default:** treeAdapters.default

              interface StartTagLocation

              interface StartTagLocation extends Location {}

                property attrs

                attrs: AttributesLocation;
                • Start tag attributes' location info

                interface TextNode

                interface TextNode {}
                • Default tree adapter TextNode interface.

                property nodeName

                nodeName: '#text';
                • The name of the node.

                property parentNode

                parentNode: ParentNode;
                • Parent node.

                property sourceCodeLocation

                sourceCodeLocation?: Location;
                • Text node source code location info. Available if location info is enabled via ParserOptions.

                property value

                value: string;
                • Text content.

                interface TreeAdapter

                interface TreeAdapter {}

                  method adoptAttributes

                  adoptAttributes: (recipient: unknown, attrs: unknown[]) => void;

                    method appendChild

                    appendChild: (parentNode: unknown, newNode: unknown) => void;

                      method createCommentNode

                      createCommentNode: (data: string) => unknown;

                        method createDocument

                        createDocument: () => unknown;

                          method createDocumentFragment

                          createDocumentFragment: () => unknown;

                            method createElement

                            createElement: (
                            tagName: string,
                            namespaceURI: string,
                            attrs: unknown[]
                            ) => unknown;

                              method detachNode

                              detachNode: (node: unknown) => void;

                                method getAttrList

                                getAttrList: (element: unknown) => unknown[];

                                  method getChildNodes

                                  getChildNodes: (node: unknown) => unknown[];

                                    method getCommentNodeContent

                                    getCommentNodeContent: (commentNode: unknown) => string;

                                      method getDocumentMode

                                      getDocumentMode: (document: unknown) => unknown;

                                        method getDocumentTypeNodeName

                                        getDocumentTypeNodeName: (doctypeNode: unknown) => string;

                                          method getDocumentTypeNodePublicId

                                          getDocumentTypeNodePublicId: (doctypeNode: unknown) => string;

                                            method getDocumentTypeNodeSystemId

                                            getDocumentTypeNodeSystemId: (doctypeNode: unknown) => string;

                                              method getFirstChild

                                              getFirstChild: (node: unknown) => unknown;

                                                method getNamespaceURI

                                                getNamespaceURI: (element: unknown) => string;

                                                  method getNodeSourceCodeLocation

                                                  getNodeSourceCodeLocation: (
                                                  node: unknown
                                                  ) => Location | StartTagLocation | ElementLocation;

                                                    method getParentNode

                                                    getParentNode: (node: unknown) => unknown;

                                                      method getTagName

                                                      getTagName: (element: unknown) => string;

                                                        method getTemplateContent

                                                        getTemplateContent: (templateElement: unknown) => unknown;

                                                          method getTextNodeContent

                                                          getTextNodeContent: (textNode: unknown) => string;

                                                            method insertBefore

                                                            insertBefore: (
                                                            parentNode: unknown,
                                                            newNode: unknown,
                                                            referenceNode: unknown
                                                            ) => void;

                                                              method insertText

                                                              insertText: (parentNode: unknown, text: string) => void;

                                                                method insertTextBefore

                                                                insertTextBefore: (
                                                                parentNode: unknown,
                                                                text: string,
                                                                referenceNode: unknown
                                                                ) => void;

                                                                  method isCommentNode

                                                                  isCommentNode: (node: unknown) => boolean;

                                                                    method isDocumentTypeNode

                                                                    isDocumentTypeNode: (node: unknown) => boolean;

                                                                      method isElementNode

                                                                      isElementNode: (node: unknown) => boolean;

                                                                        method isTextNode

                                                                        isTextNode: (node: unknown) => boolean;

                                                                          method setDocumentMode

                                                                          setDocumentMode: (document: unknown, mode: DocumentMode) => void;

                                                                            method setDocumentType

                                                                            setDocumentType: (
                                                                            document: unknown,
                                                                            name: string,
                                                                            publicId: string,
                                                                            systemId: string
                                                                            ) => void;

                                                                              method setNodeSourceCodeLocation

                                                                              setNodeSourceCodeLocation: (
                                                                              node: unknown,
                                                                              location: Location | StartTagLocation | ElementLocation
                                                                              ) => void;

                                                                                method setTemplateContent

                                                                                setTemplateContent: (templateElement: unknown, contentElement: unknown) => void;

                                                                                  interface TreeAdapterTypeMap

                                                                                  interface TreeAdapterTypeMap {}

                                                                                    property attribute

                                                                                    attribute: unknown;

                                                                                      property childNode

                                                                                      childNode: unknown;

                                                                                        property commentNode

                                                                                        commentNode: unknown;

                                                                                          property document

                                                                                          document: unknown;

                                                                                            property documentFragment

                                                                                            documentFragment: unknown;

                                                                                              property documentType

                                                                                              documentType: unknown;

                                                                                                property element

                                                                                                element: unknown;

                                                                                                  property node

                                                                                                  node: unknown;

                                                                                                    property parentNode

                                                                                                    parentNode: unknown;

                                                                                                      property textNode

                                                                                                      textNode: unknown;

                                                                                                        interface TypedTreeAdapter

                                                                                                        interface TypedTreeAdapter<T extends TreeAdapterTypeMap> extends TreeAdapter {}
                                                                                                        • 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.

                                                                                                          See Also

                                                                                                          • [default implementation](https://github.com/inikulin/parse5/blob/master/packages/parse5/lib/tree-adapters/default.js)

                                                                                                        method adoptAttributes

                                                                                                        adoptAttributes: (recipient: T['element'], attrs: Array<T['attribute']>) => void;
                                                                                                        • Copies attributes to the given element. Only attributes that are not yet present in the element are copied.

                                                                                                          Parameter recipient

                                                                                                          Element to copy attributes into.

                                                                                                          Parameter attrs

                                                                                                          Attributes to copy.

                                                                                                        method appendChild

                                                                                                        appendChild: (parentNode: T['parentNode'], newNode: T['node']) => void;
                                                                                                        • Appends a child node to the given parent node.

                                                                                                          Parameter parentNode

                                                                                                          Parent node.

                                                                                                          Parameter newNode

                                                                                                          Child node.

                                                                                                        method createCommentNode

                                                                                                        createCommentNode: (data: string) => T['commentNode'];
                                                                                                        • Creates a comment node.

                                                                                                          Parameter data

                                                                                                          Comment text.

                                                                                                        method createDocument

                                                                                                        createDocument: () => T['document'];
                                                                                                        • Creates a document node.

                                                                                                        method createDocumentFragment

                                                                                                        createDocumentFragment: () => T['documentFragment'];
                                                                                                        • Creates a document fragment node.

                                                                                                        method createElement

                                                                                                        createElement: (
                                                                                                        tagName: string,
                                                                                                        namespaceURI: string,
                                                                                                        attrs: Array<T['attribute']>
                                                                                                        ) => T['element'];
                                                                                                        • Creates an element node.

                                                                                                          Parameter tagName

                                                                                                          Tag name of the element.

                                                                                                          Parameter namespaceURI

                                                                                                          Namespace of the element.

                                                                                                          Parameter attrs

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

                                                                                                        method detachNode

                                                                                                        detachNode: (node: T['node']) => void;
                                                                                                        • Removes a node from its parent.

                                                                                                          Parameter node

                                                                                                          Node to remove.

                                                                                                        method getAttrList

                                                                                                        getAttrList: (element: T['element']) => Array<T['attribute']>;
                                                                                                        • 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.

                                                                                                          Parameter element

                                                                                                          Element.

                                                                                                        method getChildNodes

                                                                                                        getChildNodes: (node: T['parentNode']) => Array<T['childNode']>;
                                                                                                        • Returns the given node's children in an array.

                                                                                                          Parameter node

                                                                                                          Node.

                                                                                                        method getCommentNodeContent

                                                                                                        getCommentNodeContent: (commentNode: T['commentNode']) => string;
                                                                                                        • Returns the given comment node's content.

                                                                                                          Parameter commentNode

                                                                                                          Comment node.

                                                                                                        method getDocumentMode

                                                                                                        getDocumentMode: (document: T['document']) => DocumentMode;
                                                                                                        • Returns [document mode](https://dom.spec.whatwg.org/#concept-document-limited-quirks).

                                                                                                          Parameter document

                                                                                                          Document node.

                                                                                                        method getDocumentTypeNodeName

                                                                                                        getDocumentTypeNodeName: (doctypeNode: T['documentType']) => string;
                                                                                                        • Returns the given document type node's name.

                                                                                                          Parameter doctypeNode

                                                                                                          Document type node.

                                                                                                        method getDocumentTypeNodePublicId

                                                                                                        getDocumentTypeNodePublicId: (doctypeNode: T['documentType']) => string;
                                                                                                        • Returns the given document type node's public identifier.

                                                                                                          Parameter doctypeNode

                                                                                                          Document type node.

                                                                                                        method getDocumentTypeNodeSystemId

                                                                                                        getDocumentTypeNodeSystemId: (doctypeNode: T['documentType']) => string;
                                                                                                        • Returns the given document type node's system identifier.

                                                                                                          Parameter doctypeNode

                                                                                                          Document type node.

                                                                                                        method getFirstChild

                                                                                                        getFirstChild: (node: T['parentNode']) => T['childNode'] | undefined;
                                                                                                        • Returns the first child of the given node.

                                                                                                          Parameter node

                                                                                                          Node.

                                                                                                        method getNamespaceURI

                                                                                                        getNamespaceURI: (element: T['element']) => string;
                                                                                                        • Returns the given element's namespace.

                                                                                                          Parameter element

                                                                                                          Element.

                                                                                                        method getNodeSourceCodeLocation

                                                                                                        getNodeSourceCodeLocation: (
                                                                                                        node: T['node']
                                                                                                        ) => Location | StartTagLocation | ElementLocation;
                                                                                                        • Returns the given node's source code location information.

                                                                                                          Parameter node

                                                                                                          Node.

                                                                                                        method getParentNode

                                                                                                        getParentNode: (node: T['childNode']) => T['parentNode'];
                                                                                                        • Returns the given node's parent.

                                                                                                          Parameter node

                                                                                                          Node.

                                                                                                        method getTagName

                                                                                                        getTagName: (element: T['element']) => string;
                                                                                                        • Returns the given element's tag name.

                                                                                                          Parameter element

                                                                                                          Element.

                                                                                                        method getTemplateContent

                                                                                                        getTemplateContent: (templateElement: T['element']) => T['documentFragment'];
                                                                                                        • Returns the <template> element content element.

                                                                                                          Parameter templateElement

                                                                                                          <template> element.

                                                                                                        method getTextNodeContent

                                                                                                        getTextNodeContent: (textNode: T['textNode']) => string;
                                                                                                        • Returns the given text node's content.

                                                                                                          Parameter textNode

                                                                                                          Text node.

                                                                                                        method insertBefore

                                                                                                        insertBefore: (
                                                                                                        parentNode: T['parentNode'],
                                                                                                        newNode: T['node'],
                                                                                                        referenceNode: T['node']
                                                                                                        ) => void;
                                                                                                        • Inserts a child node to the given parent node before the given reference node.

                                                                                                          Parameter parentNode

                                                                                                          Parent node.

                                                                                                          Parameter newNode

                                                                                                          Child node.

                                                                                                          Parameter referenceNode

                                                                                                          Reference node.

                                                                                                        method insertText

                                                                                                        insertText: (parentNode: T['parentNode'], text: string) => 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.

                                                                                                          Parameter parentNode

                                                                                                          Node to insert text into.

                                                                                                          Parameter text

                                                                                                          Text to insert.

                                                                                                        method insertTextBefore

                                                                                                        insertTextBefore: (
                                                                                                        parentNode: T['parentNode'],
                                                                                                        text: string,
                                                                                                        referenceNode: T['node']
                                                                                                        ) => 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.

                                                                                                          Parameter parentNode

                                                                                                          Node to insert text into.

                                                                                                          Parameter text

                                                                                                          Text to insert.

                                                                                                          Parameter referenceNode

                                                                                                          Node to insert text before.

                                                                                                        method isCommentNode

                                                                                                        isCommentNode: (node: T['node']) => node is T['commentNode'];
                                                                                                        • Determines if the given node is a comment node.

                                                                                                          Parameter node

                                                                                                          Node.

                                                                                                        method isDocumentTypeNode

                                                                                                        isDocumentTypeNode: (node: T['node']) => node is T['documentType'];
                                                                                                        • Determines if the given node is a document type node.

                                                                                                          Parameter node

                                                                                                          Node.

                                                                                                        method isElementNode

                                                                                                        isElementNode: (node: T['node']) => node is T['element'];
                                                                                                        • Determines if the given node is an element.

                                                                                                          Parameter node

                                                                                                          Node.

                                                                                                        method isTextNode

                                                                                                        isTextNode: (node: T['node']) => node is T['textNode'];
                                                                                                        • Determines if the given node is a text node.

                                                                                                          Parameter node

                                                                                                          Node.

                                                                                                        method setDocumentMode

                                                                                                        setDocumentMode: (document: T['document'], mode: DocumentMode) => void;
                                                                                                        • Sets the [document mode](https://dom.spec.whatwg.org/#concept-document-limited-quirks).

                                                                                                          Parameter document

                                                                                                          Document node.

                                                                                                          Parameter mode

                                                                                                          Document mode.

                                                                                                        method setDocumentType

                                                                                                        setDocumentType: (
                                                                                                        document: T['document'],
                                                                                                        name: string,
                                                                                                        publicId: string,
                                                                                                        systemId: string
                                                                                                        ) => void;
                                                                                                        • 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.

                                                                                                          Parameter document

                                                                                                          Document node.

                                                                                                          Parameter name

                                                                                                          Document type name.

                                                                                                          Parameter publicId

                                                                                                          Document type public identifier.

                                                                                                          Parameter systemId

                                                                                                          Document type system identifier.

                                                                                                        method setNodeSourceCodeLocation

                                                                                                        setNodeSourceCodeLocation: (
                                                                                                        node: T['node'],
                                                                                                        location: Location | StartTagLocation | ElementLocation
                                                                                                        ) => void;
                                                                                                        • Attaches source code location information to the node.

                                                                                                          Parameter node

                                                                                                          Node.

                                                                                                        method setTemplateContent

                                                                                                        setTemplateContent: (
                                                                                                        templateElement: T['element'],
                                                                                                        contentElement: T['documentFragment']
                                                                                                        ) => void;
                                                                                                        • Sets the <template> element content element.

                                                                                                          Parameter templateElement

                                                                                                          <template> element.

                                                                                                          Parameter contentElement

                                                                                                          Content element.

                                                                                                        Type Aliases

                                                                                                        type ChildNode

                                                                                                        type ChildNode = TextNode | Element | CommentNode;
                                                                                                        • Default tree adapter ChildNode type.

                                                                                                        type DocumentMode

                                                                                                        type DocumentMode = 'no-quirks' | 'quirks' | 'limited-quirks';
                                                                                                        • [Document mode](https://dom.spec.whatwg.org/#concept-document-limited-quirks).

                                                                                                        type Node

                                                                                                        type Node =
                                                                                                        | CommentNode
                                                                                                        | Document
                                                                                                        | DocumentFragment
                                                                                                        | DocumentType
                                                                                                        | Element
                                                                                                        | TextNode;
                                                                                                        • Default tree adapter Node interface.

                                                                                                        type ParentNode

                                                                                                        type ParentNode = Document | DocumentFragment | Element;
                                                                                                        • Default tree adapter ParentNode type.

                                                                                                        Package Files (1)

                                                                                                        Dependencies (0)

                                                                                                        No dependencies.

                                                                                                        Dev Dependencies (0)

                                                                                                        No dev dependencies.

                                                                                                        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/@types/parse5.

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