From cdbc6b2cfb22070f2330e7e37eb61a79e21f0c4d Mon Sep 17 00:00:00 2001 From: Dylan Piercey Date: Fri, 12 Aug 2022 09:33:24 -0700 Subject: [PATCH] fix: improve missing attribute value error (#134) --- .changeset/moody-donuts-enjoy.md | 5 +++++ .../__snapshots__/attr-value-missing.expected.txt | 6 ++++++ .../fixtures/attr-value-missing/input.marko | 1 + src/states/ATTRIBUTE.ts | 13 ++++++++++--- 4 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 .changeset/moody-donuts-enjoy.md create mode 100644 src/__tests__/fixtures/attr-value-missing/__snapshots__/attr-value-missing.expected.txt create mode 100644 src/__tests__/fixtures/attr-value-missing/input.marko diff --git a/.changeset/moody-donuts-enjoy.md b/.changeset/moody-donuts-enjoy.md new file mode 100644 index 00000000..801b6479 --- /dev/null +++ b/.changeset/moody-donuts-enjoy.md @@ -0,0 +1,5 @@ +--- +"htmljs-parser": patch +--- + +Improve missing attribute error when the tag is immediately closed without the attribute value. diff --git a/src/__tests__/fixtures/attr-value-missing/__snapshots__/attr-value-missing.expected.txt b/src/__tests__/fixtures/attr-value-missing/__snapshots__/attr-value-missing.expected.txt new file mode 100644 index 00000000..27ab9c88 --- /dev/null +++ b/src/__tests__/fixtures/attr-value-missing/__snapshots__/attr-value-missing.expected.txt @@ -0,0 +1,6 @@ +1╭─ + │ ││ │ ╰─ error(INVALID_ATTRIBUTE_VALUE:Missing value for attribute) + │ ││ ╰─ attrName "class" + │ │╰─ tagName "span" + ╰─ ╰─ openTagStart +2╰─ \ No newline at end of file diff --git a/src/__tests__/fixtures/attr-value-missing/input.marko b/src/__tests__/fixtures/attr-value-missing/input.marko new file mode 100644 index 00000000..e73cd64c --- /dev/null +++ b/src/__tests__/fixtures/attr-value-missing/input.marko @@ -0,0 +1 @@ + diff --git a/src/states/ATTRIBUTE.ts b/src/states/ATTRIBUTE.ts index a5adfab6..5418ff2a 100644 --- a/src/states/ATTRIBUTE.ts +++ b/src/states/ATTRIBUTE.ts @@ -266,15 +266,22 @@ function shouldTerminateHtmlAttrName(code: number, data: string, pos: number) { } } -function shouldTerminateHtmlAttrValue(code: number, data: string, pos: number) { +function shouldTerminateHtmlAttrValue( + this: STATE.ExpressionMeta, + code: number, + data: string, + pos: number +) { switch (code) { case CODE.COMMA: return true; case CODE.FORWARD_SLASH: return data.charCodeAt(pos + 1) === CODE.CLOSE_ANGLE_BRACKET; - // Add special case for => case CODE.CLOSE_ANGLE_BRACKET: - return data.charCodeAt(pos - 1) !== CODE.EQUAL; + // Add special case for => + // We only look behind to match => if we're not at the start of the expression + // otherwise this would match something like "". + return pos === this.start || data.charCodeAt(pos - 1) !== CODE.EQUAL; default: return false; }