Conversation
|
The provided PHP code is a valid solution to escape a string for use in JavaScript. However, there's a more efficient and optimal solution using the json_encode function. The json_encode function will not only escape the string but will also take care of encoding it in a format compatible with JavaScript. Here's an alternative solution: This function simply calls json_encode with the JSON_UNESCAPED_UNICODE flag, which encodes the string in a format compatible with JavaScript, without escaping Unicode characters. This is more efficient than the original solution because it avoids manual string manipulation and uses a built-in PHP function that is designed for this purpose. Here is what I understand would motivate the current solution given:
That said, using json_encode with the JSON_UNESCAPED_UNICODE flag is generally the preferred method because it is more efficient, less error-prone, and easier to maintain.
|
|
I've copied over the code from the CMS. The goal is to remove the duplicate code between framework and CMS and making the filter package 100% compatible with the CMS classes is a prerequisite to convert over the Input package. I don't know if it would be B/C to change this to json_encode and to be on the safe side, I would keep the code as is for now. |
|
I'm closing this in favour of this being rebased to 2.0-dev to hopefully add this to Joomla 4.4 already. |
Summary of Changes
This is part of the process of deprecating the CMS Filter package in favor of the framework package. This adds the stringJSSafe() method to the OutputFilter class.
Testing Instructions
Tests should pass. Codereview.
Documentation Changes Required