From c63e244cb13ab11ec09f35fcac6380726e042769 Mon Sep 17 00:00:00 2001 From: dpiercey Date: Mon, 2 Mar 2026 09:18:12 -0700 Subject: [PATCH] fix: trailing whitespace concise blocks --- .changeset/tricky-aliens-cut.md | 5 +++++ .../concise-block-whitespace-leading.expected.txt | 7 +++++++ .../concise-block-whitespace-leading/input.marko | 3 +++ .../concise-line-whitespace-ending.expected.txt | 4 ++++ .../concise-line-whitespace-ending/input.marko | 1 + src/states/BEGIN_DELIMITED_HTML_BLOCK.ts | 4 ++-- src/util/util.ts | 12 +++++++++++- 7 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 .changeset/tricky-aliens-cut.md create mode 100644 src/__tests__/fixtures/concise-block-whitespace-leading/__snapshots__/concise-block-whitespace-leading.expected.txt create mode 100644 src/__tests__/fixtures/concise-block-whitespace-leading/input.marko create mode 100644 src/__tests__/fixtures/concise-line-whitespace-ending/__snapshots__/concise-line-whitespace-ending.expected.txt create mode 100644 src/__tests__/fixtures/concise-line-whitespace-ending/input.marko diff --git a/.changeset/tricky-aliens-cut.md b/.changeset/tricky-aliens-cut.md new file mode 100644 index 00000000..24e48509 --- /dev/null +++ b/.changeset/tricky-aliens-cut.md @@ -0,0 +1,5 @@ +--- +"htmljs-parser": patch +--- + +Fix incorrectly skipped trailing literal whitespace in concise blocks. diff --git a/src/__tests__/fixtures/concise-block-whitespace-leading/__snapshots__/concise-block-whitespace-leading.expected.txt b/src/__tests__/fixtures/concise-block-whitespace-leading/__snapshots__/concise-block-whitespace-leading.expected.txt new file mode 100644 index 00000000..5355fc00 --- /dev/null +++ b/src/__tests__/fixtures/concise-block-whitespace-leading/__snapshots__/concise-block-whitespace-leading.expected.txt @@ -0,0 +1,7 @@ +1╭─ --- +2╭─ A ${'B'} C + │ │ │ │ ╰─ text " C " + │ │ │ ╰─ placeholder:escape.value "'B'" + │ │ ╰─ placeholder:escape "${'B'}" + ╰─ ╰─ text " A " +3╰─ --- \ No newline at end of file diff --git a/src/__tests__/fixtures/concise-block-whitespace-leading/input.marko b/src/__tests__/fixtures/concise-block-whitespace-leading/input.marko new file mode 100644 index 00000000..8bf75fc0 --- /dev/null +++ b/src/__tests__/fixtures/concise-block-whitespace-leading/input.marko @@ -0,0 +1,3 @@ +--- + A ${'B'} C +--- \ No newline at end of file diff --git a/src/__tests__/fixtures/concise-line-whitespace-ending/__snapshots__/concise-line-whitespace-ending.expected.txt b/src/__tests__/fixtures/concise-line-whitespace-ending/__snapshots__/concise-line-whitespace-ending.expected.txt new file mode 100644 index 00000000..67af2ddf --- /dev/null +++ b/src/__tests__/fixtures/concise-line-whitespace-ending/__snapshots__/concise-line-whitespace-ending.expected.txt @@ -0,0 +1,4 @@ +1╭─ -- ${value} + │ │ │ ╰─ text " " + │ │ ╰─ placeholder:escape.value "value" + ╰─ ╰─ placeholder:escape "${value}" \ No newline at end of file diff --git a/src/__tests__/fixtures/concise-line-whitespace-ending/input.marko b/src/__tests__/fixtures/concise-line-whitespace-ending/input.marko new file mode 100644 index 00000000..51217d69 --- /dev/null +++ b/src/__tests__/fixtures/concise-line-whitespace-ending/input.marko @@ -0,0 +1 @@ +-- ${value} \ No newline at end of file diff --git a/src/states/BEGIN_DELIMITED_HTML_BLOCK.ts b/src/states/BEGIN_DELIMITED_HTML_BLOCK.ts index ac589b6f..b0655bac 100644 --- a/src/states/BEGIN_DELIMITED_HTML_BLOCK.ts +++ b/src/states/BEGIN_DELIMITED_HTML_BLOCK.ts @@ -6,7 +6,7 @@ import { htmlEOF, type Meta, ErrorCode, - isWhitespaceCode, + isLineCode, } from "../internal"; export interface DelimitedHTMLBlockMeta extends Meta { @@ -128,7 +128,7 @@ function handleDelimitedBlockEOL( // we will end the block const pos = parser.pos; let cur = parser.pos; - while (cur && isWhitespaceCode(parser.data.charCodeAt(cur - 1))) { + while (cur && isLineCode(parser.data.charCodeAt(cur - 1))) { cur--; } diff --git a/src/util/util.ts b/src/util/util.ts index 490db0e7..983d6ad3 100644 --- a/src/util/util.ts +++ b/src/util/util.ts @@ -9,6 +9,16 @@ export function isWhitespaceCode(code: number) { return code <= CODE.SPACE; } +export function isLineCode(code: number) { + switch (code) { + case CODE.NEWLINE: + case CODE.CARRIAGE_RETURN: + return true; + default: + return false; + } +} + export function isIndentCode(code: number) { return code === CODE.TAB || code === CODE.SPACE; } @@ -55,7 +65,7 @@ export function htmlEOF(this: Parser) { if (!this.activeTag || this.activeTag.concise) { const pos = this.pos; let cur = this.pos; - while (cur && isWhitespaceCode(this.data.charCodeAt(cur - 1))) { + while (cur && isLineCode(this.data.charCodeAt(cur - 1))) { cur--; }