index.d.ts 35.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
/// <reference types="node" />

import * as stream from "stream";
import * as events from "events";


// Markup data
//-----------------------------------------------------------------------------------
declare namespace MarkupData {
    interface Location {
        /**
         * One-based line index
         */
        line: number;
        /**
         * One-based column index
         */
        col: number;
        /**
         * Zero-based first character index
         */
        startOffset: number;
        /**
         * Zero-based last character index
         */
        endOffset: number;
    }

    interface AttributesLocation {
        [attributeName: string]: Location;
    }

    interface StartTagLocation extends Location {
        /**
         * Start tag attributes' location info
         */
        attrs: AttributesLocation
    }

    interface ElementLocation extends StartTagLocation {
        /**
         * Element's start tag location info.
         */
        startTag: StartTagLocation;
        /**
         * Element's end tag location info.
         */
        endTag: Location;
    }
}

// Options
//-----------------------------------------------------------------------------------
declare namespace Options {
    export interface ParserOptions {
        /**
         * Enables source code location information. When enabled, each node (except the root node) will have a `__location` property.
         * If the node is not an empty element, `__location` will be a {@link MarkupData.ElementLocation} object, otherwise it will be {@link MarkupData.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 `__location` property will be `undefined`.
         *
         * **Default:** `false`
         */
        locationInfo?: boolean;
        /**
         * Specifies the resulting tree format.
         *
         * **Default:** `treeAdapters.default`
         */
        treeAdapter?: AST.TreeAdapter;
    }

    export interface SAXParserOptions {
        /**
         * Enables source code location information for the tokens.
         * When enabled, each token event handler will receive {@link MarkupData.Location} (or {@link MarkupData.StartTagLocation})
         * object as its last argument.
         */
        locationInfo?: boolean;
    }

