1# Interface: TreeAdapter 2 3Tree adapter is a set of utility functions that provides minimal required abstraction layer beetween parser and a specific AST format. Note that `TreeAdapter` is not designed to be a general purpose AST manipulation library. You can build such library on top of existing `TreeAdapter` or use one of the existing libraries from npm. 4 5*__See__*: [default implementation](https://github.com/inikulin/parse5/blob/master/packages/parse5/lib/tree-adapters/default.js) 6 7### Methods 8 9* [adoptAttributes](#adoptattributes) 10* [appendChild](#appendchild) 11* [createCommentNode](#createcommentnode) 12* [createDocument](#createdocument) 13* [createDocumentFragment](#createdocumentfragment) 14* [createElement](#createelement) 15* [detachNode](#detachnode) 16* [getAttrList](#getattrlist) 17* [getChildNodes](#getchildnodes) 18* [getCommentNodeContent](#getcommentnodecontent) 19* [getDocumentMode](#getdocumentmode) 20* [getDocumentTypeNodeName](#getdocumenttypenodename) 21* [getDocumentTypeNodePublicId](#getdocumenttypenodepublicid) 22* [getDocumentTypeNodeSystemId](#getdocumenttypenodesystemid) 23* [getFirstChild](#getfirstchild) 24* [getNamespaceURI](#getnamespaceuri) 25* [getNodeSourceCodeLocation](#getnodesourcecodelocation) 26* [getParentNode](#getparentnode) 27* [getTagName](#gettagname) 28* [getTemplateContent](#gettemplatecontent) 29* [getTextNodeContent](#gettextnodecontent) 30* [insertBefore](#insertbefore) 31* [insertText](#inserttext) 32* [insertTextBefore](#inserttextbefore) 33* [isCommentNode](#iscommentnode) 34* [isDocumentTypeNode](#isdocumenttypenode) 35* [isElementNode](#iselementnode) 36* [isTextNode](#istextnode) 37* [setDocumentMode](#setdocumentmode) 38* [setDocumentType](#setdocumenttype) 39* [setNodeSourceCodeLocation](#setnodesourcecodelocation) 40* [setTemplateContent](#settemplatecontent) 41* [updateNodeSourceCodeLocation](#updatenodesourcecodelocation) 42--- 43 44## Methods 45 46<a id="adoptattributes"></a> 47 48### adoptAttributes 49 50▸ **adoptAttributes**(recipient: *Element*, attrs: *Attribute[]*): `void` 51 52Copies attributes to the given element. Only attributes that are not yet present in the element are copied. 53 54**Parameters:** 55 56| Param | Type | Description | 57| ------ | ------ | ------ | 58| recipient | Element | Element to copy attributes into. | 59| attrs | Attribute[] | Attributes to copy. | 60 61**Returns:** `void` 62 63___ 64<a id="appendchild"></a> 65 66### appendChild 67 68▸ **appendChild**(parentNode: *Node*, newNode: *Node*): `void` 69 70Appends a child node to the given parent node. 71 72**Parameters:** 73 74| Param | Type | Description | 75| ------ | ------ | ------ | 76| parentNode | ParentNode | Parent node. | 77| newNode | Node | Child node. | 78 79**Returns:** `void` 80 81___ 82<a id="createcommentnode"></a> 83 84### createCommentNode 85 86▸ **createCommentNode**(data: *`string`*): CommentNode 87 88Creates a comment node. 89 90**Parameters:** 91 92| Param | Type | Description | 93| ------ | ------ | ------ | 94| data | `string` | Comment text. | 95 96**Returns:** CommentNode 97 98___ 99<a id="createdocument"></a> 100 101### createDocument 102 103▸ **createDocument**(): Document 104 105Creates a document node. 106 107**Returns:** Document 108 109___ 110<a id="createdocumentfragment"></a> 111 112### createDocumentFragment 113 114▸ **createDocumentFragment**(): DocumentFragment 115 116Creates a document fragment node. 117 118**Returns:** DocumentFragment 119 120___ 121<a id="createelement"></a> 122 123### createElement 124 125▸ **createElement**(tagName: *`string`*, namespaceURI: *`string`*, attrs: *Attribute[]*): Element 126 127Creates an element node. 128 129**Parameters:** 130 131| Param | Type | Description | 132| ------ | ------ | ------ | 133| tagName | `string` | Tag name of the element. | 134| namespaceURI | `string` | Namespace of the element. | 135| attrs | Attribute[] | Attribute name-value pair array. Foreign attributes may contain \`namespace\` and \`prefix\` fields as well. | 136 137**Returns:** Element 138 139___ 140<a id="detachnode"></a> 141 142### detachNode 143 144▸ **detachNode**(node: *Node*): `void` 145 146Removes a node from its parent. 147 148**Parameters:** 149 150| Param | Type | Description | 151| ------ | ------ | ------ | 152| node | Node | Node to remove. | 153 154**Returns:** `void` 155 156___ 157<a id="getattrlist"></a> 158 159### getAttrList 160 161▸ **getAttrList**(element: *Element*): Attribute[] 162 163Returns the given element's attributes in an array, in the form of name-value pairs. Foreign attributes may contain `namespace` and `prefix` fields as well. 164 165**Parameters:** 166 167| Param | Type | Description | 168| ------ | ------ | ------ | 169| element | Element | Element. | 170 171**Returns:** Attribute[] 172 173___ 174<a id="getchildnodes"></a> 175 176### getChildNodes 177 178▸ **getChildNodes**(node: *Node*): Node[] 179 180Returns the given node's children in an array. 181 182**Parameters:** 183 184| Param | Type | Description | 185| ------ | ------ | ------ | 186| node | ParentNode | Node. | 187 188**Returns:** Node[] 189 190___ 191<a id="getcommentnodecontent"></a> 192 193### getCommentNodeContent 194 195▸ **getCommentNodeContent**(commentNode: *CommentNode*): `string` 196 197Returns the given comment node's content. 198 199**Parameters:** 200 201| Param | Type | Description | 202| ------ | ------ | ------ | 203| commentNode | CommentNode | Comment node. | 204 205**Returns:** `string` 206 207___ 208<a id="getdocumentmode"></a> 209 210### getDocumentMode 211 212▸ **getDocumentMode**(document: *Document*): *"no-quirks" | "quirks" | "limited-quirks"* 213 214Returns [document mode](https://dom.spec.whatwg.org/#concept-document-limited-quirks). 215 216**Parameters:** 217 218| Param | Type | Description | 219| ------ | ------ | ------ | 220| document | Document | Document node. | 221 222**Returns:** *"no-quirks" | "quirks" | "limited-quirks"* 223 224___ 225<a id="getdocumenttypenodename"></a> 226 227### getDocumentTypeNodeName 228 229▸ **getDocumentTypeNodeName**(doctypeNode: *DocumentType*): `string` 230 231Returns the given document type node's name. 232 233**Parameters:** 234 235| Param | Type | Description | 236| ------ | ------ | ------ | 237| doctypeNode | DocumentType | Document type node. | 238 239**Returns:** `string` 240 241___ 242<a id="getdocumenttypenodepublicid"></a> 243 244### getDocumentTypeNodePublicId 245 246▸ **getDocumentTypeNodePublicId**(doctypeNode: *DocumentType*): `string` 247 248Returns the given document type node's public identifier. 249 250**Parameters:** 251 252| Param | Type | Description | 253| ------ | ------ | ------ | 254| doctypeNode | DocumentType | Document type node. | 255 256**Returns:** `string` 257 258___ 259<a id="getdocumenttypenodesystemid"></a> 260 261### getDocumentTypeNodeSystemId 262 263▸ **getDocumentTypeNodeSystemId**(doctypeNode: *DocumentType*): `string` 264 265Returns the given document type node's system identifier. 266 267**Parameters:** 268 269| Param | Type | Description | 270| ------ | ------ | ------ | 271| doctypeNode | DocumentType | Document type node. | 272 273**Returns:** `string` 274 275___ 276<a id="getfirstchild"></a> 277 278### getFirstChild 279 280▸ **getFirstChild**(node: *Node*): Node 281 282Returns the first child of the given node. 283 284**Parameters:** 285 286| Param | Type | Description | 287| ------ | ------ | ------ | 288| node | ParentNode | Node. | 289 290**Returns:** Node 291 292___ 293<a id="getnamespaceuri"></a> 294 295### getNamespaceURI 296 297▸ **getNamespaceURI**(element: *Element*): `string` 298 299Returns the given element's namespace. 300 301**Parameters:** 302 303| Param | Type | Description | 304| ------ | ------ | ------ | 305| element | Element | Element. | 306 307**Returns:** `string` 308 309___ 310<a id="getnodesourcecodelocation"></a> 311 312### getNodeSourceCodeLocation 313 314▸ **getNodeSourceCodeLocation**(node: *Node*): [Location](../source-code-location/location.md) | [ElementLocation](../source-code-location/element-location.md) 315 316Returns the given node's source code location information. 317 318**Parameters:** 319 320| Param | Type | Description | 321| ------ | ------ | ------ | 322| node | Node | Node. | 323 324**Returns:** [Location](../source-code-location/location.md) | [ElementLocation](../source-code-location/element-location.md) 325 326___ 327<a id="getparentnode"></a> 328 329### getParentNode 330 331▸ **getParentNode**(node: *Node*): ParentNode 332 333Returns the given node's parent. 334 335**Parameters:** 336 337| Param | Type | Description | 338| ------ | ------ | ------ | 339| node | Node | Node. | 340 341**Returns:** ParentNode 342 343___ 344<a id="gettagname"></a> 345 346### getTagName 347 348▸ **getTagName**(element: *Element*): `string` 349 350Returns the given element's tag name. 351 352**Parameters:** 353 354| Param | Type | Description | 355| ------ | ------ | ------ | 356| element | Element | Element. | 357 358**Returns:** `string` 359 360___ 361<a id="gettemplatecontent"></a> 362 363### getTemplateContent 364 365▸ **getTemplateContent**(templateElement: *Element*): DocumentFragment 366 367Returns the `<template>` element content element. 368 369**Parameters:** 370 371| Param | Type | Description | 372| ------ | ------ | ------ | 373| templateElement | Element | `<template>` element. | 374 375**Returns:** DocumentFragment 376 377___ 378<a id="gettextnodecontent"></a> 379 380### getTextNodeContent 381 382▸ **getTextNodeContent**(textNode: *TextNode*): `string` 383 384Returns the given text node's content. 385 386**Parameters:** 387 388| Param | Type | Description | 389| ------ | ------ | ------ | 390| textNode | TextNode | Text node. | 391 392**Returns:** `string` 393 394___ 395<a id="insertbefore"></a> 396 397### insertBefore 398 399▸ **insertBefore**(parentNode: *Node*, newNode: *Node*, referenceNode: *Node*): `void` 400 401Inserts a child node to the given parent node before the given reference node. 402 403**Parameters:** 404 405| Param | Type | Description | 406| ------ | ------ | ------ | 407| parentNode | ParentNode | Parent node. | 408| newNode | Node | Child node. | 409| referenceNode | Node | Reference node. | 410 411**Returns:** `void` 412 413___ 414<a id="inserttext"></a> 415 416### insertText 417 418▸ **insertText**(parentNode: *Node*, text: *`string`*): `void` 419 420Inserts text into a node. If the last child of the node is a text node, the provided text will be appended to the text node content. Otherwise, inserts a new text node with the given text. 421 422**Parameters:** 423 424| Param | Type | Description | 425| ------ | ------ | ------ | 426| parentNode | ParentNode | Node to insert text into. | 427| text | `string` | Text to insert. | 428 429**Returns:** `void` 430 431___ 432<a id="inserttextbefore"></a> 433 434### insertTextBefore 435 436▸ **insertTextBefore**(parentNode: *Node*, text: *`string`*, referenceNode: *Node*): `void` 437 438Inserts text into a sibling node that goes before the reference node. If this sibling node is the text node, the provided text will be appended to the text node content. Otherwise, inserts a new sibling text node with the given text before the reference node. 439 440**Parameters:** 441 442| Param | Type | Description | 443| ------ | ------ | ------ | 444| parentNode | ParentNode | Node to insert text into. | 445| text | `string` | Text to insert. | 446| referenceNode | Node | Node to insert text before. | 447 448**Returns:** `void` 449 450___ 451<a id="iscommentnode"></a> 452 453### isCommentNode 454 455▸ **isCommentNode**(node: *Node*): `boolean` 456 457Determines if the given node is a comment node. 458 459**Parameters:** 460 461| Param | Type | Description | 462| ------ | ------ | ------ | 463| node | Node | Node. | 464 465**Returns:** `boolean` 466 467___ 468<a id="isdocumenttypenode"></a> 469 470### isDocumentTypeNode 471 472▸ **isDocumentTypeNode**(node: *Node*): `boolean` 473 474Determines if the given node is a document type node. 475 476**Parameters:** 477 478| Param | Type | Description | 479| ------ | ------ | ------ | 480| node | Node | Node. | 481 482**Returns:** `boolean` 483 484___ 485<a id="iselementnode"></a> 486 487### isElementNode 488 489▸ **isElementNode**(node: *Node*): `boolean` 490 491Determines if the given node is an element. 492 493**Parameters:** 494 495| Param | Type | Description | 496| ------ | ------ | ------ | 497| node | Node | Node. | 498 499**Returns:** `boolean` 500 501___ 502<a id="istextnode"></a> 503 504### isTextNode 505 506▸ **isTextNode**(node: *Node*): `boolean` 507 508Determines if the given node is a text node. 509 510**Parameters:** 511 512| Param | Type | Description | 513| ------ | ------ | ------ | 514| node | Node | Node. | 515 516**Returns:** `boolean` 517 518___ 519<a id="setdocumentmode"></a> 520 521### setDocumentMode 522 523▸ **setDocumentMode**(document: *Document*, mode: *"no-quirks" | "quirks" | "limited-quirks"*): `void` 524 525Sets the [document mode](https://dom.spec.whatwg.org/#concept-document-limited-quirks). 526 527**Parameters:** 528 529| Param | Type | Description | 530| ------ | ------ | ------ | 531| document | Document | Document node. | 532| mode | *"no-quirks" | "quirks" | "limited-quirks"* | Document mode. | 533 534**Returns:** `void` 535 536___ 537<a id="setdocumenttype"></a> 538 539### setDocumentType 540 541▸ **setDocumentType**(document: *Document*, name: *`string`*, publicId: *`string`*, systemId: *`string`*): `void` 542 543Sets the document type. If the `document` already contains a document type node, the `name`, `publicId` and `systemId` properties of this node will be updated with the provided values. Otherwise, creates a new document type node with the given properties and inserts it into the `document`. 544 545**Parameters:** 546 547| Param | Type | Description | 548| ------ | ------ | ------ | 549| document | Document | Document node. | 550| name | `string` | Document type name. | 551| publicId | `string` | Document type public identifier. | 552| systemId | `string` | Document type system identifier. | 553 554**Returns:** `void` 555 556___ 557<a id="setnodesourcecodelocation"></a> 558 559### setNodeSourceCodeLocation 560 561▸ **setNodeSourceCodeLocation**(node: *Node*, location: *[Location](../source-code-location/location.md) | [ElementLocation](../source-code-location/element-location.md)*): `void` 562 563Attaches source code location information to the node. 564 565**Parameters:** 566 567| Param | Type | Description | 568| ------ | ------ | ------ | 569| node | Node | Node. | 570| location | [Location](../source-code-location/location.md) | [ElementLocation](../source-code-location/element-location.md) | Source code location information. | 571 572**Returns:** `void` 573 574___ 575<a id="settemplatecontent"></a> 576 577### setTemplateContent 578 579▸ **setTemplateContent**(templateElement: *Element*, contentElement: *DocumentFragment*): `void` 580 581Sets the `<template>` element content element. 582 583**Parameters:** 584 585| Param | Type | Description | 586| ------ | ------ | ------ | 587| templateElement | Element | `<template>` element. | 588| contentElement | DocumentFragment | Content element. | 589 590**Returns:** `void` 591___ 592<a id="updatenodesourcecodelocation"></a> 593 594### updateNodeSourceCodeLocation 595 596▸ **updateNodeSourceCodeLocation**(node: *Node*, endLocation: *[EndLocation](../source-code-location/end-location.md)*): `void` 597 598Updates the source code location of nodes. 599 600**Parameters:** 601 602| Param | Type | Description | 603| ------ | ------ | ------ | 604| node | Node | Node. | 605| endLocation | [EndLocation](../source-code-location/end-location.md) | Source code location information of the end of the node. | 606 607**Returns:** `void` 608___ 609