From 222b145f4052596807b848116fb7f7581ddadc7c Mon Sep 17 00:00:00 2001 From: Dylan Piercey Date: Tue, 2 Aug 2022 10:30:22 -0700 Subject: [PATCH] fix: require a whitespace before js style comments inside html content (#127) --- .changeset/wicked-mangos-camp.md | 5 +++++ .../comments-within-tag-body.expected.txt | 22 ++++++++++++++----- .../comments-within-tag-body/input.marko | 4 ++++ src/states/HTML_CONTENT.ts | 5 ++++- 4 files changed, 29 insertions(+), 7 deletions(-) create mode 100644 .changeset/wicked-mangos-camp.md diff --git a/.changeset/wicked-mangos-camp.md b/.changeset/wicked-mangos-camp.md new file mode 100644 index 00000000..3bb8d7be --- /dev/null +++ b/.changeset/wicked-mangos-camp.md @@ -0,0 +1,5 @@ +--- +"htmljs-parser": patch +--- + +Fix regression around JS style comments in the body by requiring that they are preceded by a whitespace. diff --git a/src/__tests__/fixtures/comments-within-tag-body/__snapshots__/comments-within-tag-body.expected.txt b/src/__tests__/fixtures/comments-within-tag-body/__snapshots__/comments-within-tag-body.expected.txt index 6e509e61..8e816a74 100644 --- a/src/__tests__/fixtures/comments-within-tag-body/__snapshots__/comments-within-tag-body.expected.txt +++ b/src/__tests__/fixtures/comments-within-tag-body/__snapshots__/comments-within-tag-body.expected.txt @@ -16,9 +16,19 @@ 6╭─ │ │ ╰─ comment.value " and this is a comment " ╰─ ╰─ comment "" -7╭─ - │ │ │ ╰─ closeTagEnd(div) - │ │ ╰─ closeTagName "div" - │ ├─ text "\n" - ╰─ ╰─ closeTagStart " + │ │ │ ╰─ closeTagEnd(div) + │ │ ╰─ closeTagName "div" + ╰─ ╰─ closeTagStart " + But this, this is not a http://comment.com + And yet this is a //comment + And also /* this is a comment */ + Because a//comment must be preceded by whitespace. diff --git a/src/states/HTML_CONTENT.ts b/src/states/HTML_CONTENT.ts index a982d41e..6af0faee 100644 --- a/src/states/HTML_CONTENT.ts +++ b/src/states/HTML_CONTENT.ts @@ -94,7 +94,10 @@ export const HTML_CONTENT: StateDefinition = { this.endText(); this.enterState(STATE.INLINE_SCRIPT); this.pos++; // skip space - } else if (code === CODE.FORWARD_SLASH) { + } else if ( + code === CODE.FORWARD_SLASH && + isWhitespaceCode(this.lookAtCharCodeAhead(-1)) + ) { // Check next character to see if we are in a comment switch (this.lookAtCharCodeAhead(1)) { case CODE.FORWARD_SLASH: