From 042f9b1d749a1aa2fc9a5a2851133900a5cb860e Mon Sep 17 00:00:00 2001 From: Dylan Piercey Date: Fri, 11 Mar 2022 22:21:46 -0700 Subject: [PATCH] refactor: remove offsets for text helpers --- src/core/Parser.ts | 12 ++++-------- src/states/BEGIN_DELIMITED_HTML_BLOCK.ts | 5 +++-- src/states/HTML_CONTENT.ts | 2 +- test/autotest/mixed-cdata/expected.html | 2 +- 4 files changed, 9 insertions(+), 12 deletions(-) diff --git a/src/core/Parser.ts b/src/core/Parser.ts index ed16ecef..351a2dad 100644 --- a/src/core/Parser.ts +++ b/src/core/Parser.ts @@ -164,20 +164,16 @@ export class Parser { return (this.pos += offset); } - startText(offset = 0) { + startText() { if (this.textPos === -1) { - this.textPos = this.pos + offset; + this.textPos = this.pos; } } - endText(offset = 0) { + endText() { const start = this.textPos; if (start !== -1) { - const end = this.pos + offset; - if (start < end) { - this.handlers.onText?.({ start, end }); - } - + this.handlers.onText?.({ start, end: this.pos }); this.textPos = -1; } } diff --git a/src/states/BEGIN_DELIMITED_HTML_BLOCK.ts b/src/states/BEGIN_DELIMITED_HTML_BLOCK.ts index 86969633..041ae4d6 100644 --- a/src/states/BEGIN_DELIMITED_HTML_BLOCK.ts +++ b/src/states/BEGIN_DELIMITED_HTML_BLOCK.ts @@ -57,8 +57,9 @@ export function handleDelimitedBlockEOL( if (parser.lookAheadFor(endHtmlBlockLookahead, parser.pos + newLineLength)) { parser.startText(); // we want to at least include the newline as text. - parser.endText(newLineLength); - parser.skip(endHtmlBlockLookahead.length + newLineLength); + parser.skip(newLineLength); + parser.endText(); + parser.skip(endHtmlBlockLookahead.length); if (parser.consumeWhitespaceOnLine(0)) { parser.endHtmlBlock(); diff --git a/src/states/HTML_CONTENT.ts b/src/states/HTML_CONTENT.ts index 4e24c4de..cf546456 100644 --- a/src/states/HTML_CONTENT.ts +++ b/src/states/HTML_CONTENT.ts @@ -84,7 +84,7 @@ export const HTML_CONTENT: StateDefinition = { eol(len, content) { if (this.beginMixedMode) { this.beginMixedMode = false; - this.endText(len); + this.endText(); this.endHtmlBlock(); } else if (this.endingMixedModeAtEOL) { this.endingMixedModeAtEOL = false; diff --git a/test/autotest/mixed-cdata/expected.html b/test/autotest/mixed-cdata/expected.html index 2f13d939..ab98f580 100644 --- a/test/autotest/mixed-cdata/expected.html +++ b/test/autotest/mixed-cdata/expected.html @@ -1,6 +1,6 @@

cdata:"Hello World" - text:" This is after CDATA\n" + text:" This is after CDATA"

cdata:"Hello World"