🐛 Fixed escaping search terms that contain special characters #18151
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.
yarn test:all
andyarn lint
)Before, Sodo Search was not escaping search input before using the search terms in a regular expression, so using special characters could result in an invalid regular expression which would crash JavaScript.
As regular expressions date back to Perl, so does a standard solution for this, which called
quotemeta
in Perl. It doesn't exist 1:1 in JavaScript, but StackOverflow had the answer:https://stackoverflow.com/questions/6318710/javascript-equivalent-of-perls-q-e-or-quotemeta
... where it appears I answered this question in 2014. Ha.
So a line of code is added to escape the special characters in the regex for passing them through. This is the same code that the
quotemeta
module on NPM would use.Fixes: #18133
QA Log
Unfortunately, my local Ghost development environment is not set up perfectly to test this. I tried using
ghost dev --search
and that did link a local blog to a local version of Sodo search, but a new JavaScript error appears which I think is unrelated:So I'd love if someone with a working dev environment could quickly repro the issue with a "+C" and then apply my match and test again.
Thanks!