    export interface SerializerOptions {
        /***
         * Specifies input tree format.
         *
         * **Default:** `treeAdapters.default`
         */
        treeAdapter?: AST.TreeAdapter;
    }
}


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

    // Default tree adapter
    namespace Default {
        /**
         * Element attribute.
         */
        interface Attribute {
            /**
             * The name of the attribute.
             */
            name: string;
            /**
             * The value of the attribute.
             */
            value: string;
            /**
             * The namespace of the attribute.
             */
            namespace?: string;
            /**
             * The namespace-related prefix of the attribute.
             */
            prefix?: string;
        }

        /**
         * [Default tree adapter]{@link parse5.treeAdapters} Node interface.
         */
        interface Node {
            /**
             * The name of the node. E.g. {@link Document} will have `nodeName` equal to '#document'`.
             */
            nodeName: string;
        }

        /**
         * [Default tree adapter]{@link parse5.treeAdapters} ParentNode interface.
         */
        interface ParentNode {
            /**
             * Child nodes.
             */
            childNodes: Node[];
        }

        /**
         * [Default tree adapter]{@link parse5.treeAdapters} DocumentType interface.
         */
        export interface DocumentType extends Node {
            /**
             * The name of the node.
             */
            nodeName: '#documentType';
            /**
             * Document type name.
             */
            name: string;
            /**
             * Document type public identifier.
             */
            publicId: string;
            /**
             * Document type system identifier.
             */
            systemId: string;
        }

        /**
         * [Default tree adapter]{@link parse5.treeAdapters} Document interface.
         */
        export interface Document extends ParentNode {
            /**
             * The name of the node.
             */
            nodeName: '#document';
            /**
             * [Document mode](https://dom.spec.whatwg.org/#concept-document-limited-quirks).
             */
            mode: DocumentMode;
        }

        /**
         * [Default tree adapter]{@link parse5.treeAdapters} DocumentFragment interface.
         */
        export interface DocumentFragment extends ParentNode {
            /**
             * The name of the node.
             */
            nodeName: '#document-fragment';
        }

        /**
         * [Default tree adapter]{@link parse5.treeAdapters} Element interface.
         */
        export interface Element extends ParentNode {
            /**
             * The name of the node. Equals to element {@link tagName}.
             */
            nodeName: string;
            /**
             * Element tag name.
             */
            tagName: string;
            /**
             * Element namespace.
             */
            namespaceURI: string;
            /**
             * List of element attributes.
             */
            attrs: Attribute[];
            /**
             * Parent node.
             */
            parentNode: ParentNode;
            /**
             * Element source code location info. Available if location info is enabled via {@link Options.ParserOptions}.
             */
            __location?: MarkupData.ElementLocation;
        }

        /**
         * [Default tree adapter]{@link parse5.treeAdapters} CommentNode interface.
         */
        export interface CommentNode extends Node {
            /**
             * The name of the node.
             */
            nodeName: '#comment';
            /**
             * Comment text.
             */
            data: string;
            /**
             * Parent node.
             */
            parentNode: ParentNode;
            /**
             * Comment source code location info. Available if location info is enabled via {@link Options.ParserOptions}.
             */
            __location?: MarkupData.Location;
        }

        /**
         * [Default tree adapter]{@link parse5.treeAdapters} TextNode interface.
         */
        export interface TextNode extends Node {
            /**
             * The name of the node.
             */
            nodeName: '#text';
            /**
             * Text content.
             */
            value: string;
            /**
             * Parent node.
             */
            parentNode: ParentNode;
            /**
             * Text node source code location info. Available if location info is enabled via {@link Options.ParserOptions}.
             */
            __location?: MarkupData.Location;
        }
    }


    // htmlparser2 tree adapter
    namespace HtmlParser2 {
        /**
         * [htmlparser2 tree adapter]{@link parse5.treeAdapters} Node interface.
         */
        interface Node {
            /**
             * The type of the node. E.g. {@link Document} will have `type` equal to 'root'`.
             */
            type: string;
            /**
             * [DOM spec](https://dom.spec.whatwg.org/#dom-node-nodetype)-compatible node {@link type}.
             */
            nodeType: number;
            /**
             * Parent node.
             */
            parent: ParentNode;
            /**
             * Same as {@link parent}. [DOM spec](https://dom.spec.whatwg.org)-compatible alias.
             */
            parentNode: ParentNode;
            /**
             * Previous sibling.
             */
            prev: Node;
            /**
             * Same as {@link prev}. [DOM spec](https://dom.spec.whatwg.org)-compatible alias.
             */
            previousSibling: Node;
            /**
             * Next sibling.
             */
            next: Node;
            /**
             * Same as {@link next}. [DOM spec](https://dom.spec.whatwg.org)-compatible alias.
             */
            nextSibling: Node;
        }

        /**
         * [htmlparser2 tree adapter]{@link parse5.treeAdapters} ParentNode interface.
         */
        interface ParentNode extends Node {
            /**
             * Child nodes.
             */
            children: Node[];
            /**
             * Same as {@link children}. [DOM spec](https://dom.spec.whatwg.org)-compatible alias.
             */
            childNodes: Node[];
            /**
             * First child of the node.
             */
            firstChild: Node;
            /**
             * Last child of the node.
             */
            lastChild: Node;
        }

        /**
         * [htmlparser2 tree adapter]{@link parse5.treeAdapters} DocumentType interface.
         */
        export interface DocumentType extends Node {
            /**
             * The type of the node.
             */
            type: 'directive';
            /**
             * Node name.
             */
            name: '!doctype';
            /**
             * Serialized doctype {@link name}, {@link publicId} and {@link systemId}.
             */
            data: string;
            /**
             * Document type name.
             */
            'x-name':string;
            /**
             * Document type public identifier.
             */
            'x-publicId': string;
            /**
             * Document type system identifier.
             */
            'x-systemId': string;
        }

        /**
         * [htmlparser2 tree adapter]{@link parse5.treeAdapters} Document interface.
         */
        export interface Document extends ParentNode {
            /**
             * The type of the node.
             */
            type: 'root';
            /**
             * The name of the node.
             */
            name: 'root';
            /**
             * [Document mode](https://dom.spec.whatwg.org/#concept-document-limited-quirks).
             */
            'x-mode': DocumentMode;
        }

        /**
         * [htmlparser2 tree adapter]{@link parse5.treeAdapters} DocumentFragment interface.
         */
        export interface DocumentFragment extends ParentNode {
            /**
             * The type of the node.
             */
            type: 'root';
            /**
             * The name of the node.
             */
            name: 'root';
        }

        /**
         * [htmlparser2 tree adapter]{@link parse5.treeAdapters} Element interface.
         */
        export interface Element extends ParentNode {
            /**
             * The name of the node. Equals to element {@link tagName}.
             */
            name: string;
            /**
             * Element tag name.
             */
            tagName: string;
            /**
             * Element namespace.
             */
            namespace: string;
            /**
             * Element attributes.
             */
            attribs: { [name: string]: string };
            /**
             * Element attribute namespaces.
             */
            'x-attribsNamespace': { [name: string]: string };
            /**
             * Element attribute namespace-related prefixes.
             */
            'x-attribsPrefix': { [name: string]: string };
            /**
             * Element source code location info. Available if location info is enabled via {@link Options.ParserOptions}.
             */
            __location?: MarkupData.ElementLocation;
        }

        /**
         * [htmlparser2 tree adapter]{@link parse5.treeAdapters} CommentNode interface.
         */
        export interface CommentNode extends Node {
            /**
             * The name of the node.
             */
            name: 'comment';
            /**
             * Comment text.
             */
            data: string;
            /**
             * Same as {@link data}. [DOM spec](https://dom.spec.whatwg.org)-compatible alias.
             */
            nodeValue: string;
            /**
             * Comment source code location info. Available if location info is enabled via {@link Options.ParserOptions}.
             */
            __location?: MarkupData.Location;
        }

        /**
         * [htmlparser2 tree adapter]{@link parse5.treeAdapters} TextNode interface.
         */
        export interface TextNode extends Node {
            /**
             * The name of the node.
             */
            name: 'text';
            /**
             * Text content.
             */
            data: string;
            /**
             * Same as {@link data}. [DOM spec](https://dom.spec.whatwg.org)-compatible alias.
             */
            nodeValue: string;
            /**
             * Comment source code location info. Available if location info is enabled via {@link Options.ParserOptions}.
             */
            __location?: MarkupData.Location;
        }
    }


    // Unions
    // NOTE: we use `Object` in unions to support custom tree adapter implementations.
    // TypeScript Handbook suggests to always use `any` instead of `Object`, but in that
    // case language service hints `any` as type, instead of actual union name.
    /**
     * Generic Node interface.
     * Cast to the actual AST interface (e.g. {@link parse5.AST.Default.Node}) to get access to the properties.
     */
    type Node = Default.Node | HtmlParser2.Node | Object;
    /**
     * Generic ParentNode interface.
     * Cast to the actual AST interface (e.g. {@link parse5.AST.Default.ParentNode}) to get access to the properties.
     */
    type ParentNode = Default.ParentNode | HtmlParser2.ParentNode | Object;
    /**
     * Generic DocumentType interface.
     * Cast to the actual AST interface (e.g. {@link parse5.AST.Default.DocumentType}) to get access to the properties.
     */
    type DocumentType = Default.DocumentType | HtmlParser2.DocumentType | Object;
    /**
     * Generic Document interface.
     * Cast to the actual AST interface (e.g. {@link parse5.AST.Default.Document}) to get access to the properties.
     */
    type Document = Default.Document | HtmlParser2.Document | Object;
    /**
     * Generic DocumentFragment interface.
     * Cast to the actual AST interface (e.g. {@link parse5.AST.Default.DocumentFragment}) to get access to the properties.
     */
    type DocumentFragment = Default.DocumentFragment | HtmlParser2.DocumentFragment | Object;
    /**
     * Generic Element interface.
     * Cast to the actual AST interface (e.g. {@link parse5.AST.Default.Element}) to get access to the properties.
     */
    type Element = Default.Element | HtmlParser2.Element | Object;
    /**
     * Generic TextNode interface.
     * Cast to the actual AST interface (e.g. {@link parse5.AST.Default.TextNode}) to get access to the properties.
     */
    type TextNode = Default.TextNode | HtmlParser2.TextNode | Object;
    /**
     * Generic CommentNode interface.
     * Cast to the actual AST interface (e.g. {@link parse5.AST.Default.CommentNode}) to get access to the properties.
     */
    type CommentNode = Default.CommentNode | HtmlParser2.CommentNode | Object;


    // Tree adapter interface
    //-----------------------------------------------------------------------------------

    /**
     * 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 [default implementation](https://github.com/inikulin/parse5/blob/master/lib/tree_adapters/default.js)
     */
    export interface TreeAdapter {
        /**
         * Creates a document node.
         */
        createDocument(): AST.Document;
        /**
         * Creates a document fragment node.
         */
        createDocumentFragment(): AST.DocumentFragment;
        /**
         * Creates an element node.
         *
         * @param tagName - Tag name of the element.
         * @param namespaceURI - Namespace of the element.
         * @param attrs - Attribute name-value pair array. Foreign attributes may contain `namespace` and `prefix` fields as well.
         */
        createElement(tagName: string, namespaceURI: string, attrs: AST.Default.Attribute[]): AST.Element;
        /**
         * Creates a comment node.
         *
         * @param data - Comment text.
         */
        createCommentNode(data: string): AST.CommentNode;
        /**
         * Appends a child node to the given parent node.
         *
         * @param parentNode - Parent node.
         * @param newNode -  Child node.
         */
        appendChild(parentNode: AST.ParentNode, newNode: AST.Node): void;
        /**
         * Inserts a child node to the given parent node before the given reference node.
         *
         * @param parentNode - Parent node.
         * @param newNode -  Child node.
         * @param referenceNode -  Reference node.
         */
        insertBefore(parentNode: AST.ParentNode, newNode: AST.Node, referenceNode: AST.Node): void;
        /**
         * Sets the `<template>` element content element.
         *
         * @param templateElement - `<template>` element.
         * @param contentElement -  Content element.
         */
        setTemplateContent(templateElement: AST.Element, contentElement: AST.DocumentFragment): void;
        /**
         * Returns the `<template>` element content element.
         *
         * @param templateElement - `<template>` element.
         */
        getTemplateContent(templateElement: AST.Element): AST.DocumentFragment;
        /**
         * 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`.
         *
         * @param document - Document node.
         * @param name -  Document type name.
         * @param publicId - Document type public identifier.
         * @param systemId - Document type system identifier.
         */
        setDocumentType(document: AST.Document, name: string, publicId: string, systemId: string): void;
        /**
         * Sets the [document mode](https://dom.spec.whatwg.org/#concept-document-limited-quirks).
         *
         * @param document - Document node.
         * @param mode - Document mode.
         */
        setDocumentMode(document: AST.Document, mode: AST.DocumentMode): void;
        /**
         * Returns [document mode](https://dom.spec.whatwg.org/#concept-document-limited-quirks).
         *
         * @param document - Document node.
         */
        getDocumentMode(document: AST.Document): AST.DocumentMode;
        /**
         * Removes a node from its parent.
         *
         * @param node - Node to remove.
         */
        detachNode(node: AST.Node): 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.
         *
         * @param parentNode - Node to insert text into.
         * @param text - Text to insert.
         */
        insertText(parentNode: AST.ParentNode, text: string): 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.
         *
         * @param parentNode - Node to insert text into.
         * @param text - Text to insert.
         * @param referenceNode - Node to insert text before.
         */
        insertTextBefore(parentNode: AST.ParentNode, text: string, referenceNode: AST.Node): void;
        /**
         * Copies attributes to the given element. Only attributes that are not yet present in the element are copied.
         *
         * @param recipient - Element to copy attributes into.
         * @param attrs - Attributes to copy.
         */
        adoptAttributes(recipient: AST.Element, attrs: AST.Default.Attribute[]): void;
        /**
         * Returns the first child of the given node.
         *
         * @param node - Node.
         */
        getFirstChild(node: AST.ParentNode): AST.Node;
        /**
         * Returns the given node's children in an array.
         *
         * @param node - Node.
         */
        getChildNodes(node: AST.ParentNode): AST.Node[];
        /**
         * Returns the given node's parent.
         *
         * @param node - Node.
         */
        getParentNode(node: AST.Node): AST.ParentNode;
        /**
         * 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.
         *
         * @param element - Element.
         */
        getAttrList(element: AST.Element): AST.Default.Attribute[];
        /**
         * Returns the given element's tag name.
         *
         * @param element - Element.
         */
        getTagName(element: AST.Element): string;
        /**
         * Returns the given element's namespace.
         *
         * @param element - Element.
         */
        getNamespaceURI(element: AST.Element): string;
        /**
         * Returns the given text node's content.
         *
         * @param textNode - Text node.
         */
        getTextNodeContent(textNode: AST.TextNode): string;
        /**
         * Returns the given comment node's content.
         *
         * @param commentNode - Comment node.
         */
        getCommentNodeContent(commentNode: AST.CommentNode): string;
        /**
         * Returns the given document type node's name.
         *
         * @param doctypeNode - Document type node.
         */
        getDocumentTypeNodeName(doctypeNode: AST.DocumentType): string;
        /**
         * Returns the given document type node's public identifier.
         *
         * @param doctypeNode - Document type node.
         */
        getDocumentTypeNodePublicId(doctypeNode: AST.DocumentType): string;
        /**
         * Returns the given document type node's system identifier.
         *
         * @param doctypeNode - Document type node.
         */
        getDocumentTypeNodeSystemId(doctypeNode: AST.DocumentType): string;
        /**
         * Determines if the given node is a text node.
         *
         * @param node - Node.
         */
        isTextNode(node: AST.Node): boolean;
        /**
         * Determines if the given node is a comment node.
         *
         * @param node - Node.
         */
        isCommentNode(node: AST.Node): boolean;
        /**
         * Determines if the given node is a document type node.
         *
         * @param node - Node.
         */
        isDocumentTypeNode(node: AST.Node): boolean;
        /**
         * Determines if the given node is an element.
         *
         * @param node - Node.
         */
        isElementNode(node: AST.Node): boolean;
    }
}


