open-element-stack.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. import { TAG_ID as $, NS, NUMBERED_HEADERS } from '../common/html.js';
  2. //Element utils
  3. const IMPLICIT_END_TAG_REQUIRED = new Set([$.DD, $.DT, $.LI, $.OPTGROUP, $.OPTION, $.P, $.RB, $.RP, $.RT, $.RTC]);
  4. const IMPLICIT_END_TAG_REQUIRED_THOROUGHLY = new Set([
  5. ...IMPLICIT_END_TAG_REQUIRED,
  6. $.CAPTION,
  7. $.COLGROUP,
  8. $.TBODY,
  9. $.TD,
  10. $.TFOOT,
  11. $.TH,
  12. $.THEAD,
  13. $.TR,
  14. ]);
  15. const SCOPING_ELEMENTS_HTML = new Set([
  16. $.APPLET,
  17. $.CAPTION,
  18. $.HTML,
  19. $.MARQUEE,
  20. $.OBJECT,
  21. $.TABLE,
  22. $.TD,
  23. $.TEMPLATE,
  24. $.TH,
  25. ]);
  26. const SCOPING_ELEMENTS_HTML_LIST = new Set([...SCOPING_ELEMENTS_HTML, $.OL, $.UL]);
  27. const SCOPING_ELEMENTS_HTML_BUTTON = new Set([...SCOPING_ELEMENTS_HTML, $.BUTTON]);
  28. const SCOPING_ELEMENTS_MATHML = new Set([$.ANNOTATION_XML, $.MI, $.MN, $.MO, $.MS, $.MTEXT]);
  29. const SCOPING_ELEMENTS_SVG = new Set([$.DESC, $.FOREIGN_OBJECT, $.TITLE]);
  30. const TABLE_ROW_CONTEXT = new Set([$.TR, $.TEMPLATE, $.HTML]);
  31. const TABLE_BODY_CONTEXT = new Set([$.TBODY, $.TFOOT, $.THEAD, $.TEMPLATE, $.HTML]);
  32. const TABLE_CONTEXT = new Set([$.TABLE, $.TEMPLATE, $.HTML]);
  33. const TABLE_CELLS = new Set([$.TD, $.TH]);
  34. //Stack of open elements
  35. export class OpenElementStack {
  36. get currentTmplContentOrNode() {
  37. return this._isInTemplate() ? this.treeAdapter.getTemplateContent(this.current) : this.current;
  38. }
  39. constructor(document, treeAdapter, handler) {
  40. this.treeAdapter = treeAdapter;
  41. this.handler = handler;
  42. this.items = [];
  43. this.tagIDs = [];
  44. this.stackTop = -1;
  45. this.tmplCount = 0;
  46. this.currentTagId = $.UNKNOWN;
  47. this.current = document;
  48. }
  49. //Index of element
  50. _indexOf(element) {
  51. return this.items.lastIndexOf(element, this.stackTop);
  52. }
  53. //Update current element
  54. _isInTemplate() {
  55. return this.currentTagId === $.TEMPLATE && this.treeAdapter.getNamespaceURI(this.current) === NS.HTML;
  56. }
  57. _updateCurrentElement() {
  58. this.current = this.items[this.stackTop];
  59. this.currentTagId = this.tagIDs[this.stackTop];
  60. }
  61. //Mutations
  62. push(element, tagID) {
  63. this.stackTop++;
  64. this.items[this.stackTop] = element;
  65. this.current = element;
  66. this.tagIDs[this.stackTop] = tagID;
  67. this.currentTagId = tagID;
  68. if (this._isInTemplate()) {
  69. this.tmplCount++;
  70. }
  71. this.handler.onItemPush(element, tagID, true);
  72. }
  73. pop() {
  74. const popped = this.current;
  75. if (this.tmplCount > 0 && this._isInTemplate()) {
  76. this.tmplCount--;
  77. }
  78. this.stackTop--;
  79. this._updateCurrentElement();
  80. this.handler.onItemPop(popped, true);
  81. }
  82. replace(oldElement, newElement) {
  83. const idx = this._indexOf(oldElement);
  84. this.items[idx] = newElement;
  85. if (idx === this.stackTop) {
  86. this.current = newElement;
  87. }
  88. }
  89. insertAfter(referenceElement, newElement, newElementID) {
  90. const insertionIdx = this._indexOf(referenceElement) + 1;
  91. this.items.splice(insertionIdx, 0, newElement);
  92. this.tagIDs.splice(insertionIdx, 0, newElementID);
  93. this.stackTop++;
  94. if (insertionIdx === this.stackTop) {
  95. this._updateCurrentElement();
  96. }
  97. this.handler.onItemPush(this.current, this.currentTagId, insertionIdx === this.stackTop);
  98. }
  99. popUntilTagNamePopped(tagName) {
  100. let targetIdx = this.stackTop + 1;
  101. do {
  102. targetIdx = this.tagIDs.lastIndexOf(tagName, targetIdx - 1);
  103. } while (targetIdx > 0 && this.treeAdapter.getNamespaceURI(this.items[targetIdx]) !== NS.HTML);
  104. this.shortenToLength(targetIdx < 0 ? 0 : targetIdx);
  105. }
  106. shortenToLength(idx) {
  107. while (this.stackTop >= idx) {
  108. const popped = this.current;
  109. if (this.tmplCount > 0 && this._isInTemplate()) {
  110. this.tmplCount -= 1;
  111. }
  112. this.stackTop--;
  113. this._updateCurrentElement();
  114. this.handler.onItemPop(popped, this.stackTop < idx);
  115. }
  116. }
  117. popUntilElementPopped(element) {
  118. const idx = this._indexOf(element);
  119. this.shortenToLength(idx < 0 ? 0 : idx);
  120. }
  121. popUntilPopped(tagNames, targetNS) {
  122. const idx = this._indexOfTagNames(tagNames, targetNS);
  123. this.shortenToLength(idx < 0 ? 0 : idx);
  124. }
  125. popUntilNumberedHeaderPopped() {
  126. this.popUntilPopped(NUMBERED_HEADERS, NS.HTML);
  127. }
  128. popUntilTableCellPopped() {
  129. this.popUntilPopped(TABLE_CELLS, NS.HTML);
  130. }
  131. popAllUpToHtmlElement() {
  132. //NOTE: here we assume that the root <html> element is always first in the open element stack, so
  133. //we perform this fast stack clean up.
  134. this.tmplCount = 0;
  135. this.shortenToLength(1);
  136. }
  137. _indexOfTagNames(tagNames, namespace) {
  138. for (let i = this.stackTop; i >= 0; i--) {
  139. if (tagNames.has(this.tagIDs[i]) && this.treeAdapter.getNamespaceURI(this.items[i]) === namespace) {
  140. return i;
  141. }
  142. }
  143. return -1;
  144. }
  145. clearBackTo(tagNames, targetNS) {
  146. const idx = this._indexOfTagNames(tagNames, targetNS);
  147. this.shortenToLength(idx + 1);
  148. }
  149. clearBackToTableContext() {
  150. this.clearBackTo(TABLE_CONTEXT, NS.HTML);
  151. }
  152. clearBackToTableBodyContext() {
  153. this.clearBackTo(TABLE_BODY_CONTEXT, NS.HTML);
  154. }
  155. clearBackToTableRowContext() {
  156. this.clearBackTo(TABLE_ROW_CONTEXT, NS.HTML);
  157. }
  158. remove(element) {
  159. const idx = this._indexOf(element);
  160. if (idx >= 0) {
  161. if (idx === this.stackTop) {
  162. this.pop();
  163. }
  164. else {
  165. this.items.splice(idx, 1);
  166. this.tagIDs.splice(idx, 1);
  167. this.stackTop--;
  168. this._updateCurrentElement();
  169. this.handler.onItemPop(element, false);
  170. }
  171. }
  172. }
  173. //Search
  174. tryPeekProperlyNestedBodyElement() {
  175. //Properly nested <body> element (should be second element in stack).
  176. return this.stackTop >= 1 && this.tagIDs[1] === $.BODY ? this.items[1] : null;
  177. }
  178. contains(element) {
  179. return this._indexOf(element) > -1;
  180. }
  181. getCommonAncestor(element) {
  182. const elementIdx = this._indexOf(element) - 1;
  183. return elementIdx >= 0 ? this.items[elementIdx] : null;
  184. }
  185. isRootHtmlElementCurrent() {
  186. return this.stackTop === 0 && this.tagIDs[0] === $.HTML;
  187. }
  188. //Element in scope
  189. hasInDynamicScope(tagName, htmlScope) {
  190. for (let i = this.stackTop; i >= 0; i--) {
  191. const tn = this.tagIDs[i];
  192. switch (this.treeAdapter.getNamespaceURI(this.items[i])) {
  193. case NS.HTML: {
  194. if (tn === tagName)
  195. return true;
  196. if (htmlScope.has(tn))
  197. return false;
  198. break;
  199. }
  200. case NS.SVG: {
  201. if (SCOPING_ELEMENTS_SVG.has(tn))
  202. return false;
  203. break;
  204. }
  205. case NS.MATHML: {
  206. if (SCOPING_ELEMENTS_MATHML.has(tn))
  207. return false;
  208. break;
  209. }
  210. }
  211. }
  212. return true;
  213. }
  214. hasInScope(tagName) {
  215. return this.hasInDynamicScope(tagName, SCOPING_ELEMENTS_HTML);
  216. }
  217. hasInListItemScope(tagName) {
  218. return this.hasInDynamicScope(tagName, SCOPING_ELEMENTS_HTML_LIST);
  219. }
  220. hasInButtonScope(tagName) {
  221. return this.hasInDynamicScope(tagName, SCOPING_ELEMENTS_HTML_BUTTON);
  222. }
  223. hasNumberedHeaderInScope() {
  224. for (let i = this.stackTop; i >= 0; i--) {
  225. const tn = this.tagIDs[i];
  226. switch (this.treeAdapter.getNamespaceURI(this.items[i])) {
  227. case NS.HTML: {
  228. if (NUMBERED_HEADERS.has(tn))
  229. return true;
  230. if (SCOPING_ELEMENTS_HTML.has(tn))
  231. return false;
  232. break;
  233. }
  234. case NS.SVG: {
  235. if (SCOPING_ELEMENTS_SVG.has(tn))
  236. return false;
  237. break;
  238. }
  239. case NS.MATHML: {
  240. if (SCOPING_ELEMENTS_MATHML.has(tn))
  241. return false;
  242. break;
  243. }
  244. }
  245. }
  246. return true;
  247. }
  248. hasInTableScope(tagName) {
  249. for (let i = this.stackTop; i >= 0; i--) {
  250. if (this.treeAdapter.getNamespaceURI(this.items[i]) !== NS.HTML) {
  251. continue;
  252. }
  253. switch (this.tagIDs[i]) {
  254. case tagName: {
  255. return true;
  256. }
  257. case $.TABLE:
  258. case $.HTML: {
  259. return false;
  260. }
  261. }
  262. }
  263. return true;
  264. }
  265. hasTableBodyContextInTableScope() {
  266. for (let i = this.stackTop; i >= 0; i--) {
  267. if (this.treeAdapter.getNamespaceURI(this.items[i]) !== NS.HTML) {
  268. continue;
  269. }
  270. switch (this.tagIDs[i]) {
  271. case $.TBODY:
  272. case $.THEAD:
  273. case $.TFOOT: {
  274. return true;
  275. }
  276. case $.TABLE:
  277. case $.HTML: {
  278. return false;
  279. }
  280. }
  281. }
  282. return true;
  283. }
  284. hasInSelectScope(tagName) {
  285. for (let i = this.stackTop; i >= 0; i--) {
  286. if (this.treeAdapter.getNamespaceURI(this.items[i]) !== NS.HTML) {
  287. continue;
  288. }
  289. switch (this.tagIDs[i]) {
  290. case tagName: {
  291. return true;
  292. }
  293. case $.OPTION:
  294. case $.OPTGROUP: {
  295. break;
  296. }
  297. default: {
  298. return false;
  299. }
  300. }
  301. }
  302. return true;
  303. }
  304. //Implied end tags
  305. generateImpliedEndTags() {
  306. while (IMPLICIT_END_TAG_REQUIRED.has(this.currentTagId)) {
  307. this.pop();
  308. }
  309. }
  310. generateImpliedEndTagsThoroughly() {
  311. while (IMPLICIT_END_TAG_REQUIRED_THOROUGHLY.has(this.currentTagId)) {
  312. this.pop();
  313. }
  314. }
  315. generateImpliedEndTagsWithExclusion(exclusionId) {
  316. while (this.currentTagId !== exclusionId && IMPLICIT_END_TAG_REQUIRED_THOROUGHLY.has(this.currentTagId)) {
  317. this.pop();
  318. }
  319. }
  320. }