index.d.ts 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  1. /// <reference types="node" />
  2. import * as stream from "stream";
  3. import * as events from "events";
  4. // Markup data
  5. //-----------------------------------------------------------------------------------
  6. declare namespace MarkupData {
  7. interface Location {
  8. /**
  9. * One-based line index
  10. */
  11. line: number;
  12. /**
  13. * One-based column index
  14. */
  15. col: number;
  16. /**
  17. * Zero-based first character index
  18. */
  19. startOffset: number;
  20. /**
  21. * Zero-based last character index
  22. */
  23. endOffset: number;
  24. }
  25. interface AttributesLocation {
  26. [attributeName: string]: Location;
  27. }
  28. interface StartTagLocation extends Location {
  29. /**
  30. * Start tag attributes' location info
  31. */
  32. attrs: AttributesLocation
  33. }
  34. interface ElementLocation extends StartTagLocation {
  35. /**
  36. * Element's start tag location info.
  37. */
  38. startTag: StartTagLocation;
  39. /**
  40. * Element's end tag location info.
  41. */
  42. endTag: Location;
  43. }
  44. }
  45. // Options
  46. //-----------------------------------------------------------------------------------
  47. declare namespace Options {
  48. export interface ParserOptions {
  49. /**
  50. * Enables source code location information. When enabled, each node (except the root node) will have a `__location` property.
  51. * If the node is not an empty element, `__location` will be a {@link MarkupData.ElementLocation} object, otherwise it will be {@link MarkupData.Location}.
  52. * 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`.
  53. *
  54. * **Default:** `false`
  55. */
  56. locationInfo?: boolean;
  57. /**
  58. * Specifies the resulting tree format.
  59. *
  60. * **Default:** `treeAdapters.default`
  61. */
  62. treeAdapter?: AST.TreeAdapter;
  63. }
  64. export interface SAXParserOptions {
  65. /**
  66. * Enables source code location information for the tokens.
  67. * When enabled, each token event handler will receive {@link MarkupData.Location} (or {@link MarkupData.StartTagLocation})
  68. * object as its last argument.
  69. */
  70. locationInfo?: boolean;
  71. }
  72. export interface SerializerOptions {
  73. /***
  74. * Specifies input tree format.
  75. *
  76. * **Default:** `treeAdapters.default`
  77. */
  78. treeAdapter?: AST.TreeAdapter;
  79. }
  80. }
  81. // AST
  82. //-----------------------------------------------------------------------------------
  83. declare namespace AST {
  84. /**
  85. * [Document mode](https://dom.spec.whatwg.org/#concept-document-limited-quirks).
  86. */
  87. type DocumentMode = 'no-quirks' | 'quirks' | 'limited-quirks';
  88. // Default tree adapter
  89. namespace Default {
  90. /**
  91. * Element attribute.
  92. */
  93. interface Attribute {
  94. /**
  95. * The name of the attribute.
  96. */
  97. name: string;
  98. /**
  99. * The value of the attribute.
  100. */
  101. value: string;
  102. /**
  103. * The namespace of the attribute.
  104. */
  105. namespace?: string;
  106. /**
  107. * The namespace-related prefix of the attribute.
  108. */
  109. prefix?: string;
  110. }
  111. /**
  112. * [Default tree adapter]{@link parse5.treeAdapters} Node interface.
  113. */
  114. interface Node {
  115. /**
  116. * The name of the node. E.g. {@link Document} will have `nodeName` equal to '#document'`.
  117. */
  118. nodeName: string;
  119. }
  120. /**
  121. * [Default tree adapter]{@link parse5.treeAdapters} ParentNode interface.
  122. */
  123. interface ParentNode {
  124. /**
  125. * Child nodes.
  126. */
  127. childNodes: Node[];
  128. }
  129. /**
  130. * [Default tree adapter]{@link parse5.treeAdapters} DocumentType interface.
  131. */
  132. export interface DocumentType extends Node {
  133. /**
  134. * The name of the node.
  135. */
  136. nodeName: '#documentType';
  137. /**
  138. * Document type name.
  139. */
  140. name: string;
  141. /**
  142. * Document type public identifier.
  143. */
  144. publicId: string;
  145. /**
  146. * Document type system identifier.
  147. */
  148. systemId: string;
  149. }
  150. /**
  151. * [Default tree adapter]{@link parse5.treeAdapters} Document interface.
  152. */
  153. export interface Document extends ParentNode {
  154. /**
  155. * The name of the node.
  156. */
  157. nodeName: '#document';
  158. /**
  159. * [Document mode](https://dom.spec.whatwg.org/#concept-document-limited-quirks).
  160. */
  161. mode: DocumentMode;
  162. }
  163. /**
  164. * [Default tree adapter]{@link parse5.treeAdapters} DocumentFragment interface.
  165. */
  166. export interface DocumentFragment extends ParentNode {
  167. /**
  168. * The name of the node.
  169. */
  170. nodeName: '#document-fragment';
  171. }
  172. /**
  173. * [Default tree adapter]{@link parse5.treeAdapters} Element interface.
  174. */
  175. export interface Element extends ParentNode {
  176. /**
  177. * The name of the node. Equals to element {@link tagName}.
  178. */
  179. nodeName: string;
  180. /**
  181. * Element tag name.
  182. */
  183. tagName: string;
  184. /**
  185. * Element namespace.
  186. */
  187. namespaceURI: string;
  188. /**
  189. * List of element attributes.
  190. */
  191. attrs: Attribute[];
  192. /**
  193. * Parent node.
  194. */
  195. parentNode: ParentNode;
  196. /**
  197. * Element source code location info. Available if location info is enabled via {@link Options.ParserOptions}.
  198. */
  199. __location?: MarkupData.ElementLocation;
  200. }
  201. /**
  202. * [Default tree adapter]{@link parse5.treeAdapters} CommentNode interface.
  203. */
  204. export interface CommentNode extends Node {
  205. /**
  206. * The name of the node.
  207. */
  208. nodeName: '#comment';
  209. /**
  210. * Comment text.
  211. */
  212. data: string;
  213. /**
  214. * Parent node.
  215. */
  216. parentNode: ParentNode;
  217. /**
  218. * Comment source code location info. Available if location info is enabled via {@link Options.ParserOptions}.
  219. */
  220. __location?: MarkupData.Location;
  221. }
  222. /**
  223. * [Default tree adapter]{@link parse5.treeAdapters} TextNode interface.
  224. */
  225. export interface TextNode extends Node {
  226. /**
  227. * The name of the node.
  228. */
  229. nodeName: '#text';
  230. /**
  231. * Text content.
  232. */
  233. value: string;
  234. /**
  235. * Parent node.
  236. */
  237. parentNode: ParentNode;
  238. /**
  239. * Text node source code location info. Available if location info is enabled via {@link Options.ParserOptions}.
  240. */
  241. __location?: MarkupData.Location;
  242. }
  243. }
  244. // htmlparser2 tree adapter
  245. namespace HtmlParser2 {
  246. /**
  247. * [htmlparser2 tree adapter]{@link parse5.treeAdapters} Node interface.
  248. */
  249. interface Node {
  250. /**
  251. * The type of the node. E.g. {@link Document} will have `type` equal to 'root'`.
  252. */
  253. type: string;
  254. /**
  255. * [DOM spec](https://dom.spec.whatwg.org/#dom-node-nodetype)-compatible node {@link type}.
  256. */
  257. nodeType: number;
  258. /**
  259. * Parent node.
  260. */
  261. parent: ParentNode;
  262. /**
  263. * Same as {@link parent}. [DOM spec](https://dom.spec.whatwg.org)-compatible alias.
  264. */
  265. parentNode: ParentNode;
  266. /**
  267. * Previous sibling.
  268. */
  269. prev: Node;
  270. /**
  271. * Same as {@link prev}. [DOM spec](https://dom.spec.whatwg.org)-compatible alias.
  272. */
  273. previousSibling: Node;
  274. /**
  275. * Next sibling.
  276. */
  277. next: Node;
  278. /**
  279. * Same as {@link next}. [DOM spec](https://dom.spec.whatwg.org)-compatible alias.
  280. */
  281. nextSibling: Node;
  282. }
  283. /**
  284. * [htmlparser2 tree adapter]{@link parse5.treeAdapters} ParentNode interface.
  285. */
  286. interface ParentNode extends Node {
  287. /**
  288. * Child nodes.
  289. */
  290. children: Node[];
  291. /**
  292. * Same as {@link children}. [DOM spec](https://dom.spec.whatwg.org)-compatible alias.
  293. */
  294. childNodes: Node[];
  295. /**
  296. * First child of the node.
  297. */
  298. firstChild: Node;
  299. /**
  300. * Last child of the node.
  301. */
  302. lastChild: Node;
  303. }
  304. /**
  305. * [htmlparser2 tree adapter]{@link parse5.treeAdapters} DocumentType interface.
  306. */
  307. export interface DocumentType extends Node {
  308. /**
  309. * The type of the node.
  310. */
  311. type: 'directive';
  312. /**
  313. * Node name.
  314. */
  315. name: '!doctype';
  316. /**
  317. * Serialized doctype {@link name}, {@link publicId} and {@link systemId}.
  318. */
  319. data: string;
  320. /**
  321. * Document type name.
  322. */
  323. 'x-name':string;
  324. /**
  325. * Document type public identifier.
  326. */
  327. 'x-publicId': string;
  328. /**
  329. * Document type system identifier.
  330. */
  331. 'x-systemId': string;
  332. }
  333. /**
  334. * [htmlparser2 tree adapter]{@link parse5.treeAdapters} Document interface.
  335. */
  336. export interface Document extends ParentNode {
  337. /**
  338. * The type of the node.
  339. */
  340. type: 'root';
  341. /**
  342. * The name of the node.
  343. */
  344. name: 'root';
  345. /**
  346. * [Document mode](https://dom.spec.whatwg.org/#concept-document-limited-quirks).
  347. */
  348. 'x-mode': DocumentMode;
  349. }
  350. /**
  351. * [htmlparser2 tree adapter]{@link parse5.treeAdapters} DocumentFragment interface.
  352. */
  353. export interface DocumentFragment extends ParentNode {
  354. /**
  355. * The type of the node.
  356. */
  357. type: 'root';
  358. /**
  359. * The name of the node.
  360. */
  361. name: 'root';
  362. }
  363. /**
  364. * [htmlparser2 tree adapter]{@link parse5.treeAdapters} Element interface.
  365. */
  366. export interface Element extends ParentNode {
  367. /**
  368. * The name of the node. Equals to element {@link tagName}.
  369. */
  370. name: string;
  371. /**
  372. * Element tag name.
  373. */
  374. tagName: string;
  375. /**
  376. * Element namespace.
  377. */
  378. namespace: string;
  379. /**
  380. * Element attributes.
  381. */
  382. attribs: { [name: string]: string };
  383. /**
  384. * Element attribute namespaces.
  385. */
  386. 'x-attribsNamespace': { [name: string]: string };
  387. /**
  388. * Element attribute namespace-related prefixes.
  389. */
  390. 'x-attribsPrefix': { [name: string]: string };
  391. /**
  392. * Element source code location info. Available if location info is enabled via {@link Options.ParserOptions}.
  393. */
  394. __location?: MarkupData.ElementLocation;
  395. }
  396. /**
  397. * [htmlparser2 tree adapter]{@link parse5.treeAdapters} CommentNode interface.
  398. */
  399. export interface CommentNode extends Node {
  400. /**
  401. * The name of the node.
  402. */
  403. name: 'comment';
  404. /**
  405. * Comment text.
  406. */
  407. data: string;
  408. /**
  409. * Same as {@link data}. [DOM spec](https://dom.spec.whatwg.org)-compatible alias.
  410. */
  411. nodeValue: string;
  412. /**
  413. * Comment source code location info. Available if location info is enabled via {@link Options.ParserOptions}.
  414. */
  415. __location?: MarkupData.Location;
  416. }
  417. /**
  418. * [htmlparser2 tree adapter]{@link parse5.treeAdapters} TextNode interface.
  419. */
  420. export interface TextNode extends Node {
  421. /**
  422. * The name of the node.
  423. */
  424. name: 'text';
  425. /**
  426. * Text content.
  427. */
  428. data: string;
  429. /**
  430. * Same as {@link data}. [DOM spec](https://dom.spec.whatwg.org)-compatible alias.
  431. */
  432. nodeValue: string;
  433. /**
  434. * Comment source code location info. Available if location info is enabled via {@link Options.ParserOptions}.
  435. */
  436. __location?: MarkupData.Location;
  437. }
  438. }
  439. // Unions
  440. // NOTE: we use `Object` in unions to support custom tree adapter implementations.
  441. // TypeScript Handbook suggests to always use `any` instead of `Object`, but in that
  442. // case language service hints `any` as type, instead of actual union name.
  443. /**
  444. * Generic Node interface.
  445. * Cast to the actual AST interface (e.g. {@link parse5.AST.Default.Node}) to get access to the properties.
  446. */
  447. type Node = Default.Node | HtmlParser2.Node | Object;
  448. /**
  449. * Generic ParentNode interface.
  450. * Cast to the actual AST interface (e.g. {@link parse5.AST.Default.ParentNode}) to get access to the properties.
  451. */
  452. type ParentNode = Default.ParentNode | HtmlParser2.ParentNode | Object;
  453. /**
  454. * Generic DocumentType interface.
  455. * Cast to the actual AST interface (e.g. {@link parse5.AST.Default.DocumentType}) to get access to the properties.
  456. */
  457. type DocumentType = Default.DocumentType | HtmlParser2.DocumentType | Object;
  458. /**
  459. * Generic Document interface.
  460. * Cast to the actual AST interface (e.g. {@link parse5.AST.Default.Document}) to get access to the properties.
  461. */
  462. type Document = Default.Document | HtmlParser2.Document | Object;
  463. /**
  464. * Generic DocumentFragment interface.
  465. * Cast to the actual AST interface (e.g. {@link parse5.AST.Default.DocumentFragment}) to get access to the properties.
  466. */
  467. type DocumentFragment = Default.DocumentFragment | HtmlParser2.DocumentFragment | Object;
  468. /**
  469. * Generic Element interface.
  470. * Cast to the actual AST interface (e.g. {@link parse5.AST.Default.Element}) to get access to the properties.
  471. */
  472. type Element = Default.Element | HtmlParser2.Element | Object;
  473. /**
  474. * Generic TextNode interface.
  475. * Cast to the actual AST interface (e.g. {@link parse5.AST.Default.TextNode}) to get access to the properties.
  476. */
  477. type TextNode = Default.TextNode | HtmlParser2.TextNode | Object;
  478. /**
  479. * Generic CommentNode interface.
  480. * Cast to the actual AST interface (e.g. {@link parse5.AST.Default.CommentNode}) to get access to the properties.
  481. */
  482. type CommentNode = Default.CommentNode | HtmlParser2.CommentNode | Object;
  483. // Tree adapter interface
  484. //-----------------------------------------------------------------------------------
  485. /**
  486. * Tree adapter is a set of utility functions that provides minimal required abstraction layer beetween parser and a specific AST format.
  487. * Note that `TreeAdapter` is not designed to be a general purpose AST manipulation library. You can build such library
  488. * on top of existing `TreeAdapter` or use one of the existing libraries from npm.
  489. *
  490. * @see [default implementation](https://github.com/inikulin/parse5/blob/master/lib/tree_adapters/default.js)
  491. */
  492. export interface TreeAdapter {
  493. /**
  494. * Creates a document node.
  495. */
  496. createDocument(): AST.Document;
  497. /**
  498. * Creates a document fragment node.
  499. */
  500. createDocumentFragment(): AST.DocumentFragment;
  501. /**
  502. * Creates an element node.
  503. *
  504. * @param tagName - Tag name of the element.
  505. * @param namespaceURI - Namespace of the element.
  506. * @param attrs - Attribute name-value pair array. Foreign attributes may contain `namespace` and `prefix` fields as well.
  507. */
  508. createElement(tagName: string, namespaceURI: string, attrs: AST.Default.Attribute[]): AST.Element;
  509. /**
  510. * Creates a comment node.
  511. *
  512. * @param data - Comment text.
  513. */
  514. createCommentNode(data: string): AST.CommentNode;
  515. /**
  516. * Appends a child node to the given parent node.
  517. *
  518. * @param parentNode - Parent node.
  519. * @param newNode - Child node.
  520. */
  521. appendChild(parentNode: AST.ParentNode, newNode: AST.Node): void;
  522. /**
  523. * Inserts a child node to the given parent node before the given reference node.
  524. *
  525. * @param parentNode - Parent node.
  526. * @param newNode - Child node.
  527. * @param referenceNode - Reference node.
  528. */
  529. insertBefore(parentNode: AST.ParentNode, newNode: AST.Node, referenceNode: AST.Node): void;
  530. /**
  531. * Sets the `<template>` element content element.
  532. *
  533. * @param templateElement - `<template>` element.
  534. * @param contentElement - Content element.
  535. */
  536. setTemplateContent(templateElement: AST.Element, contentElement: AST.DocumentFragment): void;
  537. /**
  538. * Returns the `<template>` element content element.
  539. *
  540. * @param templateElement - `<template>` element.
  541. */
  542. getTemplateContent(templateElement: AST.Element): AST.DocumentFragment;
  543. /**
  544. * Sets the document type. If the `document` already contains a document type node, the `name`, `publicId` and `systemId`
  545. * properties of this node will be updated with the provided values. Otherwise, creates a new document type node
  546. * with the given properties and inserts it into the `document`.
  547. *
  548. * @param document - Document node.
  549. * @param name - Document type name.
  550. * @param publicId - Document type public identifier.
  551. * @param systemId - Document type system identifier.
  552. */
  553. setDocumentType(document: AST.Document, name: string, publicId: string, systemId: string): void;
  554. /**
  555. * Sets the [document mode](https://dom.spec.whatwg.org/#concept-document-limited-quirks).
  556. *
  557. * @param document - Document node.
  558. * @param mode - Document mode.
  559. */
  560. setDocumentMode(document: AST.Document, mode: AST.DocumentMode): void;
  561. /**
  562. * Returns [document mode](https://dom.spec.whatwg.org/#concept-document-limited-quirks).
  563. *
  564. * @param document - Document node.
  565. */
  566. getDocumentMode(document: AST.Document): AST.DocumentMode;
  567. /**
  568. * Removes a node from its parent.
  569. *
  570. * @param node - Node to remove.
  571. */
  572. detachNode(node: AST.Node): void;
  573. /**
  574. * Inserts text into a node. If the last child of the node is a text node, the provided text will be appended to the
  575. * text node content. Otherwise, inserts a new text node with the given text.
  576. *
  577. * @param parentNode - Node to insert text into.
  578. * @param text - Text to insert.
  579. */
  580. insertText(parentNode: AST.ParentNode, text: string): void;
  581. /**
  582. * Inserts text into a sibling node that goes before the reference node. If this sibling node is the text node,
  583. * the provided text will be appended to the text node content. Otherwise, inserts a new sibling text node with
  584. * the given text before the reference node.
  585. *
  586. * @param parentNode - Node to insert text into.
  587. * @param text - Text to insert.
  588. * @param referenceNode - Node to insert text before.
  589. */
  590. insertTextBefore(parentNode: AST.ParentNode, text: string, referenceNode: AST.Node): void;
  591. /**
  592. * Copies attributes to the given element. Only attributes that are not yet present in the element are copied.
  593. *
  594. * @param recipient - Element to copy attributes into.
  595. * @param attrs - Attributes to copy.
  596. */
  597. adoptAttributes(recipient: AST.Element, attrs: AST.Default.Attribute[]): void;
  598. /**
  599. * Returns the first child of the given node.
  600. *
  601. * @param node - Node.
  602. */
  603. getFirstChild(node: AST.ParentNode): AST.Node;
  604. /**
  605. * Returns the given node's children in an array.
  606. *
  607. * @param node - Node.
  608. */
  609. getChildNodes(node: AST.ParentNode): AST.Node[];
  610. /**
  611. * Returns the given node's parent.
  612. *
  613. * @param node - Node.
  614. */
  615. getParentNode(node: AST.Node): AST.ParentNode;
  616. /**
  617. * Returns the given element's attributes in an array, in the form of name-value pairs.
  618. * Foreign attributes may contain `namespace` and `prefix` fields as well.
  619. *
  620. * @param element - Element.
  621. */
  622. getAttrList(element: AST.Element): AST.Default.Attribute[];
  623. /**
  624. * Returns the given element's tag name.
  625. *
  626. * @param element - Element.
  627. */
  628. getTagName(element: AST.Element): string;
  629. /**
  630. * Returns the given element's namespace.
  631. *
  632. * @param element - Element.
  633. */
  634. getNamespaceURI(element: AST.Element): string;
  635. /**
  636. * Returns the given text node's content.
  637. *
  638. * @param textNode - Text node.
  639. */
  640. getTextNodeContent(textNode: AST.TextNode): string;
  641. /**
  642. * Returns the given comment node's content.
  643. *
  644. * @param commentNode - Comment node.
  645. */
  646. getCommentNodeContent(commentNode: AST.CommentNode): string;
  647. /**
  648. * Returns the given document type node's name.
  649. *
  650. * @param doctypeNode - Document type node.
  651. */
  652. getDocumentTypeNodeName(doctypeNode: AST.DocumentType): string;
  653. /**
  654. * Returns the given document type node's public identifier.
  655. *
  656. * @param doctypeNode - Document type node.
  657. */
  658. getDocumentTypeNodePublicId(doctypeNode: AST.DocumentType): string;
  659. /**
  660. * Returns the given document type node's system identifier.
  661. *
  662. * @param doctypeNode - Document type node.
  663. */
  664. getDocumentTypeNodeSystemId(doctypeNode: AST.DocumentType): string;
  665. /**
  666. * Determines if the given node is a text node.
  667. *
  668. * @param node - Node.
  669. */
  670. isTextNode(node: AST.Node): boolean;
  671. /**
  672. * Determines if the given node is a comment node.
  673. *
  674. * @param node - Node.
  675. */
  676. isCommentNode(node: AST.Node): boolean;
  677. /**
  678. * Determines if the given node is a document type node.
  679. *
  680. * @param node - Node.
  681. */
  682. isDocumentTypeNode(node: AST.Node): boolean;
  683. /**
  684. * Determines if the given node is an element.
  685. *
  686. * @param node - Node.
  687. */
  688. isElementNode(node: AST.Node): boolean;
  689. }
  690. }
  691. // Included tree adapters
  692. //-----------------------------------------------------------------------------------
  693. /**
  694. * Provides built-in tree adapters that can be used for parsing and serialization.
  695. *
  696. * @example
  697. *```js
  698. *
  699. * const parse5 = require('parse5');
  700. *
  701. * // Uses the default tree adapter for parsing.
  702. * const document = parse5.parse('<div></div>', {
  703. * treeAdapter: parse5.treeAdapters.default
  704. * });
  705. *
  706. * // Uses the htmlparser2 tree adapter with the SerializerStream.
  707. * const serializer = new parse5.SerializerStream(node, {
  708. * treeAdapter: parse5.treeAdapters.htmlparser2
  709. * });
  710. * ```
  711. */
  712. export var treeAdapters: {
  713. /**
  714. * Default tree format for parse5.
  715. */
  716. default: AST.TreeAdapter,
  717. /**
  718. * Quite popular [htmlparser2](https://github.com/fb55/htmlparser2) tree format
  719. * (e.g. used by [cheerio](https://github.com/MatthewMueller/cheerio) and [jsdom](https://github.com/tmpvar/jsdom)).
  720. */
  721. htmlparser2: AST.TreeAdapter
  722. };
  723. // Shorthand methods
  724. //-----------------------------------------------------------------------------------
  725. /**
  726. * Parses an HTML string.
  727. *
  728. * @param html - Input HTML string.
  729. * @param options - Parsing options.
  730. *
  731. * @example
  732. * ```js
  733. *
  734. * const parse5 = require('parse5');
  735. *
  736. * const document = parse5.parse('<!DOCTYPE html><html><head></head><body>Hi there!</body></html>');
  737. *
  738. * console.log(document.childNodes[1].tagName); //> 'html'
  739. * ```
  740. */
  741. export function parse(html: string, options?: Options.ParserOptions): AST.Document;
  742. /**
  743. * Parses an HTML fragment.
  744. *
  745. * @param fragmentContext - Parsing context element. If specified, given fragment will be parsed as if it was set to the context element's `innerHTML` property.
  746. * @param html - Input HTML fragment string.
  747. * @param options - Parsing options.
  748. *
  749. * @example
  750. * ```js
  751. *
  752. * const parse5 = require('parse5');
  753. *
  754. * const documentFragment = parse5.parseFragment('<table></table>');
  755. *
  756. * console.log(documentFragment.childNodes[0].tagName); //> 'table'
  757. *
  758. * // Parses the html fragment in the context of the parsed <table> element.
  759. * const trFragment = parser.parseFragment(documentFragment.childNodes[0], '<tr><td>Shake it, baby</td></tr>');
  760. *
  761. * console.log(trFragment.childNodes[0].childNodes[0].tagName); //> 'td'
  762. * ```
  763. */
  764. export function parseFragment(fragmentContext: AST.Element, html: string, options?: Options.ParserOptions): AST.DocumentFragment;
  765. export function parseFragment(html: string, options?: Options.ParserOptions): AST.DocumentFragment;
  766. /**
  767. * Serializes an AST node to an HTML string.
  768. *
  769. * @param node - Node to serialize.
  770. * @param options - Serialization options.
  771. *
  772. * @example
  773. * ```js
  774. *
  775. * const parse5 = require('parse5');
  776. *
  777. * const document = parse5.parse('<!DOCTYPE html><html><head></head><body>Hi there!</body></html>');
  778. *
  779. * // Serializes a document.
  780. * const html = parse5.serialize(document);
  781. *
  782. * // Serializes the <html> element content.
  783. * const str = parse5.serialize(document.childNodes[1]);
  784. *
  785. * console.log(str); //> '<head></head><body>Hi there!</body>'
  786. * ```
  787. */
  788. export function serialize(node: AST.Node, options?: Options.SerializerOptions): string;
  789. // Parser stream
  790. //-----------------------------------------------------------------------------------
  791. /**
  792. * Streaming HTML parser with scripting support.
  793. * A [writable stream](https://nodejs.org/api/stream.html#stream_class_stream_writable).
  794. *
  795. * ** NOTE:** This API is available only for Node.js.
  796. *
  797. * @example
  798. * ```js
  799. *
  800. * const parse5 = require('parse5');
  801. * const http = require('http');
  802. *
  803. * // Fetch the page content and obtain it's <head> node
  804. * http.get('http://inikulin.github.io/parse5/', res => {
  805. * const parser = new parse5.ParserStream();
  806. *
  807. * parser.once('finish', () => {
  808. * console.log(parser.document.childNodes[1].childNodes[0].tagName); //> 'head'
  809. * });
  810. *
  811. * res.pipe(parser);
  812. * });
  813. * ```
  814. */
  815. export class ParserStream extends stream.Writable {
  816. /**
  817. * @param options - Parsing options.
  818. */
  819. constructor(options?: Options.ParserOptions);
  820. /**
  821. * The resulting document node.
  822. */
  823. document: AST.Document;
  824. /**
  825. * Raised then parser encounters a `<script>` element.
  826. * If this event has listeners, parsing will be suspended once it is emitted.
  827. * So, if `<script>` has the `src` attribute, you can fetch it, execute and then resume parsing just like browsers do.
  828. *
  829. * @param listener.scriptElement - The script element that caused the event.
  830. * @param listener.documentWrite - Write additional `html` at the current parsing position. Suitable for implementing the DOM `document.write` and `document.writeln` methods.
  831. * @param listener.documentWrite.html - HTML to write.
  832. * @param listener.resume - Resumes parsing.
  833. *
  834. * @example
  835. * ```js
  836. *
  837. * const parse = require('parse5');
  838. * const http = require('http');
  839. *
  840. * const parser = new parse5.ParserStream();
  841. *
  842. * parser.on('script', (scriptElement, documentWrite, resume) => {
  843. * const src = parse5.treeAdapters.default.getAttrList(scriptElement)[0].value;
  844. *
  845. * http.get(src, res => {
  846. * // Fetch the script content, execute it with DOM built around `parser.document` and
  847. * // `document.write` implemented using `documentWrite`.
  848. * ...
  849. * // Then resume parsing.
  850. * resume();
  851. * });
  852. * });
  853. *
  854. * parser.end('<script src="example.com/script.js"></script>');
  855. * ```
  856. */
  857. on(event: 'script', listener: (scriptElement: AST.Element, documentWrite: (html: string) => void, resume: () => void) => void): this;
  858. /**
  859. * WritableStream events
  860. */
  861. on(event: string, listener: Function): this;
  862. }
  863. // Plaint text conversion stream
  864. //-----------------------------------------------------------------------------------
  865. /**
  866. * Converts plain text files into HTML document as required by [HTML specification](https://html.spec.whatwg.org/#read-text).
  867. * A [writable stream](https://nodejs.org/api/stream.html#stream_class_stream_writable).
  868. *
  869. * ** NOTE:** This API is available only for Node.js.
  870. *
  871. * @example
  872. * ```js
  873. *
  874. * const parse5 = require('parse5');
  875. * const fs = require('fs');
  876. *
  877. * const file = fs.createReadStream('war_and_peace.txt');
  878. * const converter = new parse5.PlainTextConversionStream();
  879. *
  880. * converter.once('finish', () => {
  881. * console.log(converter.document.childNodes[1].childNodes[0].tagName); //> 'head'
  882. * });
  883. *
  884. * file.pipe(converter);
  885. * ```
  886. */
  887. export class PlainTextConversionStream extends ParserStream { }
  888. // SAX parser
  889. //-----------------------------------------------------------------------------------
  890. /**
  891. * Streaming [SAX](https://en.wikipedia.org/wiki/Simple_API_for_XML)-style HTML parser.
  892. * A [transform stream](https://nodejs.org/api/stream.html#stream_class_stream_transform)
  893. * (which means you can pipe *through* it, see example).
  894. *
  895. * ** NOTE:** This API is available only for Node.js.
  896. *
  897. * @example
  898. * ```js
  899. *
  900. * const parse5 = require('parse5');
  901. * const http = require('http');
  902. * const fs = require('fs');
  903. *
  904. * const file = fs.createWriteStream('/home/google.com.html');
  905. * const parser = new parse5.SAXParser();
  906. *
  907. * parser.on('text', text => {
  908. * // Handle page text content
  909. * ...
  910. * });
  911. *
  912. * http.get('http://google.com', res => {
  913. * // SAXParser is the Transform stream, which means you can pipe
  914. * // through it. So, you can analyze page content and, e.g., save it
  915. * // to the file at the same time:
  916. * res.pipe(parser).pipe(file);
  917. * });
  918. * ```
  919. */
  920. export class SAXParser extends stream.Transform {
  921. /**
  922. * @param options - Parsing options.
  923. */
  924. constructor(options?: Options.SAXParserOptions);
  925. /**
  926. * Raised when the parser encounters a start tag.
  927. *
  928. * @param listener.name - Tag name.
  929. * @param listener.attrs - List of attributes.
  930. * @param listener.selfClosing - Indicates if the tag is self-closing.
  931. * @param listener.location - Start tag source code location info. Available if location info is enabled via {@link Options.SAXParserOptions}.
  932. */
  933. on(event: 'startTag', listener: (name: string, attrs: AST.Default.Attribute[], selfClosing: boolean, location?: MarkupData.StartTagLocation) => void): this;
  934. /**
  935. * Raised then parser encounters an end tag.
  936. *
  937. * @param listener.name - Tag name.
  938. * @param listener.location - End tag source code location info. Available if location info is enabled via {@link Options.SAXParserOptions}.
  939. */
  940. on(event: 'endTag', listener: (name: string, location?: MarkupData.Location) => void): this;
  941. /**
  942. * Raised then parser encounters a comment.
  943. *
  944. * @param listener.text - Comment text.
  945. * @param listener.location - Comment source code location info. Available if location info is enabled via {@link Options.SAXParserOptions}.
  946. */
  947. on(event: 'comment', listener: (text: string, location?: MarkupData.Location) => void): this;
  948. /**
  949. * Raised then parser encounters text content.
  950. *
  951. * @param listener.text - Text content.
  952. * @param listener.location - Text content code location info. Available if location info is enabled via {@link Options.SAXParserOptions}.
  953. */
  954. on(event: 'text', listener: (text: string, location?: MarkupData.Location) => void): this;
  955. /**
  956. * Raised then parser encounters a [document type declaration](https://en.wikipedia.org/wiki/Document_type_declaration).
  957. *
  958. * @param listener.name - Document type name.
  959. * @param listener.publicId - Document type public identifier.
  960. * @param listener.systemId - Document type system identifier.
  961. * @param listener.location - Document type declaration source code location info. Available if location info is enabled via {@link Options.SAXParserOptions}.
  962. */
  963. on(event: 'doctype', listener: (name: string, publicId: string, systemId: string, location?: MarkupData.Location) => void): this;
  964. /**
  965. * TransformStream events
  966. */
  967. on(event: string, listener: Function): this;
  968. /**
  969. * Stops parsing. Useful if you want the parser to stop consuming CPU time once you've obtained the desired info
  970. * from the input stream. Doesn't prevent piping, so that data will flow through the parser as usual.
  971. *
  972. * @example
  973. * ```js
  974. *
  975. * const parse5 = require('parse5');
  976. * const http = require('http');
  977. * const fs = require('fs');
  978. *
  979. * const file = fs.createWriteStream('google.com.html');
  980. * const parser = new parse5.SAXParser();
  981. *
  982. * parser.on('doctype', (name, publicId, systemId) => {
  983. * // Process doctype info ans stop parsing
  984. * ...
  985. * parser.stop();
  986. * });
  987. *
  988. * http.get('http://google.com', res => {
  989. * // Despite the fact that parser.stop() was called whole
  990. * // content of the page will be written to the file
  991. * res.pipe(parser).pipe(file);
  992. * });
  993. * ```
  994. */
  995. stop(): void;
  996. }
  997. // Serializer stream
  998. //-----------------------------------------------------------------------------------
  999. /**
  1000. * Streaming AST node to an HTML serializer.
  1001. * A [readable stream](https://nodejs.org/api/stream.html#stream_class_stream_readable).
  1002. *
  1003. * ** NOTE:** This API is available only for Node.js.
  1004. *
  1005. * @example
  1006. * ```js
  1007. *
  1008. * const parse5 = require('parse5');
  1009. * const fs = require('fs');
  1010. *
  1011. * const file = fs.createWriteStream('/home/index.html');
  1012. *
  1013. * // Serializes the parsed document to HTML and writes it to the file.
  1014. * const document = parse5.parse('<body>Who is John Galt?</body>');
  1015. * const serializer = new parse5.SerializerStream(document);
  1016. *
  1017. * serializer.pipe(file);
  1018. * ```
  1019. */
  1020. export class SerializerStream extends stream.Readable {
  1021. /**
  1022. * Streaming AST node to an HTML serializer. A readable stream.
  1023. *
  1024. * @param node - Node to serialize.
  1025. * @param options - Serialization options.
  1026. */
  1027. constructor(node: AST.Node, options?: Options.SerializerOptions);
  1028. }