diff --git a/src/__tests__/fixtures/argument-tag-nested-parens/__snapshots__/argument-tag-nested-parens.expected.txt b/src/__tests__/fixtures/argument-tag-nested-parens/__snapshots__/argument-tag-nested-parens.expected.txt index ed2d5d30..ba5b451e 100644 --- a/src/__tests__/fixtures/argument-tag-nested-parens/__snapshots__/argument-tag-nested-parens.expected.txt +++ b/src/__tests__/fixtures/argument-tag-nested-parens/__snapshots__/argument-tag-nested-parens.expected.txt @@ -4,12 +4,9 @@ │ ││ │╰─ tagArgs.value "notEmpty(data.colors)" │ ││ ╰─ tagArgs "(notEmpty(data.colors))" │ │╰─ tagName "if" - │ ├─ text "\n" ╰─ ╰─ openTagStart 3╭─ │ │ │ ╰─ closeTagEnd(if) │ │ ╰─ closeTagName "if" - │ ├─ text "\n" ╰─ ╰─ closeTagStart "You can throw in some HTML markup if you want! │ ││ ││ │ │ ╰─ closeTagEnd(strong) @@ -17,18 +17,20 @@ │ ││ ╰─ openTagEnd │ │╰─ tagName "strong" ╰─ ╰─ openTagStart -9╭─ -- - ╰─ ╰─ text "\n" +9├─ -- 10╭─ p │ ├─ closeTagEnd(p) ╰─ ╰─ tagName 11╭─ ----- ╰─ ╰─ openTagEnd 12╭─ If you really want to you can change the block delimiter for the following - ╰─ ╰─ text "\n If you really want to you can change the block delimiter for the following\n reasons:\n - Avoid accidentally ending a block\n - To improve readability\n" -13├─ reasons: -14├─ - Avoid accidentally ending a block -15├─ - To improve readability + ╰─ ╰─ text "If you really want to you can change the block delimiter for the following" +13╭─ reasons: + ╰─ ╰─ text "reasons:" +14╭─ - Avoid accidentally ending a block + ╰─ ╰─ text "- Avoid accidentally ending a block" +15╭─ - To improve readability + ╰─ ╰─ text "- To improve readability" 16├─ ----- 17╭─ i -- This text will be in italics │ │ │ ╰─ text "This text will be in italics" diff --git a/src/__tests__/fixtures/concise-multiline-content/__snapshots__/concise-multiline-content.expected.txt b/src/__tests__/fixtures/concise-multiline-content/__snapshots__/concise-multiline-content.expected.txt index ff3cce84..a2f34d8f 100644 --- a/src/__tests__/fixtures/concise-multiline-content/__snapshots__/concise-multiline-content.expected.txt +++ b/src/__tests__/fixtures/concise-multiline-content/__snapshots__/concise-multiline-content.expected.txt @@ -1,10 +1,10 @@ 1╭─ -- 2╭─ hi - ╰─ ╰─ text "\nhi\n" + ╰─ ╰─ text "hi" 3├─ -- 4├─ 5├─ -- 6╭─ foo - ╰─ ╰─ text "\nfoo\n\n" + ╰─ ╰─ text "foo" 7├─ 8╰─ \ No newline at end of file diff --git a/src/__tests__/fixtures/double-hyphen-block/__snapshots__/double-hyphen-block.expected.txt b/src/__tests__/fixtures/double-hyphen-block/__snapshots__/double-hyphen-block.expected.txt index d16a7b6b..62958ace 100644 --- a/src/__tests__/fixtures/double-hyphen-block/__snapshots__/double-hyphen-block.expected.txt +++ b/src/__tests__/fixtures/double-hyphen-block/__snapshots__/double-hyphen-block.expected.txt @@ -20,7 +20,7 @@ 6╭─ --- ╰─ ╰─ openTagEnd 7╭─ var a = 'marko'; - ╰─ ╰─ text "\n var a = 'marko';\n" + ╰─ ╰─ text "var a = 'marko';" 8╭─ --- │ ├─ closeTagEnd(html) │ ├─ closeTagEnd(body) diff --git a/src/__tests__/fixtures/multiline-html-block-empty/__snapshots__/multiline-html-block-empty.expected.txt b/src/__tests__/fixtures/multiline-html-block-empty/__snapshots__/multiline-html-block-empty.expected.txt index b4f6e3cb..22363d14 100644 --- a/src/__tests__/fixtures/multiline-html-block-empty/__snapshots__/multiline-html-block-empty.expected.txt +++ b/src/__tests__/fixtures/multiline-html-block-empty/__snapshots__/multiline-html-block-empty.expected.txt @@ -1,3 +1,2 @@ 1╭─ --- -2╭─ --- - ╰─ ╰─ text "\n" \ No newline at end of file +2╰─ --- \ No newline at end of file diff --git a/src/__tests__/fixtures/multiline-html-block-missing-end/__snapshots__/multiline-html-block-missing-end.expected.txt b/src/__tests__/fixtures/multiline-html-block-missing-end/__snapshots__/multiline-html-block-missing-end.expected.txt index e397d73c..13a4a3ec 100644 --- a/src/__tests__/fixtures/multiline-html-block-missing-end/__snapshots__/multiline-html-block-missing-end.expected.txt +++ b/src/__tests__/fixtures/multiline-html-block-missing-end/__snapshots__/multiline-html-block-missing-end.expected.txt @@ -3,19 +3,21 @@ 2╭─ -- ╰─ ╰─ openTagEnd 3╭─ Hello World - ╰─ ╰─ text "\n Hello World\n Hello Again\n" -4├─ Hello Again + ╰─ ╰─ text "Hello World" +4╭─ Hello Again + ╰─ ╰─ text "Hello Again" 5├─ 6╭─ script -- │ │ ╰─ openTagEnd │ ├─ closeTagEnd(div) ╰─ ╰─ tagName "script" 7╭─ console.log('foo'); - ╰─ ╰─ text "\n console.log('foo');\n alert('bar');\n" -8├─ alert('bar'); + ╰─ ╰─ text "console.log('foo');" +8╭─ alert('bar'); + ╰─ ╰─ text "alert('bar');" 9├─ 10╭─ -- ╰─ ╰─ closeTagEnd(script) 11╭─ Multi-line text - ╰─ ╰─ text "\nMulti-line text\nReached EOF" + ╰─ ╰─ text "Multi-line text\nReached EOF" 12╰─ Reached EOF \ No newline at end of file diff --git a/src/__tests__/fixtures/multiline-html-block-nested-empty/__snapshots__/multiline-html-block-nested-empty.expected.txt b/src/__tests__/fixtures/multiline-html-block-nested-empty/__snapshots__/multiline-html-block-nested-empty.expected.txt index b5003489..49cd64dc 100644 --- a/src/__tests__/fixtures/multiline-html-block-nested-empty/__snapshots__/multiline-html-block-nested-empty.expected.txt +++ b/src/__tests__/fixtures/multiline-html-block-nested-empty/__snapshots__/multiline-html-block-nested-empty.expected.txt @@ -3,5 +3,4 @@ 2╭─ --- ╰─ ╰─ openTagEnd 3╭─ --- - │ │ ╰─ closeTagEnd(p) - ╰─ ╰─ text "\n" \ No newline at end of file + ╰─ ╰─ closeTagEnd(p) \ No newline at end of file diff --git a/src/__tests__/fixtures/multiline-html-block-nested/__snapshots__/multiline-html-block-nested.expected.txt b/src/__tests__/fixtures/multiline-html-block-nested/__snapshots__/multiline-html-block-nested.expected.txt index 172cc19e..e17bf298 100644 --- a/src/__tests__/fixtures/multiline-html-block-nested/__snapshots__/multiline-html-block-nested.expected.txt +++ b/src/__tests__/fixtures/multiline-html-block-nested/__snapshots__/multiline-html-block-nested.expected.txt @@ -3,15 +3,15 @@ 2╭─ --- ╰─ ╰─ openTagEnd 3╭─ Hello Frank - │ │ ││ ││ │ │ ╰─ closeTagEnd(strong) - │ │ ││ ││ │ ╰─ closeTagName "strong" - │ │ ││ ││ ╰─ closeTagStart " │ ├─ error(EXTRA_CLOSING_TAG:The closing "img" tag was not expected) "" ╰─ ╰─ closeTagStart " block\n" -4├─ within the block + ╰─ ╰─ text "This is text" +4╭─ within the block + ╰─ ╰─ text "within the block" 5├─ --- 6╭─ -- This should be okay too ╰─ ╰─ text "This should be okay too" diff --git a/src/__tests__/fixtures/scriptlet-block-html/__snapshots__/scriptlet-block-html.expected.txt b/src/__tests__/fixtures/scriptlet-block-html/__snapshots__/scriptlet-block-html.expected.txt index 91a25c1f..ad97be60 100644 --- a/src/__tests__/fixtures/scriptlet-block-html/__snapshots__/scriptlet-block-html.expected.txt +++ b/src/__tests__/fixtures/scriptlet-block-html/__snapshots__/scriptlet-block-html.expected.txt @@ -1,10 +1,8 @@ 1╭─ --- 2╭─ $ { - │ ├─ text "\n" ╰─ ╰─ scriptlet:block "$ {\n var foo = 123;\n var bar = 456;\n}" 3╭─ var foo = 123; ╰─ ╰─ scriptlet:block.value "\n var foo = 123;\n var bar = 456;\n" 4├─ var bar = 456; 5├─ } -6╭─ --- - ╰─ ╰─ text "\n" \ No newline at end of file +6╰─ --- \ No newline at end of file diff --git a/src/__tests__/fixtures/scriptlet-line-html/__snapshots__/scriptlet-line-html.expected.txt b/src/__tests__/fixtures/scriptlet-line-html/__snapshots__/scriptlet-line-html.expected.txt index dd9267bc..cc90cb37 100644 --- a/src/__tests__/fixtures/scriptlet-line-html/__snapshots__/scriptlet-line-html.expected.txt +++ b/src/__tests__/fixtures/scriptlet-line-html/__snapshots__/scriptlet-line-html.expected.txt @@ -1,7 +1,5 @@ 1╭─ --- 2╭─ $ var foo = 123; │ │ ╰─ scriptlet.value "var foo = 123;" - │ ├─ text "\n" ╰─ ╰─ scriptlet "$ var foo = 123;" -3╭─ --- - ╰─ ╰─ text "\n" \ No newline at end of file +3╰─ --- \ No newline at end of file diff --git a/src/__tests__/fixtures/scriptlet-line-no-middle-html/__snapshots__/scriptlet-line-no-middle-html.expected.txt b/src/__tests__/fixtures/scriptlet-line-no-middle-html/__snapshots__/scriptlet-line-no-middle-html.expected.txt index 7a43d8d8..9de0a6ff 100644 --- a/src/__tests__/fixtures/scriptlet-line-no-middle-html/__snapshots__/scriptlet-line-no-middle-html.expected.txt +++ b/src/__tests__/fixtures/scriptlet-line-no-middle-html/__snapshots__/scriptlet-line-no-middle-html.expected.txt @@ -1,4 +1,4 @@ 1╭─ --- 2╭─ hello $ var foo = 123; - ╰─ ╰─ text "\nhello $ var foo = 123;\n" + ╰─ ╰─ text "hello $ var foo = 123;" 3╰─ --- \ No newline at end of file diff --git a/src/__tests__/fixtures/simple-text-delimited/__snapshots__/simple-text-delimited.expected.txt b/src/__tests__/fixtures/simple-text-delimited/__snapshots__/simple-text-delimited.expected.txt index 3a420756..09e75039 100644 --- a/src/__tests__/fixtures/simple-text-delimited/__snapshots__/simple-text-delimited.expected.txt +++ b/src/__tests__/fixtures/simple-text-delimited/__snapshots__/simple-text-delimited.expected.txt @@ -1,5 +1,5 @@ 1╭─ --- 2╭─ test - ╰─ ╰─ text "\ntest\n" + ╰─ ╰─ text "test" 3├─ --- 4╰─ \ No newline at end of file diff --git a/src/__tests__/fixtures/tag-var-before-concise-text/__snapshots__/tag-var-before-concise-text.expected.txt b/src/__tests__/fixtures/tag-var-before-concise-text/__snapshots__/tag-var-before-concise-text.expected.txt index 360f1fd3..9aee8217 100644 --- a/src/__tests__/fixtures/tag-var-before-concise-text/__snapshots__/tag-var-before-concise-text.expected.txt +++ b/src/__tests__/fixtures/tag-var-before-concise-text/__snapshots__/tag-var-before-concise-text.expected.txt @@ -4,8 +4,10 @@ │ │ ╰─ tagVar "/style" ╰─ ╰─ tagName "style" 2╭─ header { - ╰─ ╰─ text "\n header {\n color: green\n }\n" -3├─ color: green -4├─ } + ╰─ ╰─ text "header {" +3╭─ color: green + ╰─ ╰─ text " color: green" +4╭─ } + ╰─ ╰─ text 5╭─ ╰─ ╰─ closeTagEnd(style) \ No newline at end of file diff --git a/src/__tests__/fixtures/text-parse-mode-delimited-block/__snapshots__/text-parse-mode-delimited-block.expected.txt b/src/__tests__/fixtures/text-parse-mode-delimited-block/__snapshots__/text-parse-mode-delimited-block.expected.txt index a0e38ccb..bc995269 100644 --- a/src/__tests__/fixtures/text-parse-mode-delimited-block/__snapshots__/text-parse-mode-delimited-block.expected.txt +++ b/src/__tests__/fixtures/text-parse-mode-delimited-block/__snapshots__/text-parse-mode-delimited-block.expected.txt @@ -6,7 +6,7 @@ 3╭─ --- ╰─ ╰─ openTagEnd 4╭─ Test - ╰─ ╰─ text "\n Test\n " + ╰─ ╰─ text "Test" 5╭─ Hello World │ ││││ │ │╰─ closeTagEnd(b) │ ││││ │ ╰─ closeTagName @@ -16,6 +16,5 @@ │ │╰─ tagName ╰─ ╰─ openTagStart 6╭─ --- - │ │ ├─ closeTagEnd(div) - │ │ ╰─ closeTagEnd(span) - ╰─ ╰─ text "\n" \ No newline at end of file + │ ├─ closeTagEnd(div) + ╰─ ╰─ closeTagEnd(span) \ No newline at end of file diff --git a/src/__tests__/fixtures/whitespace-preservation/__snapshots__/whitespace-preservation.expected.txt b/src/__tests__/fixtures/whitespace-preservation/__snapshots__/whitespace-preservation.expected.txt index b01d549b..b7d4cf5f 100644 --- a/src/__tests__/fixtures/whitespace-preservation/__snapshots__/whitespace-preservation.expected.txt +++ b/src/__tests__/fixtures/whitespace-preservation/__snapshots__/whitespace-preservation.expected.txt @@ -32,7 +32,7 @@ │ ├─ closeTagEnd(span) ╰─ ╰─ tagName "span" 11╭─ hi - ╰─ ╰─ text "\n hi\n" + ╰─ ╰─ text "hi" 12├─ 13╭─ span.e -- │ │ ││ ╰─ openTagEnd @@ -41,8 +41,9 @@ │ ├─ closeTagEnd(span) ╰─ ╰─ tagName "span" 14╭─ hi - ╰─ ╰─ text "\n hi\n you\n" -15├─ you + ╰─ ╰─ text "hi" +15╭─ you + ╰─ ╰─ text "you" 16├─ 17╭─ span.f │ │ │╰─ tagShorthandClass.quasis[0] @@ -52,7 +53,7 @@ 18╭─ -- ╰─ ╰─ openTagEnd 19╭─ hi - ╰─ ╰─ text "\n hi\n" + ╰─ ╰─ text "hi" 20├─ -- 21├─ 22╭─ span.g @@ -63,8 +64,9 @@ 23╭─ -- ╰─ ╰─ openTagEnd 24╭─ hi - ╰─ ╰─ text "\n hi\n you\n" -25├─ you + ╰─ ╰─ text "hi" +25╭─ you + ╰─ ╰─ text "you" 26├─ -- 27├─ 28╭─ hi diff --git a/src/core/Parser.ts b/src/core/Parser.ts index be73e713..6e0ff88e 100644 --- a/src/core/Parser.ts +++ b/src/core/Parser.ts @@ -152,7 +152,9 @@ export class Parser { endText() { const start = this.textPos; if (start !== -1) { - this.options.onText?.({ start, end: this.pos }); + if (start !== this.pos) { + this.options.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 5b7036c7..96408d42 100644 --- a/src/states/BEGIN_DELIMITED_HTML_BLOCK.ts +++ b/src/states/BEGIN_DELIMITED_HTML_BLOCK.ts @@ -6,6 +6,7 @@ import { htmlEOF, type Meta, ErrorCode, + isWhitespaceCode, } from "../internal"; export interface DelimitedHTMLBlockMeta extends Meta { @@ -95,10 +96,9 @@ function handleDelimitedBlockEOL( const endHtmlBlockLookahead = indent + delimiter; if (parser.lookAheadFor(endHtmlBlockLookahead, parser.pos + newLineLength)) { - parser.startText(); // we want to at least include the newline as text. - parser.pos += newLineLength; parser.endText(); - parser.pos += endHtmlBlockLookahead.length; + parser.pos += newLineLength + endHtmlBlockLookahead.length; + parser.forward = 0; if (parser.consumeWhitespaceOnLine(0)) { parser.exitState(); @@ -111,20 +111,30 @@ function handleDelimitedBlockEOL( ); } } else if (parser.lookAheadFor(indent, parser.pos + newLineLength)) { + parser.endText(); // We know the next line does not end the multiline HTML block, but we need to check if there // is any indentation that we need to skip over as we continue parsing the HTML in this // multiline HTML block + parser.pos += newLineLength + indent.length; + parser.forward = 0; parser.startText(); - parser.pos += indent.length; // We stay in the same state since we are still parsing a multiline, delimited HTML block } else if (indent && !parser.onlyWhitespaceRemainsOnLine(newLineLength)) { // the next line does not have enough indentation // so unless it is blank (whitespace only), // we will end the block + const pos = parser.pos; + let cur = parser.pos; + while (cur && isWhitespaceCode(parser.data.charCodeAt(cur - 1))) { + cur--; + } + parser.pos = cur; parser.endText(); + parser.pos = pos; + parser.exitState(); parser.exitState(); } else if (parser.pos + newLineLength !== parser.maxPos) { - parser.startText(); + // parser.startText(); } } diff --git a/src/util/util.ts b/src/util/util.ts index 453e3ea8..3ce3b689 100644 --- a/src/util/util.ts +++ b/src/util/util.ts @@ -52,7 +52,18 @@ export function getLines(src: string) { } export function htmlEOF(this: Parser) { - this.endText(); + if (!this.activeTag || this.activeTag.concise) { + const pos = this.pos; + let cur = this.pos; + while (cur && isWhitespaceCode(this.data.charCodeAt(cur - 1))) { + cur--; + } + this.pos = cur; + this.endText(); + this.pos = pos; + } else { + this.endText(); + } while (this.activeTag) { if (this.activeTag.concise) {