Skip to content

Commit b6394e6

Browse files
authored
Fix regression on x-ignore when removed (#4458)
* Add failing test * Fix x-ignore regression
1 parent 2c2bdb6 commit b6394e6

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

packages/alpinejs/src/lifecycle.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,17 @@ export function initTree(el, walker = walk, intercept = () => {}) {
9393
// If the element has a marker, it's already been initialized...
9494
if (el._x_marker) return
9595

96-
// Add a marker to the element so we can tell if it's been initialized...
97-
// This is important so that we can prevent double-initialization of
98-
// elements that are moved around on the page.
99-
el._x_marker = markerDispenser++
100-
10196
intercept(el, skip)
10297

10398
initInterceptors.forEach(i => i(el, skip))
10499

105100
directives(el, el.attributes).forEach(handle => handle())
106101

102+
// Add a marker to the element so we can tell if it's been initialized...
103+
// This is important so that we can prevent double-initialization of
104+
// elements that are moved around on the page.
105+
if (!el._x_ignore) el._x_marker = markerDispenser++
106+
107107
el._x_ignore && skip()
108108
})
109109
})

tests/cypress/integration/directives/x-ignore.spec.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { haveText, html, notHaveClasses, notHaveText, test } from '../../utils'
1+
import { haveClasses, haveText, html, notHaveClasses, notHaveText, test } from '../../utils'
22

33
test('x-ignore',
44
html`
@@ -8,7 +8,9 @@ test('x-ignore',
88
</div>
99
</div>
1010
`,
11-
({ get }) => get('span').should(notHaveText('bar'))
11+
({ get }) => {
12+
get('span').should(notHaveText('bar'))
13+
}
1214
)
1315

1416
test('x-ignore.self',
@@ -24,3 +26,21 @@ test('x-ignore.self',
2426
get('h1').should(notHaveClasses(['bar']))
2527
}
2628
)
29+
30+
test('can lazyload a component',
31+
html`
32+
<div x-data="{ lazyLoad() {$el.querySelector('#lazy').removeAttribute('x-ignore'); Alpine.nextTick(() => Alpine.initTree($el.querySelector('#lazy')))} }">
33+
<button @click="lazyLoad">Load</button>
34+
<div x-data="{ foo: 'bar' }" id="lazy" x-ignore :class="foo">
35+
<span x-text="foo"></span>
36+
</div>
37+
</div>
38+
`,
39+
({ get }) => {
40+
get('span').should(notHaveText('bar'))
41+
get('div#lazy').should(notHaveClasses(['bar']))
42+
get('button').click()
43+
get('span').should(haveText('bar'))
44+
get('div#lazy').should(haveClasses(['bar']))
45+
}
46+
)

0 commit comments

Comments
 (0)