Skip to content

Commit

Permalink
Throw for bindings in tag-name position (#2071)
Browse files Browse the repository at this point in the history
  • Loading branch information
justinfagnani committed Aug 27, 2021
1 parent 724a9aa commit 0135331
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/rare-guests-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'lit-html': patch
---

In dev mode, throw for tag name bindings. These should use static templates.
7 changes: 6 additions & 1 deletion packages/lit-html/src/lit-html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,12 @@ const getTemplateHtml = (
}
regex = tagEndRegex;
} else if (match[DYNAMIC_TAG_NAME] !== undefined) {
// dynamic tag name
if (DEV_MODE) {
throw new Error(
'Bindings in tag names are not supported. Please use static templates instead. ' +
'See https://lit.dev/docs/templates/expressions/#static-expressions'
);
}
regex = tagEndRegex;
}
} else if (regex === tagEndRegex) {
Expand Down
32 changes: 21 additions & 11 deletions packages/lit-html/src/test/lit-html_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,21 +412,31 @@ suite('lit-html', () => {
});

test('"dynamic" tag name', () => {
render(html`<${'A'}></${'A'}>`, container);
assert.equal(stripExpressionMarkers(container.innerHTML), '<></>');
const template = html`<${'A'}></${'A'}>`;
if (DEV_MODE) {
assert.throws(() => {
render(template, container);
});
} else {
render(template, container);
assert.equal(stripExpressionMarkers(container.innerHTML), '<></>');
}
});

test('malformed "dynamic" tag name', () => {
// `</ ` starts a comment
render(html`<${'A'}></ ${'A'}>`, container);
assert.equal(
stripExpressionMarkers(container.innerHTML),
'<><!-- --></>'
);

// Currently fails:
// render(html`<${'A'}></ ${'A'}>${'B'}`, container);
// assert.equal(stripExpressionMarkers(container.innerHTML), '<><!-- -->B</>');
const template = html`<${'A'}></ ${'A'}>`;
if (DEV_MODE) {
assert.throws(() => {
render(template, container);
});
} else {
render(template, container);
assert.equal(
stripExpressionMarkers(container.innerHTML),
'<><!-- --></>'
);
}
});

test('binding after end tag name', () => {
Expand Down

0 comments on commit 0135331

Please sign in to comment.