open-element-stack.js 12 KB

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