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

[CSP] Add support for nested properties to CSP build #4238

Merged
merged 3 commits into from
May 28, 2024
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
17 changes: 12 additions & 5 deletions packages/csp/src/evaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,18 @@ function generateEvaluator(el, expression, dataStack) {
return (receiver = () => {}, { scope = {}, params = [] } = {}) => {
let completeScope = mergeProxies([scope, ...dataStack])

if (completeScope[expression] === undefined) {
throwExpressionError(el, expression)
}

runIfTypeOfFunction(receiver, completeScope[expression], completeScope, params)
let evaluatedExpression = expression.split('.').reduce(
(currentScope, currentExpression) => {
if (currentScope[currentExpression] === undefined) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (currentScope[currentExpression] === undefined) {
if (!currentExpression in currentScope) {

should help in scenarios where the thing is expected to be undefined.

But also, this will now exhibit a different behavior than non-csp in regards to this, where it would only error when trying to access a key on the thing that is undefined, but would be happy to return undefined.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I kept the same check as the original version. Happy to change it if Caleb is cool with that.

throwExpressionError(el, expression)
}

return currentScope[currentExpression]
},
completeScope,
);

runIfTypeOfFunction(receiver, evaluatedExpression, completeScope, params)
}
}

Expand Down
23 changes: 23 additions & 0 deletions packages/docs/src/en/advanced/csp.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,26 @@ Alpine.data('counter', () => ({
},
}))
```

The CSP build supports accessing nested properties (property accessors) using the dot notation.

```alpine
<!-- This works too -->
<div x-data="counter">
<button @click="foo.increment">Increment</button>

<span x-text="foo.count"></span>
</div>
```

```js
Alpine.data('counter', () => ({
foo: {
count: 1,

increment() {
this.count++
},
},
}))
```
23 changes: 23 additions & 0 deletions tests/cypress/integration/plugins/csp-compatibility.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,26 @@ test.csp('Can use components and basic expressions with CSP-compatible build',
get('span').should(haveText('baz'))
}
)

test.csp('Supports nested properties',
[html`
<div x-data="test">
<span x-text="foo.bar"></span>

<button @click="foo.change">Change Foo</button>
</div>
`,
`
Alpine.data('test', () => ({
foo: {
bar: 'baz',
change() { this.foo.bar = 'qux' },
}
}))
`],
({ get }) => {
get('span').should(haveText('baz'))
get('button').click()
get('span').should(haveText('qux'))
}
)
Loading