mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-15 12:20:40 -07:00
do not trim output tags and previous \n, #19
This commit is contained in:
@@ -84,11 +84,11 @@ Defaults to `["."]`
|
||||
If set to `false`, undefined variables will be rendered as empty string.
|
||||
Otherwise, undefined variables will cause an exception. Defaults to `false`.
|
||||
|
||||
* `trim_right` is used to strip blank characters (including whitespace, tabs, NL) from the right of tags (`{% %}`) and output markups (`{{ }}`) until `\n` (mandatory and inclusive). Defaults to `false`.
|
||||
* `trim_right` is used to strip blank characters (including ` `, `\t`, and `\r`) from the right of tags (`{% %}`) until `\n` (inclusive). Defaults to `false`.
|
||||
|
||||
* `trim_left` is similiar to `trim_right`, whereas it trims from the left. Defaults to `false`. See [Whitespace Control][whitespace control] for details.
|
||||
* `trim_left` is similiar to `trim_right`, whereas the `\n` is exclusive. Defaults to `false`. See [Whitespace Control][whitespace control] for details.
|
||||
|
||||
* `greedy` is used to specify whether `trim_left`/`trim_right` is greedy. When set to `true`, all blank characters will be trimed before and after, regardless of the occurences of `\n`. Defaults to `false`.
|
||||
* `greedy` is used to specify whether `trim_left`/`trim_right` is greedy. When set to `true`, all successive blank characters including `\n` will be trimed regardless of line breaks. Defaults to `false`.
|
||||
|
||||
## Use with Express.js
|
||||
|
||||
|
||||
+5
-9
@@ -9,7 +9,7 @@ function parse(html, filepath, options) {
|
||||
html = whiteSpaceCtrl(html, options);
|
||||
|
||||
var tokens = [];
|
||||
var syntax = /({%-?(.*?)-?%})|({{-?(.*?)-?}})/g;
|
||||
var syntax = /({%-?(.*?)-?%})|({{(.*?)}})/g;
|
||||
var result, htmlFragment, token;
|
||||
var lastMatchEnd = 0, lastMatchBegin = -1, parsedLinesCount = 0;
|
||||
|
||||
@@ -78,17 +78,13 @@ function parse(html, filepath, options) {
|
||||
function whiteSpaceCtrl(html, options){
|
||||
options = options || {};
|
||||
if(options.trim_left) {
|
||||
html = html
|
||||
.replace(/{{-?/g, '{{-')
|
||||
.replace(/{%-?/g, '{%-');
|
||||
html = html.replace(/{%-?/g, '{%-');
|
||||
}
|
||||
if(options.trim_right) {
|
||||
html = html
|
||||
.replace(/-?}}/g, '-}}')
|
||||
.replace(/-?%}/g, '-%}');
|
||||
html = html.replace(/-?%}/g, '-%}');
|
||||
}
|
||||
var rLeft = options.greedy ? /\s+({[{%]-)/g : /\n[\t\r ]*({[{%]-)/g;
|
||||
var rRight = options.greedy ? /(-[}%]})\s+/g : /(-[}%]})[\t\r ]*\n/g;
|
||||
var rLeft = options.greedy ? /\s+({%-)/g : /[\t\r ]*({%-)/g;
|
||||
var rRight = options.greedy ? /(-%})\s+/g : /(-%})[\t\r ]*\n?/g;
|
||||
return html.replace(rLeft, '$1').replace(rRight, '$1');
|
||||
}
|
||||
|
||||
|
||||
+5
-5
@@ -179,21 +179,21 @@ describe('liquid', function() {
|
||||
engine = Liquid({
|
||||
trim_left: true
|
||||
});
|
||||
return engine.parseAndRender(' \n \t{{"foo"}} ')
|
||||
.should.eventually.equal(' foo ');
|
||||
return engine.parseAndRender(' \n \t{%if true%}foo{%endif%} ')
|
||||
.should.eventually.equal(' \nfoo ');
|
||||
});
|
||||
it('should trim_right for tags when trim_right=true', function() {
|
||||
engine = Liquid({
|
||||
trim_right: true
|
||||
});
|
||||
return engine.parseAndRender('\t{{"foo"}} \n')
|
||||
return engine.parseAndRender('\t{%if true%}foo{%endif%} \n')
|
||||
.should.eventually.equal('\tfoo');
|
||||
});
|
||||
it('should trim all blanks before and after when greedy=true', function() {
|
||||
engine = Liquid({
|
||||
greedy: true
|
||||
});
|
||||
return engine.parseAndRender('\t{{-"foo"-}} \n \n')
|
||||
return engine.parseAndRender('\t{%-if true%}foo{%endif-%} \n \n')
|
||||
.should.eventually.equal('foo');
|
||||
});
|
||||
it('should support trim using markup', function() {
|
||||
@@ -206,7 +206,7 @@ describe('liquid', function() {
|
||||
' Hello there!',
|
||||
'{%- endif -%}\n',
|
||||
].join('\n');
|
||||
var dst = ' Wow, John G. Chalmers-Smith, you have a long name!';
|
||||
var dst = ' Wow, John G. Chalmers-Smith, you have a long name!\n';
|
||||
return engine.parseAndRender(src).should.eventually.equal(dst);
|
||||
});
|
||||
it('should not trim when not specified', function() {
|
||||
|
||||
+14
-18
@@ -48,7 +48,7 @@ describe('tokenizer', function() {
|
||||
tokens[2].value.should.equal('foo');
|
||||
});
|
||||
it('should keep white spaces and newlines', function() {
|
||||
var html = '{{foo}}\n{%bar %} \n {{alice}}';
|
||||
var html = '{%foo%}\n{%bar %} \n {%alice%}';
|
||||
var tokens = parse(html);
|
||||
expect(tokens.length).to.equal(5);
|
||||
expect(tokens[1].type).to.equal('html');
|
||||
@@ -62,41 +62,37 @@ describe('tokenizer', function() {
|
||||
expect(whiteSpaceCtrl('\n {%foo%} \n')).to.equal('\n {%foo%} \n');
|
||||
});
|
||||
it('should strip all blank characters before and after', function() {
|
||||
expect(whiteSpaceCtrl('\n \t\r{{-foo-}} \t\n')).to.equal('{{-foo-}}');
|
||||
expect(whiteSpaceCtrl(' \t\r{%-foo-%} \t\n')).to.equal('{%-foo-%}');
|
||||
});
|
||||
it('should not trim previous/next lines', function() {
|
||||
expect(whiteSpaceCtrl(' \t\n {{-foo-}}')).to.equal(' \t{{-foo-}}');
|
||||
expect(whiteSpaceCtrl('{{-foo-}} \n \tfoo')).to.equal('{{-foo-}} \tfoo');
|
||||
});
|
||||
it("should not trim inter-word blanks", function() {
|
||||
expect(whiteSpaceCtrl('\nhello {{-world-}}')).to.equal('\nhello {{-world-}}');
|
||||
expect(whiteSpaceCtrl('{{-hello-}} world')).to.equal('{{-hello-}} world');
|
||||
expect(whiteSpaceCtrl(' \t\n {%-foo-%}')).to.equal(' \t\n{%-foo-%}');
|
||||
expect(whiteSpaceCtrl('{%-foo-%} \n \tfoo')).to.equal('{%-foo-%} \tfoo');
|
||||
});
|
||||
it('should trim exactly one trailing CR', function() {
|
||||
expect(whiteSpaceCtrl('{{-foo-}} \n\n')).to.equal('{{-foo-}}\n');
|
||||
expect(whiteSpaceCtrl('{%-foo-%} \n\n')).to.equal('{%-foo-%}\n');
|
||||
});
|
||||
it('should trim all leading/trailing blanks when options.greedy set', function() {
|
||||
expect(whiteSpaceCtrl(' \n \n\t\r{{-foo-}}\n \n', {
|
||||
expect(whiteSpaceCtrl(' \n \n\t\r{%-foo-%}\n \n', {
|
||||
greedy: true
|
||||
})).to.equal('{{-foo-}}');
|
||||
})).to.equal('{%-foo-%}');
|
||||
});
|
||||
it('should strip whitespaces when set trim_left', function() {
|
||||
expect(whiteSpaceCtrl('\n {{foo}} \n', {
|
||||
expect(whiteSpaceCtrl('\n {%foo%} \n', {
|
||||
trim_left: true
|
||||
})).to.equal('{{-foo}} \n');
|
||||
})).to.equal('\n{%-foo%} \n');
|
||||
});
|
||||
it('should strip whitespaces when set trim_right', function() {
|
||||
expect(whiteSpaceCtrl('\n {{foo}} \n', {
|
||||
expect(whiteSpaceCtrl('\n {%foo%} \n', {
|
||||
trim_right: true
|
||||
})).to.equal('\n {{foo-}}');
|
||||
})).to.equal('\n {%foo-%}');
|
||||
});
|
||||
it('markup should has priority over options', function() {
|
||||
expect(whiteSpaceCtrl('\n {{-foo}} \n', {
|
||||
expect(whiteSpaceCtrl('\n {%-foo%} \n', {
|
||||
trim_left: false
|
||||
})).to.equal('{{-foo}} \n');
|
||||
})).to.equal('\n{%-foo%} \n');
|
||||
});
|
||||
it('should support a mix of markup and options', function() {
|
||||
expect(whiteSpaceCtrl('\n {%-foo%} \n', {
|
||||
expect(whiteSpaceCtrl(' {%-foo%} \n', {
|
||||
trim_left: true,
|
||||
trim_right: true
|
||||
})).to.equal('{%-foo-%}');
|
||||
|
||||
Reference in New Issue
Block a user