Skip to content

Commit

Permalink
X-html in x-for (alpinejs#2766)
Browse files Browse the repository at this point in the history
* Add failing test

* Move initTree outside of mutateDom when creating new elements as part of x-for

* Use mutateDom and initTree in x-html to ensure it initialises correctly when the mutation observer is not running (i.e. x-for)
  • Loading branch information
SimoTod authored Mar 24, 2022
1 parent 8dc1c69 commit 3fe2086
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
10 changes: 9 additions & 1 deletion packages/alpinejs/src/directives/x-html.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import { directive } from '../directives'
import { initTree } from '../lifecycle'
import { mutateDom } from '../mutation'

directive('html', (el, { expression }, { effect, evaluateLater }) => {
let evaluate = evaluateLater(expression)

effect(() => {
evaluate(value => {
el.innerHTML = value
mutateDom(() => {
el.innerHTML = value

el._x_ignoreSelf = true
initTree(el)
delete el._x_ignoreSelf
})
})
})
})
14 changes: 14 additions & 0 deletions tests/cypress/integration/directives/x-for.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -545,3 +545,17 @@ test('x-for removed dom node does not evaluate child expressions after being rem
get('span').should('not.exist')
}
)

test('renders children using directives injected by x-html correctly',
html`
<div x-data="{foo: 'bar'}">
<template x-for="i in 2">
<p x-html="'<span x-text=&quot;foo&quot;></span>'"></p>
</template>
</div>
`,
({ get }) => {
get('p:nth-of-type(1) span').should(haveText('bar'))
get('p:nth-of-type(2) span').should(haveText('bar'))
}
)

0 comments on commit 3fe2086

Please sign in to comment.