Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/tidy-carrots-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"htmljs-parser": patch
---

Fix `isValidAttrValue` (and `isValidStatement` / `isValidScriptlet`) reporting `enclosed` for a value that ends in a trailing `//` line comment (e.g. `"hello" // note`).

A line comment with no terminating newline consumes everything that would follow it on the line, so such a value is not safe to emit verbatim. It is now classified as `valid` (needs wrapping when placed inline), matching the existing behavior for an unguarded trailing newline. A comment guarded by a group (e.g. `("hello" // c\n)`) or a self-closing block comment (`"hello" /* c */`) still classifies as `enclosed`.
30 changes: 30 additions & 0 deletions src/__tests__/validate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,5 +124,35 @@ describe("validation helpers", () => {
it("rejects keyword operator with no operand", () => {
assert.equal(isValidAttrValue("a as ", true), 0);
});

it("treats a trailing line comment as unguarded", () => {
assert.equal(isValidAttrValue('"hello" // c', false), 1);
assert.equal(isValidAttrValue('"hello" // c', true), 1);
assert.equal(isValidAttrValue("foo // c", false), 1);
assert.equal(isValidAttrValue("1 + 2 // c", false), 1);
assert.equal(isValidAttrValue('"hello" /* c */ // d', false), 1);
});

it("keeps a self-closing block comment enclosed", () => {
assert.equal(isValidAttrValue('"hello" /* c */', false), 2);
});

it("keeps a line comment guarded by a group enclosed", () => {
assert.equal(isValidAttrValue('("hello" // c\n)', false), 2);
});

it("treats a newline after a value as unguarded", () => {
assert.equal(isValidAttrValue('"hello"\n// c', false), 1);
});
});

describe("trailing line comments", () => {
it("downgrades statements with a trailing line comment", () => {
assert.equal(isValidStatement("foo // c"), 1);
});

it("downgrades scriptlets with a trailing line comment", () => {
assert.equal(isValidScriptlet("foo // c"), 1);
});
});
});
9 changes: 9 additions & 0 deletions src/states/EXPRESSION.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,15 @@ export const EXPRESSION: StateDefinition<ExpressionMeta> = {
return(child, expression) {
if (child.state === STATE.JS_COMMENT_LINE) {
expression.wasComment = true;
// A line comment that runs to the end of the input (rather than being
// terminated by a newline or a closing tag) consumes everything that
// would follow it on the line, exactly like an unguarded newline. Flag
// it as unguarded so the value is not classified as `enclosed` (safe to
// inline verbatim) by the validation helpers — otherwise emitting it
// bare would comment out whatever comes next.
if (this.pos === this.maxPos && !expression.groupStack.length) {
expression.hadUnguardedNewline = true;
}
}
},
};
Expand Down
Loading