// Included tree adapters
//-----------------------------------------------------------------------------------

/**
 * Provides built-in tree adapters that can be used for parsing and serialization.
 *
 * @example
 *```js
 *
 * const parse5 = require('parse5');
 *
 * // Uses the default tree adapter for parsing.
 * const document = parse5.parse('<div></div>', {
 *     treeAdapter: parse5.treeAdapters.default
 * });
 *
 * // Uses the htmlparser2 tree adapter with the SerializerStream.
 * const serializer = new parse5.SerializerStream(node, {
 *     treeAdapter: parse5.treeAdapters.htmlparser2
 * });
 * ```
 */
export var treeAdapters: {
    /**
     * Default tree format for parse5.
     */
    default: AST.TreeAdapter,
    /**
     * Quite popular [htmlparser2](https://github.com/fb55/htmlparser2) tree format
     * (e.g. used by [cheerio](https://github.com/MatthewMueller/cheerio) and [jsdom](https://github.com/tmpvar/jsdom)).
     */
    htmlparser2: AST.TreeAdapter
};


// Shorthand methods
//-----------------------------------------------------------------------------------

/**
 * Parses an HTML string.
 *
 * @param html - Input HTML string.
 * @param options - Parsing options.
 *
 * @example
 * ```js
 *
 * 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'
 * ```
 */
