Skip to content

Commit 814b309

Browse files
committed
handles html-expected interpolations and text-expected interpolations
1 parent ac94291 commit 814b309

File tree

3 files changed

+32
-21
lines changed

3 files changed

+32
-21
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "leopard",
3-
"version": "1.0.0",
4-
"description": "",
3+
"version": "0.0.1",
4+
"description": "a simple HTML template engine",
55
"main": "index.js",
66
"scripts": {
77
"test": "./node_modules/.bin/mocha",

src/index.js

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
var escape = require('./utils').escape
2+
var escapeQuotes = function(str) {
3+
return str.replace(/"/g, '\\"')
4+
}
25

36
/**
47
* parse the given `tpl` and return Function body string
@@ -35,10 +38,10 @@ var parser = function(tpl, data) {
3538
var type = line.charAt(0)
3639
switch (type) {
3740
case '=':
38-
push(escape(line.substr(1).trim()))
41+
push('escape(' + line.substr(1).trim() + ')')
3942
break
4043
case '-':
41-
push(escape(line.substr(1).trim()))
44+
push(line.substr(1).trim())
4245
break
4346
default:
4447
body += line + '\n'
@@ -52,19 +55,13 @@ var parser = function(tpl, data) {
5255
matched !== null ? matched.index + matched[0].length : 0,
5356
curMatched.index
5457
)
55-
if (html) {
56-
push('\"' + escape(html) + '\"')
57-
}
58+
html && push('\"' + escapeQuotes(html) + '\"')
5859
var js = curMatched[1].trim()
59-
if (js) {
60-
generate(js)
61-
}
60+
js && generate(js)
6261
matched = curMatched
6362
}
6463
var end = tpl.substr(matched.index + matched[0].length)
65-
if (end) {
66-
push('\"' + escape(end) + '\"')
67-
}
64+
end && push('\"' + escapeQuotes(end) + '\"')
6865
body += 'rst = lines.join(\"\");\n' +
6966
'}\n' +
7067
'return rst;'
@@ -81,8 +78,8 @@ var parser = function(tpl, data) {
8178
*/
8279
var compiler = function(tpl, data) {
8380
var body = parser(tpl, data)
84-
var fun = new Function(body)
85-
return fun()
81+
var fun = new Function('escape', body)
82+
return fun.call(this, escape)
8683
}
8784

8885
var leo = compiler

test/index.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ var conditionData = {
1818
describe('leopard', function() {
1919
it('inits with simplest config', function() {
2020
var template = leo('<p>This is <%= a %>!</p>', basicData)
21-
assert.strictEqual(template, escape('<p>This is Leopard!</p>'))
21+
assert.strictEqual(template, '<p>This is Leopard!</p>')
2222
})
2323

2424
it('handles multiple expressions', function() {
2525
var template = leo('<p>This is <%= a %>, AKA <%= b %>!</p>', basicData)
26-
assert.strictEqual(template, escape('<p>This is Leopard, AKA leo!</p>'))
26+
assert.strictEqual(template, '<p>This is Leopard, AKA leo!</p>')
2727
})
2828

2929
it('handles complex expressions', function() {
@@ -32,8 +32,22 @@ describe('leopard', function() {
3232
'<p>I am Leopard<%= \', AKA \' + (isOk ? nickname : realname) + \'!\' %></p>',
3333
conditionData
3434
)
35-
assert.strictEqual(template, escape('<p>m + n = 3</p>'))
36-
assert.strictEqual(template_2, escape('<p>I am Leopard, AKA leopard!</p>'))
35+
assert.strictEqual(template, '<p>m + n = 3</p>')
36+
assert.strictEqual(template_2, '<p>I am Leopard, AKA leopard!</p>')
37+
})
38+
39+
it('handles html-expected interpolations and text-expected interpolations', function() {
40+
var string = '<p>html tags can be escaped and rendered as string: <%= html %>.' +
41+
' Or can still rendered as html: <%- html %></p>'
42+
var data = {
43+
html: '<em>Leopard</em>'
44+
}
45+
var template = leo(string, data)
46+
assert.strictEqual(template, '<p>html tags can be escaped and rendered as string: ' +
47+
escape(data.html) +
48+
'. Or can still rendered as html: ' +
49+
data.html +
50+
'</p>')
3751
})
3852

3953
it('handles conditions', function() {
@@ -44,7 +58,7 @@ describe('leopard', function() {
4458
'<% } %>'
4559

4660
var template = leo(conditions, conditionData)
47-
assert.strictEqual(template, escape('<span class=\"realname\">leopard</span>'))
61+
assert.strictEqual(template, '<span class=\"realname\">leopard</span>')
4862
})
4963

5064
it('handles loops', function() {
@@ -56,6 +70,6 @@ describe('leopard', function() {
5670
'</ul>'
5771

5872
var template = leo(loops)
59-
assert.strictEqual(template, escape('Now I repeat: <ul><li>0: I am Leopard!</li><li>1: I am Leopard!</li></ul>'))
73+
assert.strictEqual(template, 'Now I repeat: <ul><li>0: I am Leopard!</li><li>1: I am Leopard!</li></ul>')
6074
})
6175
})

0 commit comments

Comments
 (0)