From 59a10d57b1c6fa273ceafe19ff814a1ae03409bf Mon Sep 17 00:00:00 2001 From: Dylan Piercey Date: Mon, 8 Aug 2022 21:10:14 -0700 Subject: [PATCH] fix: regression with comments inside parsed text state (#132) --- .changeset/neat-cobras-battle.md | 5 +++ .changeset/quick-seals-watch.md | 5 +++ .../cdata/__snapshots__/cdata.expected.txt | 12 ++----- src/__tests__/fixtures/cdata/input.marko | 2 +- .../script-single-line-comment.expected.txt | 31 ++++++++++++++++++- .../script-single-line-comment/input.marko | 7 ++++- src/states/JS_COMMENT_LINE.ts | 7 +++-- src/states/PARSED_TEXT_CONTENT.ts | 5 +-- 8 files changed, 55 insertions(+), 19 deletions(-) create mode 100644 .changeset/neat-cobras-battle.md create mode 100644 .changeset/quick-seals-watch.md diff --git a/.changeset/neat-cobras-battle.md b/.changeset/neat-cobras-battle.md new file mode 100644 index 00000000..26577f80 --- /dev/null +++ b/.changeset/neat-cobras-battle.md @@ -0,0 +1,5 @@ +--- +"htmljs-parser": patch +--- + +Fix regression which caused script tags with a trailing comment as the same line as the closing tag to not always parse properly. diff --git a/.changeset/quick-seals-watch.md b/.changeset/quick-seals-watch.md new file mode 100644 index 00000000..d64e55dc --- /dev/null +++ b/.changeset/quick-seals-watch.md @@ -0,0 +1,5 @@ +--- +"htmljs-parser": patch +--- + +Remove unecessary check for cdata inside parsed text state. diff --git a/src/__tests__/fixtures/cdata/__snapshots__/cdata.expected.txt b/src/__tests__/fixtures/cdata/__snapshots__/cdata.expected.txt index bde1ee30..9ab9c67a 100644 --- a/src/__tests__/fixtures/cdata/__snapshots__/cdata.expected.txt +++ b/src/__tests__/fixtures/cdata/__snapshots__/cdata.expected.txt @@ -1,9 +1,3 @@ -1╭─
- │ ││ ││ │ │ │ ╰─ closeTagEnd(html-comment) - │ ││ ││ │ │ ╰─ closeTagName "html-comment" - │ ││ ││ │ ╰─ closeTagStart "
" - │ ││ ╰─ openTagEnd - │ │╰─ tagName "html-comment" - ╰─ ╰─ openTagStart \ No newline at end of file +1╭─
+ │ │ ╰─ cdata.value "[if lt IE 9]>
" \ No newline at end of file diff --git a/src/__tests__/fixtures/cdata/input.marko b/src/__tests__/fixtures/cdata/input.marko index ab8080b5..f757a841 100644 --- a/src/__tests__/fixtures/cdata/input.marko +++ b/src/__tests__/fixtures/cdata/input.marko @@ -1 +1 @@ -
\ No newline at end of file +
\ No newline at end of file diff --git a/src/__tests__/fixtures/script-single-line-comment/__snapshots__/script-single-line-comment.expected.txt b/src/__tests__/fixtures/script-single-line-comment/__snapshots__/script-single-line-comment.expected.txt index d2f5368d..c2f333bf 100644 --- a/src/__tests__/fixtures/script-single-line-comment/__snapshots__/script-single-line-comment.expected.txt +++ b/src/__tests__/fixtures/script-single-line-comment/__snapshots__/script-single-line-comment.expected.txt @@ -5,4 +5,33 @@ │ ││ │╰─ text "// this is a comment" │ ││ ╰─ openTagEnd │ │╰─ tagName "script" - ╰─ ╰─ openTagStart \ No newline at end of file + ╰─ ╰─ openTagStart +2├─ +3╭─
+ │ ││ ╰─ openTagEnd + │ │╰─ tagName "div" + ╰─ ╰─ openTagStart +4╭─ + │ │ ││ ││ │ │ ╰─ closeTagEnd(script) + │ │ ││ ││ │ ╰─ closeTagName "script" + │ │ ││ ││ ╰─ closeTagStart "hi + │ │ ││ ││ │ │ ╰─ closeTagEnd(span) + │ │ ││ ││ │ ╰─ closeTagName "span" + │ │ ││ ││ ╰─ closeTagStart " + │ │ │ ╰─ closeTagEnd(div) + │ │ ╰─ closeTagName "div" + │ ├─ text "\n" + ╰─ ╰─ closeTagStart "// this is a comment \ No newline at end of file + + +
+ + hi +
diff --git a/src/states/JS_COMMENT_LINE.ts b/src/states/JS_COMMENT_LINE.ts index 9e8b3fd1..55f24545 100644 --- a/src/states/JS_COMMENT_LINE.ts +++ b/src/states/JS_COMMENT_LINE.ts @@ -21,11 +21,12 @@ export const JS_COMMENT_LINE: StateDefinition = { if ( !this.isConcise && code === CODE.OPEN_ANGLE_BRACKET && - this.activeTag?.type === TagType.text + this.activeTag?.type === TagType.text && + STATE.checkForClosingTag(this) ) { - // First, see if we need to see if we reached the closing tag + // We need to see if we reached the closing tag // eg: - STATE.checkForClosingTag(this); + this.exitState(); } }, diff --git a/src/states/PARSED_TEXT_CONTENT.ts b/src/states/PARSED_TEXT_CONTENT.ts index 500436d7..4904354c 100644 --- a/src/states/PARSED_TEXT_CONTENT.ts +++ b/src/states/PARSED_TEXT_CONTENT.ts @@ -29,10 +29,7 @@ export const PARSED_TEXT_CONTENT: StateDefinition = { char(code) { switch (code) { case CODE.OPEN_ANGLE_BRACKET: - if ( - this.isConcise || - !(STATE.checkForClosingTag(this) || STATE.checkForCDATA(this)) - ) { + if (this.isConcise || !STATE.checkForClosingTag(this)) { this.startText(); } break;