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

Commit 95b9fe4

Browse files
committed
Pass primitive values to some fns
1 parent 6d6a9b6 commit 95b9fe4

1 file changed

Lines changed: 53 additions & 43 deletions

File tree

packages/parse5/lib/parser/index.ts

Lines changed: 53 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
EOFToken,
2929
LocationWithAttributes,
3030
ElementLocation,
31+
Location,
3132
} from '../common/token.js';
3233

3334
//Misc constants
@@ -273,7 +274,7 @@ export class Parser<T extends TreeAdapterTypeMap> {
273274
this._err(token, ERR.nonVoidHtmlElementStartTagWithTrailingSolidus);
274275
}
275276

276-
if (token.type === TokenType.HIBERNATION || (scriptHandler !== null && this.pendingScript)) {
277+
if (!this.tokenizer.active || (scriptHandler !== null && this.pendingScript)) {
277278
break;
278279
}
279280
}
@@ -483,25 +484,25 @@ export class Parser<T extends TreeAdapterTypeMap> {
483484
}
484485
}
485486

486-
_insertCharacters(token: CharacterToken): void {
487+
_insertCharacters(chars: string, location: Location | null): void {
487488
let parent;
488489
let beforeElement;
489490

490491
if (this._shouldFosterParentOnInsertion()) {
491492
({ parent, beforeElement } = this._findFosterParentingLocation());
492493

493494
if (beforeElement) {
494-
this.treeAdapter.insertTextBefore(parent, token.chars, beforeElement);
495+
this.treeAdapter.insertTextBefore(parent, chars, beforeElement);
495496
} else {
496-
this.treeAdapter.insertText(parent, token.chars);
497+
this.treeAdapter.insertText(parent, chars);
497498
}
498499
} else {
499500
parent = this.openElements.currentTmplContentOrNode;
500501

501-
this.treeAdapter.insertText(parent, token.chars);
502+
this.treeAdapter.insertText(parent, chars);
502503
}
503504

504-
if (!token.location) return;
505+
if (!location) return;
505506

506507
const siblings = this.treeAdapter.getChildNodes(parent);
507508
const textNodeIdx = beforeElement ? siblings.lastIndexOf(beforeElement) : siblings.length;
@@ -511,10 +512,10 @@ export class Parser<T extends TreeAdapterTypeMap> {
511512
const tnLoc = this.treeAdapter.getNodeSourceCodeLocation(textNode);
512513

513514
if (tnLoc) {
514-
const { endLine, endCol, endOffset } = token.location;
515+
const { endLine, endCol, endOffset } = location;
515516
this.treeAdapter.updateNodeSourceCodeLocation(textNode, { endLine, endCol, endOffset });
516517
} else if (this.options.sourceCodeLocationInfo) {
517-
this.treeAdapter.setNodeSourceCodeLocation(textNode, token.location);
518+
this.treeAdapter.setNodeSourceCodeLocation(textNode, location);
518519
}
519520
}
520521

@@ -530,20 +531,21 @@ export class Parser<T extends TreeAdapterTypeMap> {
530531
const ctLoc = closingToken.location;
531532
const tn = this.treeAdapter.getTagName(element);
532533

533-
// NOTE: For cases like <p> <p> </p> - First 'p' closes without a closing
534-
// tag and for cases like <td> <p> </td> - 'p' closes without a closing tag.
535-
const isClosingEndTag = closingToken.type === TokenType.END_TAG && tn === closingToken.tagName;
536-
const endLoc: Partial<ElementLocation> = {};
537-
if (isClosingEndTag) {
538-
endLoc.endTag = { ...ctLoc };
539-
endLoc.endLine = ctLoc.endLine;
540-
endLoc.endCol = ctLoc.endCol;
541-
endLoc.endOffset = ctLoc.endOffset;
542-
} else {
543-
endLoc.endLine = ctLoc.startLine;
544-
endLoc.endCol = ctLoc.startCol;
545-
endLoc.endOffset = ctLoc.startOffset;
546-
}
534+
const endLoc: Partial<ElementLocation> =
535+
// NOTE: For cases like <p> <p> </p> - First 'p' closes without a closing
536+
// tag and for cases like <td> <p> </td> - 'p' closes without a closing tag.
537+
closingToken.type === TokenType.END_TAG && tn === closingToken.tagName
538+
? {
539+
endTag: { ...ctLoc },
540+
endLine: ctLoc.endLine,
541+
endCol: ctLoc.endCol,
542+
endOffset: ctLoc.endOffset,
543+
}
544+
: {
545+
endLine: ctLoc.startLine,
546+
endCol: ctLoc.startCol,
547+
endOffset: ctLoc.startOffset,
548+
};
547549

548550
this.treeAdapter.updateNodeSourceCodeLocation(element, endLoc);
549551
}
@@ -789,7 +791,7 @@ export class Parser<T extends TreeAdapterTypeMap> {
789791
this.skipNextNewLine = false;
790792

791793
if (this.tokenizer.allowCDATA) {
792-
characterInForeignContent(this, token);
794+
characterInForeignContent(this, token.chars, token.location);
793795
return;
794796
}
795797

@@ -816,12 +818,12 @@ export class Parser<T extends TreeAdapterTypeMap> {
816818
case InsertionMode.IN_CAPTION:
817819
case InsertionMode.IN_CELL:
818820
case InsertionMode.IN_TEMPLATE:
819-
characterInBody(this, token);
821+
characterInBody(this, token.chars, token.location);
820822
break;
821823
case InsertionMode.TEXT:
822824
case InsertionMode.IN_SELECT:
823825
case InsertionMode.IN_SELECT_IN_TABLE:
824-
this._insertCharacters(token);
826+
this._insertCharacters(token.chars, token.location);
825827
break;
826828
case InsertionMode.IN_TABLE:
827829
case InsertionMode.IN_TABLE_BODY:
@@ -848,7 +850,7 @@ export class Parser<T extends TreeAdapterTypeMap> {
848850
this.skipNextNewLine = false;
849851

850852
if (this.tokenizer.allowCDATA) {
851-
nullCharacterInForeignContent(this, token);
853+
nullCharacterInForeignContent(this, token.location);
852854
return;
853855
}
854856

@@ -872,7 +874,7 @@ export class Parser<T extends TreeAdapterTypeMap> {
872874
tokenAfterHead(this, token);
873875
break;
874876
case InsertionMode.TEXT:
875-
this._insertCharacters(token);
877+
this._insertCharacters(token.chars, token.location);
876878
break;
877879
case InsertionMode.IN_TABLE:
878880
case InsertionMode.IN_TABLE_BODY:
@@ -1184,7 +1186,7 @@ export class Parser<T extends TreeAdapterTypeMap> {
11841186
}
11851187

11861188
if (this.tokenizer.allowCDATA) {
1187-
this._insertCharacters(token);
1189+
this._insertCharacters(token.chars, token.location);
11881190
return;
11891191
}
11901192

@@ -1198,7 +1200,7 @@ export class Parser<T extends TreeAdapterTypeMap> {
11981200
case InsertionMode.IN_SELECT_IN_TABLE:
11991201
case InsertionMode.IN_FRAMESET:
12001202
case InsertionMode.AFTER_FRAMESET:
1201-
this._insertCharacters(token);
1203+
this._insertCharacters(token.chars, token.location);
12021204
break;
12031205
case InsertionMode.IN_BODY:
12041206
case InsertionMode.IN_CAPTION:
@@ -1207,7 +1209,7 @@ export class Parser<T extends TreeAdapterTypeMap> {
12071209
case InsertionMode.AFTER_BODY:
12081210
case InsertionMode.AFTER_AFTER_BODY:
12091211
case InsertionMode.AFTER_AFTER_FRAMESET:
1210-
whitespaceCharacterInBody(this, token);
1212+
whitespaceCharacterInBody(this, token.chars, token.location);
12111213
break;
12121214
case InsertionMode.IN_TABLE:
12131215
case InsertionMode.IN_TABLE_BODY:
@@ -1735,11 +1737,11 @@ function tokenAfterHead<T extends TreeAdapterTypeMap>(p: Parser<T>, token: Token
17351737
function modeInBody<T extends TreeAdapterTypeMap>(p: Parser<T>, token: Token): void {
17361738
switch (token.type) {
17371739
case TokenType.CHARACTER: {
1738-
characterInBody(p, token);
1740+
characterInBody(p, token.chars, token.location);
17391741
break;
17401742
}
17411743
case TokenType.WHITESPACE_CHARACTER: {
1742-
whitespaceCharacterInBody(p, token);
1744+
whitespaceCharacterInBody(p, token.chars, token.location);
17431745
break;
17441746
}
17451747
case TokenType.COMMENT: {
@@ -1763,14 +1765,18 @@ function modeInBody<T extends TreeAdapterTypeMap>(p: Parser<T>, token: Token): v
17631765
}
17641766
}
17651767

1766-
function whitespaceCharacterInBody<T extends TreeAdapterTypeMap>(p: Parser<T>, token: CharacterToken): void {
1768+
function whitespaceCharacterInBody<T extends TreeAdapterTypeMap>(
1769+
p: Parser<T>,
1770+
chars: string,
1771+
location: Location | null
1772+
): void {
17671773
p._reconstructActiveFormattingElements();
1768-
p._insertCharacters(token);
1774+
p._insertCharacters(chars, location);
17691775
}
17701776

1771-
function characterInBody<T extends TreeAdapterTypeMap>(p: Parser<T>, token: CharacterToken): void {
1777+
function characterInBody<T extends TreeAdapterTypeMap>(p: Parser<T>, chars: string, location: Location | null): void {
17721778
p._reconstructActiveFormattingElements();
1773-
p._insertCharacters(token);
1779+
p._insertCharacters(chars, location);
17741780
p.framesetOk = false;
17751781
}
17761782

@@ -2570,7 +2576,7 @@ function eofInText<T extends TreeAdapterTypeMap>(p: Parser<T>, token: EOFToken):
25702576
//------------------------------------------------------------------
25712577
function characterInTable<T extends TreeAdapterTypeMap>(p: Parser<T>, token: CharacterToken): void {
25722578
if (TABLE_STRUCTURE_TAGS.has(p.openElements.currentTagId)) {
2573-
p.pendingCharacterTokens = [];
2579+
p.pendingCharacterTokens.length = 0;
25742580
p.hasNonWhitespacePendingCharacterToken = false;
25752581
p.originalInsertionMode = p.insertionMode;
25762582
p.insertionMode = InsertionMode.IN_TABLE_TEXT;
@@ -2762,7 +2768,8 @@ function tokenInTableText<T extends TreeAdapterTypeMap>(p: Parser<T>, token: Tok
27622768
}
27632769
} else {
27642770
for (; i < p.pendingCharacterTokens.length; i++) {
2765-
p._insertCharacters(p.pendingCharacterTokens[i]);
2771+
const { chars, location } = p.pendingCharacterTokens[i];
2772+
p._insertCharacters(chars, location);
27662773
}
27672774
}
27682775

@@ -3420,13 +3427,16 @@ function startTagAfterAfterFrameset<T extends TreeAdapterTypeMap>(p: Parser<T>,
34203427

34213428
// The rules for parsing tokens in foreign content
34223429
//------------------------------------------------------------------
3423-
function nullCharacterInForeignContent<T extends TreeAdapterTypeMap>(p: Parser<T>, token: CharacterToken): void {
3424-
token.chars = unicode.REPLACEMENT_CHARACTER;
3425-
p._insertCharacters(token);
3430+
function nullCharacterInForeignContent<T extends TreeAdapterTypeMap>(p: Parser<T>, location: Location | null): void {
3431+
p._insertCharacters(unicode.REPLACEMENT_CHARACTER, location);
34263432
}
34273433

3428-
function characterInForeignContent<T extends TreeAdapterTypeMap>(p: Parser<T>, token: CharacterToken): void {
3429-
p._insertCharacters(token);
3434+
function characterInForeignContent<T extends TreeAdapterTypeMap>(
3435+
p: Parser<T>,
3436+
chars: string,
3437+
location: Location | null
3438+
): void {
3439+
p._insertCharacters(chars, location);
34303440
p.framesetOk = false;
34313441
}
34323442

0 commit comments

Comments
 (0)