mirror of
https://github.com/harttle/liquidjs.git
synced 2026-10-03 09:05:14 -07:00
fix: increment/decrement independent from capture, #76
This commit is contained in:
+5
-1
@@ -1,7 +1,9 @@
|
|||||||
|
'use strict'
|
||||||
const Liquid = require('..')
|
const Liquid = require('..')
|
||||||
const lexical = Liquid.lexical
|
const lexical = Liquid.lexical
|
||||||
const re = new RegExp(`(${lexical.identifier.source})`)
|
const re = new RegExp(`(${lexical.identifier.source})`)
|
||||||
const assert = require('../src/util/assert.js')
|
const assert = require('../src/util/assert.js')
|
||||||
|
const types = require('../src/scope.js').types
|
||||||
|
|
||||||
module.exports = function (liquid) {
|
module.exports = function (liquid) {
|
||||||
liquid.registerTag('capture', {
|
liquid.registerTag('capture', {
|
||||||
@@ -23,7 +25,9 @@ module.exports = function (liquid) {
|
|||||||
render: function (scope, hash) {
|
render: function (scope, hash) {
|
||||||
return liquid.renderer.renderTemplates(this.templates, scope)
|
return liquid.renderer.renderTemplates(this.templates, scope)
|
||||||
.then((html) => {
|
.then((html) => {
|
||||||
scope.set(this.variable, html)
|
let ctx = Object.create(types.CaptureScope)
|
||||||
|
ctx[this.variable] = html
|
||||||
|
scope.push(ctx)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
+15
-4
@@ -1,25 +1,36 @@
|
|||||||
|
'use strict'
|
||||||
const Liquid = require('../..')
|
const Liquid = require('../..')
|
||||||
const chai = require('chai')
|
const chai = require('chai')
|
||||||
const expect = chai.expect
|
const expect = chai.expect
|
||||||
chai.use(require('chai-as-promised'))
|
chai.use(require('chai-as-promised'))
|
||||||
|
|
||||||
describe('tags/capture', function () {
|
describe('tags/capture', function () {
|
||||||
var liquid = Liquid()
|
let liquid = Liquid()
|
||||||
|
|
||||||
it('should support capture', function () {
|
it('should support capture', function () {
|
||||||
var src = '{% capture f %}{{"a" | capitalize}}{%endcapture%}{{f}}'
|
let src = '{% capture f %}{{"a" | capitalize}}{%endcapture%}{{f}}'
|
||||||
return expect(liquid.parseAndRender(src))
|
return expect(liquid.parseAndRender(src))
|
||||||
.to.eventually.equal('A')
|
.to.eventually.equal('A')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should shading rather than overwriting', function () {
|
||||||
|
let src = '{% capture var %}10{% endcapture %}{{var}}'
|
||||||
|
let ctx = {'var': 20}
|
||||||
|
return liquid.parseAndRender(src, ctx)
|
||||||
|
.then(x => {
|
||||||
|
expect(x).to.equal('10')
|
||||||
|
expect(ctx.var).to.equal(20)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
it('should throw on invalid identifier', function () {
|
it('should throw on invalid identifier', function () {
|
||||||
var src = '{% capture = %}{%endcapture%}'
|
let src = '{% capture = %}{%endcapture%}'
|
||||||
return expect(liquid.parseAndRender(src))
|
return expect(liquid.parseAndRender(src))
|
||||||
.to.be.rejectedWith(/= not valid identifier/)
|
.to.be.rejectedWith(/= not valid identifier/)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should throw when capture not closed', function () {
|
it('should throw when capture not closed', function () {
|
||||||
var src = '{%capture c%}{{c}}'
|
let src = '{%capture c%}{{c}}'
|
||||||
return expect(liquid.parseAndRender(src))
|
return expect(liquid.parseAndRender(src))
|
||||||
.to.be.rejectedWith(/tag .* not closed/)
|
.to.be.rejectedWith(/tag .* not closed/)
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -34,9 +34,27 @@ describe('tags/decrement', function () {
|
|||||||
.to.eventually.equal('-1-2-3')
|
.to.eventually.equal('-1-2-3')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should be independent from capture', function () {
|
||||||
|
let src = '{% capture var %}10{% endcapture %}{% decrement var %}{% decrement var %}{% decrement var %}'
|
||||||
|
return expect(liquid.parseAndRender(src))
|
||||||
|
.to.eventually.equal('-1-2-3')
|
||||||
|
})
|
||||||
|
|
||||||
it('should not shading assign', function () {
|
it('should not shading assign', function () {
|
||||||
let src = '{% assign var=10 %}{% decrement var %}{% decrement var %}{% decrement var %} {{var}}'
|
let src = '{% assign var=10 %}{% decrement var %}{% decrement var %}{% decrement var %} {{var}}'
|
||||||
return expect(liquid.parseAndRender(src))
|
return expect(liquid.parseAndRender(src))
|
||||||
.to.eventually.equal('-1-2-3 10')
|
.to.eventually.equal('-1-2-3 10')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should not shading capture', function () {
|
||||||
|
let src = '{% capture var %}10{% endcapture %}{% decrement var %}{% decrement var %}{% decrement var %} {{var}}'
|
||||||
|
return expect(liquid.parseAndRender(src))
|
||||||
|
.to.eventually.equal('-1-2-3 10')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should share the same variable with increment', function () {
|
||||||
|
let src = '{%increment var%}{%increment var%}{%decrement var%}{%decrement var%}{%increment var%}'
|
||||||
|
return expect(liquid.parseAndRender(src))
|
||||||
|
.to.eventually.equal('01100')
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -29,9 +29,21 @@ describe('tags/increment', function () {
|
|||||||
.to.eventually.equal('012')
|
.to.eventually.equal('012')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should be independent from capture', function () {
|
||||||
|
let src = '{% capture var %}10{% endcapture %}{% increment var %}{% increment var %}{% increment var %}'
|
||||||
|
return expect(liquid.parseAndRender(src))
|
||||||
|
.to.eventually.equal('012')
|
||||||
|
})
|
||||||
|
|
||||||
it('should not shading assign', function () {
|
it('should not shading assign', function () {
|
||||||
let src = '{% assign var=10 %}{% increment var %}{% increment var %}{% increment var %} {{var}}'
|
let src = '{% assign var=10 %}{% increment var %}{% increment var %}{% increment var %} {{var}}'
|
||||||
return expect(liquid.parseAndRender(src))
|
return expect(liquid.parseAndRender(src))
|
||||||
.to.eventually.equal('012 10')
|
.to.eventually.equal('012 10')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should not shading capture', function () {
|
||||||
|
let src = '{% capture var %}10{% endcapture %}{% increment var %}{% increment var %}{% increment var %} {{var}}'
|
||||||
|
return expect(liquid.parseAndRender(src))
|
||||||
|
.to.eventually.equal('012 10')
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user