diff --git a/src/plugins/template/index.js b/src/plugins/template/index.js index cd2728d..3ced3a0 100644 --- a/src/plugins/template/index.js +++ b/src/plugins/template/index.js @@ -9,7 +9,7 @@ function plugin(alpine) { const tpl = document.getElementById(expression); - if (!is_template(tpl)) { + if (!tpl || !is_template(tpl)) { warn("x-template directive can only reference the template tag"); return; } diff --git a/tests/playwright/x-template.spec.js b/tests/playwright/x-template.spec.js index 89a5e6a..4dd3087 100644 --- a/tests/playwright/x-template.spec.js +++ b/tests/playwright/x-template.spec.js @@ -14,3 +14,54 @@ test("x-template", async ({ page }) => { await expect(page.locator("span")).toHaveText("Foo"); }); + +test("x-template does not throw when element id does not exist", async ({ page }) => { + const errors = []; + page.on("console", msg => { + if (msg.type() === "warning") { + errors.push(msg.text()); + } + }); + + await set_html(page, ` +