mirror of
https://github.com/harttle/liquidjs.git
synced 2026-10-03 00:55:13 -07:00
fix read/write variable in parent's scope, fix #39
This commit is contained in:
+12
-1
@@ -20,7 +20,8 @@ var Scope = {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
set: function (k, v) {
|
set: function (k, v) {
|
||||||
setPropertyByPath(this.scopes[this.scopes.length - 1], k, v)
|
var scope = this.findScopeFor(k)
|
||||||
|
setPropertyByPath(scope, k, v)
|
||||||
return this
|
return this
|
||||||
},
|
},
|
||||||
push: function (ctx) {
|
push: function (ctx) {
|
||||||
@@ -30,6 +31,16 @@ var Scope = {
|
|||||||
pop: function () {
|
pop: function () {
|
||||||
return this.scopes.pop()
|
return this.scopes.pop()
|
||||||
},
|
},
|
||||||
|
findScopeFor: function (key) {
|
||||||
|
var i = this.scopes.length - 1
|
||||||
|
while (i >= 0 && !(key in this.scopes[i])) {
|
||||||
|
i--
|
||||||
|
}
|
||||||
|
if (i < 0) {
|
||||||
|
i = this.scopes.length - 1
|
||||||
|
}
|
||||||
|
return this.scopes[i]
|
||||||
|
},
|
||||||
unshift: function (ctx) {
|
unshift: function (ctx) {
|
||||||
assert(ctx, `trying to push ${ctx} into scopes`)
|
assert(ctx, `trying to push ${ctx} into scopes`)
|
||||||
return this.scopes.unshift(ctx)
|
return this.scopes.unshift(ctx)
|
||||||
|
|||||||
+19
-21
@@ -1,22 +1,20 @@
|
|||||||
const Liquid = require('..');
|
const Liquid = require('..')
|
||||||
const lexical = Liquid.lexical;
|
const lexical = Liquid.lexical
|
||||||
const Promise = require('any-promise');
|
const Promise = require('any-promise')
|
||||||
const re = new RegExp(`(${lexical.identifier.source})\\s*=(.*)`);
|
const re = new RegExp(`(${lexical.identifier.source})\\s*=(.*)`)
|
||||||
const assert = require('../src/util/assert.js');
|
const assert = require('../src/util/assert.js')
|
||||||
|
|
||||||
module.exports = function(liquid) {
|
module.exports = function (liquid) {
|
||||||
|
liquid.registerTag('assign', {
|
||||||
liquid.registerTag('assign', {
|
parse: function (token) {
|
||||||
parse: function(token){
|
var match = token.args.match(re)
|
||||||
var match = token.args.match(re);
|
assert(match, `illegal token ${token.raw}`)
|
||||||
assert(match, `illegal token ${token.raw}`);
|
this.key = match[1]
|
||||||
this.key = match[1];
|
this.value = match[2]
|
||||||
this.value = match[2];
|
},
|
||||||
},
|
render: function (scope) {
|
||||||
render: function(scope) {
|
scope.set(this.key, liquid.evalOutput(this.value, scope))
|
||||||
scope.set(this.key, liquid.evalOutput(this.value, scope));
|
return Promise.resolve('')
|
||||||
return Promise.resolve('');
|
}
|
||||||
}
|
})
|
||||||
});
|
}
|
||||||
|
|
||||||
};
|
|
||||||
|
|||||||
+3
-3
@@ -74,9 +74,9 @@ module.exports = function (liquid) {
|
|||||||
|
|
||||||
var html = ''
|
var html = ''
|
||||||
return mapSeries(contexts, (context) => {
|
return mapSeries(contexts, (context) => {
|
||||||
scope.push(context)
|
return Promise.resolve()
|
||||||
return liquid.renderer
|
.then(() => scope.push(context))
|
||||||
.renderTemplates(this.templates, scope)
|
.then(() => liquid.renderer.renderTemplates(this.templates, scope))
|
||||||
.then(partial => (html += partial))
|
.then(partial => (html += partial))
|
||||||
.catch(e => {
|
.catch(e => {
|
||||||
if (e instanceof RenderBreakError) {
|
if (e instanceof RenderBreakError) {
|
||||||
|
|||||||
+10
-3
@@ -116,9 +116,16 @@ describe('scope', function () {
|
|||||||
expect(scope.get('posts[category.diary[0]].name'), 'A Nice Day')
|
expect(scope.get('posts[category.diary[0]].name'), 'A Nice Day')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should create parents if needed', function () {
|
it('should create in parent scope if needed', function () {
|
||||||
scope.set('foo.bar.coo', 'COO')
|
scope.push({})
|
||||||
expect(scope.get('foo.bar.coo'), 'COO')
|
scope.set('bar.coo', 'COO')
|
||||||
|
expect(scope.get('bar.coo')).to.equal('COO')
|
||||||
|
})
|
||||||
|
it("should set parents' corresponding value", function () {
|
||||||
|
scope.push({})
|
||||||
|
scope.set('foo', 'bar')
|
||||||
|
scope.pop()
|
||||||
|
expect(scope.get('foo')).to.equal('bar')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
describe('strict_variables', function () {
|
describe('strict_variables', function () {
|
||||||
|
|||||||
@@ -33,6 +33,19 @@ describe('tags/for', function () {
|
|||||||
.to.eventually.equal('foo-coo-')
|
.to.eventually.equal('foo-coo-')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('scope', function () {
|
||||||
|
it('should read super scope', function () {
|
||||||
|
var src = '{%for a in (1..2)%}{{num}}{%endfor%}'
|
||||||
|
return expect(liquid.parseAndRender(src, {num: 1}))
|
||||||
|
.to.eventually.equal('11')
|
||||||
|
})
|
||||||
|
it('should write super scope', function () {
|
||||||
|
var src = '{%for a in (1..2)%}{{num}}{%assign num = 2%}{%endfor%}'
|
||||||
|
return expect(liquid.parseAndRender(src, {num: 1}))
|
||||||
|
.to.eventually.equal('12')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
describe('illegal', function () {
|
describe('illegal', function () {
|
||||||
it('should reject when for not closed', function () {
|
it('should reject when for not closed', function () {
|
||||||
var src = '{%for c in alpha%}{{c}}'
|
var src = '{%for c in alpha%}{{c}}'
|
||||||
|
|||||||
Reference in New Issue
Block a user