Skip to content

Conversation

vseager
Copy link
Contributor

@vseager vseager commented Dec 20, 2024

Fixes #87

@wubinworks
Copy link

@vseager
This fix is not enough, there are 2 problems:

  • If you use addslashes, Mickey's Store will become Mickey\'s Store.
    I suggest add this method to Block.
/**
     * Escape JSON string
     *
     * @param string $str
     * @return string
     */
    public function escapeJsonString(string $str): string
    {
        return mb_substr(json_encode(
            $str,
            JSON_UNESCAPED_SLASHES | JSON_PRESERVE_ZERO_FRACTION
        ), 1, -1);
    }

And then

"name": "<?= $block->escapeJsonString($block->escapeHtml($variable)) ?>"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Invalid JSON if string contains " or \ in multiple phtml files

2 participants