Skip to content

Commit

Permalink
Merge pull request alpinejs#667 from danprince/fix-numeric-attributes
Browse files Browse the repository at this point in the history
fixes numeric attributes and adds test
  • Loading branch information
calebporzio authored Jul 31, 2020
2 parents d0b49a3 + 3bdf961 commit 8254133
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,11 @@ function sortDirectives(directives) {
})
}

function parseHtmlAttribute({ name, value }) {
export function parseHtmlAttribute({ name, value }) {
const normalizedName = replaceAtAndColonWithStandardSyntax(name)

const typeMatch = normalizedName.match(xAttrRE)
const valueMatch = normalizedName.match(/:([a-zA-Z\-:]+)/)
const valueMatch = normalizedName.match(/:([a-zA-Z0-9\-:]+)/)
const modifiers = normalizedName.match(/\.[^.\]]+(?=[^\]]*$)/g) || []

return {
Expand Down
14 changes: 14 additions & 0 deletions test/bind.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -495,3 +495,17 @@ test('.camel modifier correctly sets name of attribute', async () => {

expect(document.querySelector('svg').getAttribute('viewBox')).toEqual('0 0 42 42')
})


test('attribute binding names can contain numbers', async () => {
document.body.innerHTML = `
<svg x-data>
<line x1="1" y1="2" :x2="3" x-bind:y2="4" />
</svg>
`;

Alpine.start();

expect(document.querySelector('line').getAttribute('x2')).toEqual('3');
expect(document.querySelector('line').getAttribute('y2')).toEqual('4');
})
13 changes: 12 additions & 1 deletion test/utils.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import { arrayUnique } from '../src/utils'
import { arrayUnique, parseHtmlAttribute } from '../src/utils'

test('utils/arrayUnique', () => {
const arrayMock = [1, 1, 2, 3, 1, 'a', 'b', 'c', 'b']
const expected = arrayUnique(arrayMock)
expect(expected).toEqual([1, 2, 3, 'a', 'b', 'c'])
})

test('utils/parseHtmlAttribute', () => {
const attribute = { name: ':x1', value: 'x' };
const expected = parseHtmlAttribute(attribute);
expect(expected).toEqual({
type: 'bind',
value: 'x1',
modifiers: [],
expression: 'x'
});
})

0 comments on commit 8254133

Please sign in to comment.