open-element-stack.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. if (this.current && this.currentTagId !== undefined) {
  101. this.handler.onItemPush(this.current, this.currentTagId, insertionIdx === this.stackTop);
  102. }
  103. }
  104. popUntilTagNamePopped(tagName) {
  105. let targetIdx = this.stackTop + 1;
  106. do {
  107. targetIdx = this.tagIDs.lastIndexOf(tagName, targetIdx - 1);
  108. } while (targetIdx > 0 && this.treeAdapter.getNamespaceURI(this.items[targetIdx]) !== html_js_1.NS.HTML);
  109. this.shortenToLength(Math.max(targetIdx, 0));
  110. }
  111. shortenToLength(idx) {
  112. while (this.stackTop >= idx) {
  113. const popped = this.current;
  114. if (this.tmplCount > 0 && this._isInTemplate()) {
  115. this.tmplCount -= 1;
  116. }
  117. this.stackTop--;
  118. this._updateCurrentElement();
  119. this.handler.onItemPop(popped, this.stackTop < idx);
  120. }
  121. }
  122. popUntilElementPopped(element) {
  123. const idx = this._indexOf(element);
  124. this.shortenToLength(Math.max(idx, 0));
  125. }
  126. popUntilPopped(tagNames, targetNS) {
  127. const idx = this._indexOfTagNames(tagNames, targetNS);
  128. this.shortenToLength(Math.max(idx, 0));
  129. }
  130. popUntilNumberedHeaderPopped() {
  131. this.popUntilPopped(html_js_1.NUMBERED_HEADERS, html_js_1.NS.HTML);
  132. }
  133. popUntilTableCellPopped() {
  134. this.popUntilPopped(TABLE_CELLS, html_js_1.NS.HTML);
  135. }
  136. popAllUpToHtmlElement() {
  137. //NOTE: here we assume that the root <html> element is always first in the open element stack, so
  138. //we perform this fast stack clean up.
  139. this.tmplCount = 0;
  140. this.shortenToLength(1);
  141. }
  142. _indexOfTagNames(tagNames, namespace) {
  143. for (let i = this.stackTop; i >= 0; i--) {
  144. if (tagNames.has(this.tagIDs[i]) && this.treeAdapter.getNamespaceURI(this.items[i]) === namespace) {
  145. return i;
  146. }
  147. }
  148. return -1;
  149. }
  150. clearBackTo(tagNames, targetNS) {
  151. const idx = this._indexOfTagNames(tagNames, targetNS);
  152. this.shortenToLength(idx + 1);
  153. }
  154. clearBackToTableContext() {
  155. this.clearBackTo(TABLE_CONTEXT, html_js_1.NS.HTML);
  156. }
  157. clearBackToTableBodyContext() {
  158. this.clearBackTo(TABLE_BODY_CONTEXT, html_js_1.NS.HTML);
  159. }
  160. clearBackToTableRowContext() {
  161. this.clearBackTo(TABLE_ROW_CONTEXT, html_js_1.NS.HTML);
  162. }
  163. remove(element) {
  164. const idx = this._indexOf(element);
  165. if (idx >= 0) {
  166. if (idx === this.stackTop) {
  167. this.pop();
  168. }
  169. else {
  170. this.items.splice(idx, 1);
  171. this.tagIDs.splice(idx, 1);
  172. this.stackTop--;
  173. this._updateCurrentElement();
  174. this.handler.onItemPop(element, false);
  175. }
  176. }
  177. }
  178. //Search
  179. tryPeekProperlyNestedBodyElement() {
  180. //Properly nested <body> element (should be second element in stack).
  181. return this.stackTop >= 1 && this.tagIDs[1] === html_js_1.TAG_ID.BODY ? this.items[1] : null;
  182. }
  183. contains(element) {
  184. return this._indexOf(element) > -1;
  185. }
  186. getCommonAncestor(element) {
  187. const elementIdx = this._indexOf(element) - 1;
  188. return elementIdx >= 0 ? this.items[elementIdx] : null;
  189. }
  190. isRootHtmlElementCurrent() {
  191. return this.stackTop === 0 && this.tagIDs[0] === html_js_1.TAG_ID.HTML;
  192. }
  193. //Element in scope
  194. hasInDynamicScope(tagName, htmlScope) {
  195. for (let i = this.stackTop; i >= 0; i--) {
  196. const tn = this.tagIDs[i];
  197. switch (this.treeAdapter.getNamespaceURI(this.items[i])) {
  198. case html_js_1.NS.HTML: {
  199. if (tn === tagName)
  200. return true;
  201. if (htmlScope.has(tn))
  202. return false;
  203. break;
  204. }
  205. case html_js_1.NS.SVG: {
  206. if (SCOPING_ELEMENTS_SVG.has(tn))
  207. return false;
  208. break;
  209. }
  210. case html_js_1.NS.MATHML: {
  211. if (SCOPING_ELEMENTS_MATHML.has(tn))
  212. return false;
  213. break;
  214. }
  215. }
  216. }
  217. return true;
  218. }
  219. hasInScope(tagName) {
  220. return this.hasInDynamicScope(tagName, SCOPING_ELEMENTS_HTML);
  221. }
  222. hasInListItemScope(tagName) {
  223. return this.hasInDynamicScope(tagName, SCOPING_ELEMENTS_HTML_LIST);
  224. }
  225. hasInButtonScope(tagName) {
  226. return this.hasInDynamicScope(tagName, SCOPING_ELEMENTS_HTML_BUTTON);
  227. }
  228. hasNumberedHeaderInScope() {
  229. for (let i = this.stackTop; i >= 0; i--) {
  230. const tn = this.tagIDs[i];
  231. switch (this.treeAdapter.getNamespaceURI(this.items[i])) {
  232. case html_js_1.NS.HTML: {
  233. if (html_js_1.NUMBERED_HEADERS.has(tn))
  234. return true;
  235. if (SCOPING_ELEMENTS_HTML.has(tn))
  236. return false;
  237. break;
  238. }
  239. case html_js_1.NS.SVG: {
  240. if (SCOPING_ELEMENTS_SVG.has(tn))
  241. return false;
  242. break;
  243. }
  244. case html_js_1.NS.MATHML: {
  245. if (SCOPING_ELEMENTS_MATHML.has(tn))
  246. return false;
  247. break;
  248. }
  249. }
  250. }
  251. return true;
  252. }
  253. hasInTableScope(tagName) {
  254. for (let i = this.stackTop; i >= 0; i--) {
  255. if (this.treeAdapter.getNamespaceURI(this.items[i]) !== html_js_1.NS.HTML) {
  256. continue;
  257. }
  258. switch (this.tagIDs[i]) {
  259. case tagName: {
  260. return true;
  261. }
  262. case html_js_1.TAG_ID.TABLE:
  263. case html_js_1.TAG_ID.HTML: {
  264. return false;
  265. }
  266. }
  267. }
  268. return true;
  269. }
  270. hasTableBodyContextInTableScope() {
  271. for (let i = this.stackTop; i >= 0; i--) {
  272. if (this.treeAdapter.getNamespaceURI(this.items[i]) !== html_js_1.NS.HTML) {
  273. continue;
  274. }
  275. switch (this.tagIDs[i]) {
  276. case html_js_1.TAG_ID.TBODY:
  277. case html_js_1.TAG_ID.THEAD:
  278. case html_js_1.TAG_ID.TFOOT: {
  279. return true;
  280. }
  281. case html_js_1.TAG_ID.TABLE:
  282. case html_js_1.TAG_ID.HTML: {
  283. return false;
  284. }
  285. }
  286. }
  287. return true;
  288. }
  289. hasInSelectScope(tagName) {
  290. for (let i = this.stackTop; i >= 0; i--) {
  291. if (this.treeAdapter.getNamespaceURI(this.items[i]) !== html_js_1.NS.HTML) {
  292. continue;
  293. }
  294. switch (this.tagIDs[i]) {
  295. case tagName: {
  296. return true;
  297. }
  298. case html_js_1.TAG_ID.OPTION:
  299. case html_js_1.TAG_ID.OPTGROUP: {
  300. break;
  301. }
  302. default: {
  303. return false;
  304. }
  305. }
  306. }
  307. return true;
  308. }
  309. //Implied end tags
  310. generateImpliedEndTags() {
  311. while (this.currentTagId !== undefined && IMPLICIT_END_TAG_REQUIRED.has(this.currentTagId)) {
  312. this.pop();
  313. }
  314. }
  315. generateImpliedEndTagsThoroughly() {
  316. while (this.currentTagId !== undefined && IMPLICIT_END_TAG_REQUIRED_THOROUGHLY.has(this.currentTagId)) {
  317. this.pop();
  318. }
  319. }
  320. generateImpliedEndTagsWithExclusion(exclusionId) {
  321. while (this.currentTagId !== undefined &&
  322. this.currentTagId !== exclusionId &&
  323. IMPLICIT_END_TAG_REQUIRED_THOROUGHLY.has(this.currentTagId)) {
  324. this.pop();
  325. }
  326. }
  327. }
  328. exports.OpenElementStack = OpenElementStack;