mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-20 06:50:47 -07:00
feature: whitespace control, fixes #17
This commit is contained in:
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
language: node_js
|
language: node_js
|
||||||
node_js:
|
node_js:
|
||||||
- "4"
|
- "5"
|
||||||
before_script:
|
before_script:
|
||||||
- npm install -g mocha
|
- npm install -g mocha
|
||||||
after_script:
|
after_script:
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ var _engine = {
|
|||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
parse: function(html, filepath) {
|
parse: function(html, filepath) {
|
||||||
var tokens = tokenizer.parse(html, filepath);
|
var tokens = tokenizer.parse(html, filepath, this.options);
|
||||||
return this.parser.parse(tokens);
|
return this.parser.parse(tokens);
|
||||||
},
|
},
|
||||||
render: function(tpl, ctx, opts) {
|
render: function(tpl, ctx, opts) {
|
||||||
@@ -115,14 +115,16 @@ var _engine = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
function factory(options) {
|
function factory(options) {
|
||||||
options = _.assign({}, options);
|
options = _.assign({
|
||||||
|
root: ['.'],
|
||||||
|
cache: false,
|
||||||
|
extname: '.liquid',
|
||||||
|
trim_right: false,
|
||||||
|
trim_left: false
|
||||||
|
}, options);
|
||||||
options.root = normalizeStringArray(options.root);
|
options.root = normalizeStringArray(options.root);
|
||||||
if (!options.root.length) options.root = ['.'];
|
|
||||||
|
|
||||||
options.extname = options.extname || '.liquid';
|
|
||||||
|
|
||||||
var engine = Object.create(_engine);
|
var engine = Object.create(_engine);
|
||||||
|
|
||||||
engine.init(Tag(), Filter(options), options);
|
engine.init(Tag(), Filter(options), options);
|
||||||
return engine;
|
return engine;
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -35,7 +35,7 @@
|
|||||||
"babelify": "^7.3.0",
|
"babelify": "^7.3.0",
|
||||||
"browserify": "^13.1.0",
|
"browserify": "^13.1.0",
|
||||||
"chai": "^3.5.0",
|
"chai": "^3.5.0",
|
||||||
"chai-as-promised": "^5.3.0",
|
"chai-as-promised": "^6.0.0",
|
||||||
"coveralls": "^2.11.9",
|
"coveralls": "^2.11.9",
|
||||||
"express": "^4.14.0",
|
"express": "^4.14.0",
|
||||||
"istanbul": "^0.4.3",
|
"istanbul": "^0.4.3",
|
||||||
|
|||||||
+12
-2
@@ -3,11 +3,13 @@ const TokenizationError = require('./util/error.js').TokenizationError;
|
|||||||
const _ = require('./util/underscore.js');
|
const _ = require('./util/underscore.js');
|
||||||
const assert = require('../src/util/assert.js');
|
const assert = require('../src/util/assert.js');
|
||||||
|
|
||||||
function parse(html, filepath) {
|
function parse(html, filepath, options) {
|
||||||
assert(_.isString(html), 'illegal input type');
|
assert(_.isString(html), 'illegal input type');
|
||||||
|
|
||||||
|
html = whiteSpaceCtrl(html, options);
|
||||||
|
|
||||||
var tokens = [];
|
var tokens = [];
|
||||||
var syntax = /({%(.*?)%})|({{(.*?)}})/g;
|
var syntax = /({%-?(.*?)-?%})|({{-?(.*?)-?}})/g;
|
||||||
var result, htmlFragment, token;
|
var result, htmlFragment, token;
|
||||||
var lastMatchEnd = 0, lastMatchBegin = -1, parsedLinesCount = 0;
|
var lastMatchEnd = 0, lastMatchBegin = -1, parsedLinesCount = 0;
|
||||||
|
|
||||||
@@ -71,4 +73,12 @@ function parse(html, filepath) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function whiteSpaceCtrl(html, options){
|
||||||
|
options = options || {};
|
||||||
|
var rLeft = options.trim_left ? /\s+({[{%])/g : /\s+({[{%]-)/g;
|
||||||
|
var rRight = options.trim_right ? /([}%]})\s+/g : /(-[}%]})\s+/g;
|
||||||
|
return html.replace(rLeft, '$1').replace(rRight, '$1');
|
||||||
|
}
|
||||||
|
|
||||||
exports.parse = parse;
|
exports.parse = parse;
|
||||||
|
exports.whiteSpaceCtrl = whiteSpaceCtrl;
|
||||||
|
|||||||
+89
-46
@@ -1,57 +1,100 @@
|
|||||||
var chai = require("chai");
|
const chai = require("chai");
|
||||||
var should = chai.should();
|
const parse = require('../src/tokenizer.js').parse;
|
||||||
var expect = chai.expect;
|
const whiteSpaceCtrl = require('../src/tokenizer.js').whiteSpaceCtrl;
|
||||||
|
|
||||||
var tokenizer = require('../src/tokenizer.js');
|
const should = chai.should();
|
||||||
|
const expect = chai.expect;
|
||||||
|
|
||||||
describe('tokenizer', function() {
|
describe('tokenizer', function() {
|
||||||
it('should handle plain HTML', function() {
|
describe('parse', function() {
|
||||||
var html = '<html><body><p>Lorem Ipsum</p></body></html>';
|
it('should handle plain HTML', function() {
|
||||||
var tokens = tokenizer.parse(html);
|
var html = '<html><body><p>Lorem Ipsum</p></body></html>';
|
||||||
|
var tokens = parse(html);
|
||||||
|
|
||||||
tokens.length.should.equal(1);
|
tokens.length.should.equal(1);
|
||||||
tokens[0].value.should.equal(html);
|
tokens[0].value.should.equal(html);
|
||||||
tokens[0].type.should.equal('html');
|
tokens[0].type.should.equal('html');
|
||||||
});
|
});
|
||||||
it('should throw when non-string passed in', function() {
|
it('should throw when non-string passed in', function() {
|
||||||
expect(function() {
|
expect(function() {
|
||||||
tokenizer.parse({});
|
parse({});
|
||||||
}).to.throw('illegal input type');
|
}).to.throw('illegal input type');
|
||||||
});
|
});
|
||||||
it('should handle tag syntax', function() {
|
it('should handle tag syntax', function() {
|
||||||
var html = '<p>{% for p in a[1]%}</p>';
|
var html = '<p>{% for p in a[1]%}</p>';
|
||||||
var tokens = tokenizer.parse(html);
|
var tokens = parse(html);
|
||||||
|
|
||||||
tokens.length.should.equal(3);
|
tokens.length.should.equal(3);
|
||||||
tokens[1].type.should.equal('tag');
|
tokens[1].type.should.equal('tag');
|
||||||
tokens[1].value.should.equal('for p in a[1]');
|
tokens[1].value.should.equal('for p in a[1]');
|
||||||
});
|
});
|
||||||
it('should handle output syntax', function() {
|
it('should handle output syntax', function() {
|
||||||
var html = '<p>{{foo | date: "%Y-%m-%d"}}</p>';
|
var html = '<p>{{foo | date: "%Y-%m-%d"}}</p>';
|
||||||
var tokens = tokenizer.parse(html);
|
var tokens = parse(html);
|
||||||
|
|
||||||
tokens.length.should.equal(3);
|
tokens.length.should.equal(3);
|
||||||
tokens[1].type.should.equal('output');
|
tokens[1].type.should.equal('output');
|
||||||
tokens[1].value.should.equal('foo | date: "%Y-%m-%d"');
|
tokens[1].value.should.equal('foo | date: "%Y-%m-%d"');
|
||||||
});
|
});
|
||||||
it('should handle successive outputs and tags', function() {
|
it('should handle successive outputs and tags', function() {
|
||||||
var html = '{{foo}}{{bar}}{%foo%}{%bar%}';
|
var html = '{{foo}}{{bar}}{%foo%}{%bar%}';
|
||||||
var tokens = tokenizer.parse(html);
|
var tokens = parse(html);
|
||||||
|
|
||||||
tokens.length.should.equal(4);
|
tokens.length.should.equal(4);
|
||||||
tokens[0].type.should.equal('output');
|
tokens[0].type.should.equal('output');
|
||||||
tokens[3].type.should.equal('tag');
|
tokens[3].type.should.equal('tag');
|
||||||
|
|
||||||
tokens[1].value.should.equal('bar');
|
tokens[1].value.should.equal('bar');
|
||||||
tokens[2].value.should.equal('foo');
|
tokens[2].value.should.equal('foo');
|
||||||
|
});
|
||||||
|
it('should keep white spaces and newlines', function() {
|
||||||
|
var html = '{{foo}}\n{%bar %} \n {{alice}}';
|
||||||
|
var tokens = parse(html);
|
||||||
|
expect(tokens.length).to.equal(5);
|
||||||
|
expect(tokens[1].type).to.equal('html');
|
||||||
|
expect(tokens[1].raw).to.equal('\n');
|
||||||
|
expect(tokens[3].type).to.equal('html');
|
||||||
|
expect(tokens[3].raw).to.equal(' \n ');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
it('should keep white spaces and newlines', function() {
|
describe('whitespace control', function() {
|
||||||
var html = '{{foo}}\n{%bar %} \n {{alice}}';
|
it('should strip left whitespaces', function() {
|
||||||
var tokens = tokenizer.parse(html);
|
expect(whiteSpaceCtrl(' {{- foo }}')).to.equal('{{- foo }}');
|
||||||
expect(tokens.length).to.equal(5);
|
});
|
||||||
expect(tokens[1].type).to.equal('html');
|
it('should strip right whitespaces', function() {
|
||||||
expect(tokens[1].raw).to.equal('\n');
|
expect(whiteSpaceCtrl('{{ foo -}} ')).to.equal('{{ foo -}}');
|
||||||
expect(tokens[3].type).to.equal('html');
|
});
|
||||||
expect(tokens[3].raw).to.equal(' \n ');
|
it('should not strip left when not specified', function() {
|
||||||
|
expect(whiteSpaceCtrl(' {%foo-%} ')).to.equal(' {%foo-%}');
|
||||||
|
});
|
||||||
|
it('should not strip right when not specified', function() {
|
||||||
|
expect(whiteSpaceCtrl(' {{-foo}} ')).to.equal('{{-foo}} ');
|
||||||
|
});
|
||||||
|
it('should strip all blank characters', function() {
|
||||||
|
expect(whiteSpaceCtrl('\t\r{{-foo-}}\n \n')).to.equal('{{-foo-}}');
|
||||||
|
});
|
||||||
|
it('should stop stripping when encountered normal chars', () =>
|
||||||
|
expect(whiteSpaceCtrl('\ta\r{{-foo-}} b ')).to.equal('\ta{{-foo-}}b '));
|
||||||
|
it('should strip whitespaces when set trim_left', function() {
|
||||||
|
expect(whiteSpaceCtrl(' {{foo}} ', {
|
||||||
|
trim_left: true
|
||||||
|
})).to.equal('{{foo}} ');
|
||||||
|
});
|
||||||
|
it('should strip whitespaces when set trim_right', function() {
|
||||||
|
expect(whiteSpaceCtrl(' {{foo}} ', {
|
||||||
|
trim_right: true
|
||||||
|
})).to.equal(' {{foo}}');
|
||||||
|
});
|
||||||
|
it('markup should has priority over options', function() {
|
||||||
|
expect(whiteSpaceCtrl(' {{-foo}} ', {
|
||||||
|
trim_left: false
|
||||||
|
})).to.equal('{{-foo}} ');
|
||||||
|
});
|
||||||
|
it('should support a mix of markup and options', function() {
|
||||||
|
expect(whiteSpaceCtrl(' {%-foo%} ', {
|
||||||
|
trim_left: true,
|
||||||
|
trim_right: true
|
||||||
|
})).to.equal('{%-foo%}');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
+2
-2
@@ -233,7 +233,7 @@ describe('error', function() {
|
|||||||
.be.rejected
|
.be.rejected
|
||||||
.then(function(err) {
|
.then(function(err) {
|
||||||
expect(err.stack).to.contain('intended render reject');
|
expect(err.stack).to.contain('intended render reject');
|
||||||
expect(err.stack).to.contain('at Object.engine.registerTag.render');
|
expect(err.stack).to.match(/at .*:\d+:\d+\)/);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -365,7 +365,7 @@ describe('error', function() {
|
|||||||
.be.rejected
|
.be.rejected
|
||||||
.then(function(err) {
|
.then(function(err) {
|
||||||
expect(err.stack).to.contain('AssertionError: tag -a not found');
|
expect(err.stack).to.contain('AssertionError: tag -a not found');
|
||||||
expect(err.stack).to.contain('at Object._tagInstance.parse');
|
expect(err.stack).to.match(/at .*:\d+:\d+\)$/);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user