export function parse(html: string, options?: Options.ParserOptions): AST.Document;

/**
 * Parses an HTML fragment.
 *
 * @param fragmentContext - Parsing context element. If specified, given fragment will be parsed as if it was set to the context element's `innerHTML` property.
 * @param html - Input HTML fragment string.
 * @param options - Parsing options.
 *
 * @example
 * ```js
 *
 * 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'
 * ```
 */
export function parseFragment(fragmentContext: AST.Element, html: string, options?: Options.ParserOptions): AST.DocumentFragment;
export function parseFragment(html: string, options?: Options.ParserOptions): AST.DocumentFragment;

/**
 * Serializes an AST node to an HTML string.
 *
 * @param node - Node to serialize.
 * @param options - Serialization options.
 *
 * @example
 * ```js
 *
 * 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>'
 * ```
 */
export function serialize(node: AST.Node, options?: Options.SerializerOptions): string;


// Parser stream
//-----------------------------------------------------------------------------------

/**
 * Streaming HTML parser with scripting support.
 * A [writable stream](https://nodejs.org/api/stream.html#stream_class_stream_writable).
 *
 * @example
 * ```js
 *
 * const parse5 = require('parse5');
 * const http = require('http');
 *
 * // Fetch the page content and obtain it's <head> node
 * http.get('http://inikulin.github.io/parse5/', res => {
 *    const parser = new parse5.ParserStream();
 *
 *    parser.once('finish', () => {
 *        console.log(parser.document.childNodes[1].childNodes[0].tagName); //> 'head'
 *    });
 *
 *    res.pipe(parser);
 * });
 * ```
 */
