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

Improve x-attributes regexp #234

Merged
merged 5 commits into from
Mar 7, 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
4 changes: 2 additions & 2 deletions dist/alpine.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,15 @@

return new Function(['dataContext', ...Object.keys(additionalHelperVariables)], `with(dataContext) { ${expression} }`)(dataContext, ...Object.values(additionalHelperVariables));
}
const xAttrRE = /^x-(on|bind|data|text|html|model|if|for|show|cloak|transition|ref)\b/;
function isXAttr(attr) {
const name = replaceAtAndColonWithStandardSyntax(attr.name);
const xAttrRE = /x-(on|bind|data|text|html|model|if|for|show|cloak|transition|ref)/;
return xAttrRE.test(name);
}
function getXAttrs(el, type) {
return Array.from(el.attributes).filter(isXAttr).map(attr => {
const name = replaceAtAndColonWithStandardSyntax(attr.name);
const typeMatch = name.match(/x-(on|bind|data|text|html|model|if|for|show|cloak|transition|ref)/);
const typeMatch = name.match(xAttrRE);
const valueMatch = name.match(/:([a-zA-Z\-:]+)/);
const modifiers = name.match(/\.[^.\]]+(?=[^\]]*$)/g) || [];
return {
Expand Down
7 changes: 3 additions & 4 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,10 @@ export function saferEvalNoReturn(expression, dataContext, additionalHelperVaria
)
}

const xAttrRE = /^x-(on|bind|data|text|html|model|if|for|show|cloak|transition|ref)\b/

export function isXAttr(attr) {
const name = replaceAtAndColonWithStandardSyntax(attr.name)

const xAttrRE = /x-(on|bind|data|text|html|model|if|for|show|cloak|transition|ref)/

return xAttrRE.test(name)
}

Expand All @@ -95,7 +94,7 @@ export function getXAttrs(el, type) {
.map(attr => {
const name = replaceAtAndColonWithStandardSyntax(attr.name)

const typeMatch = name.match(/x-(on|bind|data|text|html|model|if|for|show|cloak|transition|ref)/)
const typeMatch = name.match(xAttrRE)
const valueMatch = name.match(/:([a-zA-Z\-:]+)/)
const modifiers = name.match(/\.[^.\]]+(?=[^\]]*$)/g) || []

Expand Down
18 changes: 17 additions & 1 deletion test/constructor.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ test('auto-detect new components at the top level', async () => {
await wait(() => { expect(document.querySelector('span').innerText).toEqual('bar') })
})

test('auto-detect newsted new components at the top level', async () => {
test('auto-detect nested new components at the top level', async () => {
var runObservers = []

global.MutationObserver = class {
Expand Down Expand Up @@ -279,3 +279,19 @@ test('can clone an existing component to a new element', async () => {

expect(document.querySelector('span').innerText).toEqual('bar')
})

test('x-attributes are matched exactly', async () => {
document.body.innerHTML = `
<div x-data="{ showElement: false }">
<div id="el1" x-show="showElement" />
<div id="el2" xxx-show="showElement" />
<div id="el3" x-showabc="showElement" />
</div>
`

Alpine.start()

expect(document.getElementById('el1').style.display).toEqual('none')
expect(document.getElementById('el2').style.display).not.toEqual('none')
await wait(() => { expect(document.getElementById('el3').style.display).not.toEqual('none') })
})
6 changes: 3 additions & 3 deletions test/on.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ global.MutationObserver = class {
observe() {}
}

test('data modified in event listener updates effected attribute bindings', async () => {
test('data modified in event listener updates affected attribute bindings', async () => {
document.body.innerHTML = `
<div x-data="{ foo: 'bar' }">
<button x-on:click="foo = 'baz'"></button>
Expand All @@ -24,7 +24,7 @@ test('data modified in event listener updates effected attribute bindings', asyn
await wait(() => { expect(document.querySelector('span').getAttribute('foo')).toEqual('baz') })
})

test('nested data modified in event listener updates effected attribute bindings', async () => {
test('nested data modified in event listener updates affected attribute bindings', async () => {
document.body.innerHTML = `
<div x-data="{ nested: { foo: 'bar' } }">
<button x-on:click="nested.foo = 'baz'"></button>
Expand Down Expand Up @@ -161,7 +161,7 @@ test('.once modifier', async () => {
expect(document.querySelector('span').getAttribute('foo')).toEqual('1')
})

test('.once modifier doest remove listener if false is returned', async () => {
test('.once modifier does not remove listener if false is returned', async () => {
document.body.innerHTML = `
<div x-data="{ count: 0 }">
<button x-on:click.once="return ++count === 2"></button>
Expand Down