From de0927e5ed8331a74499107a0c31a09fa39f95eb Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Fri, 8 Jul 2022 09:22:56 -0400 Subject: [PATCH] Fix bug with attach-template and vars (#144) * Fix bug with attach-template and vars * Adds a changeset --- .changeset/old-balloons-divide.md | 5 +++++ src/render.js | 1 + test/test-attach.js | 18 ++++++++++++++++++ 3 files changed, 24 insertions(+) create mode 100644 .changeset/old-balloons-divide.md diff --git a/.changeset/old-balloons-divide.md b/.changeset/old-balloons-divide.md new file mode 100644 index 0000000..ff871fd --- /dev/null +++ b/.changeset/old-balloons-divide.md @@ -0,0 +1,5 @@ +--- +"corset": patch +--- + +Fix bug with attach-template and vars diff --git a/src/render.js b/src/render.js index 2d08f7e..cf1762e 100644 --- a/src/render.js +++ b/src/render.js @@ -124,6 +124,7 @@ function render(element, bindings, root, changeset) { if(binding.dirty(changeset)) { /** @type {HTMLTemplateElement} */ let result = binding.update(changeset); + if(Array.isArray(result)) result = result[0]; let doc = element.ownerDocument || document; let frag = doc.importNode(result.content, true); element.replaceChildren(frag); diff --git a/test/test-attach.js b/test/test-attach.js index 9497107..a4bdf13 100644 --- a/test/test-attach.js +++ b/test/test-attach.js @@ -45,4 +45,22 @@ QUnit.test('Restores the original value when there is no patch', assert => { assert.equal(app.firstChild.localName, 'span'); template(false).update(root); assert.equal(app.firstChild.localName, 'strong'); +}); + +QUnit.test('Can use a variable from another selector', assert => { + let root = document.createElement('main'); + root.innerHTML = `
`; + let template = document.createElement('template'); + template.innerHTML = `
works
`; + let bindings = sheet` + #app { + --template: ${template}; + class-toggle: attached true; + } + .attached { + attach-template: var(--template); + } + `; + bindings.update(root); + assert.equal(root.firstElementChild.firstElementChild.textContent, 'works'); }); \ No newline at end of file