Skip to content

Commit

Permalink
Bug - Add support for x-data on html tag (alpinejs#1786)
Browse files Browse the repository at this point in the history
* Add failing test

* Allow x-data on the html element

* Revert to failed scenario with different test

* Reapply fix
  • Loading branch information
SimoTod authored Jul 16, 2021
1 parent de04a2e commit 63d82c2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
5 changes: 3 additions & 2 deletions packages/alpinejs/src/lifecycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ export function start() {
directives(el, attrs).forEach(handle => handle())
})

let outNestedComponents = el => ! closestRoot(el.parentNode || closestRoot(el))

let outNestedComponents = el => ! closestRoot(el.parentElement)
Array.from(document.querySelectorAll(allSelectors()))
.filter(outNestedComponents)
.forEach(el => {
Expand All @@ -46,6 +45,8 @@ export function addRootSelector(selectorCallback) { rootSelectorCallbacks.push(s
export function addInitSelector(selectorCallback) { initSelectorCallbacks.push(selectorCallback) }

export function closestRoot(el) {
if (!el) return

if (rootSelectors().some(selector => el.matches(selector))) return el

if (! el.parentElement) return
Expand Down
14 changes: 14 additions & 0 deletions tests/cypress/integration/directives/x-data.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,17 @@ test('functions in x-data have access to proper this context',
get('span').should(haveText('baz'))
}
)

test('x-data works on the html tag',
[html`
<div>
<span x-text="'foo'"></span>
</div>
`,
`
document.querySelector('html').setAttribute('x-data', '')
`],
({ get }) => {
get('span').should(haveText('foo'))
}
)

0 comments on commit 63d82c2

Please sign in to comment.