Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make StreamElement.templateElement more flexible #665

Merged
merged 1 commit into from
Aug 4, 2022
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
6 changes: 5 additions & 1 deletion src/elements/stream_element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,11 @@ export class StreamElement extends HTMLElement {
* Gets the main `<template>` used for rendering
*/
get templateElement() {
if (this.firstElementChild instanceof HTMLTemplateElement) {
if (this.firstElementChild === null) {
const template = this.ownerDocument.createElement("template")
this.appendChild(template)
return template
} else if (this.firstElementChild instanceof HTMLTemplateElement) {
return this.firstElementChild
}
this.raise("first child element must be a <template> element")
Expand Down
10 changes: 10 additions & 0 deletions src/tests/functional/stream_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ test("test receiving a stream message with css selector target", async ({ page }
assert.deepEqual(await messages3.allTextContents(), ["Third", "Hello CSS!"])
})

test("test receiving a message without a template", async ({ page }) => {
await page.evaluate(() =>
window.Turbo.renderStreamMessage(`
<turbo-stream action="remove" target="messages"></turbo-stream>
`)
)

assert.equal(await await page.locator("#messages").count(), 0, "removes target element")
})

test("test receiving a message with a <script> element", async ({ page }) => {
const messages = await page.locator("#messages .message")

Expand Down