Skip to content

Commit

Permalink
Fix bug with attach-template and vars (#144)
Browse files Browse the repository at this point in the history
* Fix bug with attach-template and vars

* Adds a changeset
  • Loading branch information
matthewp authored Jul 8, 2022
1 parent 3055dbe commit de0927e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/old-balloons-divide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"corset": patch
---

Fix bug with attach-template and vars
1 change: 1 addition & 0 deletions src/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
18 changes: 18 additions & 0 deletions test/test-attach.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `<div id="app"></div>`;
let template = document.createElement('template');
template.innerHTML = `<div>works</div>`;
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');
});

0 comments on commit de0927e

Please sign in to comment.