interface.d.ts 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. import type { DOCUMENT_MODE, NS } from '../common/html.js';
  2. import type { Attribute, ElementLocation } from '../common/token.js';
  3. export interface TreeAdapterTypeMap<Node = unknown, ParentNode = unknown, ChildNode = unknown, Document = unknown, DocumentFragment = unknown, Element = unknown, CommentNode = unknown, TextNode = unknown, Template = unknown, DocumentType = unknown> {
  4. node: Node;
  5. parentNode: ParentNode;
  6. childNode: ChildNode;
  7. document: Document;
  8. documentFragment: DocumentFragment;
  9. element: Element;
  10. commentNode: CommentNode;
  11. textNode: TextNode;
  12. template: Template;
  13. documentType: DocumentType;
  14. }
  15. /**
  16. * Tree adapter is a set of utility functions that provides minimal required abstraction layer beetween parser and a specific AST format.
  17. * Note that `TreeAdapter` is not designed to be a general purpose AST manipulation library. You can build such library
  18. * on top of existing `TreeAdapter` or use one of the existing libraries from npm.
  19. *
  20. * @see Have a look at the default tree adapter for reference.
  21. */
  22. export interface TreeAdapter<T extends TreeAdapterTypeMap = TreeAdapterTypeMap> {
  23. /**
  24. * Copies attributes to the given element. Only attributes that are not yet present in the element are copied.
  25. *
  26. * @param recipient - Element to copy attributes into.
  27. * @param attrs - Attributes to copy.
  28. */
  29. adoptAttributes(recipient: T['element'], attrs: Attribute[]): void;
  30. /**
  31. * Appends a child node to the given parent node.
  32. *
  33. * @param parentNode - Parent node.
  34. * @param newNode - Child node.
  35. */
  36. appendChild(parentNode: T['parentNode'], newNode: T['childNode']): void;
  37. /**
  38. * Creates a comment node.
  39. *
  40. * @param data - Comment text.
  41. */
  42. createCommentNode(data: string): T['commentNode'];
  43. /**
  44. * Creates a text node.
  45. *
  46. * @param value - Text.
  47. */
  48. createTextNode(value: string): T['textNode'];
  49. /**
  50. * Creates a document node.
  51. */
  52. createDocument(): T['document'];
  53. /**
  54. * Creates a document fragment node.
  55. */
  56. createDocumentFragment(): T['documentFragment'];
  57. /**
  58. * Creates an element node.
  59. *
  60. * @param tagName - Tag name of the element.
  61. * @param namespaceURI - Namespace of the element.
  62. * @param attrs - Attribute name-value pair array. Foreign attributes may contain `namespace` and `prefix` fields as well.
  63. */
  64. createElement(tagName: string, namespaceURI: NS, attrs: Attribute[]): T['element'];
  65. /**
  66. * Removes a node from its parent.
  67. *
  68. * @param node - Node to remove.
  69. */
  70. detachNode(node: T['childNode']): void;
  71. /**
  72. * Returns the given element's attributes in an array, in the form of name-value pairs.
  73. * Foreign attributes may contain `namespace` and `prefix` fields as well.
  74. *
  75. * @param element - Element.
  76. */
  77. getAttrList(element: T['element']): Attribute[];
  78. /**
  79. * Returns the given node's children in an array.
  80. *
  81. * @param node - Node.
  82. */
  83. getChildNodes(node: T['parentNode']): T['childNode'][];
  84. /**
  85. * Returns the given comment node's content.
  86. *
  87. * @param commentNode - Comment node.
  88. */
  89. getCommentNodeContent(commentNode: T['commentNode']): string;
  90. /**
  91. * Returns [document mode](https://dom.spec.whatwg.org/#concept-document-limited-quirks).
  92. *
  93. * @param document - Document node.
  94. */
  95. getDocumentMode(document: T['document']): DOCUMENT_MODE;
  96. /**
  97. * Returns the given document type node's name.
  98. *
  99. * @param doctypeNode - Document type node.
  100. */
  101. getDocumentTypeNodeName(doctypeNode: T['documentType']): string;
  102. /**
  103. * Returns the given document type node's public identifier.
  104. *
  105. * @param doctypeNode - Document type node.
  106. */
  107. getDocumentTypeNodePublicId(doctypeNode: T['documentType']): string;
  108. /**
  109. * Returns the given document type node's system identifier.
  110. *
  111. * @param doctypeNode - Document type node.
  112. */
  113. getDocumentTypeNodeSystemId(doctypeNode: T['documentType']): string;
  114. /**
  115. * Returns the first child of the given node.
  116. *
  117. * @param node - Node.
  118. */
  119. getFirstChild(node: T['parentNode']): T['childNode'] | null;
  120. /**
  121. * Returns the given element's namespace.
  122. *
  123. * @param element - Element.
  124. */
  125. getNamespaceURI(element: T['element']): NS;
  126. /**
  127. * Returns the given node's source code location information.
  128. *
  129. * @param node - Node.
  130. */
  131. getNodeSourceCodeLocation(node: T['node']): ElementLocation | undefined | null;
  132. /**
  133. * Returns the given node's parent.
  134. *
  135. * @param node - Node.
  136. */
  137. getParentNode(node: T['node']): T['parentNode'] | null;
  138. /**
  139. * Returns the given element's tag name.
  140. *
  141. * @param element - Element.
  142. */
  143. getTagName(element: T['element']): string;
  144. /**
  145. * Returns the given text node's content.
  146. *
  147. * @param textNode - Text node.
  148. */
  149. getTextNodeContent(textNode: T['textNode']): string;
  150. /**
  151. * Returns the `<template>` element content element.
  152. *
  153. * @param templateElement - `<template>` element.
  154. */
  155. getTemplateContent(templateElement: T['template']): T['documentFragment'];
  156. /**
  157. * Inserts a child node to the given parent node before the given reference node.
  158. *
  159. * @param parentNode - Parent node.
  160. * @param newNode - Child node.
  161. * @param referenceNode - Reference node.
  162. */
  163. insertBefore(parentNode: T['parentNode'], newNode: T['childNode'], referenceNode: T['childNode']): void;
  164. /**
  165. * Inserts text into a node. If the last child of the node is a text node, the provided text will be appended to the
  166. * text node content. Otherwise, inserts a new text node with the given text.
  167. *
  168. * @param parentNode - Node to insert text into.
  169. * @param text - Text to insert.
  170. */
  171. insertText(parentNode: T['parentNode'], text: string): void;
  172. /**
  173. * Inserts text into a sibling node that goes before the reference node. If this sibling node is the text node,
  174. * the provided text will be appended to the text node content. Otherwise, inserts a new sibling text node with
  175. * the given text before the reference node.
  176. *
  177. * @param parentNode - Node to insert text into.
  178. * @param text - Text to insert.
  179. * @param referenceNode - Node to insert text before.
  180. */
  181. insertTextBefore(parentNode: T['parentNode'], text: string, referenceNode: T['childNode']): void;
  182. /**
  183. * Determines if the given node is a comment node.
  184. *
  185. * @param node - Node.
  186. */
  187. isCommentNode(node: T['node']): node is T['commentNode'];
  188. /**
  189. * Determines if the given node is a document type node.
  190. *
  191. * @param node - Node.
  192. */
  193. isDocumentTypeNode(node: T['node']): node is T['documentType'];
  194. /**
  195. * Determines if the given node is an element.
  196. *
  197. * @param node - Node.
  198. */
  199. isElementNode(node: T['node']): node is T['element'];
  200. /**
  201. * Determines if the given node is a text node.
  202. *
  203. * @param node - Node.
  204. */
  205. isTextNode(node: T['node']): node is T['textNode'];
  206. /**
  207. * Sets the [document mode](https://dom.spec.whatwg.org/#concept-document-limited-quirks).
  208. *
  209. * @param document - Document node.
  210. * @param mode - Document mode.
  211. */
  212. setDocumentMode(document: T['document'], mode: DOCUMENT_MODE): void;
  213. /**
  214. * Sets the document type. If the `document` already contains a document type node, the `name`, `publicId` and `systemId`
  215. * properties of this node will be updated with the provided values. Otherwise, creates a new document type node
  216. * with the given properties and inserts it into the `document`.
  217. *
  218. * @param document - Document node.
  219. * @param name - Document type name.
  220. * @param publicId - Document type public identifier.
  221. * @param systemId - Document type system identifier.
  222. */
  223. setDocumentType(document: T['document'], name: string, publicId: string, systemId: string): void;
  224. /**
  225. * Attaches source code location information to the node.
  226. *
  227. * @param node - Node.
  228. */
  229. setNodeSourceCodeLocation(node: T['node'], location: ElementLocation | null): void;
  230. /**
  231. * Updates the source code location information of the node.
  232. *
  233. * @param node - Node.
  234. */
  235. updateNodeSourceCodeLocation(node: T['node'], location: Partial<ElementLocation>): void;
  236. /**
  237. * Sets the `<template>` element content element.
  238. *
  239. * @param templateElement - `<template>` element.
  240. * @param contentElement - Content element.
  241. */
  242. setTemplateContent(templateElement: T['template'], contentElement: T['documentFragment']): void;
  243. /**
  244. * Optional callback for elements being pushed to the stack of open elements.
  245. *
  246. * @param element The element being pushed to the stack of open elements.
  247. */
  248. onItemPush?: (item: T['element']) => void;
  249. /**
  250. * Optional callback for elements being popped from the stack of open elements.
  251. *
  252. * @param item The element being popped.
  253. */
  254. onItemPop?: (item: T['element'], newTop: T['parentNode']) => void;
  255. }