fix
This commit is contained in:
36
book/node_modules/parse5/dist/cjs/parser/formatting-element-list.d.ts
generated
vendored
Normal file
36
book/node_modules/parse5/dist/cjs/parser/formatting-element-list.d.ts
generated
vendored
Normal file
@ -0,0 +1,36 @@
|
||||
import type { TagToken } from '../common/token.js';
|
||||
import type { TreeAdapter, TreeAdapterTypeMap } from '../tree-adapters/interface.js';
|
||||
export declare enum EntryType {
|
||||
Marker = 0,
|
||||
Element = 1
|
||||
}
|
||||
interface MarkerEntry {
|
||||
type: EntryType.Marker;
|
||||
}
|
||||
export interface ElementEntry<T extends TreeAdapterTypeMap> {
|
||||
type: EntryType.Element;
|
||||
element: T['element'];
|
||||
token: TagToken;
|
||||
}
|
||||
export type Entry<T extends TreeAdapterTypeMap> = MarkerEntry | ElementEntry<T>;
|
||||
export declare class FormattingElementList<T extends TreeAdapterTypeMap> {
|
||||
private treeAdapter;
|
||||
entries: Entry<T>[];
|
||||
bookmark: Entry<T> | null;
|
||||
constructor(treeAdapter: TreeAdapter<T>);
|
||||
private _getNoahArkConditionCandidates;
|
||||
private _ensureNoahArkCondition;
|
||||
insertMarker(): void;
|
||||
pushElement(element: T['element'], token: TagToken): void;
|
||||
insertElementAfterBookmark(element: T['element'], token: TagToken): void;
|
||||
removeEntry(entry: Entry<T>): void;
|
||||
/**
|
||||
* Clears the list of formatting elements up to the last marker.
|
||||
*
|
||||
* @see https://html.spec.whatwg.org/multipage/parsing.html#clear-the-list-of-active-formatting-elements-up-to-the-last-marker
|
||||
*/
|
||||
clearToLastMarker(): void;
|
||||
getElementEntryInScopeWithTagName(tagName: string): ElementEntry<T> | null;
|
||||
getElementEntry(element: T['element']): ElementEntry<T> | undefined;
|
||||
}
|
||||
export {};
|
114
book/node_modules/parse5/dist/cjs/parser/formatting-element-list.js
generated
vendored
Normal file
114
book/node_modules/parse5/dist/cjs/parser/formatting-element-list.js
generated
vendored
Normal file
@ -0,0 +1,114 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.FormattingElementList = exports.EntryType = void 0;
|
||||
//Const
|
||||
const NOAH_ARK_CAPACITY = 3;
|
||||
var EntryType;
|
||||
(function (EntryType) {
|
||||
EntryType[EntryType["Marker"] = 0] = "Marker";
|
||||
EntryType[EntryType["Element"] = 1] = "Element";
|
||||
})(EntryType || (exports.EntryType = EntryType = {}));
|
||||
const MARKER = { type: EntryType.Marker };
|
||||
//List of formatting elements
|
||||
class FormattingElementList {
|
||||
constructor(treeAdapter) {
|
||||
this.treeAdapter = treeAdapter;
|
||||
this.entries = [];
|
||||
this.bookmark = null;
|
||||
}
|
||||
//Noah Ark's condition
|
||||
//OPTIMIZATION: at first we try to find possible candidates for exclusion using
|
||||
//lightweight heuristics without thorough attributes check.
|
||||
_getNoahArkConditionCandidates(newElement, neAttrs) {
|
||||
const candidates = [];
|
||||
const neAttrsLength = neAttrs.length;
|
||||
const neTagName = this.treeAdapter.getTagName(newElement);
|
||||
const neNamespaceURI = this.treeAdapter.getNamespaceURI(newElement);
|
||||
for (let i = 0; i < this.entries.length; i++) {
|
||||
const entry = this.entries[i];
|
||||
if (entry.type === EntryType.Marker) {
|
||||
break;
|
||||
}
|
||||
const { element } = entry;
|
||||
if (this.treeAdapter.getTagName(element) === neTagName &&
|
||||
this.treeAdapter.getNamespaceURI(element) === neNamespaceURI) {
|
||||
const elementAttrs = this.treeAdapter.getAttrList(element);
|
||||
if (elementAttrs.length === neAttrsLength) {
|
||||
candidates.push({ idx: i, attrs: elementAttrs });
|
||||
}
|
||||
}
|
||||
}
|
||||
return candidates;
|
||||
}
|
||||
_ensureNoahArkCondition(newElement) {
|
||||
if (this.entries.length < NOAH_ARK_CAPACITY)
|
||||
return;
|
||||
const neAttrs = this.treeAdapter.getAttrList(newElement);
|
||||
const candidates = this._getNoahArkConditionCandidates(newElement, neAttrs);
|
||||
if (candidates.length < NOAH_ARK_CAPACITY)
|
||||
return;
|
||||
//NOTE: build attrs map for the new element, so we can perform fast lookups
|
||||
const neAttrsMap = new Map(neAttrs.map((neAttr) => [neAttr.name, neAttr.value]));
|
||||
let validCandidates = 0;
|
||||
//NOTE: remove bottommost candidates, until Noah's Ark condition will not be met
|
||||
for (let i = 0; i < candidates.length; i++) {
|
||||
const candidate = candidates[i];
|
||||
// We know that `candidate.attrs.length === neAttrs.length`
|
||||
if (candidate.attrs.every((cAttr) => neAttrsMap.get(cAttr.name) === cAttr.value)) {
|
||||
validCandidates += 1;
|
||||
if (validCandidates >= NOAH_ARK_CAPACITY) {
|
||||
this.entries.splice(candidate.idx, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
//Mutations
|
||||
insertMarker() {
|
||||
this.entries.unshift(MARKER);
|
||||
}
|
||||
pushElement(element, token) {
|
||||
this._ensureNoahArkCondition(element);
|
||||
this.entries.unshift({
|
||||
type: EntryType.Element,
|
||||
element,
|
||||
token,
|
||||
});
|
||||
}
|
||||
insertElementAfterBookmark(element, token) {
|
||||
const bookmarkIdx = this.entries.indexOf(this.bookmark);
|
||||
this.entries.splice(bookmarkIdx, 0, {
|
||||
type: EntryType.Element,
|
||||
element,
|
||||
token,
|
||||
});
|
||||
}
|
||||
removeEntry(entry) {
|
||||
const entryIndex = this.entries.indexOf(entry);
|
||||
if (entryIndex >= 0) {
|
||||
this.entries.splice(entryIndex, 1);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Clears the list of formatting elements up to the last marker.
|
||||
*
|
||||
* @see https://html.spec.whatwg.org/multipage/parsing.html#clear-the-list-of-active-formatting-elements-up-to-the-last-marker
|
||||
*/
|
||||
clearToLastMarker() {
|
||||
const markerIdx = this.entries.indexOf(MARKER);
|
||||
if (markerIdx >= 0) {
|
||||
this.entries.splice(0, markerIdx + 1);
|
||||
}
|
||||
else {
|
||||
this.entries.length = 0;
|
||||
}
|
||||
}
|
||||
//Search
|
||||
getElementEntryInScopeWithTagName(tagName) {
|
||||
const entry = this.entries.find((entry) => entry.type === EntryType.Marker || this.treeAdapter.getTagName(entry.element) === tagName);
|
||||
return entry && entry.type === EntryType.Element ? entry : null;
|
||||
}
|
||||
getElementEntry(element) {
|
||||
return this.entries.find((entry) => entry.type === EntryType.Element && entry.element === element);
|
||||
}
|
||||
}
|
||||
exports.FormattingElementList = FormattingElementList;
|
221
book/node_modules/parse5/dist/cjs/parser/index.d.ts
generated
vendored
Normal file
221
book/node_modules/parse5/dist/cjs/parser/index.d.ts
generated
vendored
Normal file
@ -0,0 +1,221 @@
|
||||
import { Tokenizer, TokenizerMode, type TokenHandler } from '../tokenizer/index.js';
|
||||
import { OpenElementStack, type StackHandler } from './open-element-stack.js';
|
||||
import { FormattingElementList } from './formatting-element-list.js';
|
||||
import { ERR, type ParserErrorHandler } from '../common/error-codes.js';
|
||||
import { TAG_ID as $, NS } from '../common/html.js';
|
||||
import type { TreeAdapter, TreeAdapterTypeMap } from '../tree-adapters/interface.js';
|
||||
import { type Token, type CommentToken, type CharacterToken, type TagToken, type DoctypeToken, type EOFToken, type LocationWithAttributes } from '../common/token.js';
|
||||
declare enum InsertionMode {
|
||||
INITIAL = 0,
|
||||
BEFORE_HTML = 1,
|
||||
BEFORE_HEAD = 2,
|
||||
IN_HEAD = 3,
|
||||
IN_HEAD_NO_SCRIPT = 4,
|
||||
AFTER_HEAD = 5,
|
||||
IN_BODY = 6,
|
||||
TEXT = 7,
|
||||
IN_TABLE = 8,
|
||||
IN_TABLE_TEXT = 9,
|
||||
IN_CAPTION = 10,
|
||||
IN_COLUMN_GROUP = 11,
|
||||
IN_TABLE_BODY = 12,
|
||||
IN_ROW = 13,
|
||||
IN_CELL = 14,
|
||||
IN_SELECT = 15,
|
||||
IN_SELECT_IN_TABLE = 16,
|
||||
IN_TEMPLATE = 17,
|
||||
AFTER_BODY = 18,
|
||||
IN_FRAMESET = 19,
|
||||
AFTER_FRAMESET = 20,
|
||||
AFTER_AFTER_BODY = 21,
|
||||
AFTER_AFTER_FRAMESET = 22
|
||||
}
|
||||
export interface ParserOptions<T extends TreeAdapterTypeMap> {
|
||||
/**
|
||||
* The [scripting flag](https://html.spec.whatwg.org/multipage/parsing.html#scripting-flag). If set
|
||||
* to `true`, `noscript` element content will be parsed as text.
|
||||
*
|
||||
* @default `true`
|
||||
*/
|
||||
scriptingEnabled?: boolean;
|
||||
/**
|
||||
* Enables source code location information. When enabled, each node (except the root node)
|
||||
* will have a `sourceCodeLocation` property. If the node is not an empty element, `sourceCodeLocation` will
|
||||
* be a {@link ElementLocation} object, otherwise it will be {@link Location}.
|
||||
* If the element was implicitly created by the parser (as part of
|
||||
* [tree correction](https://html.spec.whatwg.org/multipage/syntax.html#an-introduction-to-error-handling-and-strange-cases-in-the-parser)),
|
||||
* its `sourceCodeLocation` property will be `undefined`.
|
||||
*
|
||||
* @default `false`
|
||||
*/
|
||||
sourceCodeLocationInfo?: boolean;
|
||||
/**
|
||||
* Specifies the resulting tree format.
|
||||
*
|
||||
* @default `treeAdapters.default`
|
||||
*/
|
||||
treeAdapter?: TreeAdapter<T>;
|
||||
/**
|
||||
* Callback for parse errors.
|
||||
*
|
||||
* @default `null`
|
||||
*/
|
||||
onParseError?: ParserErrorHandler | null;
|
||||
}
|
||||
export declare class Parser<T extends TreeAdapterTypeMap> implements TokenHandler, StackHandler<T> {
|
||||
/** @internal */
|
||||
fragmentContext: T['element'] | null;
|
||||
/** @internal */
|
||||
scriptHandler: null | ((pendingScript: T['element']) => void);
|
||||
treeAdapter: TreeAdapter<T>;
|
||||
/** @internal */
|
||||
onParseError: ParserErrorHandler | null;
|
||||
protected currentToken: Token | null;
|
||||
options: Required<ParserOptions<T>>;
|
||||
document: T['document'];
|
||||
constructor(options?: ParserOptions<T>, document?: T['document'],
|
||||
/** @internal */
|
||||
fragmentContext?: T['element'] | null,
|
||||
/** @internal */
|
||||
scriptHandler?: null | ((pendingScript: T['element']) => void));
|
||||
static parse<T extends TreeAdapterTypeMap>(html: string, options?: ParserOptions<T>): T['document'];
|
||||
static getFragmentParser<T extends TreeAdapterTypeMap>(fragmentContext?: T['parentNode'] | null, options?: ParserOptions<T>): Parser<T>;
|
||||
getFragment(): T['documentFragment'];
|
||||
tokenizer: Tokenizer;
|
||||
stopped: boolean;
|
||||
/** @internal */
|
||||
insertionMode: InsertionMode;
|
||||
/** @internal */
|
||||
originalInsertionMode: InsertionMode;
|
||||
/** @internal */
|
||||
fragmentContextID: $;
|
||||
/** @internal */
|
||||
headElement: null | T['element'];
|
||||
/** @internal */
|
||||
formElement: null | T['element'];
|
||||
/** @internal */
|
||||
openElements: OpenElementStack<T>;
|
||||
/** @internal */
|
||||
activeFormattingElements: FormattingElementList<T>;
|
||||
/** Indicates that the current node is not an element in the HTML namespace */
|
||||
protected currentNotInHTML: boolean;
|
||||
/**
|
||||
* The template insertion mode stack is maintained from the left.
|
||||
* Ie. the topmost element will always have index 0.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
tmplInsertionModeStack: InsertionMode[];
|
||||
/** @internal */
|
||||
pendingCharacterTokens: CharacterToken[];
|
||||
/** @internal */
|
||||
hasNonWhitespacePendingCharacterToken: boolean;
|
||||
/** @internal */
|
||||
framesetOk: boolean;
|
||||
/** @internal */
|
||||
skipNextNewLine: boolean;
|
||||
/** @internal */
|
||||
fosterParentingEnabled: boolean;
|
||||
/** @internal */
|
||||
_err(token: Token, code: ERR, beforeToken?: boolean): void;
|
||||
/** @internal */
|
||||
onItemPush(node: T['parentNode'], tid: number, isTop: boolean): void;
|
||||
/** @internal */
|
||||
onItemPop(node: T['parentNode'], isTop: boolean): void;
|
||||
protected _setContextModes(current: T['parentNode'], tid: number): void;
|
||||
/** @protected */
|
||||
_switchToTextParsing(currentToken: TagToken, nextTokenizerState: (typeof TokenizerMode)[keyof typeof TokenizerMode]): void;
|
||||
switchToPlaintextParsing(): void;
|
||||
/** @protected */
|
||||
_getAdjustedCurrentElement(): T['element'];
|
||||
/** @protected */
|
||||
_findFormInFragmentContext(): void;
|
||||
protected _initTokenizerForFragmentParsing(): void;
|
||||
/** @protected */
|
||||
_setDocumentType(token: DoctypeToken): void;
|
||||
/** @protected */
|
||||
_attachElementToTree(element: T['element'], location: LocationWithAttributes | null): void;
|
||||
/**
|
||||
* For self-closing tags. Add an element to the tree, but skip adding it
|
||||
* to the stack.
|
||||
*/
|
||||
/** @protected */
|
||||
_appendElement(token: TagToken, namespaceURI: NS): void;
|
||||
/** @protected */
|
||||
_insertElement(token: TagToken, namespaceURI: NS): void;
|
||||
/** @protected */
|
||||
_insertFakeElement(tagName: string, tagID: $): void;
|
||||
/** @protected */
|
||||
_insertTemplate(token: TagToken): void;
|
||||
/** @protected */
|
||||
_insertFakeRootElement(): void;
|
||||
/** @protected */
|
||||
_appendCommentNode(token: CommentToken, parent: T['parentNode']): void;
|
||||
/** @protected */
|
||||
_insertCharacters(token: CharacterToken): void;
|
||||
/** @protected */
|
||||
_adoptNodes(donor: T['parentNode'], recipient: T['parentNode']): void;
|
||||
/** @protected */
|
||||
_setEndLocation(element: T['element'], closingToken: Token): void;
|
||||
protected shouldProcessStartTagTokenInForeignContent(token: TagToken): boolean;
|
||||
/** @protected */
|
||||
_processToken(token: Token): void;
|
||||
/** @protected */
|
||||
_isIntegrationPoint(tid: $, element: T['element'], foreignNS?: NS): boolean;
|
||||
/** @protected */
|
||||
_reconstructActiveFormattingElements(): void;
|
||||
/** @protected */
|
||||
_closeTableCell(): void;
|
||||
/** @protected */
|
||||
_closePElement(): void;
|
||||
/** @protected */
|
||||
_resetInsertionMode(): void;
|
||||
/** @protected */
|
||||
_resetInsertionModeForSelect(selectIdx: number): void;
|
||||
/** @protected */
|
||||
_isElementCausesFosterParenting(tn: $): boolean;
|
||||
/** @protected */
|
||||
_shouldFosterParentOnInsertion(): boolean;
|
||||
/** @protected */
|
||||
_findFosterParentingLocation(): {
|
||||
parent: T['parentNode'];
|
||||
beforeElement: T['element'] | null;
|
||||
};
|
||||
/** @protected */
|
||||
_fosterParentElement(element: T['element']): void;
|
||||
/** @protected */
|
||||
_isSpecialElement(element: T['element'], id: $): boolean;
|
||||
/** @internal */
|
||||
onCharacter(token: CharacterToken): void;
|
||||
/** @internal */
|
||||
onNullCharacter(token: CharacterToken): void;
|
||||
/** @internal */
|
||||
onComment(token: CommentToken): void;
|
||||
/** @internal */
|
||||
onDoctype(token: DoctypeToken): void;
|
||||
/** @internal */
|
||||
onStartTag(token: TagToken): void;
|
||||
/**
|
||||
* Processes a given start tag.
|
||||
*
|
||||
* `onStartTag` checks if a self-closing tag was recognized. When a token
|
||||
* is moved inbetween multiple insertion modes, this check for self-closing
|
||||
* could lead to false positives. To avoid this, `_processStartTag` is used
|
||||
* for nested calls.
|
||||
*
|
||||
* @param token The token to process.
|
||||
* @protected
|
||||
*/
|
||||
_processStartTag(token: TagToken): void;
|
||||
/** @protected */
|
||||
_startTagOutsideForeignContent(token: TagToken): void;
|
||||
/** @internal */
|
||||
onEndTag(token: TagToken): void;
|
||||
/** @protected */
|
||||
_endTagOutsideForeignContent(token: TagToken): void;
|
||||
/** @internal */
|
||||
onEof(token: EOFToken): void;
|
||||
/** @internal */
|
||||
onWhitespaceCharacter(token: CharacterToken): void;
|
||||
}
|
||||
export {};
|
3235
book/node_modules/parse5/dist/cjs/parser/index.js
generated
vendored
Normal file
3235
book/node_modules/parse5/dist/cjs/parser/index.js
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
53
book/node_modules/parse5/dist/cjs/parser/open-element-stack.d.ts
generated
vendored
Normal file
53
book/node_modules/parse5/dist/cjs/parser/open-element-stack.d.ts
generated
vendored
Normal file
@ -0,0 +1,53 @@
|
||||
import { TAG_ID as $ } from '../common/html.js';
|
||||
import type { TreeAdapter, TreeAdapterTypeMap } from '../tree-adapters/interface.js';
|
||||
export interface StackHandler<T extends TreeAdapterTypeMap> {
|
||||
onItemPush: (node: T['parentNode'], tid: number, isTop: boolean) => void;
|
||||
onItemPop: (node: T['parentNode'], isTop: boolean) => void;
|
||||
}
|
||||
export declare class OpenElementStack<T extends TreeAdapterTypeMap> {
|
||||
private treeAdapter;
|
||||
private handler;
|
||||
items: T['parentNode'][];
|
||||
tagIDs: $[];
|
||||
current: T['parentNode'];
|
||||
stackTop: number;
|
||||
tmplCount: number;
|
||||
currentTagId: $;
|
||||
get currentTmplContentOrNode(): T['parentNode'];
|
||||
constructor(document: T['document'], treeAdapter: TreeAdapter<T>, handler: StackHandler<T>);
|
||||
private _indexOf;
|
||||
private _isInTemplate;
|
||||
private _updateCurrentElement;
|
||||
push(element: T['element'], tagID: $): void;
|
||||
pop(): void;
|
||||
replace(oldElement: T['element'], newElement: T['element']): void;
|
||||
insertAfter(referenceElement: T['element'], newElement: T['element'], newElementID: $): void;
|
||||
popUntilTagNamePopped(tagName: $): void;
|
||||
shortenToLength(idx: number): void;
|
||||
popUntilElementPopped(element: T['element']): void;
|
||||
private popUntilPopped;
|
||||
popUntilNumberedHeaderPopped(): void;
|
||||
popUntilTableCellPopped(): void;
|
||||
popAllUpToHtmlElement(): void;
|
||||
private _indexOfTagNames;
|
||||
private clearBackTo;
|
||||
clearBackToTableContext(): void;
|
||||
clearBackToTableBodyContext(): void;
|
||||
clearBackToTableRowContext(): void;
|
||||
remove(element: T['element']): void;
|
||||
tryPeekProperlyNestedBodyElement(): T['element'] | null;
|
||||
contains(element: T['element']): boolean;
|
||||
getCommonAncestor(element: T['element']): T['element'] | null;
|
||||
isRootHtmlElementCurrent(): boolean;
|
||||
private hasInDynamicScope;
|
||||
hasInScope(tagName: $): boolean;
|
||||
hasInListItemScope(tagName: $): boolean;
|
||||
hasInButtonScope(tagName: $): boolean;
|
||||
hasNumberedHeaderInScope(): boolean;
|
||||
hasInTableScope(tagName: $): boolean;
|
||||
hasTableBodyContextInTableScope(): boolean;
|
||||
hasInSelectScope(tagName: $): boolean;
|
||||
generateImpliedEndTags(): void;
|
||||
generateImpliedEndTagsThoroughly(): void;
|
||||
generateImpliedEndTagsWithExclusion(exclusionId: $): void;
|
||||
}
|
324
book/node_modules/parse5/dist/cjs/parser/open-element-stack.js
generated
vendored
Normal file
324
book/node_modules/parse5/dist/cjs/parser/open-element-stack.js
generated
vendored
Normal file
@ -0,0 +1,324 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.OpenElementStack = void 0;
|
||||
const html_js_1 = require("../common/html.js");
|
||||
//Element utils
|
||||
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]);
|
||||
const IMPLICIT_END_TAG_REQUIRED_THOROUGHLY = new Set([
|
||||
...IMPLICIT_END_TAG_REQUIRED,
|
||||
html_js_1.TAG_ID.CAPTION,
|
||||
html_js_1.TAG_ID.COLGROUP,
|
||||
html_js_1.TAG_ID.TBODY,
|
||||
html_js_1.TAG_ID.TD,
|
||||
html_js_1.TAG_ID.TFOOT,
|
||||
html_js_1.TAG_ID.TH,
|
||||
html_js_1.TAG_ID.THEAD,
|
||||
html_js_1.TAG_ID.TR,
|
||||
]);
|
||||
const SCOPING_ELEMENTS_HTML = new Set([
|
||||
html_js_1.TAG_ID.APPLET,
|
||||
html_js_1.TAG_ID.CAPTION,
|
||||
html_js_1.TAG_ID.HTML,
|
||||
html_js_1.TAG_ID.MARQUEE,
|
||||
html_js_1.TAG_ID.OBJECT,
|
||||
html_js_1.TAG_ID.TABLE,
|
||||
html_js_1.TAG_ID.TD,
|
||||
html_js_1.TAG_ID.TEMPLATE,
|
||||
html_js_1.TAG_ID.TH,
|
||||
]);
|
||||
const SCOPING_ELEMENTS_HTML_LIST = new Set([...SCOPING_ELEMENTS_HTML, html_js_1.TAG_ID.OL, html_js_1.TAG_ID.UL]);
|
||||
const SCOPING_ELEMENTS_HTML_BUTTON = new Set([...SCOPING_ELEMENTS_HTML, html_js_1.TAG_ID.BUTTON]);
|
||||
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]);
|
||||
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]);
|
||||
const TABLE_ROW_CONTEXT = new Set([html_js_1.TAG_ID.TR, html_js_1.TAG_ID.TEMPLATE, html_js_1.TAG_ID.HTML]);
|
||||
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]);
|
||||
const TABLE_CONTEXT = new Set([html_js_1.TAG_ID.TABLE, html_js_1.TAG_ID.TEMPLATE, html_js_1.TAG_ID.HTML]);
|
||||
const TABLE_CELLS = new Set([html_js_1.TAG_ID.TD, html_js_1.TAG_ID.TH]);
|
||||
//Stack of open elements
|
||||
class OpenElementStack {
|
||||
get currentTmplContentOrNode() {
|
||||
return this._isInTemplate() ? this.treeAdapter.getTemplateContent(this.current) : this.current;
|
||||
}
|
||||
constructor(document, treeAdapter, handler) {
|
||||
this.treeAdapter = treeAdapter;
|
||||
this.handler = handler;
|
||||
this.items = [];
|
||||
this.tagIDs = [];
|
||||
this.stackTop = -1;
|
||||
this.tmplCount = 0;
|
||||
this.currentTagId = html_js_1.TAG_ID.UNKNOWN;
|
||||
this.current = document;
|
||||
}
|
||||
//Index of element
|
||||
_indexOf(element) {
|
||||
return this.items.lastIndexOf(element, this.stackTop);
|
||||
}
|
||||
//Update current element
|
||||
_isInTemplate() {
|
||||
return this.currentTagId === html_js_1.TAG_ID.TEMPLATE && this.treeAdapter.getNamespaceURI(this.current) === html_js_1.NS.HTML;
|
||||
}
|
||||
_updateCurrentElement() {
|
||||
this.current = this.items[this.stackTop];
|
||||
this.currentTagId = this.tagIDs[this.stackTop];
|
||||
}
|
||||
//Mutations
|
||||
push(element, tagID) {
|
||||
this.stackTop++;
|
||||
this.items[this.stackTop] = element;
|
||||
this.current = element;
|
||||
this.tagIDs[this.stackTop] = tagID;
|
||||
this.currentTagId = tagID;
|
||||
if (this._isInTemplate()) {
|
||||
this.tmplCount++;
|
||||
}
|
||||
this.handler.onItemPush(element, tagID, true);
|
||||
}
|
||||
pop() {
|
||||
const popped = this.current;
|
||||
if (this.tmplCount > 0 && this._isInTemplate()) {
|
||||
this.tmplCount--;
|
||||
}
|
||||
this.stackTop--;
|
||||
this._updateCurrentElement();
|
||||
this.handler.onItemPop(popped, true);
|
||||
}
|
||||
replace(oldElement, newElement) {
|
||||
const idx = this._indexOf(oldElement);
|
||||
this.items[idx] = newElement;
|
||||
if (idx === this.stackTop) {
|
||||
this.current = newElement;
|
||||
}
|
||||
}
|
||||
insertAfter(referenceElement, newElement, newElementID) {
|
||||
const insertionIdx = this._indexOf(referenceElement) + 1;
|
||||
this.items.splice(insertionIdx, 0, newElement);
|
||||
this.tagIDs.splice(insertionIdx, 0, newElementID);
|
||||
this.stackTop++;
|
||||
if (insertionIdx === this.stackTop) {
|
||||
this._updateCurrentElement();
|
||||
}
|
||||
this.handler.onItemPush(this.current, this.currentTagId, insertionIdx === this.stackTop);
|
||||
}
|
||||
popUntilTagNamePopped(tagName) {
|
||||
let targetIdx = this.stackTop + 1;
|
||||
do {
|
||||
targetIdx = this.tagIDs.lastIndexOf(tagName, targetIdx - 1);
|
||||
} while (targetIdx > 0 && this.treeAdapter.getNamespaceURI(this.items[targetIdx]) !== html_js_1.NS.HTML);
|
||||
this.shortenToLength(targetIdx < 0 ? 0 : targetIdx);
|
||||
}
|
||||
shortenToLength(idx) {
|
||||
while (this.stackTop >= idx) {
|
||||
const popped = this.current;
|
||||
if (this.tmplCount > 0 && this._isInTemplate()) {
|
||||
this.tmplCount -= 1;
|
||||
}
|
||||
this.stackTop--;
|
||||
this._updateCurrentElement();
|
||||
this.handler.onItemPop(popped, this.stackTop < idx);
|
||||
}
|
||||
}
|
||||
popUntilElementPopped(element) {
|
||||
const idx = this._indexOf(element);
|
||||
this.shortenToLength(idx < 0 ? 0 : idx);
|
||||
}
|
||||
popUntilPopped(tagNames, targetNS) {
|
||||
const idx = this._indexOfTagNames(tagNames, targetNS);
|
||||
this.shortenToLength(idx < 0 ? 0 : idx);
|
||||
}
|
||||
popUntilNumberedHeaderPopped() {
|
||||
this.popUntilPopped(html_js_1.NUMBERED_HEADERS, html_js_1.NS.HTML);
|
||||
}
|
||||
popUntilTableCellPopped() {
|
||||
this.popUntilPopped(TABLE_CELLS, html_js_1.NS.HTML);
|
||||
}
|
||||
popAllUpToHtmlElement() {
|
||||
//NOTE: here we assume that the root <html> element is always first in the open element stack, so
|
||||
//we perform this fast stack clean up.
|
||||
this.tmplCount = 0;
|
||||
this.shortenToLength(1);
|
||||
}
|
||||
_indexOfTagNames(tagNames, namespace) {
|
||||
for (let i = this.stackTop; i >= 0; i--) {
|
||||
if (tagNames.has(this.tagIDs[i]) && this.treeAdapter.getNamespaceURI(this.items[i]) === namespace) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
clearBackTo(tagNames, targetNS) {
|
||||
const idx = this._indexOfTagNames(tagNames, targetNS);
|
||||
this.shortenToLength(idx + 1);
|
||||
}
|
||||
clearBackToTableContext() {
|
||||
this.clearBackTo(TABLE_CONTEXT, html_js_1.NS.HTML);
|
||||
}
|
||||
clearBackToTableBodyContext() {
|
||||
this.clearBackTo(TABLE_BODY_CONTEXT, html_js_1.NS.HTML);
|
||||
}
|
||||
clearBackToTableRowContext() {
|
||||
this.clearBackTo(TABLE_ROW_CONTEXT, html_js_1.NS.HTML);
|
||||
}
|
||||
remove(element) {
|
||||
const idx = this._indexOf(element);
|
||||
if (idx >= 0) {
|
||||
if (idx === this.stackTop) {
|
||||
this.pop();
|
||||
}
|
||||
else {
|
||||
this.items.splice(idx, 1);
|
||||
this.tagIDs.splice(idx, 1);
|
||||
this.stackTop--;
|
||||
this._updateCurrentElement();
|
||||
this.handler.onItemPop(element, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
//Search
|
||||
tryPeekProperlyNestedBodyElement() {
|
||||
//Properly nested <body> element (should be second element in stack).
|
||||
return this.stackTop >= 1 && this.tagIDs[1] === html_js_1.TAG_ID.BODY ? this.items[1] : null;
|
||||
}
|
||||
contains(element) {
|
||||
return this._indexOf(element) > -1;
|
||||
}
|
||||
getCommonAncestor(element) {
|
||||
const elementIdx = this._indexOf(element) - 1;
|
||||
return elementIdx >= 0 ? this.items[elementIdx] : null;
|
||||
}
|
||||
isRootHtmlElementCurrent() {
|
||||
return this.stackTop === 0 && this.tagIDs[0] === html_js_1.TAG_ID.HTML;
|
||||
}
|
||||
//Element in scope
|
||||
hasInDynamicScope(tagName, htmlScope) {
|
||||
for (let i = this.stackTop; i >= 0; i--) {
|
||||
const tn = this.tagIDs[i];
|
||||
switch (this.treeAdapter.getNamespaceURI(this.items[i])) {
|
||||
case html_js_1.NS.HTML: {
|
||||
if (tn === tagName)
|
||||
return true;
|
||||
if (htmlScope.has(tn))
|
||||
return false;
|
||||
break;
|
||||
}
|
||||
case html_js_1.NS.SVG: {
|
||||
if (SCOPING_ELEMENTS_SVG.has(tn))
|
||||
return false;
|
||||
break;
|
||||
}
|
||||
case html_js_1.NS.MATHML: {
|
||||
if (SCOPING_ELEMENTS_MATHML.has(tn))
|
||||
return false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
hasInScope(tagName) {
|
||||
return this.hasInDynamicScope(tagName, SCOPING_ELEMENTS_HTML);
|
||||
}
|
||||
hasInListItemScope(tagName) {
|
||||
return this.hasInDynamicScope(tagName, SCOPING_ELEMENTS_HTML_LIST);
|
||||
}
|
||||
hasInButtonScope(tagName) {
|
||||
return this.hasInDynamicScope(tagName, SCOPING_ELEMENTS_HTML_BUTTON);
|
||||
}
|
||||
hasNumberedHeaderInScope() {
|
||||
for (let i = this.stackTop; i >= 0; i--) {
|
||||
const tn = this.tagIDs[i];
|
||||
switch (this.treeAdapter.getNamespaceURI(this.items[i])) {
|
||||
case html_js_1.NS.HTML: {
|
||||
if (html_js_1.NUMBERED_HEADERS.has(tn))
|
||||
return true;
|
||||
if (SCOPING_ELEMENTS_HTML.has(tn))
|
||||
return false;
|
||||
break;
|
||||
}
|
||||
case html_js_1.NS.SVG: {
|
||||
if (SCOPING_ELEMENTS_SVG.has(tn))
|
||||
return false;
|
||||
break;
|
||||
}
|
||||
case html_js_1.NS.MATHML: {
|
||||
if (SCOPING_ELEMENTS_MATHML.has(tn))
|
||||
return false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
hasInTableScope(tagName) {
|
||||
for (let i = this.stackTop; i >= 0; i--) {
|
||||
if (this.treeAdapter.getNamespaceURI(this.items[i]) !== html_js_1.NS.HTML) {
|
||||
continue;
|
||||
}
|
||||
switch (this.tagIDs[i]) {
|
||||
case tagName: {
|
||||
return true;
|
||||
}
|
||||
case html_js_1.TAG_ID.TABLE:
|
||||
case html_js_1.TAG_ID.HTML: {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
hasTableBodyContextInTableScope() {
|
||||
for (let i = this.stackTop; i >= 0; i--) {
|
||||
if (this.treeAdapter.getNamespaceURI(this.items[i]) !== html_js_1.NS.HTML) {
|
||||
continue;
|
||||
}
|
||||
switch (this.tagIDs[i]) {
|
||||
case html_js_1.TAG_ID.TBODY:
|
||||
case html_js_1.TAG_ID.THEAD:
|
||||
case html_js_1.TAG_ID.TFOOT: {
|
||||
return true;
|
||||
}
|
||||
case html_js_1.TAG_ID.TABLE:
|
||||
case html_js_1.TAG_ID.HTML: {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
hasInSelectScope(tagName) {
|
||||
for (let i = this.stackTop; i >= 0; i--) {
|
||||
if (this.treeAdapter.getNamespaceURI(this.items[i]) !== html_js_1.NS.HTML) {
|
||||
continue;
|
||||
}
|
||||
switch (this.tagIDs[i]) {
|
||||
case tagName: {
|
||||
return true;
|
||||
}
|
||||
case html_js_1.TAG_ID.OPTION:
|
||||
case html_js_1.TAG_ID.OPTGROUP: {
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
//Implied end tags
|
||||
generateImpliedEndTags() {
|
||||
while (IMPLICIT_END_TAG_REQUIRED.has(this.currentTagId)) {
|
||||
this.pop();
|
||||
}
|
||||
}
|
||||
generateImpliedEndTagsThoroughly() {
|
||||
while (IMPLICIT_END_TAG_REQUIRED_THOROUGHLY.has(this.currentTagId)) {
|
||||
this.pop();
|
||||
}
|
||||
}
|
||||
generateImpliedEndTagsWithExclusion(exclusionId) {
|
||||
while (this.currentTagId !== exclusionId && IMPLICIT_END_TAG_REQUIRED_THOROUGHLY.has(this.currentTagId)) {
|
||||
this.pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
exports.OpenElementStack = OpenElementStack;
|
Reference in New Issue
Block a user