From fb6e143ca340cb01579edba87f4e9d7d1be42aed Mon Sep 17 00:00:00 2001 From: Adrian Greeve Date: Fri, 6 Oct 2017 13:53:35 +0800 Subject: [PATCH 1/4] MDL-60286 lib: Moodle changes to mustache upgrade --- lib/mustache/readme_moodle.txt | 6 +++--- lib/thirdpartylibs.xml | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/mustache/readme_moodle.txt b/lib/mustache/readme_moodle.txt index 27e56b4f9fc..6bc4acffe4f 100644 --- a/lib/mustache/readme_moodle.txt +++ b/lib/mustache/readme_moodle.txt @@ -6,8 +6,8 @@ at https://github.com/bobthecow/mustache.php/releases) 2) Move the src/ and LICENSE file into lib/mustache e.g. -wget https://github.com/bobthecow/mustache.php/archive/v2.11.1.zip -unzip v2.11.1.zip -cd mustache.php-2.11.1/ +wget https://github.com/bobthecow/mustache.php/archive/v2.12.0.zip +unzip v2.12.0.zip +cd mustache.php-2.12.0/ mv src /path/to/moodle/lib/mustache/ mv LICENSE /path/to/moodle/lib/mustache/ diff --git a/lib/thirdpartylibs.xml b/lib/thirdpartylibs.xml index ce97f483e9b..4d0b4e4068f 100644 --- a/lib/thirdpartylibs.xml +++ b/lib/thirdpartylibs.xml @@ -227,7 +227,7 @@ mustache Mustache MIT - 2.11.1 + 2.12.0 amd/src/mustache.js From 703baed252068b6fce88beb2167311cf04b6b378 Mon Sep 17 00:00:00 2001 From: Adrian Greeve Date: Fri, 6 Oct 2017 13:54:39 +0800 Subject: [PATCH 2/4] MDL-60286 lib: Update mustache to 2.12.0 --- lib/mustache/src/Mustache/Autoloader.php | 18 +++- lib/mustache/src/Mustache/Cache.php | 2 +- .../src/Mustache/Cache/AbstractCache.php | 2 +- .../src/Mustache/Cache/FilesystemCache.php | 8 +- lib/mustache/src/Mustache/Cache/NoopCache.php | 2 +- lib/mustache/src/Mustache/Compiler.php | 59 +++++++------ lib/mustache/src/Mustache/Context.php | 2 +- lib/mustache/src/Mustache/Engine.php | 78 +++++++++++++---- lib/mustache/src/Mustache/Exception.php | 2 +- .../Exception/InvalidArgumentException.php | 2 +- .../src/Mustache/Exception/LogicException.php | 2 +- .../Mustache/Exception/RuntimeException.php | 2 +- .../Mustache/Exception/SyntaxException.php | 4 +- .../Exception/UnknownFilterException.php | 4 +- .../Exception/UnknownHelperException.php | 4 +- .../Exception/UnknownTemplateException.php | 4 +- .../src/Mustache/HelperCollection.php | 2 +- lib/mustache/src/Mustache/LambdaHelper.php | 2 +- lib/mustache/src/Mustache/Loader.php | 4 +- .../src/Mustache/Loader/ArrayLoader.php | 2 +- .../src/Mustache/Loader/CascadingLoader.php | 2 +- .../src/Mustache/Loader/FilesystemLoader.php | 2 +- .../src/Mustache/Loader/InlineLoader.php | 2 +- .../src/Mustache/Loader/MutableLoader.php | 2 +- .../Loader/ProductionFilesystemLoader.php | 86 +++++++++++++++++++ .../src/Mustache/Loader/StringLoader.php | 2 +- lib/mustache/src/Mustache/Logger.php | 2 +- .../src/Mustache/Logger/AbstractLogger.php | 2 +- .../src/Mustache/Logger/StreamLogger.php | 2 +- lib/mustache/src/Mustache/Parser.php | 2 +- lib/mustache/src/Mustache/Source.php | 40 +++++++++ .../src/Mustache/Source/FilesystemSource.php | 77 +++++++++++++++++ lib/mustache/src/Mustache/Template.php | 2 +- lib/mustache/src/Mustache/Tokenizer.php | 7 +- 34 files changed, 355 insertions(+), 80 deletions(-) create mode 100644 lib/mustache/src/Mustache/Loader/ProductionFilesystemLoader.php create mode 100644 lib/mustache/src/Mustache/Source.php create mode 100644 lib/mustache/src/Mustache/Source/FilesystemSource.php diff --git a/lib/mustache/src/Mustache/Autoloader.php b/lib/mustache/src/Mustache/Autoloader.php index b16ac1427f4..e8ea3f4a054 100644 --- a/lib/mustache/src/Mustache/Autoloader.php +++ b/lib/mustache/src/Mustache/Autoloader.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2010-2016 Justin Hileman + * (c) 2010-2017 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -16,6 +16,14 @@ class Mustache_Autoloader { private $baseDir; + /** + * An array where the key is the baseDir and the key is an instance of this + * class. + * + * @var array + */ + private static $instances; + /** * Autoloader constructor. * @@ -45,7 +53,13 @@ class Mustache_Autoloader */ public static function register($baseDir = null) { - $loader = new self($baseDir); + $key = $baseDir ? $baseDir : 0; + + if (!isset(self::$instances[$key])) { + self::$instances[$key] = new self($baseDir); + } + + $loader = self::$instances[$key]; spl_autoload_register(array($loader, 'autoload')); return $loader; diff --git a/lib/mustache/src/Mustache/Cache.php b/lib/mustache/src/Mustache/Cache.php index 7b465abdffd..3b5b3f1bf47 100644 --- a/lib/mustache/src/Mustache/Cache.php +++ b/lib/mustache/src/Mustache/Cache.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2010-2016 Justin Hileman + * (c) 2010-2017 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/lib/mustache/src/Mustache/Cache/AbstractCache.php b/lib/mustache/src/Mustache/Cache/AbstractCache.php index 495090b6d96..365eafac6a0 100644 --- a/lib/mustache/src/Mustache/Cache/AbstractCache.php +++ b/lib/mustache/src/Mustache/Cache/AbstractCache.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2010-2016 Justin Hileman + * (c) 2010-2017 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/lib/mustache/src/Mustache/Cache/FilesystemCache.php b/lib/mustache/src/Mustache/Cache/FilesystemCache.php index 0dbe8f9df5d..3e742b70311 100644 --- a/lib/mustache/src/Mustache/Cache/FilesystemCache.php +++ b/lib/mustache/src/Mustache/Cache/FilesystemCache.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2010-2016 Justin Hileman + * (c) 2010-2017 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -108,9 +108,11 @@ class Mustache_Cache_FilesystemCache extends Mustache_Cache_AbstractCache ); @mkdir($dirName, 0777, true); + // @codeCoverageIgnoreStart if (!is_dir($dirName)) { throw new Mustache_Exception_RuntimeException(sprintf('Failed to create cache directory "%s".', $dirName)); } + // @codeCoverageIgnoreEnd } return $dirName; @@ -143,13 +145,17 @@ class Mustache_Cache_FilesystemCache extends Mustache_Cache_AbstractCache return; } + // @codeCoverageIgnoreStart $this->log( Mustache_Logger::ERROR, 'Unable to rename Mustache temp cache file: "{tempName}" -> "{fileName}"', array('tempName' => $tempFile, 'fileName' => $fileName) ); + // @codeCoverageIgnoreEnd } + // @codeCoverageIgnoreStart throw new Mustache_Exception_RuntimeException(sprintf('Failed to write cache file "%s".', $fileName)); + // @codeCoverageIgnoreEnd } } diff --git a/lib/mustache/src/Mustache/Cache/NoopCache.php b/lib/mustache/src/Mustache/Cache/NoopCache.php index ca0007dee13..ed9eec9d5aa 100644 --- a/lib/mustache/src/Mustache/Cache/NoopCache.php +++ b/lib/mustache/src/Mustache/Cache/NoopCache.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2010-2016 Justin Hileman + * (c) 2010-2017 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/lib/mustache/src/Mustache/Compiler.php b/lib/mustache/src/Mustache/Compiler.php index 2a831d6ecde..610369ed6e9 100644 --- a/lib/mustache/src/Mustache/Compiler.php +++ b/lib/mustache/src/Mustache/Compiler.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2010-2016 Justin Hileman + * (c) 2010-2017 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -191,7 +191,6 @@ class Mustache_Compiler { $this->lambdaHelper = new Mustache_LambdaHelper($this->mustache, $context); $buffer = \'\'; - $blocksContext = array(); %s return $buffer; @@ -207,7 +206,6 @@ class Mustache_Compiler public function renderInternal(Mustache_Context $context, $indent = \'\') { $buffer = \'\'; - $blocksContext = array(); %s return $buffer; @@ -240,10 +238,11 @@ class Mustache_Compiler $blockFunction = $context->findInBlock(%s); if (is_callable($blockFunction)) { $buffer .= call_user_func($blockFunction, $context); - } else {%s - } + %s} '; + const BLOCK_VAR_ELSE = '} else {%s'; + /** * Generate Mustache Template inheritance block variable PHP source. * @@ -261,10 +260,15 @@ class Mustache_Compiler { $id = var_export($id, true); - return sprintf($this->prepare(self::BLOCK_VAR, $level), $id, $this->walk($nodes, $level)); + $else = $this->walk($nodes, $level); + if ($else !== '') { + $else = sprintf($this->prepare(self::BLOCK_VAR_ELSE, $level + 1, false, true), $else); + } + + return sprintf($this->prepare(self::BLOCK_VAR, $level), $id, $else); } - const BLOCK_ARG = '$blocksContext[%s] = array($this, \'block%s\');'; + const BLOCK_ARG = '%s => array($this, \'block%s\'),'; /** * Generate Mustache Template inheritance block argument PHP source. @@ -285,14 +289,13 @@ class Mustache_Compiler $keystr = var_export($key, true); $id = var_export($id, true); - return sprintf($this->prepare(self::BLOCK_ARG, 1), $id, $key); + return sprintf($this->prepare(self::BLOCK_ARG, $level), $id, $key); } const BLOCK_FUNCTION = ' public function block%s($context) { - $indent = $buffer = \'\'; - $blocksContext = array();%s + $indent = $buffer = \'\';%s return $buffer; } @@ -327,7 +330,6 @@ class Mustache_Compiler private function section%s(Mustache_Context $context, $indent, $value) { $buffer = \'\'; - $blocksContext = array(); if (%s) { $source = %s; @@ -363,11 +365,10 @@ class Mustache_Compiler * @param string $otag Current Mustache opening tag * @param string $ctag Current Mustache closing tag * @param int $level - * @param bool $arg (default: false) * * @return string Generated section PHP source code */ - private function section($nodes, $id, $filters, $start, $end, $otag, $ctag, $level, $arg = false) + private function section($nodes, $id, $filters, $start, $end, $otag, $ctag, $level) { $source = var_export(substr($this->source, $start, $end - $start), true); $callable = $this->getCallable(); @@ -387,15 +388,11 @@ class Mustache_Compiler $this->sections[$key] = sprintf($this->prepare(self::SECTION), $key, $callable, $source, $helper, $delims, $this->walk($nodes, 2)); } - if ($arg === true) { - return $key; - } else { - $method = $this->getFindMethod($id); - $id = var_export($id, true); - $filters = $this->getFilters($filters, $level); + $method = $this->getFindMethod($id); + $id = var_export($id, true); + $filters = $this->getFilters($filters, $level); - return sprintf($this->prepare(self::SECTION_CALL, $level), $id, $method, $id, $filters, $key); - } + return sprintf($this->prepare(self::SECTION_CALL, $level), $id, $method, $id, $filters, $key); } const INVERTED_SECTION = ' @@ -457,15 +454,20 @@ class Mustache_Compiler } const PARENT = ' - %s - if ($parent = $this->mustache->loadPartial(%s)) { - $context->pushBlockContext($blocksContext); + $context->pushBlockContext(array(%s + )); $buffer .= $parent->renderInternal($context, $indent); $context->popBlockContext(); } '; + const PARENT_NO_CONTEXT = ' + if ($parent = $this->mustache->loadPartial(%s)) { + $buffer .= $parent->renderInternal($context, $indent); + } + '; + /** * Generate Mustache Template inheritance parent call PHP source. * @@ -480,11 +482,14 @@ class Mustache_Compiler { $realChildren = array_filter($children, array(__CLASS__, 'onlyBlockArgs')); + if (empty($realChildren)) { + return sprintf($this->prepare(self::PARENT_NO_CONTEXT, $level), var_export($id, true)); + } + return sprintf( $this->prepare(self::PARENT, $level), - $this->walk($realChildren, $level), var_export($id, true), - var_export($indent, true) + $this->walk($realChildren, $level + 1) ); } @@ -621,7 +626,7 @@ class Mustache_Compiler /** * Select the appropriate Context `find` method for a given $id. * - * The return value will be one of `find`, `findDot` or `last`. + * The return value will be one of `find`, `findDot`, `findAnchoredDot` or `last`. * * @see Mustache_Context::find * @see Mustache_Context::findDot diff --git a/lib/mustache/src/Mustache/Context.php b/lib/mustache/src/Mustache/Context.php index f59faea8a62..69c02e017ce 100644 --- a/lib/mustache/src/Mustache/Context.php +++ b/lib/mustache/src/Mustache/Context.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2010-2016 Justin Hileman + * (c) 2010-2017 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/lib/mustache/src/Mustache/Engine.php b/lib/mustache/src/Mustache/Engine.php index 856d4efe65e..91109771baa 100644 --- a/lib/mustache/src/Mustache/Engine.php +++ b/lib/mustache/src/Mustache/Engine.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2010-2016 Justin Hileman + * (c) 2010-2017 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -23,7 +23,7 @@ */ class Mustache_Engine { - const VERSION = '2.11.1'; + const VERSION = '2.12.0'; const SPEC_VERSION = '1.1.2'; const PRAGMA_FILTERS = 'FILTERS'; @@ -54,6 +54,7 @@ class Mustache_Engine private $logger; private $strictCallables = false; private $pragmas = array(); + private $delimiters; // Services private $tokenizer; @@ -81,6 +82,14 @@ class Mustache_Engine * // sections are often too dynamic to benefit from caching. * 'cache_lambda_templates' => true, * + * // Customize the tag delimiters used by this engine instance. Note that overriding here changes the + * // delimiters used to parse all templates and partials loaded by this instance. To override just for a + * // single template, use an inline "change delimiters" tag at the start of the template file: + * // + * // {{=<% %>=}} + * // + * 'delimiters' => '<% %>', + * * // A Mustache template loader instance. Uses a StringLoader if not specified. * 'loader' => new Mustache_Loader_FilesystemLoader(dirname(__FILE__).'/views'), * @@ -133,6 +142,10 @@ class Mustache_Engine public function __construct(array $options = array()) { if (isset($options['template_class_prefix'])) { + if ((string) $options['template_class_prefix'] === '') { + throw new Mustache_Exception_InvalidArgumentException('Mustache Constructor "template_class_prefix" must not be empty'); + } + $this->templateClassPrefix = $options['template_class_prefix']; } @@ -191,6 +204,10 @@ class Mustache_Engine $this->strictCallables = $options['strict_callables']; } + if (isset($options['delimiters'])) { + $this->delimiters = $options['delimiters']; + } + if (isset($options['pragmas'])) { foreach ($options['pragmas'] as $pragma) { if (!isset(self::$knownPragmas[$pragma])) { @@ -589,22 +606,43 @@ class Mustache_Engine /** * Helper method to generate a Mustache template class. * - * @param string $source + * This method must be updated any time options are added which make it so + * the same template could be parsed and compiled multiple different ways. + * + * @param string|Mustache_Source $source * * @return string Mustache Template class name */ public function getTemplateClassName($source) { - return $this->templateClassPrefix . md5(sprintf( - 'version:%s,escape:%s,entity_flags:%i,charset:%s,strict_callables:%s,pragmas:%s,source:%s', - self::VERSION, - isset($this->escape) ? 'custom' : 'default', - $this->entityFlags, - $this->charset, - $this->strictCallables ? 'true' : 'false', - implode(' ', $this->getPragmas()), - $source - )); + // For the most part, adding a new option here should do the trick. + // + // Pick a value here which is unique for each possible way the template + // could be compiled... but not necessarily unique per option value. See + // escape below, which only needs to differentiate between 'custom' and + // 'default' escapes. + // + // Keep this list in alphabetical order :) + $chunks = array( + 'charset' => $this->charset, + 'delimiters' => $this->delimiters ? $this->delimiters : '{{ }}', + 'entityFlags' => $this->entityFlags, + 'escape' => isset($this->escape) ? 'custom' : 'default', + 'key' => ($source instanceof Mustache_Source) ? $source->getKey() : 'source', + 'pragmas' => $this->getPragmas(), + 'strictCallables' => $this->strictCallables, + 'version' => self::VERSION, + ); + + $key = json_encode($chunks); + + // Template Source instances have already provided their own source key. For strings, just include the whole + // source string in the md5 hash. + if (!$source instanceof Mustache_Source) { + $key .= "\n" . $source; + } + + return $this->templateClassPrefix . md5($key); } /** @@ -681,8 +719,8 @@ class Mustache_Engine * @see Mustache_Engine::loadPartial * @see Mustache_Engine::loadLambda * - * @param string $source - * @param Mustache_Cache $cache (default: null) + * @param string|Mustache_Source $source + * @param Mustache_Cache $cache (default: null) * * @return Mustache_Template */ @@ -725,7 +763,7 @@ class Mustache_Engine */ private function tokenize($source) { - return $this->getTokenizer()->scan($source); + return $this->getTokenizer()->scan($source, $this->delimiters); } /** @@ -750,13 +788,12 @@ class Mustache_Engine * * @see Mustache_Compiler::compile * - * @param string $source + * @param string|Mustache_Source $source * * @return string generated Mustache template class code */ private function compile($source) { - $tree = $this->parse($source); $name = $this->getTemplateClassName($source); $this->log( @@ -765,6 +802,11 @@ class Mustache_Engine array('className' => $name) ); + if ($source instanceof Mustache_Source) { + $source = $source->getSource(); + } + $tree = $this->parse($source); + $compiler = $this->getCompiler(); $compiler->setPragmas($this->getPragmas()); diff --git a/lib/mustache/src/Mustache/Exception.php b/lib/mustache/src/Mustache/Exception.php index 66bd6bd47a0..d4001a9bf9e 100644 --- a/lib/mustache/src/Mustache/Exception.php +++ b/lib/mustache/src/Mustache/Exception.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2010-2016 Justin Hileman + * (c) 2010-2017 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/lib/mustache/src/Mustache/Exception/InvalidArgumentException.php b/lib/mustache/src/Mustache/Exception/InvalidArgumentException.php index 46b200c6c8a..becf2ed1bbe 100644 --- a/lib/mustache/src/Mustache/Exception/InvalidArgumentException.php +++ b/lib/mustache/src/Mustache/Exception/InvalidArgumentException.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2010-2016 Justin Hileman + * (c) 2010-2017 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/lib/mustache/src/Mustache/Exception/LogicException.php b/lib/mustache/src/Mustache/Exception/LogicException.php index 8e75e7e1101..b2424d67cc8 100644 --- a/lib/mustache/src/Mustache/Exception/LogicException.php +++ b/lib/mustache/src/Mustache/Exception/LogicException.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2010-2016 Justin Hileman + * (c) 2010-2017 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/lib/mustache/src/Mustache/Exception/RuntimeException.php b/lib/mustache/src/Mustache/Exception/RuntimeException.php index 9a7b90b52cc..b6369f4bbce 100644 --- a/lib/mustache/src/Mustache/Exception/RuntimeException.php +++ b/lib/mustache/src/Mustache/Exception/RuntimeException.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2010-2016 Justin Hileman + * (c) 2010-2017 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/lib/mustache/src/Mustache/Exception/SyntaxException.php b/lib/mustache/src/Mustache/Exception/SyntaxException.php index 4fcb39d7e96..b1879a3d1d6 100644 --- a/lib/mustache/src/Mustache/Exception/SyntaxException.php +++ b/lib/mustache/src/Mustache/Exception/SyntaxException.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2010-2016 Justin Hileman + * (c) 2010-2017 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -27,7 +27,7 @@ class Mustache_Exception_SyntaxException extends LogicException implements Musta if (version_compare(PHP_VERSION, '5.3.0', '>=')) { parent::__construct($msg, 0, $previous); } else { - parent::__construct($msg); + parent::__construct($msg); // @codeCoverageIgnore } } diff --git a/lib/mustache/src/Mustache/Exception/UnknownFilterException.php b/lib/mustache/src/Mustache/Exception/UnknownFilterException.php index 44505e493b5..0651c1730a6 100644 --- a/lib/mustache/src/Mustache/Exception/UnknownFilterException.php +++ b/lib/mustache/src/Mustache/Exception/UnknownFilterException.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2010-2016 Justin Hileman + * (c) 2010-2017 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -27,7 +27,7 @@ class Mustache_Exception_UnknownFilterException extends UnexpectedValueException if (version_compare(PHP_VERSION, '5.3.0', '>=')) { parent::__construct($message, 0, $previous); } else { - parent::__construct($message); + parent::__construct($message); // @codeCoverageIgnore } } diff --git a/lib/mustache/src/Mustache/Exception/UnknownHelperException.php b/lib/mustache/src/Mustache/Exception/UnknownHelperException.php index e1bc4d2cd3d..193be782503 100644 --- a/lib/mustache/src/Mustache/Exception/UnknownHelperException.php +++ b/lib/mustache/src/Mustache/Exception/UnknownHelperException.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2010-2016 Justin Hileman + * (c) 2010-2017 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -27,7 +27,7 @@ class Mustache_Exception_UnknownHelperException extends InvalidArgumentException if (version_compare(PHP_VERSION, '5.3.0', '>=')) { parent::__construct($message, 0, $previous); } else { - parent::__construct($message); + parent::__construct($message); // @codeCoverageIgnore } } diff --git a/lib/mustache/src/Mustache/Exception/UnknownTemplateException.php b/lib/mustache/src/Mustache/Exception/UnknownTemplateException.php index 0c37796280f..32a778a5079 100644 --- a/lib/mustache/src/Mustache/Exception/UnknownTemplateException.php +++ b/lib/mustache/src/Mustache/Exception/UnknownTemplateException.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2010-2016 Justin Hileman + * (c) 2010-2017 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -27,7 +27,7 @@ class Mustache_Exception_UnknownTemplateException extends InvalidArgumentExcepti if (version_compare(PHP_VERSION, '5.3.0', '>=')) { parent::__construct($message, 0, $previous); } else { - parent::__construct($message); + parent::__construct($message); // @codeCoverageIgnore } } diff --git a/lib/mustache/src/Mustache/HelperCollection.php b/lib/mustache/src/Mustache/HelperCollection.php index 86c56b78147..5d8f73c1924 100644 --- a/lib/mustache/src/Mustache/HelperCollection.php +++ b/lib/mustache/src/Mustache/HelperCollection.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2010-2016 Justin Hileman + * (c) 2010-2017 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/lib/mustache/src/Mustache/LambdaHelper.php b/lib/mustache/src/Mustache/LambdaHelper.php index 1d203d5ddac..e93dbfa3db9 100644 --- a/lib/mustache/src/Mustache/LambdaHelper.php +++ b/lib/mustache/src/Mustache/LambdaHelper.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2010-2016 Justin Hileman + * (c) 2010-2017 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/lib/mustache/src/Mustache/Loader.php b/lib/mustache/src/Mustache/Loader.php index c5e1a19af43..23adba1a8dd 100644 --- a/lib/mustache/src/Mustache/Loader.php +++ b/lib/mustache/src/Mustache/Loader.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2010-2016 Justin Hileman + * (c) 2010-2017 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -21,7 +21,7 @@ interface Mustache_Loader * * @param string $name * - * @return string Mustache Template source + * @return string|Mustache_Source Mustache Template source */ public function load($name); } diff --git a/lib/mustache/src/Mustache/Loader/ArrayLoader.php b/lib/mustache/src/Mustache/Loader/ArrayLoader.php index c0d6a4d4ac5..4276493a942 100644 --- a/lib/mustache/src/Mustache/Loader/ArrayLoader.php +++ b/lib/mustache/src/Mustache/Loader/ArrayLoader.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2010-2016 Justin Hileman + * (c) 2010-2017 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/lib/mustache/src/Mustache/Loader/CascadingLoader.php b/lib/mustache/src/Mustache/Loader/CascadingLoader.php index b4726bc779f..3fb6353c7da 100644 --- a/lib/mustache/src/Mustache/Loader/CascadingLoader.php +++ b/lib/mustache/src/Mustache/Loader/CascadingLoader.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2010-2016 Justin Hileman + * (c) 2010-2017 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/lib/mustache/src/Mustache/Loader/FilesystemLoader.php b/lib/mustache/src/Mustache/Loader/FilesystemLoader.php index a31375133ed..e366df7076b 100644 --- a/lib/mustache/src/Mustache/Loader/FilesystemLoader.php +++ b/lib/mustache/src/Mustache/Loader/FilesystemLoader.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2010-2016 Justin Hileman + * (c) 2010-2017 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/lib/mustache/src/Mustache/Loader/InlineLoader.php b/lib/mustache/src/Mustache/Loader/InlineLoader.php index f6d4b179d5e..ae297fec62b 100644 --- a/lib/mustache/src/Mustache/Loader/InlineLoader.php +++ b/lib/mustache/src/Mustache/Loader/InlineLoader.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2010-2016 Justin Hileman + * (c) 2010-2017 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/lib/mustache/src/Mustache/Loader/MutableLoader.php b/lib/mustache/src/Mustache/Loader/MutableLoader.php index f228d90794e..57fe5be33a7 100644 --- a/lib/mustache/src/Mustache/Loader/MutableLoader.php +++ b/lib/mustache/src/Mustache/Loader/MutableLoader.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2010-2016 Justin Hileman + * (c) 2010-2017 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/lib/mustache/src/Mustache/Loader/ProductionFilesystemLoader.php b/lib/mustache/src/Mustache/Loader/ProductionFilesystemLoader.php new file mode 100644 index 00000000000..e73533279b1 --- /dev/null +++ b/lib/mustache/src/Mustache/Loader/ProductionFilesystemLoader.php @@ -0,0 +1,86 @@ + '.ms', + * 'stat_props' => array('size', 'mtime'), + * ); + * + * Specifying 'stat_props' overrides the stat properties used to invalidate the template cache. By default, this + * uses 'mtime' and 'size', but this can be set to any of the properties supported by stat(): + * + * http://php.net/manual/en/function.stat.php + * + * You can also disable filesystem stat entirely: + * + * $options = array('stat_props' => null); + * + * But with great power comes great responsibility. Namely, if you disable stat-based cache invalidation, + * YOU MUST CLEAR THE TEMPLATE CACHE YOURSELF when your templates change. Make it part of your build or deploy + * process so you don't forget! + * + * @throws Mustache_Exception_RuntimeException if $baseDir does not exist. + * + * @param string $baseDir Base directory containing Mustache template files. + * @param array $options Array of Loader options (default: array()) + */ + public function __construct($baseDir, array $options = array()) + { + parent::__construct($baseDir, $options); + + if (array_key_exists('stat_props', $options)) { + if (empty($options['stat_props'])) { + $this->statProps = array(); + } else { + $this->statProps = $options['stat_props']; + } + } else { + $this->statProps = array('size', 'mtime'); + } + } + + /** + * Helper function for loading a Mustache file by name. + * + * @throws Mustache_Exception_UnknownTemplateException If a template file is not found. + * + * @param string $name + * + * @return Mustache_Source Mustache Template source + */ + protected function loadFile($name) + { + $fileName = $this->getFileName($name); + + if (!file_exists($fileName)) { + throw new Mustache_Exception_UnknownTemplateException($name); + } + + return new Mustache_Source_FilesystemSource($fileName, $this->statProps); + } +} diff --git a/lib/mustache/src/Mustache/Loader/StringLoader.php b/lib/mustache/src/Mustache/Loader/StringLoader.php index 79b616bf9ee..7012c03b01c 100644 --- a/lib/mustache/src/Mustache/Loader/StringLoader.php +++ b/lib/mustache/src/Mustache/Loader/StringLoader.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2010-2016 Justin Hileman + * (c) 2010-2017 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/lib/mustache/src/Mustache/Logger.php b/lib/mustache/src/Mustache/Logger.php index 324a7ce5a60..cb4037a2528 100644 --- a/lib/mustache/src/Mustache/Logger.php +++ b/lib/mustache/src/Mustache/Logger.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2010-2016 Justin Hileman + * (c) 2010-2017 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/lib/mustache/src/Mustache/Logger/AbstractLogger.php b/lib/mustache/src/Mustache/Logger/AbstractLogger.php index 64bdf663753..a169f9c6b1c 100644 --- a/lib/mustache/src/Mustache/Logger/AbstractLogger.php +++ b/lib/mustache/src/Mustache/Logger/AbstractLogger.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2010-2016 Justin Hileman + * (c) 2010-2017 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/lib/mustache/src/Mustache/Logger/StreamLogger.php b/lib/mustache/src/Mustache/Logger/StreamLogger.php index efb32318276..402a148e836 100644 --- a/lib/mustache/src/Mustache/Logger/StreamLogger.php +++ b/lib/mustache/src/Mustache/Logger/StreamLogger.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2010-2016 Justin Hileman + * (c) 2010-2017 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/lib/mustache/src/Mustache/Parser.php b/lib/mustache/src/Mustache/Parser.php index adefebdd1bb..c36a84afeab 100644 --- a/lib/mustache/src/Mustache/Parser.php +++ b/lib/mustache/src/Mustache/Parser.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2010-2016 Justin Hileman + * (c) 2010-2017 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/lib/mustache/src/Mustache/Source.php b/lib/mustache/src/Mustache/Source.php new file mode 100644 index 00000000000..278c2cb322e --- /dev/null +++ b/lib/mustache/src/Mustache/Source.php @@ -0,0 +1,40 @@ +fileName = $fileName; + $this->statProps = $statProps; + } + + /** + * Get the Source key (used to generate the compiled class name). + * + * @throws RuntimeException when a source file cannot be read + * + * @return string + */ + public function getKey() + { + $chunks = array( + 'fileName' => $this->fileName, + ); + + if (!empty($this->statProps)) { + if (!isset($this->stat)) { + $this->stat = @stat($this->fileName); + } + + if ($this->stat === false) { + throw new RuntimeException(sprintf('Failed to read source file "%s".', $this->fileName)); + } + + foreach ($this->statProps as $prop) { + $chunks[$prop] = $this->stat[$prop]; + } + } + + return json_encode($chunks); + } + + /** + * Get the template Source. + * + * @return string + */ + public function getSource() + { + return file_get_contents($this->fileName); + } +} diff --git a/lib/mustache/src/Mustache/Template.php b/lib/mustache/src/Mustache/Template.php index 2b7771c4268..4de82393072 100644 --- a/lib/mustache/src/Mustache/Template.php +++ b/lib/mustache/src/Mustache/Template.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2010-2016 Justin Hileman + * (c) 2010-2017 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/lib/mustache/src/Mustache/Tokenizer.php b/lib/mustache/src/Mustache/Tokenizer.php index 27e4d05ec88..b1f3d73892e 100644 --- a/lib/mustache/src/Mustache/Tokenizer.php +++ b/lib/mustache/src/Mustache/Tokenizer.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2010-2016 Justin Hileman + * (c) 2010-2017 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -91,11 +91,14 @@ class Mustache_Tokenizer { // Setting mbstring.func_overload makes things *really* slow. // Let's do everyone a favor and scan this string as ASCII instead. + // + // @codeCoverageIgnoreStart $encoding = null; if (function_exists('mb_internal_encoding') && ini_get('mbstring.func_overload') & 2) { $encoding = mb_internal_encoding(); mb_internal_encoding('ASCII'); } + // @codeCoverageIgnoreEnd $this->reset(); @@ -202,9 +205,11 @@ class Mustache_Tokenizer $this->flushBuffer(); // Restore the user's encoding... + // @codeCoverageIgnoreStart if ($encoding) { mb_internal_encoding($encoding); } + // @codeCoverageIgnoreEnd return $this->tokens; } From 5adc70b77a3d3d0698662018ed4d7b9a1380b801 Mon Sep 17 00:00:00 2001 From: Adrian Greeve Date: Fri, 6 Oct 2017 14:54:03 +0800 Subject: [PATCH 3/4] MDL-60286 lib: Upgrade mustache.js to 2.3.0 --- lib/amd/build/mustache.min.js | 2 +- lib/amd/src/mustache.js | 116 ++-------------------------------- 2 files changed, 7 insertions(+), 111 deletions(-) diff --git a/lib/amd/build/mustache.min.js b/lib/amd/build/mustache.min.js index 338028719af..e28c52846bb 100644 --- a/lib/amd/build/mustache.min.js +++ b/lib/amd/build/mustache.min.js @@ -1 +1 @@ -!function(a,b){"object"==typeof exports&&exports&&"string"!=typeof exports.nodeName?b(exports):"function"==typeof define&&define.amd?define(["exports"],b):(a.Mustache={},b(a.Mustache))}(this,function(a){function b(a){return"function"==typeof a}function c(a){return p(a)?"array":typeof a}function d(a){return a.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g,"\\$&")}function e(a,b){return null!=a&&"object"==typeof a&&b in a}function f(a,b){return q.call(a,b)}function g(a){return!f(r,a)}function h(a){return String(a).replace(/[&<>"'`=\/]/g,function(a){return s[a]})}function i(b,c){function e(){if(r&&!s)for(;q.length;)delete o[q.pop()];else q=[];r=!1,s=!1}function f(a){if("string"==typeof a&&(a=a.split(u,2)),!p(a)||2!==a.length)throw new Error("Invalid tags: "+a);h=new RegExp(d(a[0])+"\\s*"),i=new RegExp("\\s*"+d(a[1])),m=new RegExp("\\s*"+d("}"+a[1]))}if(!b)return[];var h,i,m,n=[],o=[],q=[],r=!1,s=!1;f(c||a.tags);for(var y,z,A,B,C,D,E=new l(b);!E.eos();){if(y=E.pos,A=E.scanUntil(h))for(var F=0,G=A.length;F0?f[f.length-1][4]:d;break;default:e.push(b)}return d}function l(a){this.string=a,this.tail=a,this.pos=0}function m(a,b){this.view=a,this.blocks={},this.cache={".":this.view},this.parent=b}function n(){this.cache={}}var o=Object.prototype.toString,p=Array.isArray||function(a){return"[object Array]"===o.call(a)},q=RegExp.prototype.test,r=/\S/,s={"&":"&","<":"<",">":">",'"':""","'":"'","/":"/","`":"`","=":"="},t=/\s*/,u=/\s+/,v=/\s*=/,w=/\s*\}/,x=/#|\^|\/|>|\{|&|=|!|\$|0)for(c=h.view,f=a.split("."),g=0;null!=c&&g"===f?g=this.renderPartial(e,b,c,d):"<"===f?g=this.renderBlock(e,b,c,d):"$"===f?g=this.renderBlockVariable(e,b,c,d):"&"===f?g=this.unescapedValue(e,b):"name"===f?g=this.escapedValue(e,b):"text"===f&&(g=this.rawValue(e)),void 0!==g&&(h+=g);return h},n.prototype.renderSection=function(a,c,d,e){function f(a){return g.render(a,c,d)}var g=this,h="",i=c.lookup(a[1]);if(i){if(p(i))for(var j=0,k=i.length;j"'`=\/]/g,function(a){return s[a]})}function i(b,c){function e(){if(r&&!s)for(;q.length;)delete o[q.pop()];else q=[];r=!1,s=!1}function f(a){if("string"==typeof a&&(a=a.split(u,2)),!p(a)||2!==a.length)throw new Error("Invalid tags: "+a);h=new RegExp(d(a[0])+"\\s*"),i=new RegExp("\\s*"+d(a[1])),m=new RegExp("\\s*"+d("}"+a[1]))}if(!b)return[];var h,i,m,n=[],o=[],q=[],r=!1,s=!1;f(c||a.tags);for(var y,z,A,B,C,D,E=new l(b);!E.eos();){if(y=E.pos,A=E.scanUntil(h))for(var F=0,G=A.length;F0?f[f.length-1][4]:d;break;default:e.push(b)}return d}function l(a){this.string=a,this.tail=a,this.pos=0}function m(a,b){this.view=a,this.blocks={},this.cache={".":this.view},this.parent=b}function n(){this.cache={}}var o=Object.prototype.toString,p=Array.isArray||function(a){return"[object Array]"===o.call(a)},q=RegExp.prototype.test,r=/\S/,s={"&":"&","<":"<",">":">",'"':""","'":"'","/":"/","`":"`","=":"="},t=/\s*/,u=/\s+/,v=/\s*=/,w=/\s*\}/,x=/#|\^|\/|>|\{|&|=|!|\$|0)for(c=h.view,f=a.split("."),g=0;null!=c&&g"===f?g=this.renderPartial(e,b,c,d):"<"===f?g=this.renderBlock(e,b,c,d):"$"===f?g=this.renderBlockVariable(e,b,c,d):"&"===f?g=this.unescapedValue(e,b):"name"===f?g=this.escapedValue(e,b):"text"===f&&(g=this.rawValue(e)),void 0!==g&&(h+=g);return h},n.prototype.renderSection=function(a,c,d,e){function f(a){return g.render(a,c,d)}var g=this,h="",i=c.lookup(a[1]);if(i){if(p(i))for(var j=0,k=i.length;j|\{|&|=|!|\$||\{|&|=|!/; /** * Breaks up the given `template` string into a tree of tokens. If the `tags` @@ -235,7 +200,7 @@ token = [ type, value, start, scanner.pos ]; tokens.push(token); - if (type === '#' || type === '^' || type === '$' || type === '<') { + if (type === '#' || type === '^') { sections.push(token); } else if (type === '/') { // Check section nesting. @@ -304,8 +269,6 @@ token = tokens[i]; switch (token[0]) { - case '$': - case '<': case '#': case '^': collector.push(token); @@ -391,7 +354,6 @@ */ function Context (view, parentContext) { this.view = view; - this.blocks = {}; this.cache = { '.': this.view }; this.parent = parentContext; } @@ -404,42 +366,6 @@ return new Context(view, this); }; - /** - * Set a value in the current block context. - */ - Context.prototype.setBlockVar = function set (name, value) { - var blocks = this.blocks; - - blocks[name] = value; - - return value; - }; - - /** - * Clear all current block vars. - */ - Context.prototype.clearBlockVars = function clearBlockVars () { - this.blocks = {}; - }; - - /** - * Get a value only from the current block context. - */ - Context.prototype.getBlockVar = function getBlockVar (name) { - var blocks = this.blocks; - - var value; - if (blocks.hasOwnProperty(name)) { - value = blocks[name]; - } else { - if (this.parent) { - value = this.parent.getBlockVar(name); - } - } - // Can return undefined. - return value; - }; - /** * Returns the value of the given name in this context, traversing * up the context hierarchy if the value is absent in this context's view. @@ -521,7 +447,7 @@ var tokens = cache[template]; if (tokens == null) - tokens = cache[template] = parseTemplate(template, tags); + tokens = cache[template + ':' + (tags || mustache.tags).join(':')] = parseTemplate(template, tags); return tokens; }; @@ -562,8 +488,6 @@ if (symbol === '#') value = this.renderSection(token, context, partials, originalTemplate); else if (symbol === '^') value = this.renderInverted(token, context, partials, originalTemplate); else if (symbol === '>') value = this.renderPartial(token, context, partials, originalTemplate); - else if (symbol === '<') value = this.renderBlock(token, context, partials, originalTemplate); - else if (symbol === '$') value = this.renderBlockVariable(token, context, partials, originalTemplate); else if (symbol === '&') value = this.unescapedValue(token, context); else if (symbol === 'name') value = this.escapedValue(token, context); else if (symbol === 'text') value = this.rawValue(token); @@ -626,34 +550,6 @@ return this.renderTokens(this.parse(value), context, partials, value); }; - Writer.prototype.renderBlock = function renderBlock (token, context, partials, originalTemplate) { - if (!partials) return; - - var value = isFunction(partials) ? partials(token[1]) : partials[token[1]]; - if (value != null) - // Ignore any wrongly set block vars before we started. - context.clearBlockVars(); - // We are only rendering to record the default block variables. - this.renderTokens(token[4], context, partials, originalTemplate); - // Now we render and return the result. - var result = this.renderTokens(this.parse(value), context, partials, value); - // Don't leak the block variables outside this include. - context.clearBlockVars(); - return result; - }; - - Writer.prototype.renderBlockVariable = function renderBlockVariable (token, context, partials, originalTemplate) { - var value = token[1]; - - var exists = context.getBlockVar(value); - if (!exists) { - context.setBlockVar(value, originalTemplate.slice(token[3], token[5])); - return this.renderTokens(token[4], context, partials, originalTemplate); - } else { - return this.renderTokens(this.parse(exists), context, partials, exists); - } - }; - Writer.prototype.unescapedValue = function unescapedValue (token, context) { var value = context.lookup(token[1]); if (value != null) @@ -671,7 +567,7 @@ }; mustache.name = 'mustache.js'; - mustache.version = '2.2.1'; + mustache.version = '2.3.0'; mustache.tags = [ '{{', '}}' ]; // All high-level mustache.* functions use this writer. @@ -730,5 +626,5 @@ mustache.Context = Context; mustache.Writer = Writer; + return mustache; })); -/* jshint ignore:end */ From dc9c4c5141ca40bb3924b184b434f521eb478397 Mon Sep 17 00:00:00 2001 From: Adrian Greeve Date: Fri, 6 Oct 2017 15:03:17 +0800 Subject: [PATCH 4/4] MDL-60286 lib: Moodle additions to mustache.js --- lib/amd/src/mustache.js | 111 ++++++++++++++++++++++++++++++++++++++-- lib/thirdpartylibs.xml | 2 +- 2 files changed, 109 insertions(+), 4 deletions(-) diff --git a/lib/amd/src/mustache.js b/lib/amd/src/mustache.js index 413b6f25ede..29135a1fa44 100644 --- a/lib/amd/src/mustache.js +++ b/lib/amd/src/mustache.js @@ -1,9 +1,44 @@ +// The MIT License +// +// Copyright (c) 2009 Chris Wanstrath (Ruby) +// Copyright (c) 2010-2014 Jan Lehnardt (JavaScript) +// Copyright (c) 2010-2015 The mustache.js community +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// + +// Description of import into Moodle: +// Checkout from https://github.com/moodle/custom-mustache.js +// Rebase onto latest release tag from https://github.com/janl/mustache.js +// Copy mustache.js into lib/amd/src/ in Moodle folder. +// Add the license as a comment to the file and these instructions. +// Add jshint tags so this file is not linted. +// Remove the "global define:" comment (hint for linter) +// Make sure that you have not removed the custom code for '$' and '<'. + /*! * mustache.js - Logic-less {{mustache}} templates with JavaScript * http://github.com/janl/mustache.js */ -/*global define: false Mustache: true*/ +/* jshint ignore:start */ (function defineMustache (global, factory) { if (typeof exports === 'object' && exports && typeof exports.nodeName !== 'string') { @@ -78,7 +113,7 @@ var spaceRe = /\s+/; var equalsRe = /\s*=/; var curlyRe = /\s*\}/; - var tagRe = /#|\^|\/|>|\{|&|=|!/; + var tagRe = /#|\^|\/|>|\{|&|=|!|\$|') value = this.renderPartial(token, context, partials, originalTemplate); + else if (symbol === '<') value = this.renderBlock(token, context, partials, originalTemplate); + else if (symbol === '$') value = this.renderBlockVariable(token, context, partials, originalTemplate); else if (symbol === '&') value = this.unescapedValue(token, context); else if (symbol === 'name') value = this.escapedValue(token, context); else if (symbol === 'text') value = this.rawValue(token); @@ -550,6 +626,34 @@ return this.renderTokens(this.parse(value), context, partials, value); }; + Writer.prototype.renderBlock = function renderBlock (token, context, partials, originalTemplate) { + if (!partials) return; + + var value = isFunction(partials) ? partials(token[1]) : partials[token[1]]; + if (value != null) + // Ignore any wrongly set block vars before we started. + context.clearBlockVars(); + // We are only rendering to record the default block variables. + this.renderTokens(token[4], context, partials, originalTemplate); + // Now we render and return the result. + var result = this.renderTokens(this.parse(value), context, partials, value); + // Don't leak the block variables outside this include. + context.clearBlockVars(); + return result; + }; + + Writer.prototype.renderBlockVariable = function renderBlockVariable (token, context, partials, originalTemplate) { + var value = token[1]; + + var exists = context.getBlockVar(value); + if (!exists) { + context.setBlockVar(value, originalTemplate.slice(token[3], token[5])); + return this.renderTokens(token[4], context, partials, originalTemplate); + } else { + return this.renderTokens(this.parse(exists), context, partials, exists); + } + }; + Writer.prototype.unescapedValue = function unescapedValue (token, context) { var value = context.lookup(token[1]); if (value != null) @@ -628,3 +732,4 @@ return mustache; })); +/* jshint ignore:end */ diff --git a/lib/thirdpartylibs.xml b/lib/thirdpartylibs.xml index 4d0b4e4068f..a4d6afd12ed 100644 --- a/lib/thirdpartylibs.xml +++ b/lib/thirdpartylibs.xml @@ -233,7 +233,7 @@ amd/src/mustache.js Mustache.js MIT - 2.2.1 + 2.3.0 graphlib.php