Fix passing state containing regex with backslashes down to the webview #132
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.
Steps to Reproduce
\((\d+)\)
), replace something successfully.Expected Results
You see the same regex and all other settings you've just used on the previous step.
Actual Results
Explanation
We're currently passing
state
to the webview by converting it to a JS expression string to be evaluated.This lead to special characters (e.g. backslashes) not being escaped properly in regular expressions (they need to be escaped twice for this to work) which, in turn, prevents the web app from parsing this JSON string back into an object.
Solution
The solution is to pass the same stringified state but encoded it as base64 so we don't have to worry about escaping special characters and
JSON.parse()
throwing unhandled exceptions.Note
I'm attaching a pre-built version of the plugin with this fix applied so anyone can try it and see if it works for them 👇
Find-and-replace.sketchplugin-fix-for-regex.zip
Note
An alternative workaround is to reset the plugin settings, although it won't prevent the issue from re-appearing again the next time you use regular expressions with backslashes to replace something.