Merge branch 'MDL-57304_template_comments_fix' of https://github.com/gthomas2/moodle

This commit is contained in:
Jake Dallimore
2017-06-06 15:54:33 +08:00
4 changed files with 169 additions and 4 deletions
+1 -1
View File
@@ -1 +1 @@
define(["jquery","core/ajax","core/log","core/notification","core/templates","core/config","core/str"],function(a,b,c,d,e,f,g){var h=function(a,b){if(!a)return!1;var c="@template "+b,d=0,e=[];if(e=a.match(/{{!([\s\S]*?)}}/g),null!==e)for(d=0;d<e.length;d++){var f=e[d],g=f.indexOf(c);if(g!==-1){var h=g+c.length+1;return f=f.substr(h,f.length-2-h)}}return!1},i=function(b,f,i){g.get_string("templateselected","tool_templatelibrary",b).done(function(b){a('[data-region="displaytemplateheader"]').text(b)}).fail(d.exception);var j=h(f,b);j===!1&&(j=h(i,b)),j&&(f=j),a('[data-region="displaytemplatesource"]').text(f);var k=f.match(/Example context \(json\):([\s\S]*)/),l=!1;if(k){var m=k[1].trim();try{l=a.parseJSON(m)}catch(n){c.debug("Could not parse json example context for template."),c.debug(n)}}l?e.render(b,l).done(function(b,c){e.replaceNodeContents(a('[data-region="displaytemplateexample"]'),b,c)}).fail(d.exception):g.get_string("templatehasnoexample","tool_templatelibrary").done(function(b){a('[data-region="displaytemplateexample"]').text(b)}).fail(d.exception)},j=function(c){var e=c.split("/"),g=e.shift(),h=e.shift(),j=b.call([{methodname:"core_output_load_template",args:{component:g,template:h,themename:f.theme}},{methodname:"tool_templatelibrary_load_canonical_template",args:{component:g,template:h}}],!0,!1);a.when.apply(a,j).done(function(a,b){i(c,a,b)}).fail(d.exception)};return a('[data-region="list-templates"]').on("click","[data-templatename]",function(b){var c=a(this).data("templatename");b.preventDefault(),j(c)}),{}});
define(["jquery","core/ajax","core/log","core/notification","core/templates","core/config","core/str"],function(a,b,c,d,e,f,g){var h=function(a,b){if(!a)return!1;var c="@template "+b,d=0,e=[];if(e=a.match(/{{!([\s\S]*?)}}/g),null!==e)for(d=0;d<e.length;d++){var f=e[d],g=f.indexOf(c);if(g!==-1){var h=g+c.length+1;return f=f.substr(h,f.length-2-h)}}return!1},i=function(b,f,i){g.get_string("templateselected","tool_templatelibrary",b).done(function(b){a('[data-region="displaytemplateheader"]').text(b)}).fail(d.exception);var j=h(f,b);j===!1&&(j=h(i,b)),j&&(f=j),a('[data-region="displaytemplatesource"]').text(f);var k=f.match(/Example context \(json\):([\s\S]*)/),l=!1;if(k){var m=k[1].trim();try{l=a.parseJSON(m)}catch(n){c.debug("Could not parse json example context for template."),c.debug(n)}}l?e.render(b,l).done(function(b,c){e.replaceNodeContents(a('[data-region="displaytemplateexample"]'),b,c)}).fail(d.exception):g.get_string("templatehasnoexample","tool_templatelibrary").done(function(b){a('[data-region="displaytemplateexample"]').text(b)}).fail(d.exception)},j=function(c){var e=c.split("/"),g=e.shift(),h=e.shift(),j=b.call([{methodname:"core_output_load_template",args:{component:g,template:h,themename:f.theme,includecomments:!0}},{methodname:"tool_templatelibrary_load_canonical_template",args:{component:g,template:h}}],!0,!1);a.when.apply(a,j).done(function(a,b){i(c,a,b)}).fail(d.exception)};return a('[data-region="list-templates"]').on("click","[data-templatename]",function(b){var c=a(this).data("templatename");b.preventDefault(),j(c)}),{}});
@@ -126,7 +126,8 @@ define(['jquery', 'core/ajax', 'core/log', 'core/notification', 'core/templates'
args: {
component: component,
template: name,
themename: config.theme
themename: config.theme,
includecomments: true
}
}, {
methodname: 'tool_templatelibrary_load_canonical_template',
+19 -2
View File
@@ -53,10 +53,20 @@ class external extends external_api {
array('component' => new external_value(PARAM_COMPONENT, 'component containing the template'),
'template' => new external_value(PARAM_ALPHANUMEXT, 'name of the template'),
'themename' => new external_value(PARAM_ALPHANUMEXT, 'The current theme.'),
'includecomments' => new external_value(PARAM_BOOL, 'Include comments or not', VALUE_DEFAULT, false)
)
);
}
/**
* Remove comments from mustache template.
* @param string $templatestr
* @return mixed
*/
protected static function strip_template_comments($templatestr) {
return preg_replace('/(?={{!)(.*)(}})/sU', '', $templatestr);
}
/**
* Return a mustache template, and all the strings it requires.
*
@@ -65,17 +75,19 @@ class external extends external_api {
* @param string $themename The name of the current theme.
* @return string the template
*/
public static function load_template($component, $template, $themename) {
public static function load_template($component, $template, $themename, $includecomments = false) {
global $DB, $CFG, $PAGE;
$params = self::validate_parameters(self::load_template_parameters(),
array('component' => $component,
'template' => $template,
'themename' => $themename));
'themename' => $themename,
'includecomments' => $includecomments));
$component = $params['component'];
$template = $params['template'];
$themename = $params['themename'];
$includecomments = $params['includecomments'];
$templatename = $component . '/' . $template;
@@ -83,6 +95,11 @@ class external extends external_api {
$filename = mustache_template_finder::get_template_filepath($templatename, $themename);
$templatestr = file_get_contents($filename);
// Remove comments from template.
if (!$includecomments) {
$templatestr = self::strip_template_comments($templatestr);
}
return $templatestr;
}
+147
View File
@@ -0,0 +1,147 @@
<?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/>.
/**
* Unit tests for lib/classes/output/external.php
* @author Guy Thomas <[email protected]>
* @copyright Copyright (c) 2017 Blackboard Inc.
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
use core\output\external;
require_once(__DIR__.'/../../lib/externallib.php');
require_once(__DIR__.'/../../lib/mustache/src/Mustache/Tokenizer.php');
require_once(__DIR__.'/../../lib/mustache/src/Mustache/Parser.php');
/**
* Class core_output_external_testcase - test \core\output\external class.
* @package core
* @author Guy Thomas <[email protected]>
* @copyright Copyright (c) 2017 Blackboard Inc.
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_output_external_testcase extends base_testcase {
/**
* Ensure that stripping comments from templates does not mutilate the template body.
*/
public function test_strip_template_comments() {
$templatebody = <<<'TBD'
<h1>{{# str }} pluginname, mod_lemmings {{/ str }}</h1>
<div>{{test}}</div>
<div>{{{unescapedtest}}}</div>
{{#lemmings}}
<div>
<h2>{{name}}</h2>
{{> mod_lemmings/lemmingprofile }}
{{# pix }} t/edit, core, Edit Lemming {{/ pix }}
</div>
{{/lemmings}}
{{^lemmings}}Sorry, no lemmings today{{/lemmings}}
<div id="{{ uniqid }}-tab-container">
{{# tabheader }}
<ul role="tablist" class="nav nav-tabs">
{{# iconlist }}
{{# icons }}
{{> core/pix_icon }}
{{/ icons }}
{{/ iconlist }}
</ul>
{{/ tabheader }}
{{# tabbody }}
<div class="tab-content">
{{# tabcontent }}
{{# tabs }}
{{> core/notification_info}}
{{/ tabs }}
{{/ tabcontent }}
</div>
{{/ tabbody }}
</div>
{{#js}}
require(['jquery','core/tabs'], function($, tabs) {
var container = $("#{{ uniqid }}-tab-container");
tabs.create(container);
});
{{/js}}
TBD;
$templatewithcomment = <<<TBC
{{!
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/>.
}}
{{!
@template mod_lemmings/lemmings
Lemmings template.
The purpose of this template is to render a lot of lemmings.
Classes required for JS:
* none
Data attributes required for JS:
* none
Context variables required for this template:
* attributes Array of name / value pairs.
Example context (json):
{
"lemmings": [
{ "name": "Lemmy Winks", "age" : 1, "size" : "big" },
{ "name": "Rocky", "age" : 2, "size" : "small" }
]
}
}}
$templatebody
{{!
Here's some more comment text
Note, there is no need to test bracketed variables inside comments as gherkin does not support that!
See this issue: https://github.com/mustache/spec/issues/8
}}
TBC;
// Ensure that the template when stripped of comments just includes the body.
$stripped = phpunit_util::call_internal_method(null, 'strip_template_comments',
[$templatewithcomment], 'core\output\external');
$this->assertEquals(trim($templatebody), trim($stripped));
$tokenizer = new Mustache_Tokenizer();
$tokens = $tokenizer->scan($templatebody);
$parser = new Mustache_Parser();
$tree = $parser->parse($tokens);
$this->assertNotEmpty($tree);
}
}