export class ParserStream extends stream.Writable {
    /**
     * @param options - Parsing options.
     */
    constructor(options?: Options.ParserOptions);

    /**
     * The resulting document node.
     */
    document: AST.Document;

    /**
     * Raised then parser encounters a `<script>` element.
     * If this event has listeners, parsing will be suspended once it is emitted.
     * So, if `<script>` has the `src` attribute, you can fetch it, execute and then resume parsing just like browsers do.
     *
     * @param listener.scriptElement - The script element that caused the event.
     * @param listener.documentWrite - Write additional `html` at the current parsing position. Suitable for implementing the DOM `document.write` and `document.writeln` methods.
     * @param listener.documentWrite.html - HTML to write.
     * @param listener.resume - Resumes parsing.
     *
     * @example
     * ```js
     *
     * const parse = require('parse5');
     * const http = require('http');
     *
     * const parser = new parse5.ParserStream();
     *
     * parser.on('script', (scriptElement, documentWrite, resume) => {
     *     const src = parse5.treeAdapters.default.getAttrList(scriptElement)[0].value;
     *
     *     http.get(src, res => {
     *        // Fetch the script content, execute it with DOM built around `parser.document` and
     *        // `document.write` implemented using `documentWrite`.
     *        ...
     *        // Then resume parsing.
     *        resume();
     *     });
     * });
     *
     * parser.end('<script src="example.com/script.js"></script>');
     * ```
     */
    on(event: 'script', listener: (scriptElement: AST.Element, documentWrite: (html: string) => void, resume: () => void) => void): this;
    /**
     * WritableStream events
     */
    on(event: string, listener: Function): this;
}


