-
Notifications
You must be signed in to change notification settings - Fork 435
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Activate
<script>
in Turbo Streams (#660)
Closes #527 When a `<turbo-stream>` element connects to the document, "activate" each `<script>` element by re-creating it as an element owned by the `document` instance. To re-use the existing `Renderer.createdScriptElement`, this commit extracts that logic to the `src/util.ts` module, and renames it to `activateScriptElement`. Then, we replace each `this.createdScriptElement` call site with a call to `activateScriptElement`. Along with that change, this commit changes the signature to expect an [HTMLScriptElement][] instance, Finally, as part of adding test coverage for the new behavior, this commit re-structures existing tests to be more particular about their expectations, and to throw failure messages that provide more clarity. [HTMLScriptElement]: https://developer.mozilla.org/en-US/docs/Web/API/HTMLScriptElement Co-authored-by: Shia Labeouf <felixfearest@gmail.com> Co-authored-by: Shia Labeouf <felixfearest@gmail.com>
- Loading branch information
1 parent
3a18111
commit 97ce0d7
Showing
10 changed files
with
105 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,33 @@ | ||
import { StreamElement } from "../../elements/stream_element" | ||
import { activateScriptElement, createDocumentFragment } from "../../util" | ||
|
||
export class StreamMessage { | ||
static readonly contentType = "text/vnd.turbo-stream.html" | ||
readonly templateElement = document.createElement("template") | ||
readonly fragment: DocumentFragment | ||
|
||
static wrap(message: StreamMessage | string) { | ||
if (typeof message == "string") { | ||
return new this(message) | ||
return new this(createDocumentFragment(message)) | ||
} else { | ||
return message | ||
} | ||
} | ||
|
||
constructor(html: string) { | ||
this.templateElement.innerHTML = html | ||
constructor(fragment: DocumentFragment) { | ||
this.fragment = importStreamElements(fragment) | ||
} | ||
} | ||
|
||
function importStreamElements(fragment: DocumentFragment): DocumentFragment { | ||
for (const element of fragment.querySelectorAll<StreamElement>("turbo-stream")) { | ||
const streamElement = document.importNode(element, true) | ||
|
||
get fragment() { | ||
const fragment = document.createDocumentFragment() | ||
for (const element of this.foreignElements) { | ||
fragment.appendChild(document.importNode(element, true)) | ||
for (const inertScriptElement of streamElement.templateElement.content.querySelectorAll("script")) { | ||
inertScriptElement.replaceWith(activateScriptElement(inertScriptElement)) | ||
} | ||
return fragment | ||
} | ||
|
||
get foreignElements() { | ||
return this.templateChildren.reduce((streamElements, child) => { | ||
if (child.tagName.toLowerCase() == "turbo-stream") { | ||
return [...streamElements, child as StreamElement] | ||
} else { | ||
return streamElements | ||
} | ||
}, [] as StreamElement[]) | ||
element.replaceWith(streamElement) | ||
} | ||
|
||
get templateChildren() { | ||
return Array.from(this.templateElement.content.children) | ||
} | ||
return fragment | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters