FIX: Safari strict mode errors

This commit is contained in:
Robin Ward 2016-07-13 15:36:34 -04:00
parent 29c9979b9b
commit 00e45c0d3c
3 changed files with 84 additions and 73 deletions

View File

@ -14,7 +14,7 @@ const RawHandlebars = Handlebars.create();
RawHandlebars.helper = function() {};
RawHandlebars.helpers = objectCreate(Handlebars.helpers);
RawHandlebars.helpers.get = function(context, options){
RawHandlebars.helpers['get'] = function(context, options) {
var firstContext = options.contexts[0];
var val = firstContext[context];
@ -38,7 +38,7 @@ function stringCompatHelper(fn) {
RawHandlebars.registerHelper('each', function(localName,inKeyword,contextName,options){
var list = Em.get(this, contextName);
var output = [];
var innerContext = Object.create(this);
var innerContext = objectCreate(this);
for (var i=0; i<list.length; i++) {
innerContext[localName] = list[i];
output.push(options.fn(innerContext));
@ -51,6 +51,49 @@ stringCompatHelper("unless");
stringCompatHelper("with");
function buildPath(blk, args) {
var result = { type: "PathExpression",
data: false,
depth: blk.path.depth,
loc: blk.path.loc };
// Server side precompile doesn't have jquery.extend
Object.keys(args).forEach(function (a) {
result[a] = args[a];
});
return result;
}
function replaceGet(ast) {
var visitor = new Handlebars.Visitor();
visitor.mutating = true;
visitor.MustacheStatement = function(mustache) {
if (!(mustache.params.length || mustache.hash)) {
mustache.params[0] = mustache.path;
mustache.path = buildPath(mustache, { parts: ['get'], original: 'get', strict: true, falsy: true });
}
return Handlebars.Visitor.prototype.MustacheStatement.call(this, mustache);
};
// rewrite `each x as |y|` as each y in x`
// This allows us to use the same syntax in all templates
visitor.BlockStatement = function(block) {
if (block.path.original === 'each' && block.params.length === 1) {
var paramName = block.program.blockParams[0];
block.params = [ buildPath(block, { original: paramName }),
{ type: "CommentStatement", value: "in" },
block.params[0] ];
delete block.program.blockParams;
}
return Handlebars.Visitor.prototype.BlockStatement.call(this, block);
};
visitor.accept(ast);
}
if (Handlebars.Compiler) {
RawHandlebars.Compiler = function() {};
RawHandlebars.Compiler.prototype = objectCreate(Handlebars.Compiler.prototype);
@ -62,50 +105,6 @@ if (Handlebars.Compiler) {
RawHandlebars.JavaScriptCompiler.prototype.compiler = RawHandlebars.JavaScriptCompiler;
RawHandlebars.JavaScriptCompiler.prototype.namespace = "RawHandlebars";
function buildPath(blk, args) {
var result = { type: "PathExpression",
data: false,
depth: blk.path.depth,
loc: blk.path.loc };
// Server side precompile doesn't have jquery.extend
Object.keys(args).forEach(function (a) {
result[a] = args[a];
});
return result;
}
function replaceGet(ast) {
var visitor = new Handlebars.Visitor();
visitor.mutating = true;
visitor.MustacheStatement = function(mustache) {
if (!(mustache.params.length || mustache.hash)) {
mustache.params[0] = mustache.path;
mustache.path = buildPath(mustache, { parts: ['get'], original: 'get', strict: true, falsy: true });
}
return Handlebars.Visitor.prototype.MustacheStatement.call(this, mustache);
};
// rewrite `each x as |y|` as each y in x`
// This allows us to use the same syntax in all templates
visitor.BlockStatement = function(block) {
if (block.path.original === 'each' && block.params.length === 1) {
var paramName = block.program.blockParams[0];
block.params = [ buildPath(block, { original: paramName }),
{ type: "CommentStatement", value: "in" },
block.params[0] ];
delete block.program.blockParams;
}
return Handlebars.Visitor.prototype.BlockStatement.call(this, block);
};
visitor.accept(ast);
}
RawHandlebars.precompile = function(value, asObject) {
var ast = Handlebars.parse(value);
replaceGet(ast);

View File

@ -399,7 +399,7 @@ export function cook(raw, opts) {
if (keys.length) {
let found = true;
function unhoist(key) {
const unhoist = function(key) {
result = result.replace(new RegExp(key, "g"), function() {
found = true;
return hoisted[key];

View File

@ -45,10 +45,7 @@ export function register(helper, codeName, args, emitter) {
});
};
export function setup(helper) {
helper.whiteList(['span.bbcode-b', 'span.bbcode-i', 'span.bbcode-u', 'span.bbcode-s']);
export function builders(helper) {
function replaceBBCode(tag, emitter, opts) {
const start = `[${tag}]`;
const stop = `[/${tag}]`;
@ -59,34 +56,49 @@ export function setup(helper) {
opts = _.merge(opts, { start: start.toUpperCase(), stop: stop.toUpperCase(), emitter });
helper.inlineBetween(opts);
};
function rawBBCode(tag, emitter) {
replaceBBCode(tag, emitter, { rawContents: true });
}
function removeEmptyLines(contents) {
const result = [];
for (let i=0; i < contents.length; i++) {
if (contents[i] !== "\n") { result.push(contents[i]); }
}
return result;
}
return {
replaceBBCode,
function replaceBBCodeParamsRaw(tag, emitter) {
var opts = {
rawContents: true,
emitter(contents) {
const m = /^([^\]]+)\]([\S\s]*)$/.exec(contents);
if (m) { return emitter.call(this, m[1], m[2]); }
register(codeName, args, emitter) {
register(helper, codeName, args, emitter);
},
rawBBCode(tag, emitter) {
replaceBBCode(tag, emitter, { rawContents: true });
},
removeEmptyLines(contents) {
const result = [];
for (let i=0; i < contents.length; i++) {
if (contents[i] !== "\n") { result.push(contents[i]); }
}
};
return result;
},
helper.inlineBetween(_.merge(opts, { start: "[" + tag + "=", stop: "[/" + tag + "]" }));
replaceBBCodeParamsRaw(tag, emitter) {
var opts = {
rawContents: true,
emitter(contents) {
const m = /^([^\]]+)\]([\S\s]*)$/.exec(contents);
if (m) { return emitter.call(this, m[1], m[2]); }
}
};
tag = tag.toUpperCase();
helper.inlineBetween(_.merge(opts, { start: "[" + tag + "=", stop: "[/" + tag + "]" }));
}
helper.inlineBetween(_.merge(opts, { start: "[" + tag + "=", stop: "[/" + tag + "]" }));
tag = tag.toUpperCase();
helper.inlineBetween(_.merge(opts, { start: "[" + tag + "=", stop: "[/" + tag + "]" }));
}
};
}
export function setup(helper) {
helper.whiteList(['span.bbcode-b', 'span.bbcode-i', 'span.bbcode-u', 'span.bbcode-s']);
const { replaceBBCode, rawBBCode, removeEmptyLines, replaceBBCodeParamsRaw } = builders(helper);
replaceBBCode('b', contents => ['span', {'class': 'bbcode-b'}].concat(contents));
replaceBBCode('i', contents => ['span', {'class': 'bbcode-i'}].concat(contents));