UNPKG

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