Skip to content
This repository has been archived by the owner on Mar 24, 2022. It is now read-only.

Commit

Permalink
refactor: Remove FormattingElemList statics
Browse files Browse the repository at this point in the history
  • Loading branch information
fb55 committed Nov 22, 2021
1 parent ff5fac8 commit fe68cf6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 17 deletions.
14 changes: 7 additions & 7 deletions packages/parse5/lib/parser/formatting-element-list.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as assert from 'assert';
import { TAG_NAMES as $, NAMESPACES as NS } from '../common/html.js';
import { TagToken, TokenType } from '../common/token.js';
import { FormattingElementList } from './formatting-element-list.js';
import { FormattingElementList, EntryType } from './formatting-element-list.js';
import { generateTestsForEachTreeAdapter } from '../../../../test/utils/common.js';

generateTestsForEachTreeAdapter('FormattingElementList', (treeAdapter) => {
Expand All @@ -20,11 +20,11 @@ generateTestsForEachTreeAdapter('FormattingElementList', (treeAdapter) => {

list.insertMarker();
assert.strictEqual(list.entries.length, 1);
assert.strictEqual(list.entries[0].type, FormattingElementList.MARKER_ENTRY);
assert.strictEqual(list.entries[0].type, EntryType.Marker);

list.insertMarker();
assert.strictEqual(list.entries.length, 2);
assert.strictEqual(list.entries[0].type, FormattingElementList.MARKER_ENTRY);
assert.strictEqual(list.entries[0].type, EntryType.Marker);
});

test('Push element', () => {
Expand All @@ -36,13 +36,13 @@ generateTestsForEachTreeAdapter('FormattingElementList', (treeAdapter) => {

list.pushElement(element1, element1Token);
assert.strictEqual(list.entries.length, 1);
assert.strictEqual(list.entries[0].type, FormattingElementList.ELEMENT_ENTRY);
assert.strictEqual(list.entries[0].type, EntryType.Element as const);
assert.strictEqual(list.entries[0].element, element1);
assert.strictEqual(list.entries[0].token, element1Token);

list.pushElement(element2, element2Token);
assert.strictEqual(list.entries.length, 2);
assert.strictEqual(list.entries[0].type, FormattingElementList.ELEMENT_ENTRY);
assert.strictEqual(list.entries[0].type, EntryType.Element);
assert.strictEqual(list.entries[0].element, element2);
assert.strictEqual(list.entries[0].token, element2Token);
});
Expand Down Expand Up @@ -113,7 +113,7 @@ generateTestsForEachTreeAdapter('FormattingElementList', (treeAdapter) => {
expect(list.entries[4]).toHaveProperty('token', token3);
expect(list.entries[3]).toHaveProperty('token', token4);
expect(list.entries[2]).toHaveProperty('token', token5);
expect(list.entries[1]).toHaveProperty('type', FormattingElementList.MARKER_ENTRY);
expect(list.entries[1]).toHaveProperty('type', EntryType.Marker);
expect(list.entries[0]).toHaveProperty('token', token6);
});

Expand Down Expand Up @@ -201,7 +201,7 @@ generateTestsForEachTreeAdapter('FormattingElementList', (treeAdapter) => {

const entry = list.getElementEntry(element1)!;

assert.strictEqual(entry.type, FormattingElementList.ELEMENT_ENTRY);
assert.strictEqual(entry.type, EntryType.Element);
assert.strictEqual(entry.token, token);
assert.strictEqual(entry.element, element1);
});
Expand Down
10 changes: 3 additions & 7 deletions packages/parse5/lib/parser/formatting-element-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import type { TreeAdapter, TreeAdapterTypeMap } from '../tree-adapters/interface
//Const
const NOAH_ARK_CAPACITY = 3;

enum EntryType {
Marker = 'MARKER_ENTRY',
Element = 'ELEMENT_ENTRY',
export enum EntryType {
Marker,
Element,
}

interface MarkerEntry {
Expand Down Expand Up @@ -141,8 +141,4 @@ export class FormattingElementList<T extends TreeAdapterTypeMap> {
(entry) => entry.type === EntryType.Element && entry.element === element
) as ElementEntry<T> | null;
}

//Entry types
static MARKER_ENTRY = EntryType.Marker as const;
static ELEMENT_ENTRY = EntryType.Element as const;
}
5 changes: 2 additions & 3 deletions packages/parse5/lib/parser/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Tokenizer, TokenizerMode } from '../tokenizer/index.js';
import { OpenElementStack } from './open-element-stack.js';
import { FormattingElementList, ElementEntry } from './formatting-element-list.js';
import { FormattingElementList, ElementEntry, EntryType } from './formatting-element-list.js';
import { LocationInfoParserMixin } from '../extensions/location-info/parser-mixin.js';
import { ErrorReportingParserMixin } from '../extensions/error-reporting/parser-mixin.js';
import { Mixin } from '../utils/mixin.js';
Expand Down Expand Up @@ -582,8 +582,7 @@ export class Parser<T extends TreeAdapterTypeMap> {

if (listLength) {
const endIndex = this.activeFormattingElements.entries.findIndex(
(entry) =>
entry.type === FormattingElementList.MARKER_ENTRY || this.openElements.contains(entry.element)
(entry) => entry.type === EntryType.Marker || this.openElements.contains(entry.element)
);

const unopenIdx = endIndex < 0 ? listLength - 1 : endIndex - 1;
Expand Down

0 comments on commit fe68cf6

Please sign in to comment.