MDL-70802 javascript: Normalise component consistently in templates

This commit is contained in:
Andrew Nicols 2021-02-05 15:51:18 +08:00 committed by Travis CI
parent 4c26696e7e
commit 1b7ce2dd0d
3 changed files with 50 additions and 15 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -68,6 +68,22 @@ define([
/** @var {Array} disallowedNestedHelpers - List of helpers that can't be called within other helpers */
var disallowedNestedHelpers = ['js'];
/**
* Normalise the provided component such that '', 'moodle', and 'core' are treated consistently.
*
* @param {String} component
* @returns {String}
*/
var getNormalisedComponent = function(component) {
if (component) {
if (component !== 'moodle' && component !== 'core') {
return component;
}
}
return 'core';
};
/**
* Search the various caches for a template promise for the given search key.
* The search key should be in the format <theme>/<component>/<template> e.g. boost/core/modal.
@ -133,7 +149,7 @@ define([
var requests = [];
// Get a list of promises for each of the templates we need to load.
var templatePromises = templatesToLoad.map(function(templateData) {
var component = templateData.component;
var component = getNormalisedComponent(templateData.component);
var name = templateData.name;
var searchKey = templateData.searchKey;
var theme = templateData.theme;
@ -177,6 +193,7 @@ define([
// Process all of the template dependencies for this template and add
// them to the caches so that we don't request them again later.
response.templates.forEach(function(data) {
data.component = getNormalisedComponent(data.component);
// Generate the search key for this template in the response so that we
// can add it to the caches.
var tempSearchKey = [theme, data.component, data.name].join('/');
@ -200,7 +217,7 @@ define([
// with them now so that we don't need to re-fetch them.
str.cache_strings(response.strings.map(function(data) {
return {
component: data.component,
component: getNormalisedComponent(data.component),
key: data.name,
value: data.value
};
@ -315,7 +332,7 @@ define([
// This is the first time this has been requested so let's add it to the buffer
// to be loaded.
var parts = templateName.split('/');
var component = parts.shift();
var component = getNormalisedComponent(parts.shift());
var name = parts.join('/');
var deferred = $.Deferred();
@ -361,7 +378,7 @@ define([
// This is the first time this has been requested so let's add it to the buffer to be loaded.
var parts = templateName.split('/');
var component = parts.shift();
var component = getNormalisedComponent(parts.shift());
var name = parts.join('/');
// Add this template to the buffer to be loaded.
@ -409,6 +426,7 @@ define([
Renderer.prototype.renderIcon = function(key, component, title) {
// Preload the module to do the icon rendering based on the theme iconsystem.
var modulename = config.iconsystemmodule;
component = getNormalisedComponent(component);
// RequireJS does not return a promise.
var ready = $.Deferred();
@ -425,7 +443,12 @@ define([
return ready.then(function(iconSystem) {
return this.getTemplate(iconSystem.getTemplateName());
}.bind(this)).then(function(template) {
return iconSystem.renderIcon(key, component, title, template);
return iconSystem.renderIcon(
key,
component,
title,
template
);
});
};
@ -456,15 +479,21 @@ define([
}
var templateName = iconSystem.getTemplateName();
var searchKey = this.currentThemeName + '/' + templateName;
var template = templateCache[searchKey];
component = getNormalisedComponent(component);
// The key might have been escaped by the JS Mustache engine which
// converts forward slashes to HTML entities. Let us undo that here.
key = key.replace(/&#x2F;/gi, '/');
return iconSystem.renderIcon(key, component, text, template);
return iconSystem.renderIcon(
key,
component,
text,
template
);
};
/**
@ -508,9 +537,7 @@ define([
param = parts.join(',').trim();
}
if (!component || component === 'moodle') {
component = 'core';
}
component = getNormalisedComponent(component);
if (param !== '') {
// Allow variable expansion in the param part only.
@ -522,7 +549,11 @@ define([
}
var index = this.requiredStrings.length;
this.requiredStrings.push({key: key, component: component, param: param});
this.requiredStrings.push({
key: key,
component: component,
param: param
});
// The placeholder must not use {{}} as those can be misinterpreted by the engine.
return '[[_s' + index + ']]';
@ -545,7 +576,7 @@ define([
// This involves wrapping {{, and }} in change delimeter tags.
content = content
.replace(/"/g, '\\"')
.replace(/([\{\}]{2,3})/g, '{{=<% %>=}}$1<%={{ }}=%>')
.replace(/([{}]{2,3})/g, '{{=<% %>=}}$1<%={{ }}=%>')
.replace(/(\r\n|\r|\n)/g, '&#x0a;')
;
return '"' + content + '"';
@ -1173,7 +1204,11 @@ define([
*/
renderPix: function(key, component, title) {
var renderer = new Renderer();
return renderer.renderIcon(key, component, title);
return renderer.renderIcon(
key,
getNormalisedComponent(component),
title
);
},
/**