Repository navigation
Conversation
Alpine's CSP build can't see window globals, so the driver's `Statamic.$conditions.showField(...)` expressions never evaluated and conditional fields were always shown. The frontend helpers now register a `$statamic` magic and the driver uses it. `window.Statamic` is unchanged.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request makes the
alpineforms JS driver work with Alpine's CSP build, including the copy bundled in Livewire'slivewire.csp.js.Conditional fields were never hidden on sites using the CSP build. The
x-dataandx-modeloutput worked, but everyshow_fieldexpression evaluated toundefined.This was happening because the driver renders
Statamic.$conditions.showField(...), and the CSP build's expression evaluator can only see Alpine scope and magics, notwindowglobals. SoStatamicis undefined. It also rejects any value that is awindowglobal, so a magic that returnswindow.Statamicfails too (Accessing global variables is prohibited in the CSP build) whenhelpers.jsloads before Alpine.This PR fixes it by having
helpers.jsregister a$statamicmagic, and having the driver render$statamic.$conditions.showField(...)instead. The magic returns a copy ofwindow.Statamicthat shares the same$conditionsand$passkeysinstances, which the CSP evaluator accepts. It registers immediately ifwindow.Alpineexists, otherwise onalpine:init, sohelpers.jscan load before or after Alpine.window.Statamicis unchanged, so any custom templates that useStatamic.$conditionsstill work on the standard build.Testing
FormCreateAlpineTestassertions for the new expression.AlpineMagic.test.js, which covers both registration paths, checks the magic doesn't return the global itself, and toggles a field with real Alpine.script-src 'self'(nounsafe-eval):livewire.csp.js, withhelpers.jsloaded before and after it: the conditional field hides and shows correctly. On6.xit never hides.Notes
helpers.jsstill has to load before Alpine starts, same as now. If Alpine starts first, the fields break on6.xand on this branch.helpers.js.statamic:installrepublishes it with--force, which runs oncomposer updatefor standard sites. A site that bundles or pins its own copy would need to update it.@alpinejs/cspisn't a dependency. Happy to add it as a dev dependency if you want that covered.