// Plaint text conversion stream
//-----------------------------------------------------------------------------------

/**
 * Converts plain text files into HTML document as required by [HTML specification](https://html.spec.whatwg.org/#read-text).
 * A [writable stream](https://nodejs.org/api/stream.html#stream_class_stream_writable).
 *
 * @example
 * ```js
 *
 * const parse5 = require('parse5');
 * const fs = require('fs');
 *
 * const file = fs.createReadStream('war_and_peace.txt');
 * const converter = new parse5.PlainTextConversionStream();
 *
 * converter.once('finish', () => {
 *     console.log(converter.document.childNodes[1].childNodes[0].tagName); //> 'head'
 * });
 *
 * file.pipe(converter);
 * ```
 */
export class PlainTextConversionStream extends ParserStream { }


// SAX parser
//-----------------------------------------------------------------------------------
/**
 * Streaming [SAX](https://en.wikipedia.org/wiki/Simple_API_for_XML)-style HTML parser.
 * A [transform stream](https://nodejs.org/api/stream.html#stream_class_stream_transform)
 * (which means you can pipe *through* it, see example).
 *
 * @example
 * ```js
 *
 * const parse5 = require('parse5');
 * const http = require('http');
 * const fs = require('fs');
 *
 * const file = fs.createWriteStream('/home/google.com.html');
 * const parser = new parse5.SAXParser();
 *
 * parser.on('text', text => {
 *    // Handle page text content
 *    ...
 * });
 *
 * http.get('http://google.com', res => {
 *    // SAXParser is the Transform stream, which means you can pipe
 *    // through it. So, you can analyze page content and, e.g., save it
 *    // to the file at the same time:
 *    res.pipe(parser).pipe(file);
 * });
 * ```
 */
