Skip to content

Commit

Permalink
Merge branch 'MDL-69553' of https://github.com/NeillM/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
sarjona committed Oct 22, 2020
2 parents 8006aac + 661fa18 commit 6d6f30f
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 12 deletions.
60 changes: 49 additions & 11 deletions lib/classes/output/mustache_template_source_loader.php
Expand Up @@ -319,17 +319,9 @@ protected function scan_template_source_for_dependencies(string $source) : array
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;

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';
list($id, $component) = $this->get_string_identifiers($tokens, $index);

if ($id) {
$strings = $addtodependencies($strings, $component, $id);
}
}
Expand All @@ -343,4 +335,50 @@ protected function scan_template_source_for_dependencies(string $source) : array
'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];
}
}
83 changes: 82 additions & 1 deletion lib/tests/mustache_template_source_loader_test.php
Expand Up @@ -357,12 +357,43 @@ public function test_scan_template_source_for_dependencies_test_cases() {
$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);
Expand Down Expand Up @@ -409,6 +440,56 @@ public function test_scan_template_source_for_dependencies_test_cases() {
]
]
],
'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']
]
]
],
];
}

Expand Down

0 comments on commit 6d6f30f

Please sign in to comment.