[Pkg-javascript-commits] [ltx] 423/469: add support for es6 tagged templates

Jonas Smedegaard dr at jones.dk
Wed Aug 31 13:03:34 UTC 2016


This is an automated email from the git hooks/post-receive script.

js pushed a commit to branch master
in repository ltx.

commit ebb531081486309a3d2f3ebd84c73114879666d1
Author: Sonny Piers <sonny at fastmail.net>
Date:   Sat Jan 9 03:48:42 2016 +0100

    add support for es6 tagged templates
---
 index.js         |  5 +++++
 lib/tag.js       | 19 +++++++++++++++++++
 test/tag-test.js | 23 +++++++++++++++++++++++
 3 files changed, 47 insertions(+)

diff --git a/index.js b/index.js
index 011bd7f..2ca68f7 100644
--- a/index.js
+++ b/index.js
@@ -6,6 +6,9 @@ var escape = require('./lib/escape')
 var Element = require('./lib/Element')
 var equal = require('./lib/equal')
 var createElement = require('./lib/createElement')
+var tag = require('./lib/tag')
+
+exports = module.exports = tag
 
 exports.Element = Element
 
@@ -21,3 +24,5 @@ exports.escapeXMLText = escape.escapeXMLText
 
 exports.Parser = Parser
 exports.parse = parse
+
+exports.tag = tag
diff --git a/lib/tag.js b/lib/tag.js
new file mode 100644
index 0000000..6874eed
--- /dev/null
+++ b/lib/tag.js
@@ -0,0 +1,19 @@
+'use strict'
+
+var escape = require('./escape').escapeXML
+var parse = require('./parse')
+
+module.exports = function tag (/* [literals], ...substitutions */) {
+  var literals = arguments[0]
+  var substitutions = Array.prototype.slice.call(arguments, 1)
+
+  var str = ''
+
+  for (var i = 0; i < substitutions.length; i++) {
+    str += literals[i]
+    str += escape(substitutions[i])
+  }
+  str += literals[literals.length - 1]
+
+  return parse(str)
+}
diff --git a/test/tag-test.js b/test/tag-test.js
new file mode 100644
index 0000000..939882a
--- /dev/null
+++ b/test/tag-test.js
@@ -0,0 +1,23 @@
+'use strict'
+
+var vows = require('vows')
+var assert = require('assert')
+var ltx = require('..')
+var tag = require('../lib/tag')
+var Element = ltx.Element
+
+vows.describe('tag').addBatch({
+  'exported correctly': function () {
+    assert.equal(ltx, tag)
+    assert.equal(ltx.tag, tag)
+  },
+  'parses the string and return an Element object': function () {
+    // var r = tag`<foo>${'bar'}</foo>`
+    var r = tag(['<foo>', '</foo>'], 'bar')
+    assert(r instanceof Element)
+    var c = ltx.createElement('foo').t('bar')
+    assert(c.equals(r))
+    assert(r.equals(c))
+    assert.strictEqual(r.toString(), c.toString())
+  }
+}).export(module)

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/ltx.git



More information about the Pkg-javascript-commits mailing list