Merge branch 'MDL-69553-m310' of https://github.com/NeillM/moodle into MOODLE_310_STABLE
This commit is contained in:
@@ -319,17 +319,9 @@ class mustache_template_source_loader {
|
||||
break;
|
||||
case Mustache_Tokenizer::T_SECTION:
|
||||
if ($name == 'str') {
|
||||
// The token that containts the string identifiers (key and component) should
|
||||
// immediately follow the #str token.
|
||||
$identifiertoken = isset($tokens[$index + 1]) ? $tokens[$index + 1] : null;
|
||||
list($id, $component) = $this->get_string_identifiers($tokens, $index);
|
||||
|
||||
if ($identifiertoken) {
|
||||
// The string identifier is the key and component comma separated.
|
||||
$identifierstring = $identifiertoken['value'];
|
||||
$parts = explode(',', $identifierstring);
|
||||
$id = $parts[0];
|
||||
// Default to 'core' for the component, if not specified.
|
||||
$component = isset($parts[1]) ? $parts[1] : 'core';
|
||||
if ($id) {
|
||||
$strings = $addtodependencies($strings, $component, $id);
|
||||
}
|
||||
}
|
||||
@@ -343,4 +335,50 @@ class mustache_template_source_loader {
|
||||
'strings' => $strings
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the identifier and component of the string.
|
||||
*
|
||||
* The string could be defined on one, or multiple lines.
|
||||
*
|
||||
* @param array $tokens The templates token.
|
||||
* @param int $start The index of the start of the string token.
|
||||
* @return array A list of the string identifier and component.
|
||||
*/
|
||||
protected function get_string_identifiers(array $tokens, int $start): array {
|
||||
$current = $start + 1;
|
||||
$parts = [];
|
||||
|
||||
// Get the contents of the string tag.
|
||||
while ($tokens[$current]['type'] !== Mustache_Tokenizer::T_END_SECTION) {
|
||||
if (!isset($tokens[$current]['value']) || empty(trim($tokens[$current]['value']))) {
|
||||
// An empty line, so we should ignore it.
|
||||
$current++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// We need to remove any spaces before and after the string.
|
||||
$nospaces = trim($tokens[$current]['value']);
|
||||
|
||||
// We need to remove any trailing commas so that the explode will not add an
|
||||
// empty entry where two paramters are on multiple lines.
|
||||
$clean = rtrim($nospaces, ',');
|
||||
|
||||
// We separate the parts of a string with commas.
|
||||
$subparts = explode(',', $clean);
|
||||
|
||||
// Store the parts.
|
||||
$parts = array_merge($parts, $subparts);
|
||||
|
||||
$current++;
|
||||
}
|
||||
|
||||
// The first text should be the first part of a str tag.
|
||||
$id = isset($parts[0]) ? trim($parts[0]) : null;
|
||||
|
||||
// Default to 'core' for the component, if not specified.
|
||||
$component = isset($parts[1]) ? trim($parts[1]) : 'core';
|
||||
|
||||
return [$id, $component];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -357,12 +357,43 @@ TBC;
|
||||
$bar = '{{! a comment }}{{> core/baz }}';
|
||||
$baz = '{{! a comment }}{{#str}} hide, core {{/str}}';
|
||||
$bop = '{{! a comment }}hello';
|
||||
$multiline1 = <<<TEMPLATE
|
||||
{{! a comment }}{{#str}} authorreplyingprivatelytoauthor,
|
||||
mod_forum {{/str}}
|
||||
TEMPLATE;
|
||||
$multiline2 = <<<TEMPLATE
|
||||
{{! a comment }}{{#str}}
|
||||
authorreplyingprivatelytoauthor,
|
||||
mod_forum {{/str}}
|
||||
TEMPLATE;
|
||||
$multiline3 = <<<TEMPLATE
|
||||
{{! a comment }}{{#str}}
|
||||
authorreplyingprivatelytoauthor,
|
||||
mod_forum
|
||||
{{/str}}
|
||||
TEMPLATE;
|
||||
$multiline4 = <<<TEMPLATE
|
||||
{{! a comment }}{{#str}}
|
||||
authorreplyingprivatelytoauthor, mod_forum
|
||||
{{/str}}
|
||||
TEMPLATE;
|
||||
$multiline5 = <<<TEMPLATE
|
||||
{{! a comment }}{{#str}}
|
||||
hide
|
||||
{{/str}}
|
||||
TEMPLATE;
|
||||
|
||||
$cache = [
|
||||
'core' => [
|
||||
'foo' => $foo,
|
||||
'bar' => $bar,
|
||||
'baz' => $baz,
|
||||
'bop' => $bop
|
||||
'bop' => $bop,
|
||||
'multiline1' => $multiline1,
|
||||
'multiline2' => $multiline2,
|
||||
'multiline3' => $multiline3,
|
||||
'multiline4' => $multiline4,
|
||||
'multiline5' => $multiline5,
|
||||
]
|
||||
];
|
||||
$loader = $this->build_loader_from_static_cache($cache);
|
||||
@@ -409,6 +440,56 @@ TBC;
|
||||
]
|
||||
]
|
||||
],
|
||||
'string: component on new line' => [
|
||||
'loader' => $loader,
|
||||
'source' => $multiline1,
|
||||
'expected' => [
|
||||
'templates' => [],
|
||||
'strings' => [
|
||||
'mod_forum' => ['authorreplyingprivatelytoauthor']
|
||||
]
|
||||
]
|
||||
],
|
||||
'string: identifier on own line' => [
|
||||
'loader' => $loader,
|
||||
'source' => $multiline2,
|
||||
'expected' => [
|
||||
'templates' => [],
|
||||
'strings' => [
|
||||
'mod_forum' => ['authorreplyingprivatelytoauthor']
|
||||
]
|
||||
]
|
||||
],
|
||||
'string: all parts on new lines' => [
|
||||
'loader' => $loader,
|
||||
'source' => $multiline3,
|
||||
'expected' => [
|
||||
'templates' => [],
|
||||
'strings' => [
|
||||
'mod_forum' => ['authorreplyingprivatelytoauthor']
|
||||
]
|
||||
]
|
||||
],
|
||||
'string: id and component on own line' => [
|
||||
'loader' => $loader,
|
||||
'source' => $multiline4,
|
||||
'expected' => [
|
||||
'templates' => [],
|
||||
'strings' => [
|
||||
'mod_forum' => ['authorreplyingprivatelytoauthor']
|
||||
]
|
||||
]
|
||||
],
|
||||
'string: no component' => [
|
||||
'loader' => $loader,
|
||||
'source' => $multiline5,
|
||||
'expected' => [
|
||||
'templates' => [],
|
||||
'strings' => [
|
||||
'core' => ['hide']
|
||||
]
|
||||
]
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user