saxes.d.ts 19.8 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
/**
 * The list of supported events.
 */
export declare const EVENTS: readonly ["xmldecl", "text", "processinginstruction", "doctype", "comment", "opentagstart", "attribute", "opentag", "closetag", "cdata", "error", "end", "ready"];
/**
 * Event handler for the
 *
 * @param text The text data encountered by the parser.
 *
 */
export declare type XMLDeclHandler = (decl: XMLDecl) => void;
/**
 * Event handler for text data.
 *
 * @param text The text data encountered by the parser.
 *
 */
export declare type TextHandler = (text: string) => void;
/**
 * Event handler for processing instructions.
 *
 * @param data The target and body of the processing instruction.
 */
export declare type PIHandler = (data: {
    target: string;
    body: string;
}) => void;
/**
 * Event handler for doctype.
 *
 * @param doctype The doctype contents.
 */
export declare type DoctypeHandler = (doctype: string) => void;
/**
 * Event handler for comments.
 *
 * @param comment The comment contents.
 */
export declare type CommentHandler = (comment: string) => void;
/**
 * Event handler for the start of an open tag. This is called as soon as we
 * have a tag name.
 *
 * @param tag The tag.
 */
export declare type OpenTagStartHandler<O> = (tag: StartTagForOptions<O>) => void;
export declare type AttributeEventForOptions<O extends SaxesOptions> = O extends {
    xmlns: true;
} ? SaxesAttributeNSIncomplete : O extends {
    xmlns?: false | undefined;
} ? SaxesAttributePlain : SaxesAttribute;
/**
 * Event handler for attributes.
 */
export declare type AttributeHandler<O> = (attribute: AttributeEventForOptions<O>) => void;
/**
 * Event handler for an open tag. This is called when the open tag is
 * complete. (We've encountered the ">" that ends the open tag.)
 *
 * @param tag The tag.
 */
export declare type OpenTagHandler<O> = (tag: TagForOptions<O>) => void;
/**
 * Event handler for a close tag. Note that for self-closing tags, this is
 * called right after ``opentag``.
 *
 * @param tag The tag.
 */
export declare type CloseTagHandler<O> = (tag: TagForOptions<O>) => void;
/**
 * Event handler for a CDATA section. This is called when ending the
 * CDATA section.
 *
 * @param cdata The contents of the CDATA section.
 */
export declare type CDataHandler = (cdata: string) => void;
/**
 * Event handler for the stream end. This is called when the stream has been
 * closed with ``close`` or by passing ``null`` to ``write``.
 */
export declare type EndHandler = () => void;
/**
 * Event handler indicating parser readiness . This is called when the parser
 * is ready to parse a new document.
 */
export declare type ReadyHandler = () => void;
/**
 * Event handler indicating an error.
 *
 * @param err The error that occurred.
 */
export declare type ErrorHandler = (err: Error) => void;
export declare type EventName = (typeof EVENTS)[number];
export declare type EventNameToHandler<O, N extends EventName> = {
    "xmldecl": XMLDeclHandler;
    "text": TextHandler;
    "processinginstruction": PIHandler;
    "doctype": DoctypeHandler;
    "comment": CommentHandler;
    "opentagstart": OpenTagStartHandler<O>;
    "attribute": AttributeHandler<O>;
    "opentag": OpenTagHandler<O>;
    "closetag": CloseTagHandler<O>;
    "cdata": CDataHandler;
    "error": ErrorHandler;
    "end": EndHandler;
    "ready": ReadyHandler;
}[N];
/**
 * This interface defines the structure of attributes when the parser is
 * processing namespaces (created with ``xmlns: true``).
 */
