Skip to content
This repository
Browse code

added {% spaceless %} tag support

  • Loading branch information...
commit 2712520676706b60243be89db97b0f17a697d79f 1 parent 22a7dcf
Christian Shum-Harden authored February 08, 2013
3  lib/tags.js
@@ -13,5 +13,6 @@ _.extend(exports, {
13 13
   macro: require('./tags/macro'),
14 14
   parent: require('./tags/parent'),
15 15
   raw: require('./tags/raw'),
16  
-  set: require('./tags/set')
  16
+  set: require('./tags/set'),
  17
+  spaceless: require('./tags/spaceless')
17 18
 });
19  lib/tags/spaceless.js
... ...
@@ -0,0 +1,19 @@
  1
+/**
  2
+ * spaceless
  3
+ */
  4
+module.exports = function (indent, parser) {
  5
+	var output = [],
  6
+		i = this.tokens.length - 1;
  7
+
  8
+	for (i; i >= 0; i -= 1) {
  9
+		this.tokens[i] = this.tokens[i]
  10
+			.replace(/^\s+/gi, "") // trim leading white-space
  11
+			.replace(/>\s+</gi,  "><") // trim white-space between tags
  12
+			.replace(/\s+$/gi, ""); // trim trailing white-space
  13
+	}
  14
+
  15
+	output.push(parser.compile.call(this, indent + '    '));
  16
+	return output.join('');
  17
+};
  18
+
  19
+module.exports.ends = true;
1  tests/browser/index.html
@@ -28,6 +28,7 @@
28 28
     <script src="parser.test.js"></script>
29 29
     <script src="raw.test.js"></script>
30 30
     <script src="set.test.js"></script>
  31
+    <script src="spaceless.test.js"></script>
31 32
     <script src="swig.test.js"></script>
32 33
     <script src="tags.test.js"></script>
33 34
 </head>
60  tests/node/tags/spaceless.js
... ...
@@ -0,0 +1,60 @@
  1
+var require = require('../testutils').require,
  2
+  expect = require('expect.js'),
  3
+  swig = require('../../lib/swig');
  4
+
  5
+describe('Tag: spaceless', function () {
  6
+  beforeEach(function () {
  7
+    swig.init({ allowErrors: true });
  8
+  });
  9
+
  10
+  it('removes leading white-space', function () {
  11
+    var spaceless_tmpl = [
  12
+        '{% spaceless %}',
  13
+        ' ',
  14
+        '',
  15
+        '<li>1</li>',
  16
+        '{% endspaceless %}'
  17
+      ].join('\n'),
  18
+
  19
+      tpl = swig.compile(spaceless_tmpl);
  20
+
  21
+    expect(tpl({})).to.equal('<li>1</li>');
  22
+
  23
+  });
  24
+
  25
+  it('removes white-spaces betwen tags', function () {
  26
+    var spaceless_tmpl = [
  27
+        '{% spaceless %}',
  28
+        '<li>1</li>',
  29
+        ' <li>2</li>',
  30
+        '<li>3',
  31
+        '</li>',
  32
+        '<li>4',
  33
+        ' </li>',
  34
+        '<li id="5">',
  35
+        '   </li>',
  36
+
  37
+        '{% endspaceless %}',
  38
+      ].join('\n'),
  39
+
  40
+      tpl = swig.compile(spaceless_tmpl);
  41
+
  42
+    expect(tpl({})).to.equal('<li>1</li><li>2</li><li>3\n</li><li>4\n </li><li id="5"></li>');
  43
+
  44
+  });
  45
+
  46
+  it('removes trailing white-space', function () {
  47
+    var spaceless_tmpl = [
  48
+        '{% spaceless %}',
  49
+        '<li>1</li>',
  50
+        '',
  51
+        '  ',
  52
+        '{% endspaceless %}'
  53
+      ].join('\n'),
  54
+
  55
+      tpl = swig.compile(spaceless_tmpl);
  56
+
  57
+    expect(tpl({})).to.equal('<li>1</li>');
  58
+
  59
+  });
  60
+});

3 notes on commit 2712520

Wolfram Kriesing

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

Christian Shum-Harden

It was merged in with pull request #193.

Wolfram Kriesing

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.
Something went wrong with that request. Please try again.