MDL-70721 core: a new mustache helper to clean string after get_string
This commit is contained in:
Vendored
+1
-1
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -557,6 +557,25 @@ define([
|
||||
return '[[_s' + index + ']]';
|
||||
};
|
||||
|
||||
/**
|
||||
* String helper to render {{#cleanstr}}abd component { a : 'fish'}{{/cleanstr}}
|
||||
* into a get_string following by an HTML escape.
|
||||
*
|
||||
* @method cleanStringHelper
|
||||
* @private
|
||||
* @param {object} context The current mustache context.
|
||||
* @param {string} sectionText The text to parse the arguments from.
|
||||
* @param {function} helper Used to render subsections of the text.
|
||||
* @return {string}
|
||||
*/
|
||||
Renderer.prototype.cleanStringHelper = function(context, sectionText, helper) {
|
||||
var str = this.stringHelper(context, sectionText, helper);
|
||||
|
||||
// We're going to use [[_cx]] format for clean strings, where x is a number.
|
||||
// Hence, replacing 's' with 'c' in the placeholder that stringHelper returns.
|
||||
return str.replace('s', 'c');
|
||||
};
|
||||
|
||||
/**
|
||||
* Quote helper used to wrap content in quotes, and escape all quotes present in the content.
|
||||
*
|
||||
@@ -703,6 +722,7 @@ define([
|
||||
this.requiredJS = [];
|
||||
context.uniqid = (uniqInstances++);
|
||||
context.str = this.addHelperFunction(this.stringHelper, context);
|
||||
context.cleanstr = this.addHelperFunction(this.cleanStringHelper, context);
|
||||
context.pix = this.addHelperFunction(this.pixHelper, context);
|
||||
context.js = this.addHelperFunction(this.jsHelper, context);
|
||||
context.quote = this.addHelperFunction(this.quoteHelper, context);
|
||||
@@ -746,13 +766,14 @@ define([
|
||||
* @return {String} The treated content.
|
||||
*/
|
||||
Renderer.prototype.treatStringsInContent = function(content, strings) {
|
||||
var pattern = /\[\[_s\d+\]\]/,
|
||||
var pattern = /\[\[_(s|c)\d+\]\]/,
|
||||
treated,
|
||||
index,
|
||||
strIndex,
|
||||
walker,
|
||||
char,
|
||||
strFinal;
|
||||
strFinal,
|
||||
isClean;
|
||||
|
||||
do {
|
||||
treated = '';
|
||||
@@ -762,8 +783,9 @@ define([
|
||||
// Copy the part prior to the placeholder to the treated string.
|
||||
treated += content.substring(0, index);
|
||||
content = content.substr(index);
|
||||
isClean = content[3] == 'c';
|
||||
strIndex = '';
|
||||
walker = 4; // 4 is the length of '[[_s'.
|
||||
walker = 4; // 4 is the length of either '[[_s' or '[[_c'.
|
||||
|
||||
// Walk the characters to manually extract the index of the string from the placeholder.
|
||||
char = content.substr(walker, 1);
|
||||
@@ -776,11 +798,15 @@ define([
|
||||
// Get the string, add it to the treated result, and remove the placeholder from the content to treat.
|
||||
strFinal = strings[parseInt(strIndex, 10)];
|
||||
if (typeof strFinal === 'undefined') {
|
||||
Log.debug('Could not find string for pattern [[_s' + strIndex + ']].');
|
||||
Log.debug('Could not find string for pattern [[_' + (isClean ? 'c' : 's') + strIndex + ']].');
|
||||
strFinal = '';
|
||||
}
|
||||
if (isClean) {
|
||||
strFinal = mustache.escape(strFinal);
|
||||
}
|
||||
treated += strFinal;
|
||||
content = content.substr(6 + strIndex.length); // 6 is the length of the placeholder without the index: '[[_s]]'.
|
||||
content = content.substr(6 + strIndex.length); // 6 is the length of the placeholder without the index.
|
||||
// That's either '[[_s]]' or '[[_c]]'.
|
||||
|
||||
// Find the next placeholder.
|
||||
index = content.search(pattern);
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Mustache helper to load strings from string_manager and perform HTML escaping on them.
|
||||
*
|
||||
* @package core
|
||||
* @category output
|
||||
* @copyright 2021 Shamim Rezaie <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace core\output;
|
||||
|
||||
use Mustache_LambdaHelper;
|
||||
|
||||
/**
|
||||
* This class will load language strings in a template.
|
||||
*
|
||||
* @copyright 2021 Shamim Rezaie <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since 4.0
|
||||
*/
|
||||
class mustache_clean_string_helper {
|
||||
|
||||
/** @var mustache_string_helper A string helper instance that is being used internally for fetching strings */
|
||||
private $stringhelper;
|
||||
|
||||
/**
|
||||
* Create new instance of mustache clean string helper.
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->stringhelper = new \core\output\mustache_string_helper();
|
||||
}
|
||||
|
||||
/**
|
||||
* Read a lang string from a template and get it from get_string.
|
||||
*
|
||||
* Some examples for calling this from a template are:
|
||||
*
|
||||
* {{#cleanstr}}activity{{/cleanstr}}
|
||||
* {{#cleanstr}}actionchoice, core, {{#str}}delete{{/str}}{{/cleanstr}} (Together with the str helper)
|
||||
* {{#cleanstr}}addinganewto, core, {"what":"This", "to":"That"}{{/cleanstr}} (Complex $a)
|
||||
*
|
||||
* The args are comma separated and only the first is required.
|
||||
* The last is a $a argument for get string. For complex data here, use JSON.
|
||||
*
|
||||
* @param string $text The text to parse for arguments.
|
||||
* @param Mustache_LambdaHelper $helper Used to render nested mustache variables.
|
||||
* @return string
|
||||
*/
|
||||
public function cleanstr($text, Mustache_LambdaHelper $helper) {
|
||||
return s($this->stringhelper->str($text, $helper));
|
||||
}
|
||||
}
|
||||
@@ -105,6 +105,7 @@ class renderer_base {
|
||||
|
||||
$loader = new \core\output\mustache_filesystem_loader();
|
||||
$stringhelper = new \core\output\mustache_string_helper();
|
||||
$cleanstringhelper = new \core\output\mustache_clean_string_helper();
|
||||
$quotehelper = new \core\output\mustache_quote_helper();
|
||||
$jshelper = new \core\output\mustache_javascript_helper($this->page);
|
||||
$pixhelper = new \core\output\mustache_pix_helper($this);
|
||||
@@ -116,6 +117,7 @@ class renderer_base {
|
||||
|
||||
$helpers = array('config' => $safeconfig,
|
||||
'str' => array($stringhelper, 'str'),
|
||||
'cleanstr' => array($cleanstringhelper, 'cleanstr'),
|
||||
'quote' => array($quotehelper, 'quote'),
|
||||
'js' => array($jshelper, 'help'),
|
||||
'pix' => array($pixhelper, 'pix'),
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
<?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/>.
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace core\output;
|
||||
|
||||
/**
|
||||
* Unit tests for the mustache_clean_string_helper class.
|
||||
*
|
||||
* @package core
|
||||
* @category test
|
||||
* @copyright 2021 Shamim Rezaie <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @coversDefaultClass mustache_clean_string_helper
|
||||
*/
|
||||
class mustache_clean_string_helper_test extends \basic_testcase {
|
||||
|
||||
/**
|
||||
* Test the get_lang_menu
|
||||
*
|
||||
* @covers ::cleanstr
|
||||
*/
|
||||
function test_cleanstr() {
|
||||
$engine = new \Mustache_Engine();
|
||||
$context = new \Mustache_Context();
|
||||
$lambdahelper = new \Mustache_LambdaHelper($engine, $context);
|
||||
|
||||
$cleanstringhelper = new mustache_clean_string_helper();
|
||||
|
||||
// Simple string.
|
||||
$this->assertEquals('Log in', $cleanstringhelper->cleanstr('login, core', $lambdahelper));
|
||||
|
||||
// Quotes in string.
|
||||
$this->assertEquals('Today's logs', $cleanstringhelper->cleanstr('todaylogs, core', $lambdahelper));
|
||||
|
||||
// Quotes in string with parameter.
|
||||
$this->assertEquals('After "test"', $cleanstringhelper->cleanstr('movecontentafter, core, test', $lambdahelper));
|
||||
|
||||
// Quotes in parameter.
|
||||
$this->assertEquals('Add a new "'&', $cleanstringhelper->cleanstr('addnew, core, "\'&', $lambdahelper));
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,9 @@
|
||||
This files describes API changes in core libraries and APIs,
|
||||
information provided here is intended especially for developers.
|
||||
|
||||
=== 3.11.5 ===
|
||||
* Added the cleanstr mustache template helper to clean strings after loading them from language packs.
|
||||
|
||||
=== 3.11.4 ===
|
||||
* A new option dontforcesvgdownload has been added to the $options parameter of the send_file() function.
|
||||
Note: This option overrides the forced download of directly accessed SVGs, so should only be used where the calling method is
|
||||
|
||||
Reference in New Issue
Block a user