export interface SaxesAttributeNS {
    /**
     * The attribute's name. This is the combination of prefix and local name.
     * For instance ``a:b="c"`` would have ``a:b`` for name.
     */
    name: string;
    /**
     * The attribute's prefix. For instance ``a:b="c"`` would have ``"a"`` for
     * ``prefix``.
     */
    prefix: string;
    /**
     * The attribute's local name. For instance ``a:b="c"`` would have ``"b"`` for
     * ``local``.
     */
    local: string;
    /** The namespace URI of this attribute. */
    uri: string;
    /** The attribute's value. */
    value: string;
}
/**
 * This is an attribute, as recorded by a parser which parses namespaces but
 * prior to the URI being resolvable. This is what is passed to the attribute
 * event handler.
 */
export declare type SaxesAttributeNSIncomplete = Exclude<SaxesAttributeNS, "uri">;
/**
 * This interface defines the structure of attributes when the parser is
 * NOT processing namespaces (created with ``xmlns: false``).
 */
export interface SaxesAttributePlain {
    /**
     * The attribute's name.
     */
    name: string;
    /** The attribute's value. */
    value: string;
}
/**
 * A saxes attribute, with or without namespace information.
 */
export declare type SaxesAttribute = SaxesAttributeNS | SaxesAttributePlain;
/**
 * This are the fields that MAY be present on a complete tag.
 */
export interface SaxesTag {
    /**
     * The tag's name. This is the combination of prefix and global name. For
     * instance ``<a:b>`` would have ``"a:b"`` for ``name``.
     */
    name: string;
    /**
     * A map of attribute name to attributes. If namespaces are tracked, the
     * values in the map are attribute objects. Otherwise, they are strings.
     */
    attributes: Record<string, SaxesAttributeNS> | Record<string, string>;
    /**
     * The namespace bindings in effect.
     */
    ns?: Record<string, string>;
    /**
     * The tag's prefix. For instance ``<a:b>`` would have ``"a"`` for
     * ``prefix``. Undefined if we do not track namespaces.
     */
    prefix?: string;
    /**
     * The tag's local name. For instance ``<a:b>`` would
     * have ``"b"`` for ``local``. Undefined if we do not track namespaces.
     */
    local?: string;
    /**
     * The namespace URI of this tag. Undefined if we do not track namespaces.
     */
    uri?: string;
    /** Whether the tag is self-closing (e.g. ``<foo/>``). */
    isSelfClosing: boolean;
}
/**
 * This type defines the fields that are present on a tag object when
 * ``onopentagstart`` is called. This interface is namespace-agnostic.
 */
export declare type SaxesStartTag = Pick<SaxesTag, "name" | "attributes" | "ns">;
/**
 * This type defines the fields that are present on a tag object when
 * ``onopentagstart`` is called on a parser that does not processes namespaces.
 */
export declare type SaxesStartTagPlain = Pick<SaxesStartTag, "name" | "attributes">;
/**
 * This type defines the fields that are present on a tag object when
 * ``onopentagstart`` is called on a parser that does process namespaces.
 */
export declare type SaxesStartTagNS = Required<SaxesStartTag>;
/**
 * This are the fields that are present on a complete tag produced by a parser
 * that does process namespaces.
 */
export declare type SaxesTagNS = Required<SaxesTag> & {
    attributes: Record<string, SaxesAttributeNS>;
};
/**
 * This are the fields that are present on a complete tag produced by a parser
 * that does not process namespaces.
 */
export declare type SaxesTagPlain = Pick<SaxesTag, "name" | "attributes" | "isSelfClosing"> & {
    attributes: Record<string, string>;
};
/**
 * An XML declaration.
 */
export interface XMLDecl {
    /** The version specified by the XML declaration. */
    version?: string;
    /** The encoding specified by the XML declaration. */
    encoding?: string;
    /** The value of the standalone parameter */
    standalone?: string;
}
/**
 * A callback for resolving name prefixes.
 *
 * @param prefix The prefix to check.
 *
 * @returns The URI corresponding to the prefix, if any.
 */
