open-element-stack.js 11 KB

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