export class SAXParser extends stream.Transform {
    /**
     * @param options - Parsing options.
     */
    constructor(options?: Options.SAXParserOptions);

    /**
     * Raised when the parser encounters a start tag.
     *
     * @param listener.name - Tag name.
     * @param listener.attrs - List of attributes.
     * @param listener.selfClosing - Indicates if the tag is self-closing.
     * @param listener.location - Start tag source code location info. Available if location info is enabled via {@link Options.SAXParserOptions}.
     */
    on(event: 'startTag', listener: (name: string, attrs: AST.Default.Attribute[], selfClosing: boolean, location?: MarkupData.StartTagLocation) => void): this;
    /**
     * Raised then parser encounters an end tag.
     *
     * @param listener.name - Tag name.
     * @param listener.location - End tag source code location info. Available if location info is enabled via {@link Options.SAXParserOptions}.
     */
    on(event: 'endTag', listener: (name: string, location?: MarkupData.Location) => void): this;
    /**
     * Raised then parser encounters a comment.
     *
     * @param listener.text - Comment text.
     * @param listener.location - Comment source code location info. Available if location info is enabled via {@link Options.SAXParserOptions}.
     */
    on(event: 'comment', listener: (text: string, location?: MarkupData.Location) => void): this;
    /**
     * Raised then parser encounters text content.
     *
     * @param listener.text - Text content.
     * @param listener.location - Text content code location info. Available if location info is enabled via {@link Options.SAXParserOptions}.
     */
    on(event: 'text', listener: (text: string, location?: MarkupData.Location) => void): this;
    /**
     * Raised then parser encounters a [document type declaration](https://en.wikipedia.org/wiki/Document_type_declaration).
     *
     * @param listener.name - Document type name.
     * @param listener.publicId - Document type public identifier.
     * @param listener.systemId - Document type system identifier.
     * @param listener.location - Document type declaration source code location info. Available if location info is enabled via {@link Options.SAXParserOptions}.
     */
    on(event: 'doctype', listener: (name: string, publicId: string, systemId: string, location?: MarkupData.Location) => void): this;
    /**
     * TransformStream events
     */
    on(event: string, listener: Function): this;

    /**
     * Stops parsing. Useful if you want the parser to stop consuming CPU time once you've obtained the desired info
     * from the input stream. Doesn't prevent piping, so that data will flow through the parser as usual.
     *
     * @example
     * ```js
     *
     * const parse5 = require('parse5');
     * const http = require('http');
     * const fs = require('fs');
     *
     * const file = fs.createWriteStream('google.com.html');
     * const parser = new parse5.SAXParser();
     *
     * parser.on('doctype', (name, publicId, systemId) => {
     *    // Process doctype info ans stop parsing
     *    ...
     *    parser.stop();
     * });
     *
     * http.get('http://google.com', res => {
     *    // Despite the fact that parser.stop() was called whole
     *    // content of the page will be written to the file
     *    res.pipe(parser).pipe(file);
     * });
     * ```
     */
    stop(): void;
}


// Serializer stream
//-----------------------------------------------------------------------------------

/**
 * Streaming AST node to an HTML serializer.
 * A [readable stream](https://nodejs.org/api/stream.html#stream_class_stream_readable).
 *
 * @example
 * ```js
 *
 * const parse5 = require('parse5');
 * const fs = require('fs');
 *
 * const file = fs.createWriteStream('/home/index.html');
 *
 * // Serializes the parsed document to HTML and writes it to the file.
 * const document = parse5.parse('<body>Who is John Galt?</body>');
 * const serializer = new parse5.SerializerStream(document);
 *
 * serializer.pipe(file);
 * ```
 */
export class SerializerStream extends stream.Readable {
    /**
     * Streaming AST node to an HTML serializer. A readable stream.
     *
     * @param node - Node to serialize.
     * @param options - Serialization options.
     */
    constructor(node: AST.Node, options?: Options.SerializerOptions);
}