export declare type ResolvePrefix = (prefix: string) => string | undefined;
export interface CommonOptions {
    /** Whether to accept XML fragments. Unset means ``false``. */
    fragment?: boolean;
    /** Whether to track positions. Unset means ``true``. */
    position?: boolean;
    /**
     * A file name to use for error reporting. "File name" is a loose concept. You
     * could use a URL to some resource, or any descriptive name you like.
     */
    fileName?: string;
}
export interface NSOptions {
    /** Whether to track namespaces. Unset means ``false``. */
    xmlns?: boolean;
    /**
     * A plain object whose key, value pairs define namespaces known before
     * parsing the XML file. It is not legal to pass bindings for the namespaces
     * ``"xml"`` or ``"xmlns"``.
     */
    additionalNamespaces?: Record<string, string>;
    /**
     * A function that will be used if the parser cannot resolve a namespace
     * prefix on its own.
     */
    resolvePrefix?: ResolvePrefix;
}
export interface NSOptionsWithoutNamespaces extends NSOptions {
    xmlns?: false;
    additionalNamespaces?: undefined;
    resolvePrefix?: undefined;
}
export interface NSOptionsWithNamespaces extends NSOptions {
    xmlns: true;
}
export interface XMLVersionOptions {
    /**
     * The default XML version to use. If unspecified, and there is no XML
     * encoding declaration, the default version is "1.0".
     */
    defaultXMLVersion?: "1.0" | "1.1";
    /**
     * A flag indicating whether to force the XML version used for parsing to the
     * value of ``defaultXMLVersion``. When this flag is ``true``,
     * ``defaultXMLVersion`` must be specified. If unspecified, the default value
     * of this flag is ``false``.
     */
    forceXMLVersion?: boolean;
}
export interface NoForcedXMLVersion extends XMLVersionOptions {
    forceXMLVersion?: false;
}
export interface ForcedXMLVersion extends XMLVersionOptions {
    forceXMLVersion: true;
    defaultXMLVersion: Exclude<XMLVersionOptions["defaultXMLVersion"], undefined>;
}
/**
 * The entire set of options supported by saxes.
 */
