Skip to content

Commit

Permalink
Remove negated function calling from CSP build
Browse files Browse the repository at this point in the history
To keep feature parity with the Alpine standard build, we remove
function evaluation of negated function calls (`x-show=!foo`) where foo
is a function.

Also adjust the test so it checks for proper trimming and modify the
trimming of the expression to be evaluated to allow for whitespace
between the negation operatior and the expression: `x-show="! foo`.
  • Loading branch information
ClemensA4t1qbit committed Nov 25, 2024
1 parent 33522ab commit c289708
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 28 deletions.
8 changes: 3 additions & 5 deletions packages/csp/src/evaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function generateEvaluator(el, expression, dataStack) {
let completeScope = mergeProxies([scope, ...dataStack])

let isNegated = expression.trim().charAt(0) === '!'
let expressionToEvaluate = isNegated ? expression.trim().slice(1) : expression.trim()
let expressionToEvaluate = isNegated ? expression.trim().slice(1).trim() : expression.trim()

let evaluatedExpression = expressionToEvaluate.split('.').reduce(
(currentScope, currentExpression) => {
Expand All @@ -42,11 +42,9 @@ function generateEvaluator(el, expression, dataStack) {
completeScope,
);

function handleResult(result) {
receiver(isNegated ? !result : result)
}
evaluatedExpression = isNegated ? !evaluatedExpression : evaluatedExpression

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

Expand Down
24 changes: 1 addition & 23 deletions tests/cypress/integration/plugins/csp-compatibility.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ test.csp('Supports nested properties',
test.csp('Supports simple negation',
[html`
<div x-data="test">
<span x-text="!foo"></span>
<span x-text=" ! foo"></span>
<button @click="toggle">Toggle Foo</button>
</div>
Expand All @@ -63,26 +63,4 @@ test.csp('Supports simple negation',
get('button').click()
get('span').should(haveText('false'))
}
)

test.csp("Supports negation of function calls",
[html`
<div x-data="test">
<span x-text="!foo"></span>
<button @click="toggle">Toggle Foo</button>
</div>
`,
`
Alpine.data('test', () => ({
_foo: false,
foo() { return this._foo },
toggle() { this._foo = !this._foo },
}))
`],
({ get }) => {
get('span').should(haveText('true'))
get('button').click()
get('span').should(haveText('false'))
}
)

0 comments on commit c289708

Please sign in to comment.