mirror of
https://github.com/moodle/moodle.git
synced 2025-08-06 17:36:38 +02:00

This is required for when helpers include json-encoded variables as arguments. As an example, imagine a template with content: {{# str }} somekey, someidentifier, { "fullname": "{{ fullname }}" } {{/ str }} If the fullname variable were to include the double-quote character (e.g. John "Trevor" Doe) because of the way in which mustache renders content, it would become: {{# str }} somekey, someidentifier, { "fullname": "John "Trevor" Doe" } {{/ str }} This results in an invalid JSON structure. To work around this issue, the quote characters in the passed variable must be escaped: {{# str }} somekey, someidentifier, { "fullname": "John \"Trevor\" Doe" } {{/ str }} Unfortunately, Mustache provides no way of doing so natively. With this function, we can quote the text as appropriate: {{# str }} somekey, someidentifier, { "fullname": {{# quote }}{{ fullname }}{{/ quote }} } {{/ str }} This also handles the case where the quoted content includes the Mustache delimeter ({{ or }}). For example: fullname = 'John "}}Trevor{{" Doe' Ordinarily this would be rendered as: {{# str }} somekey, someidentifier, { "fullname": "John "}}Trevor{{" Doe" } {{/ str }} This rendering is both a JSON error, and also a mustache syntax error because of the mustache delimeters. The quote helper also escapes these by wrapping them in change delimeter tags: {{# str }} somekey, someidentifier, { "fullname": "John "{{=<% %>=}}}}<%={{ }}=%>Trevor{{=<% %>=}}{{{{=<% %>=}}" Doe" } {{/ str }}
55 lines
1.9 KiB
PHP
55 lines
1.9 KiB
PHP
<?php
|
|
// This file is part of Moodle - http://moodle.org/
|
|
//
|
|
// Moodle is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// Moodle is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
/**
|
|
* Wrap content in quotes, and escape all quotes used.
|
|
*
|
|
* @package core
|
|
* @category output
|
|
* @copyright 2016 Andrew Nicols <andrew@nicols.co.uk>
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
*/
|
|
|
|
namespace core\output;
|
|
|
|
/**
|
|
* Wrap content in quotes, and escape all quotes used.
|
|
*
|
|
* @copyright 2016 Andrew Nicols <andrew@nicols.co.uk>
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
*/
|
|
class mustache_quote_helper {
|
|
|
|
/**
|
|
* Wrap content in quotes, and escape all quotes used.
|
|
*
|
|
* Note: This helper is only compatible with the standard {{ }} delimeters.
|
|
*
|
|
* @param string $text The text to parse for arguments.
|
|
* @param Mustache_LambdaHelper $helper Used to render nested mustache variables.
|
|
* @return string
|
|
*/
|
|
public function quote($text, \Mustache_LambdaHelper $helper) {
|
|
// Split the text into an array of variables.
|
|
$content = trim($text);
|
|
$content = $helper->render($content);
|
|
|
|
// Escape the {{ and the ".
|
|
$content = str_replace('"', '\\"', $content);
|
|
$content = preg_replace('([{}]{2,3})', '{{=<% %>=}}${0}<%={{ }}=%>', $content);
|
|
return '"' . $content . '"';
|
|
}
|
|
}
|