Permalink
Browse files

added {% spaceless %} tag support

  • Loading branch information...
1 parent 22a7dcf commit 2712520676706b60243be89db97b0f17a697d79f @potench potench committed Feb 8, 2013
Showing with 82 additions and 1 deletion.
  1. +2 −1 lib/tags.js
  2. +19 −0 lib/tags/spaceless.js
  3. +1 −0 tests/browser/index.html
  4. +60 −0 tests/node/tags/spaceless.js
View
3 lib/tags.js
@@ -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')
});
View
19 lib/tags/spaceless.js
@@ -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;
View
1 tests/browser/index.html
@@ -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>
View
60 tests/node/tags/spaceless.js
@@ -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>');
+
+ });
+});

3 comments on commit 2712520

@wolframkriesing

is that going to be in a new version soon? want that really bad :)

@potench

It was merged in with pull request #193.

@wolframkriesing

thx, i saw that. I was curious if there is going to be a release containing that soon.
since the last release is 3 months old and therefore not available via npm install

Please sign in to comment.