Fix incorrect handlebars execution context in helpers (#645)
This commit is contained in:
parent
a16a8f53e6
commit
bc6d855f3d
@ -73,21 +73,28 @@ class TemplateRenderer {
|
|||||||
];
|
];
|
||||||
|
|
||||||
for (const [name, helper] of helpers) {
|
for (const [name, helper] of helpers) {
|
||||||
Handlebars.registerHelper(name, helper);
|
this._registerHelper(name, helper);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_registerHelper(name, helper) {
|
||||||
|
function wrapper(...args) {
|
||||||
|
return helper(this, ...args);
|
||||||
|
}
|
||||||
|
Handlebars.registerHelper(name, wrapper);
|
||||||
|
}
|
||||||
|
|
||||||
_escape(text) {
|
_escape(text) {
|
||||||
return Handlebars.Utils.escapeExpression(text);
|
return Handlebars.Utils.escapeExpression(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
_dumpObject(options) {
|
_dumpObject(context, options) {
|
||||||
const dump = JSON.stringify(options.fn(this), null, 4);
|
const dump = JSON.stringify(options.fn(context), null, 4);
|
||||||
return this._escape(dump);
|
return this._escape(dump);
|
||||||
}
|
}
|
||||||
|
|
||||||
_furigana(options) {
|
_furigana(context, options) {
|
||||||
const definition = options.fn(this);
|
const definition = options.fn(context);
|
||||||
const segs = jp.distributeFurigana(definition.expression, definition.reading);
|
const segs = jp.distributeFurigana(definition.expression, definition.reading);
|
||||||
|
|
||||||
let result = '';
|
let result = '';
|
||||||
@ -102,8 +109,8 @@ class TemplateRenderer {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
_furiganaPlain(options) {
|
_furiganaPlain(context, options) {
|
||||||
const definition = options.fn(this);
|
const definition = options.fn(context);
|
||||||
const segs = jp.distributeFurigana(definition.expression, definition.reading);
|
const segs = jp.distributeFurigana(definition.expression, definition.reading);
|
||||||
|
|
||||||
let result = '';
|
let result = '';
|
||||||
@ -118,9 +125,9 @@ class TemplateRenderer {
|
|||||||
return result.trimLeft();
|
return result.trimLeft();
|
||||||
}
|
}
|
||||||
|
|
||||||
_kanjiLinks(options) {
|
_kanjiLinks(context, options) {
|
||||||
let result = '';
|
let result = '';
|
||||||
for (const c of options.fn(this)) {
|
for (const c of options.fn(context)) {
|
||||||
if (jp.isCodePointKanji(c.codePointAt(0))) {
|
if (jp.isCodePointKanji(c.codePointAt(0))) {
|
||||||
result += `<a href="#" class="kanji-link">${c}</a>`;
|
result += `<a href="#" class="kanji-link">${c}</a>`;
|
||||||
} else {
|
} else {
|
||||||
@ -131,22 +138,22 @@ class TemplateRenderer {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
_multiLine(options) {
|
_multiLine(context, options) {
|
||||||
return options.fn(this).split('\n').join('<br>');
|
return options.fn(context).split('\n').join('<br>');
|
||||||
}
|
}
|
||||||
|
|
||||||
_sanitizeCssClass(options) {
|
_sanitizeCssClass(context, options) {
|
||||||
return options.fn(this).replace(/[^_a-z0-9\u00a0-\uffff]/ig, '_');
|
return options.fn(context).replace(/[^_a-z0-9\u00a0-\uffff]/ig, '_');
|
||||||
}
|
}
|
||||||
|
|
||||||
_regexReplace(...args) {
|
_regexReplace(context, ...args) {
|
||||||
// Usage:
|
// Usage:
|
||||||
// {{#regexReplace regex string [flags]}}content{{/regexReplace}}
|
// {{#regexReplace regex string [flags]}}content{{/regexReplace}}
|
||||||
// regex: regular expression string
|
// regex: regular expression string
|
||||||
// string: string to replace
|
// string: string to replace
|
||||||
// flags: optional flags for regular expression
|
// flags: optional flags for regular expression
|
||||||
// e.g. "i" for case-insensitive, "g" for replace all
|
// e.g. "i" for case-insensitive, "g" for replace all
|
||||||
let value = args[args.length - 1].fn(this);
|
let value = args[args.length - 1].fn(context);
|
||||||
if (args.length >= 3) {
|
if (args.length >= 3) {
|
||||||
try {
|
try {
|
||||||
const flags = args.length > 3 ? args[2] : 'g';
|
const flags = args.length > 3 ? args[2] : 'g';
|
||||||
@ -159,13 +166,13 @@ class TemplateRenderer {
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
_regexMatch(...args) {
|
_regexMatch(context, ...args) {
|
||||||
// Usage:
|
// Usage:
|
||||||
// {{#regexMatch regex [flags]}}content{{/regexMatch}}
|
// {{#regexMatch regex [flags]}}content{{/regexMatch}}
|
||||||
// regex: regular expression string
|
// regex: regular expression string
|
||||||
// flags: optional flags for regular expression
|
// flags: optional flags for regular expression
|
||||||
// e.g. "i" for case-insensitive, "g" for match all
|
// e.g. "i" for case-insensitive, "g" for match all
|
||||||
let value = args[args.length - 1].fn(this);
|
let value = args[args.length - 1].fn(context);
|
||||||
if (args.length >= 2) {
|
if (args.length >= 2) {
|
||||||
try {
|
try {
|
||||||
const flags = args.length > 2 ? args[1] : '';
|
const flags = args.length > 2 ? args[1] : '';
|
||||||
@ -180,7 +187,7 @@ class TemplateRenderer {
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
_mergeTags(object, isGroupMode, isMergeMode) {
|
_mergeTags(context, object, isGroupMode, isMergeMode) {
|
||||||
const tagSources = [];
|
const tagSources = [];
|
||||||
if (isGroupMode || isMergeMode) {
|
if (isGroupMode || isMergeMode) {
|
||||||
for (const definition of object.definitions) {
|
for (const definition of object.definitions) {
|
||||||
|
Loading…
Reference in New Issue
Block a user