DOMAdaptor.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. /*************************************************************
  2. *
  3. * Copyright (c) 2018-2022 The MathJax Consortium
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /**
  18. * @fileoverview The DOMAdaptor interface and abstract class
  19. *
  20. * @author dpvc@mathjax.org (Davide Cervone)
  21. */
  22. import {OptionList} from '../util/Options.js';
  23. /**
  24. * The data for an attribute
  25. */
  26. export type AttributeData = {
  27. name: string,
  28. value: string
  29. };
  30. /**
  31. * The data for an elements page-based bounding box
  32. */
  33. export type PageBBox = {
  34. left: number,
  35. right: number,
  36. top: number,
  37. bottom: number
  38. };
  39. /*****************************************************************/
  40. /**
  41. * The interface for the DOMAdaptor
  42. *
  43. * @template N The HTMLElement node class
  44. * @template T The Text node class
  45. * @template D The Document class
  46. */
  47. export interface DOMAdaptor<N, T, D> {
  48. /**
  49. * Document in which the nodes are to be created
  50. */
  51. document: D;
  52. /**
  53. * @param {string} text The serialized document to be parsed
  54. * @param {string} format The format (e.g., 'text/html' or 'text/xhtml')
  55. * @return {D} The parsed document
  56. */
  57. parse(text: string, format?: string): D;
  58. /**
  59. * @param {string} kind The tag name of the HTML node to be created
  60. * @param {OptionList} def The properties to set for the created node
  61. * @param {(N|T)[]} children The child nodes for the created HTML node
  62. * @param {string} ns The namespace in which to create the node
  63. * @return {N} The generated HTML tree
  64. */
  65. node(kind: string, def?: OptionList, children?: (N | T)[], ns?: string): N;
  66. /**
  67. * @param {string} text The text from which to create an HTML text node
  68. * @return {T} The generated text node with the given text
  69. */
  70. text(text: string): T;
  71. /**
  72. * @param {D} doc The document whose head is to be obtained
  73. * @return {N} The document.head element
  74. */
  75. head(doc: D): N;
  76. /**
  77. * @param {D} doc The document whose body is to be obtained
  78. * @return {N} The document.body element
  79. */
  80. body(doc: D): N;
  81. /**
  82. * @param {D} doc The document whose documentElement is to be obtained
  83. * @return {N} The documentElement
  84. */
  85. root(doc: D): N;
  86. /**
  87. * @param {D} doc The document whose doctype is to be obtained
  88. * @return {string} The DOCTYPE comment
  89. */
  90. doctype(doc: D): string;
  91. /**
  92. * @param {N} node The node to search for tags
  93. * @param {string} name The name of the tag to search for
  94. * @param {string} ns The namespace to search in (or null for no namespace)
  95. * @return {N[]} The list of tags found
  96. */
  97. tags(node: N, name: string, ns?: string): N[];
  98. /**
  99. * Get a list of containers (to be searched for math). These can be
  100. * specified by CSS selector, or as actual DOM elements or arrays of such.
  101. *
  102. * @param {(string | N | N[])[]} nodes The array of items to make into a container list
  103. * @param {D} document The document in which to search
  104. * @return {N[]} The array of containers to search
  105. */
  106. getElements(nodes: (string | N | N[])[], document: D): N[];
  107. /**
  108. * Determine if a container node contains a given node somewhere in its DOM tree
  109. *
  110. * @param {N} container The container to search
  111. * @param {N|T} node The node to look for
  112. * @return {boolean} True if the node is in the container's DOM tree
  113. */
  114. contains(container: N, node: N | T): boolean;
  115. /**
  116. * @param {N|T} node The HTML node whose parent is to be obtained
  117. * @return {N} The parent node of the given one
  118. */
  119. parent(node: N | T): N;
  120. /**
  121. * @param {N} node The HTML node to be appended to
  122. * @param {N|T} child The node or text to be appended
  123. * @return {N|T} The appended node
  124. */
  125. append(node: N, child: N | T): N | T;
  126. /**
  127. * @param {N|T} nchild The node or text to be inserted
  128. * @param {N|T} ochild The node or text where the new child is to be added before it
  129. */
  130. insert(nchild: N | T, ochild: N | T): void;
  131. /**
  132. * @param {N|T} child The node or text to be removed from its parent
  133. * @return {N|T} The removed node
  134. */
  135. remove(child: N | T): N | T;
  136. /**
  137. * @param {N|T} nnode The node to replace with
  138. * @param {N|T} onode The child to be replaced
  139. * @return {N|T} The removed node
  140. */
  141. replace(nnode: N | T, onode: N | T): N | T;
  142. /**
  143. * @param {N} node The HTML node to be cloned
  144. * @return {N} The copied node
  145. */
  146. clone(node: N): N;
  147. /**
  148. * @param {T} node The HTML text node to be split
  149. * @param {number} n The index of the character where the split will occur
  150. */
  151. split(node: T, n: number): T;
  152. /**
  153. * @param {N|T} node The HTML node whose sibling is to be obtained
  154. * @return {N|T} The node following the given one (or null)
  155. */
  156. next(node: N | T): N | T;
  157. /**
  158. * @param {N|T} node The HTML node whose sibling is to be obtained
  159. * @return {N|T} The node preceding the given one (or null)
  160. */
  161. previous(node: N | T): N | T;
  162. /**
  163. * @param {N} node The HTML node whose child is to be obtained
  164. * @return {N|T} The first child of the given node (or null)
  165. */
  166. firstChild(node: N): N | T;
  167. /**
  168. * @param {N} node The HTML node whose child is to be obtained
  169. * @return {N} The last child of the given node (or null)
  170. */
  171. lastChild(node: N): N | T;
  172. /**
  173. * @param {N} node The HTML node whose children are to be obtained
  174. * @return {(N|T)[]} Array of children for the given node (not a live list)
  175. */
  176. childNodes(node: N): (N | T)[];
  177. /**
  178. * @param {N} node The HTML node whose child is to be obtained
  179. * @param {number} i The index of the child to return
  180. * @return {N|T} The i-th child node of the given node (or null)
  181. */
  182. childNode(node: N, i: number): N | T;
  183. /**
  184. * @param {N | T} node The HTML node whose tag or node name is to be obtained
  185. * @return {string} The tag or node name of the given node
  186. */
  187. kind(node: N | T): string;
  188. /**
  189. * @param {N|T} node The HTML node whose value is to be obtained
  190. * @return {string} The value of the given node
  191. */
  192. value(node: N | T): string;
  193. /**
  194. * @param {N} node The HTML node whose text content is to be obtained
  195. * @return {string} The text content of the given node
  196. */
  197. textContent(node: N): string;
  198. /**
  199. * @param {N} node The HTML node whose inner HTML string is to be obtained
  200. * @return {string} The serialized content of the node
  201. */
  202. innerHTML(node: N): string;
  203. /**
  204. * @param {N} node The HTML node whose outer HTML string is to be obtained
  205. * @return {string} The serialized node and its content
  206. */
  207. outerHTML(node: N): string;
  208. /**
  209. * @param {N} node The HTML node whose serialized string is to be obtained
  210. * @return {string} The serialized node and its content
  211. */
  212. serializeXML(node: N): string;
  213. /**
  214. * @param {N} node The HTML node whose attribute is to be set
  215. * @param {string|number} name The name of the attribute to set
  216. * @param {string} value The new value of the attribute
  217. * @param {string=} ns The namespace to use for the attribute
  218. */
  219. setAttribute(node: N, name: string, value: string | number, ns?: string): void;
  220. /**
  221. * @param {N} node The HTML element whose attributes are to be set
  222. * @param {OptionList} def The attributes to set on that node
  223. */
  224. setAttributes(node: N, def: OptionList): void;
  225. /**
  226. * @param {N} node The HTML node whose attribute is to be obtained
  227. * @param {string} name The name of the attribute to get
  228. * @return {string} The value of the given attribute of the given node
  229. */
  230. getAttribute(node: N, name: string): string;
  231. /**
  232. * @param {N} node The HTML node whose attribute is to be removed
  233. * @param {string} name The name of the attribute to remove
  234. */
  235. removeAttribute(node: N, name: string): void;
  236. /**
  237. * @param {N} node The HTML node whose attribute is to be tested
  238. * @param {string} name The name of the attribute to test
  239. * @return {boolean} True of the node has the given attribute defined
  240. */
  241. hasAttribute(node: N, name: string): boolean;
  242. /**
  243. * @param {N} node The HTML node whose attributes are to be returned
  244. * @return {AttributeData[]} The list of attributes
  245. */
  246. allAttributes(node: N): AttributeData[];
  247. /**
  248. * @param {N} node The HTML node whose class is to be augmented
  249. * @param {string} name The class to be added
  250. */
  251. addClass(node: N, name: string): void;
  252. /**
  253. * @param {N} node The HTML node whose class is to be changed
  254. * @param {string} name The class to be removed
  255. */
  256. removeClass(node: N, name: string): void;
  257. /**
  258. * @param {N} node The HTML node whose class is to be tested
  259. * @param {string} name The class to test
  260. * @return {boolean} True if the node has the given class
  261. */
  262. hasClass(node: N, name: string): boolean;
  263. /**
  264. * @param {N} node The HTML node whose class list is needed
  265. * @return {string[]} An array of the class names for this node
  266. */
  267. allClasses(node: N): string[];
  268. /**
  269. * @param {N} node The HTML node whose style is to be changed
  270. * @param {string} name The style to be set
  271. * @param {string} value The new value of the style
  272. */
  273. setStyle(node: N, name: string, value: string): void;
  274. /**
  275. * @param {N} node The HTML node whose style is to be obtained
  276. * @param {string} name The style to be obtained
  277. * @return {string} The value of the style
  278. */
  279. getStyle(node: N, name: string): string;
  280. /**
  281. * @param {N} node The HTML node whose styles are to be returned
  282. * @return {string} The cssText for the styles
  283. */
  284. allStyles(node: N): string;
  285. /**
  286. * @param {N} node The stylesheet node where the rule will be added
  287. * @param {string[]} rules The rule to add at the beginning of the stylesheet
  288. */
  289. insertRules(node: N, rules: string[]): void;
  290. /**
  291. * @param {N} node The HTML node whose font size is to be determined
  292. * @return {number} The font size (in pixels) of the node
  293. */
  294. fontSize(node: N): number;
  295. /**
  296. * @param {N} node The HTML node whose font family is to be determined
  297. * @return {string} The font family
  298. */
  299. fontFamily(node: N): string;
  300. /**
  301. * @param {N} node The HTML node whose dimensions are to be determined
  302. * @param {number} em The number of pixels in an em
  303. * @param {boolean} local True if local coordinates are to be used in SVG elements
  304. * @return {[number, number]} The width and height (in ems) of the element
  305. */
  306. nodeSize(node: N, em?: number, local?: boolean): [number, number];
  307. /**
  308. * @param {N} node The HTML node whose BBox is to be determined
  309. * @return {PageBBox} BBox as {left, right, top, bottom} position on the page (in pixels)
  310. */
  311. nodeBBox(node: N): PageBBox;
  312. }
  313. /*****************************************************************/
  314. /**
  315. * Abstract DOMAdaptor class for creating HTML elements
  316. *
  317. * @template N The HTMLElement node class
  318. * @template T The Text node class
  319. * @template D The Document class
  320. */
  321. export abstract class AbstractDOMAdaptor<N, T, D> implements DOMAdaptor<N, T, D> {
  322. /**
  323. * The document in which the HTML nodes will be created
  324. */
  325. public document: D;
  326. /**
  327. * @param {D} document The document in which the nodes will be created
  328. * @constructor
  329. */
  330. constructor(document: D = null) {
  331. this.document = document;
  332. }
  333. /**
  334. * @override
  335. */
  336. public abstract parse(text: string, format?: string): D;
  337. /**
  338. * @override
  339. */
  340. public node(kind: string, def: OptionList = {}, children: (N | T)[] = [], ns?: string) {
  341. const node = this.create(kind, ns);
  342. this.setAttributes(node, def);
  343. for (const child of children) {
  344. this.append(node, child);
  345. }
  346. return node as N;
  347. }
  348. /**
  349. * @param {string} kind The type of the node to create
  350. * @param {string} ns The optional namespace in which to create the node
  351. * @return {N} The created node
  352. */
  353. protected abstract create(kind: string, ns?: string): N;
  354. /**
  355. * @override
  356. */
  357. public abstract text(text: string): T;
  358. /**
  359. * @param {N} node The HTML element whose attributes are to be set
  360. * @param {OptionList} def The attributes to set on that node
  361. */
  362. public setAttributes(node: N, def: OptionList) {
  363. if (def.style && typeof(def.style) !== 'string') {
  364. for (let key of Object.keys(def.style)) {
  365. this.setStyle(node, key.replace(/-([a-z])/g, (_m, c) => c.toUpperCase()), def.style[key]);
  366. }
  367. }
  368. if (def.properties) {
  369. for (let key of Object.keys(def.properties)) {
  370. (node as OptionList)[key] = def.properties[key];
  371. }
  372. }
  373. for (let key of Object.keys(def)) {
  374. if ((key !== 'style' || typeof(def.style) === 'string') && key !== 'properties') {
  375. this.setAttribute(node, key, def[key]);
  376. }
  377. }
  378. }
  379. /**
  380. * @override
  381. */
  382. public abstract head(doc: D): N;
  383. /**
  384. * @override
  385. */
  386. public abstract body(doc: D): N;
  387. /**
  388. * @override
  389. */
  390. public abstract root(doc: D): N;
  391. /**
  392. * @override
  393. */
  394. public abstract doctype(doc: D): string;
  395. /**
  396. * @override
  397. */
  398. public abstract tags(node: N, name: string, ns?: string): N[];
  399. /**
  400. * @override
  401. */
  402. public abstract getElements(nodes: (string | N | N[])[], document: D): N[];
  403. /**
  404. * @override
  405. */
  406. public abstract contains(container: N, node: N | T): boolean;
  407. /**
  408. * @override
  409. */
  410. public abstract parent(node: N | T): N;
  411. /**
  412. * @override
  413. */
  414. public abstract append(node: N, child: N | T): N | T;
  415. /**
  416. * @override
  417. */
  418. public abstract insert(nchild: N | T, ochild: N | T): void;
  419. /**
  420. * @override
  421. */
  422. public abstract remove(child: N | T): N | T;
  423. /**
  424. * @override
  425. */
  426. public replace(nnode: N | T, onode: N | T) {
  427. this.insert(nnode, onode);
  428. this.remove(onode);
  429. return onode;
  430. }
  431. /**
  432. * @override
  433. */
  434. public abstract clone(node: N): N;
  435. /**
  436. * @override
  437. */
  438. public abstract split(node: T, n: number): T;
  439. /**
  440. * @override
  441. */
  442. public abstract next(node: N | T): N | T;
  443. /**
  444. * @override
  445. */
  446. public abstract previous(node: N | T): N | T;
  447. /**
  448. * @override
  449. */
  450. public abstract firstChild(node: N): N | T;
  451. /**
  452. * @override
  453. */
  454. public abstract lastChild(node: N): N | T;
  455. /**
  456. * @override
  457. */
  458. public abstract childNodes(node: N): (N | T)[];
  459. /**
  460. * @override
  461. */
  462. public childNode(node: N, i: number) {
  463. return this.childNodes(node)[i];
  464. }
  465. /**
  466. * @override
  467. */
  468. public abstract kind(node: N | T): string;
  469. /**
  470. * @override
  471. */
  472. public abstract value(node: N | T): string;
  473. /**
  474. * @override
  475. */
  476. public abstract textContent(node: N): string;
  477. /**
  478. * @override
  479. */
  480. public abstract innerHTML(node: N): string;
  481. /**
  482. * @override
  483. */
  484. public abstract outerHTML(node: N): string;
  485. /**
  486. * @override
  487. */
  488. public abstract serializeXML(node: N): string;
  489. /**
  490. * @override
  491. */
  492. public abstract setAttribute(node: N, name: string, value: string, ns?: string): void;
  493. /**
  494. * @override
  495. */
  496. public abstract getAttribute(node: N, name: string): string;
  497. /**
  498. * @override
  499. */
  500. public abstract removeAttribute(node: N, name: string): void;
  501. /**
  502. * @override
  503. */
  504. public abstract hasAttribute(node: N, name: string): boolean;
  505. /**
  506. * @override
  507. */
  508. public abstract allAttributes(node: N): AttributeData[];
  509. /**
  510. * @override
  511. */
  512. public abstract addClass(node: N, name: string): void;
  513. /**
  514. * @override
  515. */
  516. public abstract removeClass(node: N, name: string): void;
  517. /**
  518. * @override
  519. */
  520. public abstract hasClass(node: N, name: string): boolean;
  521. /**
  522. * @override
  523. */
  524. public allClasses(node: N) {
  525. const classes = this.getAttribute(node, 'class');
  526. return (!classes ? [] as string[] :
  527. classes.replace(/ +/g, ' ').replace(/^ /, '').replace(/ $/, '').split(/ /));
  528. }
  529. /**
  530. * @override
  531. */
  532. public abstract setStyle(node: N, name: string, value: string): void;
  533. /**
  534. * @override
  535. */
  536. public abstract getStyle(node: N, name: string): string;
  537. /**
  538. * @override
  539. */
  540. public abstract allStyles(node: N): string;
  541. /**
  542. * @override
  543. */
  544. public abstract insertRules(node: N, rules: string[]): void;
  545. /**
  546. * @override
  547. */
  548. public abstract fontSize(node: N): number;
  549. /**
  550. * @override
  551. */
  552. public abstract fontFamily(node: N): string;
  553. /**
  554. * @override
  555. */
  556. public abstract nodeSize(node: N, em?: number, local?: boolean): [number, number];
  557. /**
  558. * @override
  559. */
  560. public abstract nodeBBox(node: N): PageBBox;
  561. }