formatting-element-list.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.FormattingElementList = exports.EntryType = void 0;
  4. //Const
  5. const NOAH_ARK_CAPACITY = 3;
  6. var EntryType;
  7. (function (EntryType) {
  8. EntryType[EntryType["Marker"] = 0] = "Marker";
  9. EntryType[EntryType["Element"] = 1] = "Element";
  10. })(EntryType = exports.EntryType || (exports.EntryType = {}));
  11. const MARKER = { type: EntryType.Marker };
  12. //List of formatting elements
  13. class FormattingElementList {
  14. constructor(treeAdapter) {
  15. this.treeAdapter = treeAdapter;
  16. this.entries = [];
  17. this.bookmark = null;
  18. }
  19. //Noah Ark's condition
  20. //OPTIMIZATION: at first we try to find possible candidates for exclusion using
  21. //lightweight heuristics without thorough attributes check.
  22. _getNoahArkConditionCandidates(newElement, neAttrs) {
  23. const candidates = [];
  24. const neAttrsLength = neAttrs.length;
  25. const neTagName = this.treeAdapter.getTagName(newElement);
  26. const neNamespaceURI = this.treeAdapter.getNamespaceURI(newElement);
  27. for (let i = 0; i < this.entries.length; i++) {
  28. const entry = this.entries[i];
  29. if (entry.type === EntryType.Marker) {
  30. break;
  31. }
  32. const { element } = entry;
  33. if (this.treeAdapter.getTagName(element) === neTagName &&
  34. this.treeAdapter.getNamespaceURI(element) === neNamespaceURI) {
  35. const elementAttrs = this.treeAdapter.getAttrList(element);
  36. if (elementAttrs.length === neAttrsLength) {
  37. candidates.push({ idx: i, attrs: elementAttrs });
  38. }
  39. }
  40. }
  41. return candidates;
  42. }
  43. _ensureNoahArkCondition(newElement) {
  44. if (this.entries.length < NOAH_ARK_CAPACITY)
  45. return;
  46. const neAttrs = this.treeAdapter.getAttrList(newElement);
  47. const candidates = this._getNoahArkConditionCandidates(newElement, neAttrs);
  48. if (candidates.length < NOAH_ARK_CAPACITY)
  49. return;
  50. //NOTE: build attrs map for the new element, so we can perform fast lookups
  51. const neAttrsMap = new Map(neAttrs.map((neAttr) => [neAttr.name, neAttr.value]));
  52. let validCandidates = 0;
  53. //NOTE: remove bottommost candidates, until Noah's Ark condition will not be met
  54. for (let i = 0; i < candidates.length; i++) {
  55. const candidate = candidates[i];
  56. // We know that `candidate.attrs.length === neAttrs.length`
  57. if (candidate.attrs.every((cAttr) => neAttrsMap.get(cAttr.name) === cAttr.value)) {
  58. validCandidates += 1;
  59. if (validCandidates >= NOAH_ARK_CAPACITY) {
  60. this.entries.splice(candidate.idx, 1);
  61. }
  62. }
  63. }
  64. }
  65. //Mutations
  66. insertMarker() {
  67. this.entries.unshift(MARKER);
  68. }
  69. pushElement(element, token) {
  70. this._ensureNoahArkCondition(element);
  71. this.entries.unshift({
  72. type: EntryType.Element,
  73. element,
  74. token,
  75. });
  76. }
  77. insertElementAfterBookmark(element, token) {
  78. const bookmarkIdx = this.entries.indexOf(this.bookmark);
  79. this.entries.splice(bookmarkIdx, 0, {
  80. type: EntryType.Element,
  81. element,
  82. token,
  83. });
  84. }
  85. removeEntry(entry) {
  86. const entryIndex = this.entries.indexOf(entry);
  87. if (entryIndex >= 0) {
  88. this.entries.splice(entryIndex, 1);
  89. }
  90. }
  91. /**
  92. * Clears the list of formatting elements up to the last marker.
  93. *
  94. * @see https://html.spec.whatwg.org/multipage/parsing.html#clear-the-list-of-active-formatting-elements-up-to-the-last-marker
  95. */
  96. clearToLastMarker() {
  97. const markerIdx = this.entries.indexOf(MARKER);
  98. if (markerIdx >= 0) {
  99. this.entries.splice(0, markerIdx + 1);
  100. }
  101. else {
  102. this.entries.length = 0;
  103. }
  104. }
  105. //Search
  106. getElementEntryInScopeWithTagName(tagName) {
  107. const entry = this.entries.find((entry) => entry.type === EntryType.Marker || this.treeAdapter.getTagName(entry.element) === tagName);
  108. return entry && entry.type === EntryType.Element ? entry : null;
  109. }
  110. getElementEntry(element) {
  111. return this.entries.find((entry) => entry.type === EntryType.Element && entry.element === element);
  112. }
  113. }
  114. exports.FormattingElementList = FormattingElementList;
  115. //# sourceMappingURL=formatting-element-list.js.map