formatting-element-list.js 4.3 KB

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