UNPKG

20.1 kBTypeScriptView Raw
1// Type definitions for parse5 6.0
2// Project: https://github.com/inikulin/parse5
3// Definitions by: Ivan Nikulin <https://github.com/inikulin>
4// ExE Boss <https://github.com/ExE-Boss>
5// James Garbutt <https://github.com/43081j>
6// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
7
8export {};
9interface EndLocationBase {
10 /**
11 * One-based column index of the last character
12 */
13 endCol: number;
14
15 /**
16 * Zero-based last character index
17 */
18 endOffset: number;
19
20 /**
21 * One-based line index of the last character
22 */
23 endLine: number;
24}
25
26export interface Location extends EndLocationBase {
27 /**
28 * One-based column index of the first character
29 */
30 startCol: number;
31
32 /**
33 * Zero-based first character index
34 */
35 startOffset: number;
36
37 /**
38 * One-based line index of the first character
39 */
40 startLine: number;
41}
42
43export interface EndLocation extends EndLocationBase, Partial<ElementLocationBase> {}
44
45export interface AttributesLocation {
46 [attributeName: string]: Location;
47}
48
49export interface StartTagLocation extends Location {
50 /**
51 * Start tag attributes' location info
52 */
53 attrs?: AttributesLocation;
54}
55
56export interface ElementLocation extends ElementLocationBase, StartTagLocation {
57 /**
58 * Element's start tag location info.
59 */
60 startTag: StartTagLocation;
61}
62
63interface ElementLocationBase {
64 /**
65 * Element's end tag location info.
66 */
67 endTag: Location;
68}
69
70export interface ParserOptions<T extends TreeAdapter = TreeAdapter> {
71 /**
72 * The [scripting flag](https://html.spec.whatwg.org/multipage/parsing.html#scripting-flag).
73 * If set to `true`, `<noscript>` element content will be parsed as text.
74 *
75 * @default true
76 */
77 scriptingEnabled?: boolean | undefined;
78
79 /**
80 * Enables source code location information. When enabled, each node (except the root node)
81 * will have a `sourceCodeLocation` property. If the node is not an empty element, `sourceCodeLocation` will
82 * be a {@link ElementLocation} object, otherwise it will be {@link Location}.
83 * If the element was implicitly created by the parser (as part of
84 * [tree correction](https://html.spec.whatwg.org/multipage/syntax.html#an-introduction-to-error-handling-and-strange-cases-in-the-parser)),
85 * its `sourceCodeLocation` property will be `undefined`.
86 *
87 * @default false
88 */
89 sourceCodeLocationInfo?: boolean | undefined;
90
91 /**
92 * Specifies the resulting tree format.
93 *
94 * @default require("./lib/tree-adapters/default")
95 */
96 treeAdapter?: T | undefined;
97}
98
99export interface SerializerOptions<T extends TreeAdapter = TreeAdapter> {
100 /***
101 * Specifies input tree format.
102 *
103 * @default require("./lib/tree-adapters/default")
104 */
105 treeAdapter?: T | undefined;
106}
107
108/**
109 * [Document mode](https://dom.spec.whatwg.org/#concept-document-limited-quirks).
110 */
111export type DocumentMode = 'no-quirks' | 'quirks' | 'limited-quirks';
112
113// Default tree adapter
114
115/**
116 * Element attribute.
117 */
118export interface Attribute {
119 /**
120 * The name of the attribute.
121 */
122 name: string;
123
124 /**
125 * The value of the attribute.
126 */
127 value: string;
128
129 /**
130 * The namespace of the attribute.
131 */
132 namespace?: string | undefined;
133
134 /**
135 * The namespace-related prefix of the attribute.
136 */
137 prefix?: string | undefined;
138}
139
140/**
141 * Default tree adapter DocumentType interface.
142 */
143export interface DocumentType {
144 /**
145 * The name of the node.
146 */
147 nodeName: '#documentType';
148
149 /**
150 * Document type name.
151 */
152 name: string;
153
154 /**
155 * Document type public identifier.
156 */
157 publicId: string;
158
159 /**
160 * Document type system identifier.
161 */
162 systemId: string;
163}
164
165/**
166 * Default tree adapter Document interface.
167 */
168export interface Document {
169 /**
170 * The name of the node.
171 */
172 nodeName: '#document';
173
174 /**
175 * [Document mode](https://dom.spec.whatwg.org/#concept-document-limited-quirks).
176 */
177 mode: DocumentMode;
178
179 /**
180 * Child nodes.
181 */
182 childNodes: ChildNode[];
183}
184
185/**
186 * Default tree adapter DocumentFragment interface.
187 */
188export interface DocumentFragment {
189 /**
190 * The name of the node.
191 */
192 nodeName: '#document-fragment';
193
194 /**
195 * Child nodes.
196 */
197 childNodes: ChildNode[];
198}
199
200/**
201 * Default tree adapter Element interface.
202 */
203export interface Element {
204 /**
205 * The name of the node. Equals to element {@link tagName}.
206 */
207 nodeName: string;
208
209 /**
210 * Element tag name.
211 */
212 tagName: string;
213
214 /**
215 * Element namespace.
216 */
217 namespaceURI: string;
218
219 /**
220 * List of element attributes.
221 */
222 attrs: Attribute[];
223
224 /**
225 * Element source code location info. Available if location info is enabled via {@link ParserOptions}.
226 */
227 sourceCodeLocation?: ElementLocation | undefined;
228
229 /**
230 * Child nodes.
231 */
232 childNodes: ChildNode[];
233
234 /**
235 * Parent node.
236 */
237 parentNode: ParentNode;
238}
239
240/**
241 * Default tree adapter CommentNode interface.
242 */
243export interface CommentNode {
244 /**
245 * The name of the node.
246 */
247 nodeName: '#comment';
248
249 /**
250 * Comment text.
251 */
252 data: string;
253
254 /**
255 * Comment source code location info. Available if location info is enabled via {@link ParserOptions}.
256 */
257 sourceCodeLocation?: Location | undefined;
258
259 /**
260 * Parent node.
261 */
262 parentNode: ParentNode;
263}
264
265/**
266 * Default tree adapter TextNode interface.
267 */
268export interface TextNode {
269 /**
270 * The name of the node.
271 */
272 nodeName: '#text';
273
274 /**
275 * Text content.
276 */
277 value: string;
278
279 /**
280 * Text node source code location info. Available if location info is enabled via {@link ParserOptions}.
281 */
282 sourceCodeLocation?: Location | undefined;
283
284 /**
285 * Parent node.
286 */
287 parentNode: ParentNode;
288}
289
290/**
291 * Default tree adapter Node interface.
292 */
293export type Node = CommentNode | Document | DocumentFragment | DocumentType | Element | TextNode;
294
295/**
296 * Default tree adapter ChildNode type.
297 */
298export type ChildNode = TextNode | Element | CommentNode;
299
300/**
301 * Default tree adapter ParentNode type.
302 */
303export type ParentNode = Document | DocumentFragment | Element;
304
305export interface TreeAdapterTypeMap {
306 attribute: unknown;
307 childNode: unknown;
308 commentNode: unknown;
309 document: unknown;
310 documentFragment: unknown;
311 documentType: unknown;
312 element: unknown;
313 node: unknown;
314 parentNode: unknown;
315 textNode: unknown;
316}
317
318/**
319 * Tree adapter is a set of utility functions that provides minimal required abstraction layer beetween parser and a specific AST format.
320 * Note that `TreeAdapter` is not designed to be a general purpose AST manipulation library. You can build such library
321 * on top of existing `TreeAdapter` or use one of the existing libraries from npm.
322 *
323 * @see [default implementation](https://github.com/inikulin/parse5/blob/master/packages/parse5/lib/tree-adapters/default.js)
324 */
325export interface TreeAdapter {
326 /**
327 * Copies attributes to the given element. Only attributes that are not yet present in the element are copied.
328 *
329 * @param recipient - Element to copy attributes into.
330 * @param attrs - Attributes to copy.
331 */
332 adoptAttributes(recipient: unknown, attrs: unknown[]): void;
333
334 /**
335 * Appends a child node to the given parent node.
336 *
337 * @param parentNode - Parent node.
338 * @param newNode - Child node.
339 */
340 appendChild(parentNode: unknown, newNode: unknown): void;
341
342 /**
343 * Creates a comment node.
344 *
345 * @param data - Comment text.
346 */
347 createCommentNode(data: string): unknown;
348
349 /**
350 * Creates a document node.
351 */
352 createDocument(): unknown;
353
354 /**
355 * Creates a document fragment node.
356 */
357 createDocumentFragment(): unknown;
358
359 /**
360 * Creates an element node.
361 *
362 * @param tagName - Tag name of the element.
363 * @param namespaceURI - Namespace of the element.
364 * @param attrs - Attribute name-value pair array. Foreign attributes may contain `namespace` and `prefix` fields as well.
365 */
366 createElement(tagName: string, namespaceURI: string, attrs: unknown[]): unknown;
367
368 /**
369 * Removes a node from its parent.
370 *
371 * @param node - Node to remove.
372 */
373 detachNode(node: unknown): void;
374 /**
375 * Returns the given element's attributes in an array, in the form of name-value pairs.
376 * Foreign attributes may contain `namespace` and `prefix` fields as well.
377 *
378 * @param element - Element.
379 */
380 getAttrList(element: unknown): unknown[];
381
382 /**
383 * Returns the given node's children in an array.
384 *
385 * @param node - Node.
386 */
387 getChildNodes(node: unknown): unknown[];
388
389 /**
390 * Returns the given comment node's content.
391 *
392 * @param commentNode - Comment node.
393 */
394 getCommentNodeContent(commentNode: unknown): string;
395
396 /**
397 * Returns [document mode](https://dom.spec.whatwg.org/#concept-document-limited-quirks).
398 *
399 * @param document - Document node.
400 */
401 getDocumentMode(document: unknown): unknown;
402
403 /**
404 * Returns the given document type node's name.
405 *
406 * @param doctypeNode - Document type node.
407 */
408 getDocumentTypeNodeName(doctypeNode: unknown): string;
409
410 /**
411 * Returns the given document type node's public identifier.
412 *
413 * @param doctypeNode - Document type node.
414 */
415 getDocumentTypeNodePublicId(doctypeNode: unknown): string;
416
417 /**
418 * Returns the given document type node's system identifier.
419 *
420 * @param doctypeNode - Document type node.
421 */
422 getDocumentTypeNodeSystemId(doctypeNode: unknown): string;
423
424 /**
425 * Returns the first child of the given node.
426 *
427 * @param node - Node.
428 */
429 getFirstChild(node: unknown): unknown;
430
431 /**
432 * Returns the given element's namespace.
433 *
434 * @param element - Element.
435 */
436 getNamespaceURI(element: unknown): string;
437
438 /**
439 * Returns the given node's source code location information.
440 *
441 * @param node - Node.
442 */
443 getNodeSourceCodeLocation(node: unknown): Location | ElementLocation | null;
444
445 /**
446 * Returns the given node's parent.
447 *
448 * @param node - Node.
449 */
450 getParentNode(node: unknown): unknown;
451
452 /**
453 * Returns the given element's tag name.
454 *
455 * @param element - Element.
456 */
457 getTagName(element: unknown): string;
458
459 /**
460 * Returns the given text node's content.
461 *
462 * @param textNode - Text node.
463 */
464 getTextNodeContent(textNode: unknown): string;
465
466 /**
467 * Returns the `<template>` element content element.
468 *
469 * @param templateElement - `<template>` element.
470 */
471 getTemplateContent(templateElement: unknown): unknown;
472
473 /**
474 * Inserts a child node to the given parent node before the given reference node.
475 *
476 * @param parentNode - Parent node.
477 * @param newNode - Child node.
478 * @param referenceNode - Reference node.
479 */
480 insertBefore(parentNode: unknown, newNode: unknown, referenceNode: unknown): void;
481
482 /**
483 * Inserts text into a node. If the last child of the node is a text node, the provided text will be appended to the
484 * text node content. Otherwise, inserts a new text node with the given text.
485 *
486 * @param parentNode - Node to insert text into.
487 * @param text - Text to insert.
488 */
489 insertText(parentNode: unknown, text: string): void;
490
491 /**
492 * Inserts text into a sibling node that goes before the reference node. If this sibling node is the text node,
493 * the provided text will be appended to the text node content. Otherwise, inserts a new sibling text node with
494 * the given text before the reference node.
495 *
496 * @param parentNode - Node to insert text into.
497 * @param text - Text to insert.
498 * @param referenceNode - Node to insert text before.
499 */
500 insertTextBefore(parentNode: unknown, text: string, referenceNode: unknown): void;
501
502 /**
503 * Determines if the given node is a comment node.
504 *
505 * @param node - Node.
506 */
507 isCommentNode(node: unknown): boolean;
508
509 /**
510 * Determines if the given node is a document type node.
511 *
512 * @param node - Node.
513 */
514 isDocumentTypeNode(node: unknown): boolean;
515
516 /**
517 * Determines if the given node is an element.
518 *
519 * @param node - Node.
520 */
521 isElementNode(node: unknown): boolean;
522
523 /**
524 * Determines if the given node is a text node.
525 *
526 * @param node - Node.
527 */
528 isTextNode(node: unknown): boolean;
529
530 /**
531 * Sets the [document mode](https://dom.spec.whatwg.org/#concept-document-limited-quirks).
532 *
533 * @param document - Document node.
534 * @param mode - Document mode.
535 */
536 setDocumentMode(document: unknown, mode: DocumentMode): void;
537
538 /**
539 * Sets the document type. If the `document` already contains a document type node, the `name`, `publicId` and `systemId`
540 * properties of this node will be updated with the provided values. Otherwise, creates a new document type node
541 * with the given properties and inserts it into the `document`.
542 *
543 * @param document - Document node.
544 * @param name - Document type name.
545 * @param publicId - Document type public identifier.
546 * @param systemId - Document type system identifier.
547 */
548 setDocumentType(document: unknown, name: string, publicId: string, systemId: string): void;
549
550 /**
551 * Attaches source code location information to the node.
552 *
553 * @param node - Node.
554 * @param location - The node location.
555 */
556 setNodeSourceCodeLocation(node: unknown, location: Location | ElementLocation | null): void;
557
558 /**
559 * Sets the `<template>` element content element.
560 *
561 * @param templateElement - `<template>` element.
562 * @param contentElement - Content element.
563 */
564 setTemplateContent(templateElement: unknown, contentElement: unknown): void;
565
566 /**
567 * Updates source code location information of the node.
568 *
569 * @param node - Node.
570 * @param location - The updated node end location.
571 */
572 updateNodeSourceCodeLocation(node: unknown, location: EndLocation): void;
573}
574
575/**
576 * Tree adapter is a set of utility functions that provides minimal required abstraction layer beetween parser and a specific AST format.
577 * Note that `TreeAdapter` is not designed to be a general purpose AST manipulation library. You can build such library
578 * on top of existing `TreeAdapter` or use one of the existing libraries from npm.
579 *
580 * @see [default implementation](https://github.com/inikulin/parse5/blob/master/packages/parse5/lib/tree-adapters/default.js)
581 */
582export interface TypedTreeAdapter<T extends TreeAdapterTypeMap> extends TreeAdapter {
583 adoptAttributes(recipient: T['element'], attrs: Array<T['attribute']>): void;
584 appendChild(parentNode: T['parentNode'], newNode: T['node']): void;
585 createCommentNode(data: string): T['commentNode'];
586 createDocument(): T['document'];
587 createDocumentFragment(): T['documentFragment'];
588 createElement(tagName: string, namespaceURI: string, attrs: Array<T['attribute']>): T['element'];
589 detachNode(node: T['node']): void;
590 getAttrList(element: T['element']): Array<T['attribute']>;
591 getChildNodes(node: T['parentNode']): Array<T['childNode']>;
592 getCommentNodeContent(commentNode: T['commentNode']): string;
593 getDocumentMode(document: T['document']): DocumentMode;
594 getDocumentTypeNodeName(doctypeNode: T['documentType']): string;
595 getDocumentTypeNodePublicId(doctypeNode: T['documentType']): string;
596 getDocumentTypeNodeSystemId(doctypeNode: T['documentType']): string;
597 getFirstChild(node: T['parentNode']): T['childNode'] | undefined;
598 getNamespaceURI(element: T['element']): string;
599 getNodeSourceCodeLocation(node: T['node']): Location | ElementLocation | null;
600 getParentNode(node: T['childNode']): T['parentNode'];
601 getTagName(element: T['element']): string;
602 getTextNodeContent(textNode: T['textNode']): string;
603 getTemplateContent(templateElement: T['element']): T['documentFragment'];
604 insertBefore(parentNode: T['parentNode'], newNode: T['node'], referenceNode: T['node']): void;
605 insertText(parentNode: T['parentNode'], text: string): void;
606 insertTextBefore(parentNode: T['parentNode'], text: string, referenceNode: T['node']): void;
607 isCommentNode(node: T['node']): node is T['commentNode'];
608 isDocumentTypeNode(node: T['node']): node is T['documentType'];
609 isElementNode(node: T['node']): node is T['element'];
610 isTextNode(node: T['node']): node is T['textNode'];
611 setDocumentMode(document: T['document'], mode: DocumentMode): void;
612 setDocumentType(document: T['document'], name: string, publicId: string, systemId: string): void;
613 setNodeSourceCodeLocation(node: T['node'], location: Location | ElementLocation | null): void;
614 setTemplateContent(templateElement: T['element'], contentElement: T['documentFragment']): void;
615 updateNodeSourceCodeLocation(node: T['node'], location: EndLocation): void;
616}
617
618/**
619 * Parses an HTML string.
620 *
621 * @param html - Input HTML string.
622 * @param options - Parsing options.
623 *
624 * @example
625 * ```js
626 *
627 * const parse5 = require('parse5');
628 *
629 * const document = parse5.parse('<!DOCTYPE html><html><head></head><body>Hi there!</body></html>');
630 *
631 * console.log(document.childNodes[1].tagName); //> 'html'
632 * ```
633 */
634export function parse<T extends TreeAdapter = typeof import('./lib/tree-adapters/default')>(
635 html: string,
636 options?: ParserOptions<T>,
637): T extends TypedTreeAdapter<infer TMap> ? TMap['document'] : Document;
638
639/**
640 * Parses an HTML fragment.
641 *
642 * @param fragmentContext - Parsing context element. If specified, given fragment will be parsed as if it was set to the context element's `innerHTML` property.
643 * @param html - Input HTML fragment string.
644 * @param options - Parsing options.
645 *
646 * @example
647 * ```js
648 *
649 * const parse5 = require('parse5');
650 *
651 * const documentFragment = parse5.parseFragment('<table></table>');
652 *
653 * console.log(documentFragment.childNodes[0].tagName); //> 'table'
654 *
655 * // Parses the html fragment in the context of the parsed <table> element.
656 * const trFragment = parser.parseFragment(documentFragment.childNodes[0], '<tr><td>Shake it, baby</td></tr>');
657 *
658 * console.log(trFragment.childNodes[0].childNodes[0].tagName); //> 'td'
659 * ```
660 */
661export function parseFragment<T extends TreeAdapter = typeof import('./lib/tree-adapters/default')>(
662 html: string,
663 options?: ParserOptions<T>,
664): T extends TypedTreeAdapter<infer TMap> ? TMap['documentFragment'] : DocumentFragment;
665export function parseFragment<T extends TreeAdapter = typeof import('./lib/tree-adapters/default')>(
666 fragmentContext: Element,
667 html: string,
668 options?: ParserOptions<T>,
669): T extends TypedTreeAdapter<infer TMap> ? TMap['documentFragment'] : DocumentFragment;
670
671/**
672 * Serializes an AST node to an HTML string.
673 *
674 * @param node - Node to serialize.
675 * @param options - Serialization options.
676 *
677 * @example
678 * ```js
679 *
680 * const parse5 = require('parse5');
681 *
682 * const document = parse5.parse('<!DOCTYPE html><html><head></head><body>Hi there!</body></html>');
683 *
684 * // Serializes a document.
685 * const html = parse5.serialize(document);
686 *
687 * // Serializes the <html> element content.
688 * const str = parse5.serialize(document.childNodes[1]);
689 *
690 * console.log(str); //> '<head></head><body>Hi there!</body>'
691 * ```
692 */
693export function serialize<T extends TreeAdapter = typeof import('./lib/tree-adapters/default')>(
694 node: T extends TypedTreeAdapter<infer TMap> ? TMap['node'] : Node,
695 options?: SerializerOptions<T>,
696): string;