diff --git a/src/core/streams/stream_actions.js b/src/core/streams/stream_actions.js index a038add0d..abdfe6a3d 100644 --- a/src/core/streams/stream_actions.js +++ b/src/core/streams/stream_actions.js @@ -1,5 +1,24 @@ import { session } from "../" import { morphElements, morphChildren } from "../morphing" +import { attributeToNumber } from "../../util" + +export function versionCheck(streamElement, targetElement) { + const targetVersion = attributeToNumber(targetElement.getAttribute("data-turbo-version")) + const streamVersion = streamElement.version + + const streamVersionPresent = streamVersion != null + const targetVersionPresent = targetVersion != null + + if (!streamVersionPresent && targetVersionPresent) { + return false // Don't allow an unversioned element to replace a versioned element + } else if (streamVersionPresent && !targetVersionPresent) { + return true // Do allow a versioned element to replace an unversioned element + } else if (streamVersionPresent && targetVersionPresent) { + return streamVersion > targetVersion + } else { + return true + } +} export const StreamActions = { after() { @@ -28,6 +47,11 @@ export const StreamActions = { const method = this.getAttribute("method") this.targetElements.forEach((targetElement) => { + if (!versionCheck(this, targetElement)) { + console.debug("Skipping replace action because the version is not greater than the target element's version.") + return + } + if (method === "morph") { morphElements(targetElement, this.templateContent) } else { @@ -40,6 +64,11 @@ export const StreamActions = { const method = this.getAttribute("method") this.targetElements.forEach((targetElement) => { + if (!versionCheck(this, targetElement)) { + console.debug("Skipping replace action because the version is not greater than the target element's version.") + return + } + if (method === "morph") { morphChildren(targetElement, this.templateContent) } else { diff --git a/src/elements/stream_element.js b/src/elements/stream_element.js index 77f447594..72205e4be 100644 --- a/src/elements/stream_element.js +++ b/src/elements/stream_element.js @@ -1,5 +1,5 @@ import { StreamActions } from "../core/streams/stream_actions" -import { nextRepaint } from "../util" +import { attributeToNumber, nextRepaint } from "../util" //