Skip to content
This repository has been archived by the owner on Apr 11, 2018. It is now read-only.

Commit

Permalink
Merge pull request #193 from potench/feature/spaceless
Browse files Browse the repository at this point in the history
added {% spaceless %} tag support
  • Loading branch information
paularmstrong committed Feb 20, 2013
2 parents 22a7dcf + 2712520 commit c5e83ae
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ _.extend(exports, {
macro: require('./tags/macro'),
parent: require('./tags/parent'),
raw: require('./tags/raw'),
set: require('./tags/set')
set: require('./tags/set'),
spaceless: require('./tags/spaceless')
});
19 changes: 19 additions & 0 deletions lib/tags/spaceless.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* spaceless
*/
module.exports = function (indent, parser) {
var output = [],
i = this.tokens.length - 1;

for (i; i >= 0; i -= 1) {
this.tokens[i] = this.tokens[i]
.replace(/^\s+/gi, "") // trim leading white-space
.replace(/>\s+</gi, "><") // trim white-space between tags
.replace(/\s+$/gi, ""); // trim trailing white-space
}

output.push(parser.compile.call(this, indent + ' '));
return output.join('');
};

module.exports.ends = true;
1 change: 1 addition & 0 deletions tests/browser/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<script src="parser.test.js"></script>
<script src="raw.test.js"></script>
<script src="set.test.js"></script>
<script src="spaceless.test.js"></script>
<script src="swig.test.js"></script>
<script src="tags.test.js"></script>
</head>
Expand Down
60 changes: 60 additions & 0 deletions tests/node/tags/spaceless.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
var require = require('../testutils').require,
expect = require('expect.js'),
swig = require('../../lib/swig');

describe('Tag: spaceless', function () {
beforeEach(function () {
swig.init({ allowErrors: true });
});

it('removes leading white-space', function () {
var spaceless_tmpl = [
'{% spaceless %}',
' ',
'',
'<li>1</li>',
'{% endspaceless %}'
].join('\n'),

tpl = swig.compile(spaceless_tmpl);

expect(tpl({})).to.equal('<li>1</li>');

});

it('removes white-spaces betwen tags', function () {
var spaceless_tmpl = [
'{% spaceless %}',
'<li>1</li>',
' <li>2</li>',
'<li>3',
'</li>',
'<li>4',
' </li>',
'<li id="5">',
' </li>',

'{% endspaceless %}',
].join('\n'),

tpl = swig.compile(spaceless_tmpl);

expect(tpl({})).to.equal('<li>1</li><li>2</li><li>3\n</li><li>4\n </li><li id="5"></li>');

});

it('removes trailing white-space', function () {
var spaceless_tmpl = [
'{% spaceless %}',
'<li>1</li>',
'',
' ',
'{% endspaceless %}'
].join('\n'),

tpl = swig.compile(spaceless_tmpl);

expect(tpl({})).to.equal('<li>1</li>');

});
});

0 comments on commit c5e83ae

Please sign in to comment.