export declare type SaxesOptions = CommonOptions & NSOptions & XMLVersionOptions;
export declare type TagForOptions<O extends SaxesOptions> = O extends {
    xmlns: true;
} ? SaxesTagNS : O extends {
    xmlns?: false | undefined;
} ? SaxesTagPlain : SaxesTag;
export declare type StartTagForOptions<O extends SaxesOptions> = O extends {
    xmlns: true;
} ? SaxesStartTagNS : O extends {
    xmlns?: false | undefined;
} ? SaxesStartTagPlain : SaxesStartTag;
export declare class SaxesParser<O extends SaxesOptions = {}> {
    private readonly fragmentOpt;
    private readonly xmlnsOpt;
    private readonly trackPosition;
    private readonly fileName?;
    private readonly nameStartCheck;
    private readonly nameCheck;
    private readonly isName;
    private readonly ns;
    private openWakaBang;
    private text;
    private name;
    private piTarget;
    private entity;
    private q;
    private tags;
    private tag;
    private topNS;
    private chunk;
    private chunkPosition;
    private i;
    private prevI;
    private carriedFromPrevious?;
    private forbiddenState;
    private attribList;
    private state;
    private reportedTextBeforeRoot;
    private reportedTextAfterRoot;
    private closedRoot;
    private sawRoot;
    private xmlDeclPossible;
    private xmlDeclExpects;
    private entityReturnState?;
    private processAttribs;
    private positionAtNewLine;
    private doctype;
    private getCode;
    private isChar;
    private pushAttrib;
    private _closed;
    private currentXMLVersion;
    private readonly stateTable;
    private xmldeclHandler?;
    private textHandler?;
    private piHandler?;
    private doctypeHandler?;
    private commentHandler?;
    private openTagStartHandler?;
    private openTagHandler?;
    private closeTagHandler?;
    private cdataHandler?;
    private errorHandler?;
    private endHandler?;
    private readyHandler?;
    private attributeHandler?;
    /**
     * Indicates whether or not the parser is closed. If ``true``, wait for
     * the ``ready`` event to write again.
     */
    get closed(): boolean;
    readonly opt: SaxesOptions;
    /**
     * The XML declaration for this document.
     */
    xmlDecl: XMLDecl;
    /**
     * The line number of the next character to be read by the parser. This field
     * is one-based. (The first line is numbered 1.)
     */
    line: number;
    /**
     * The column number of the next character to be read by the parser.  *
     * This field is zero-based. (The first column is 0.)
     *
     * This field counts columns by *Unicode character*. Note that this *can*
     * be different from the index of the character in a JavaScript string due
     * to how JavaScript handles astral plane characters.
     *
     * See [[columnIndex]] for a number that corresponds to the JavaScript index.
     */
    column: number;
    /**
     * A map of entity name to expansion.
     */
    ENTITIES: Record<string, string>;
    /**
     * @param opt The parser options.
     */
    constructor(opt?: O);
    _init(): void;
    /**
     * The stream position the parser is currently looking at. This field is
     * zero-based.
     *
     * This field is not based on counting Unicode characters but is to be
     * interpreted as a plain index into a JavaScript string.
     */
    get position(): number;
    /**
     * The column number of the next character to be read by the parser.  *
     * This field is zero-based. (The first column in a line is 0.)
     *
     * This field reports the index at which the next character would be in the
     * line if the line were represented as a JavaScript string.  Note that this
     * *can* be different to a count based on the number of *Unicode characters*
     * due to how JavaScript handles astral plane characters.
     *
     * See [[column]] for a number that corresponds to a count of Unicode
     * characters.
     */
    get columnIndex(): number;
    /**
     * Set an event listener on an event. The parser supports one handler per
     * event type. If you try to set an event handler over an existing handler,
     * the old handler is silently overwritten.
     *
     * @param name The event to listen to.
     *
     * @param handler The handler to set.
     */
    on<N extends EventName>(name: N, handler: EventNameToHandler<O, N>): void;
    /**
     * Unset an event handler.
     *
     * @parma name The event to stop listening to.
     */
    off(name: EventName): void;
    /**
     * Make an error object. The error object will have a message that contains
     * the ``fileName`` option passed at the creation of the parser. If position
     * tracking was turned on, it will also have line and column number
     * information.
     *
     * @param message The message describing the error to report.
     *
     * @returns An error object with a properly formatted message.
     */
    makeError(message: string): Error;
    /**
     * Report a parsing error. This method is made public so that client code may
     * check for issues that are outside the scope of this project and can report
     * errors.
     *
     * @param message The error to report.
     *
     * @returns this
     */
    fail(message: string): this;
    /**
     * Write a XML data to the parser.
     *
     * @param chunk The XML data to write.
     *
     * @returns this
     */
    write(chunk: string | {} | null): this;
    /**
     * Close the current stream. Perform final well-formedness checks and reset
     * the parser tstate.
     *
     * @returns this
     */
    close(): this;
    /**
     * Get a single code point out of the current chunk. This updates the current
     * position if we do position tracking.
     *
     * This is the algorithm to use for XML 1.0.
     *
     * @returns The character read.
     */
    private getCode10;
    /**
     * Get a single code point out of the current chunk. This updates the current
     * position if we do position tracking.
     *
     * This is the algorithm to use for XML 1.1.
     *
     * @returns {number} The character read.
     */
    private getCode11;
    /**
     * Like ``getCode`` but with the return value normalized so that ``NL`` is
     * returned for ``NL_LIKE``.
     */
    private getCodeNorm;
    private unget;
    /**
     * Capture characters into a buffer until encountering one of a set of
     * characters.
     *
     * @param chars An array of codepoints. Encountering a character in the array
     * ends the capture. (``chars`` may safely contain ``NL``.)
     *
     * @return The character code that made the capture end, or ``EOC`` if we hit
     * the end of the chunk. The return value cannot be NL_LIKE: NL is returned
     * instead.
     */
    private captureTo;
    /**
     * Capture characters into a buffer until encountering a character.
     *
     * @param char The codepoint that ends the capture. **NOTE ``char`` MAY NOT
     * CONTAIN ``NL``.** Passing ``NL`` will result in buggy behavior.
     *
     * @return ``true`` if we ran into the character. Otherwise, we ran into the
     * end of the current chunk.
     */
    private captureToChar;
    /**
     * Capture characters that satisfy ``isNameChar`` into the ``name`` field of
     * this parser.
     *
     * @return The character code that made the test fail, or ``EOC`` if we hit
     * the end of the chunk. The return value cannot be NL_LIKE: NL is returned
     * instead.
     */
    private captureNameChars;
    /**
     * Skip white spaces.
     *
     * @return The character that ended the skip, or ``EOC`` if we hit
     * the end of the chunk. The return value cannot be NL_LIKE: NL is returned
     * instead.
     */
    private skipSpaces;
    private setXMLVersion;
    private sBegin;
    private sBeginWhitespace;
    private sDoctype;
    private sDoctypeQuote;
    private sDTD;
    private sDTDQuoted;
    private sDTDOpenWaka;
    private sDTDOpenWakaBang;
    private sDTDComment;
    private sDTDCommentEnding;
    private sDTDCommentEnded;
    private sDTDPI;
    private sDTDPIEnding;
    private sText;
    private sEntity;
    private sOpenWaka;
    private sOpenWakaBang;
    private sComment;
    private sCommentEnding;
    private sCommentEnded;
    private sCData;
    private sCDataEnding;
    private sCDataEnding2;
    private sPIFirstChar;
    private sPIRest;
    private sPIBody;
    private sPIEnding;
    private sXMLDeclNameStart;
    private sXMLDeclName;
    private sXMLDeclEq;
    private sXMLDeclValueStart;
    private sXMLDeclValue;
    private sXMLDeclSeparator;
    private sXMLDeclEnding;
    private sOpenTag;
    private sOpenTagSlash;
    private sAttrib;
    private sAttribName;
    private sAttribNameSawWhite;
    private sAttribValue;
    private sAttribValueQuoted;
    private sAttribValueClosed;
    private sAttribValueUnquoted;
    private sCloseTag;
    private sCloseTagSawWhite;
    private handleTextInRoot;
    private handleTextOutsideRoot;
    private pushAttribNS;
    private pushAttribPlain;
    /**
     * End parsing. This performs final well-formedness checks and resets the
     * parser to a clean state.
     *
     * @returns this
     */
    private end;
    /**
     * Resolve a namespace prefix.
     *
     * @param prefix The prefix to resolve.
     *
     * @returns The namespace URI or ``undefined`` if the prefix is not defined.
     */
    resolve(prefix: string): string | undefined;
    /**
     * Parse a qname into its prefix and local name parts.
     *
     * @param name The name to parse
     *
     * @returns
     */
    private qname;
    private processAttribsNS;
    private processAttribsPlain;
    /**
     * Handle a complete open tag. This parser code calls this once it has seen
     * the whole tag. This method checks for well-formeness and then emits
     * ``onopentag``.
     */
    private openTag;
    /**
     * Handle a complete self-closing tag. This parser code calls this once it has
     * seen the whole tag. This method checks for well-formeness and then emits
     * ``onopentag`` and ``onclosetag``.
     */
    private openSelfClosingTag;
    /**
     * Handle a complete close tag. This parser code calls this once it has seen
     * the whole tag. This method checks for well-formeness and then emits
     * ``onclosetag``.
     */
    private closeTag;
    /**
     * Resolves an entity. Makes any necessary well-formedness checks.
     *
     * @param entity The entity to resolve.
     *
     * @returns The parsed entity.
     */
    private parseEntity;
}