Skip to content

Commit

Permalink
Streams searching for targets inside a template
Browse files Browse the repository at this point in the history
  • Loading branch information
viniciusoyama committed Jun 11, 2024
1 parent c339144 commit 4bf703b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/elements/stream_element.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,14 @@ export class StreamElement extends HTMLElement {
}
}

get targetElementsByQuery() {
const elements = this.ownerDocument?.querySelectorAll(this.targets)

if (elements.length !== 0) {
return Array.prototype.slice.call(elements)
} else {
return []
get targetElementsByQuery() {
let elements = [...this.ownerDocument?.querySelectorAll(this.targets)];

for (const template of this.ownerDocument?.querySelectorAll("template")) {
elements.push(...template.content.querySelectorAll(this.targets))
}

return elements
}
}
7 changes: 7 additions & 0 deletions src/tests/fixtures/stream.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,12 @@
<div id="container">
<input id="container-element">
</div>

<template id="messages_template">
<div class="messages">
<div class="message">Message inside template</div>
</div>
</template>

</body>
</html>
21 changes: 21 additions & 0 deletions src/tests/functional/stream_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,27 @@ test("receiving a stream message with css selector target", async ({ page }) =>
assert.deepEqual(await messages3.allTextContents(), ["Third", "Hello CSS!"])
})


test("receiving a stream message with a targets css selector for an element in a template", async ({ page }) => {
const templateNode = await page.locator("#messages_template")

const fetchMessagesInTemplateNode = (node) => {
const messagesNode = node.content.querySelector(".messages")
return messagesNode.innerText.trim().split(/\s{2,}/g);
}

let messagesInTemplate = await templateNode.evaluate(fetchMessagesInTemplateNode)

assert.deepEqual(messagesInTemplate, ["Message inside template"])

await page.click("#append-targets button")
await nextBeat()

messagesInTemplate = await templateNode.evaluate(fetchMessagesInTemplateNode)

assert.deepEqual(messagesInTemplate, ["Message inside template", "Hello CSS!"])
})

test("receiving a message without a template", async ({ page }) => {
await page.evaluate(() =>
window.Turbo.renderStreamMessage(`
Expand Down

0 comments on commit 4bf703b

Please sign in to comment.