Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow previously diffed elements to be updated #315

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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: 6 additions & 0 deletions packages/diffhtml/lib/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ export default class Transaction {
this.endedCallbacks = new Set();

StateCache.set(mount, this.state);

// When a previous mount is injected into a new location, remove the
// state held for it, this allows previously diffed elements to be updated.
if (StateCache.has(input)) {
StateCache.delete(input);
}
}

/**
Expand Down
18 changes: 18 additions & 0 deletions packages/diffhtml/test/integration/inner.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,24 @@ describe('Integration: innerHTML', function() {
diff.innerHTML(this.fixture, diff.html`<div>${domNode}<p>after</p></div>`);
assert.equal(this.fixture.innerHTML, '<div><div>test</div><p>after</p></div>');
});

it("will diff an element when element's children have been diffed before", function (cb) {
const p = document.createElement("p");
diff.innerHTML(p, "<span>Test</span>");
// this.fixture is <div></div>
diff.innerHTML(this.fixture, p); // <div><p><span>Test</span></p></div>

// diff element p child span
diff.innerHTML(this.fixture.querySelector("span"), "Test 2"); // this works: <div><p><span>Test 2</span></p></div>

// our test case: diff element p when child span has been diffed previously
diff.innerHTML(this.fixture.querySelector("p"), "<span>Test 3</span>"); // this doesn't work - still <div><p><span>Test 2</span></p></div>

setTimeout(() => {
assert.equal(this.fixture.querySelector("span").innerHTML, "Test 3"); // fails because it's still Test 2
cb();
});
});
});

describe('Comments', function() {
Expand Down