Skip to content
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/morphlex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ class Morph {
this.#mapIdSets(from)
}

if (to instanceof NodeList) {
if (isNodeList(to)) {
this.#mapIdArraysForEach(to)
this.#morphOneToMany(from, to)
} else {
Expand Down Expand Up @@ -992,6 +992,10 @@ function isParentNode(node: Node): node is ParentNode {
return !!IS_PARENT_NODE_TYPE[node.nodeType]
}

function isNodeList(value: ChildNode | NodeListOf<ChildNode>): value is NodeListOf<ChildNode> {
return Object.prototype.toString.call(value) === "[object NodeList]"
}

// Find longest increasing subsequence to minimize moves during reordering
// Returns the indices in the sequence that form the LIS
function longestIncreasingSubsequence(sequence: Array<number | undefined>): Array<number> {
Expand Down
24 changes: 24 additions & 0 deletions test/new/cross-realm.browser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { test, expect } from "vitest"
import { morph } from "../../src/morphlex"

test("morphs to a NodeList from another realm", () => {
const iframe = document.createElement("iframe")
document.body.appendChild(iframe)

try {
const iframeDocument = iframe.contentDocument!
const template = iframeDocument.createElement("template")
template.innerHTML = "<span>First</span><span>Second</span>"

const parent = document.createElement("div")
const child = document.createElement("span")
child.textContent = "Original"
parent.appendChild(child)

morph(child, template.content.childNodes)

expect(parent.innerHTML).toBe("<span>First</span><span>Second</span>")
} finally {
iframe.remove()
}
})
Loading