Skip to content

Commit

Permalink
🐛 Fixes reinitializing moved elements (#3995)
Browse files Browse the repository at this point in the history
  • Loading branch information
ekwoka authored Jan 24, 2024
1 parent 0bf054e commit e394287
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/alpinejs/src/mutation.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ function onMutate(mutations) {
for (let node of addedNodes) {
// If the node was eventually removed as part of one of his
// parent mutations, skip it
if (removedNodes.has(node)) continue

This comment has been minimized.

Copy link
@Matsa59

Matsa59 Jan 26, 2024

Contributor

@ekwoka I think we should check if the node has been already init (using _x_isInit) instead of checking the it's inside the removedNodes list

This comment has been minimized.

Copy link
@ekwoka

ekwoka Jan 28, 2024

Author Contributor

Probably. My focus was just on getting it back to a known good first, as troubleshooting the new behaviors would delay a fix.

if (! node.isConnected) continue

delete node._x_ignoreSelf
Expand Down
24 changes: 24 additions & 0 deletions tests/cypress/integration/mutation.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,27 @@ test('no side effects when directives are added to an element that is removed af
get('span').should(haveText('0'))
}
)
test(
"previously initialized elements are not reinitialized on being moved",
html`
<script>
let count = 0;
document.addEventListener('alpine:init', () => {
Alpine.directive('test', el => {
if (count++ > 3) return;
el.textContent = count;
let wrapper = document.createElement('div');
wrapper.setAttribute('class', 'bg-blue-300 p-8');
el.parentNode.replaceChild(wrapper, el);
wrapper.appendChild(el);
});
});
</script>
<div x-data>
<div class="bg-red-300 w-32 h-32" x-test></div>
</div>
`,
({ get }) => {
get("[x-test]").should(haveText("1"));
}
);

0 comments on commit e394287

Please sign in to comment.