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

Fix "Cannot read property 'split' of undefined" error on empty class binding #847

Merged
merged 1 commit into from
Nov 2, 2020
Merged
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
2 changes: 1 addition & 1 deletion dist/alpine-ie11.js
Original file line number Diff line number Diff line change
Expand Up @@ -6528,7 +6528,7 @@
} else {
var _originalClasses = el.__x_original_classes || [];

var newClasses = convertClassStringToArray(value);
var newClasses = value ? convertClassStringToArray(value) : [];
el.setAttribute('class', arrayUnique(_originalClasses.concat(newClasses)).join(' '));
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion dist/alpine.js
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@
});
} else {
const originalClasses = el.__x_original_classes || [];
const newClasses = convertClassStringToArray(value);
const newClasses = value ? convertClassStringToArray(value) : [];
el.setAttribute('class', arrayUnique(originalClasses.concat(newClasses)).join(' '));
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/directives/bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export function handleAttributeBindingDirective(component, el, attrName, express
})
} else {
const originalClasses = el.__x_original_classes || []
const newClasses = convertClassStringToArray(value)
const newClasses = value ? convertClassStringToArray(value) : []
el.setAttribute('class', arrayUnique(originalClasses.concat(newClasses)).join(' '))
}
} else {
Expand Down
18 changes: 18 additions & 0 deletions test/bind.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,24 @@ test('extra whitespace in class binding string syntax is ignored', async () => {
expect(document.querySelector('span').classList.contains('bar')).toBeTruthy()
})

test('undefined class binding resolves to empty string', async () => {
jest.spyOn(window, 'setTimeout').mockImplementation((callback,time) => {
callback()
});

document.body.innerHTML = `
<div x-data="{ errorClass: (hasError) => { if (hasError) { return 'red' } } }">
<span id="error" x-bind:class="errorClass(true)">should be red</span>
<span id="empty" x-bind:class="errorClass(false)">should be empty</span>
</div>
`

await expect(Alpine.start()).resolves.toBeUndefined()

expect(document.querySelector('#error').classList.value).toEqual('red')
expect(document.querySelector('#empty').classList.value).toEqual('')
})

test('.camel modifier correctly sets name of attribute', async () => {
document.body.innerHTML = `
<div x-data>
Expand Down