feature: whitespace control, fixes #17

This commit is contained in:
harttle
2016-12-26 23:23:58 +08:00
parent fbb1dc8a36
commit 564d9cd9f4
6 changed files with 113 additions and 58 deletions
+89 -46
View File
@@ -1,57 +1,100 @@
var chai = require("chai");
var should = chai.should();
var expect = chai.expect;
const chai = require("chai");
const parse = require('../src/tokenizer.js').parse;
const whiteSpaceCtrl = require('../src/tokenizer.js').whiteSpaceCtrl;
var tokenizer = require('../src/tokenizer.js');
const should = chai.should();
const expect = chai.expect;
describe('tokenizer', function() {
it('should handle plain HTML', function() {
var html = '<html><body><p>Lorem Ipsum</p></body></html>';
var tokens = tokenizer.parse(html);
describe('parse', function() {
it('should handle plain HTML', function() {
var html = '<html><body><p>Lorem Ipsum</p></body></html>';
var tokens = parse(html);
tokens.length.should.equal(1);
tokens[0].value.should.equal(html);
tokens[0].type.should.equal('html');
});
it('should throw when non-string passed in', function() {
expect(function() {
tokenizer.parse({});
}).to.throw('illegal input type');
});
it('should handle tag syntax', function() {
var html = '<p>{% for p in a[1]%}</p>';
var tokens = tokenizer.parse(html);
tokens.length.should.equal(1);
tokens[0].value.should.equal(html);
tokens[0].type.should.equal('html');
});
it('should throw when non-string passed in', function() {
expect(function() {
parse({});
}).to.throw('illegal input type');
});
it('should handle tag syntax', function() {
var html = '<p>{% for p in a[1]%}</p>';
var tokens = parse(html);
tokens.length.should.equal(3);
tokens[1].type.should.equal('tag');
tokens[1].value.should.equal('for p in a[1]');
});
it('should handle output syntax', function() {
var html = '<p>{{foo | date: "%Y-%m-%d"}}</p>';
var tokens = tokenizer.parse(html);
tokens.length.should.equal(3);
tokens[1].type.should.equal('tag');
tokens[1].value.should.equal('for p in a[1]');
});
it('should handle output syntax', function() {
var html = '<p>{{foo | date: "%Y-%m-%d"}}</p>';
var tokens = parse(html);
tokens.length.should.equal(3);
tokens[1].type.should.equal('output');
tokens[1].value.should.equal('foo | date: "%Y-%m-%d"');
});
it('should handle successive outputs and tags', function() {
var html = '{{foo}}{{bar}}{%foo%}{%bar%}';
var tokens = tokenizer.parse(html);
tokens.length.should.equal(3);
tokens[1].type.should.equal('output');
tokens[1].value.should.equal('foo | date: "%Y-%m-%d"');
});
it('should handle successive outputs and tags', function() {
var html = '{{foo}}{{bar}}{%foo%}{%bar%}';
var tokens = parse(html);
tokens.length.should.equal(4);
tokens[0].type.should.equal('output');
tokens[3].type.should.equal('tag');
tokens.length.should.equal(4);
tokens[0].type.should.equal('output');
tokens[3].type.should.equal('tag');
tokens[1].value.should.equal('bar');
tokens[2].value.should.equal('foo');
tokens[1].value.should.equal('bar');
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() {
var html = '{{foo}}\n{%bar %} \n {{alice}}';
var tokens = tokenizer.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 ');
describe('whitespace control', function() {
it('should strip left whitespaces', function() {
expect(whiteSpaceCtrl(' {{- foo }}')).to.equal('{{- foo }}');
});
it('should strip right whitespaces', function() {
expect(whiteSpaceCtrl('{{ foo -}} ')).to.equal('{{ foo -}}');
});
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
View File
@@ -233,7 +233,7 @@ describe('error', function() {
.be.rejected
.then(function(err) {
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
.then(function(err) {
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+\)$/);
});
});