Skip to content

Commit 1c6dacf

Browse files
committed
text-parser deal with triple mustache
1 parent 7943d0b commit 1c6dacf

File tree

3 files changed

+37
-10
lines changed

3 files changed

+37
-10
lines changed

src/compiler.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -368,10 +368,14 @@ CompilerProto.compileTextNode = function (node) {
368368
partialNodes = slice.call(el.childNodes)
369369
}
370370
} else { // a real binding
371-
el = document.createTextNode('')
372-
directive = Directive.parse('text', token.key, this, el)
373-
if (directive) {
374-
this.bindDirective(directive)
371+
if (!token.html) // text binding
372+
el = document.createTextNode('')
373+
directive = Directive.parse('text', token.key, this, el)
374+
if (directive) {
375+
this.bindDirective(directive)
376+
}
377+
} else { // html binding
378+
375379
}
376380
}
377381
} else { // a plain string

src/text-parser.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1-
var BINDING_RE = /\{\{(.+?)\}\}/
1+
var BINDING_RE = /{{{?([^{}]+?)}?}}/,
2+
TRIPLE_RE = /{{{[^{}]+}}}/
23

34
/**
45
* Parse a piece of text, return an array of tokens
56
*/
67
function parse (text) {
78
if (!BINDING_RE.test(text)) return null
8-
var m, i, tokens = []
9+
var m, i, token, tokens = []
910
/* jshint boss: true */
1011
while (m = text.match(BINDING_RE)) {
1112
i = m.index
1213
if (i > 0) tokens.push(text.slice(0, i))
13-
tokens.push({ key: m[1].trim() })
14+
token = { key: m[1].trim() }
15+
if (TRIPLE_RE.test(m[0])) token.html = true
16+
tokens.push(token)
1417
text = text.slice(i + m[0].length)
1518
}
1619
if (text.length) tokens.push(text)

test/unit/specs/text-parser.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ describe('UNIT: TextNode Parser', function () {
33
var TextParser = require('vue/src/text-parser')
44

55
describe('.parse()', function () {
6+
7+
var tokens, badTokens
8+
9+
before(function () {
10+
tokens = TextParser.parse('hello {{a}}! {{ bcd }}{{d.e.f}} {{a + (b || c) ? d : e}} {{>test}}{{{ a + "<em>" }}}')
11+
badTokens = TextParser.parse('{{{a}}{{{{{{{{b}}}}')
12+
})
613

714
it('should return null if no interpolate tags are present', function () {
815
var result = TextParser.parse('hello no tags')
@@ -14,11 +21,9 @@ describe('UNIT: TextNode Parser', function () {
1421
assert.strictEqual(result.length, 3)
1522
assert.strictEqual(result[2], ' &#123;&#123;hello&#125;&#125;')
1623
})
17-
18-
var tokens = TextParser.parse('hello {{a}}! {{ bcd }}{{d.e.f}} {{a + (b || c) ? d : e}} {{>test}}')
1924

2025
it('should extract correct amount of tokens', function () {
21-
assert.strictEqual(tokens.length, 9)
26+
assert.strictEqual(tokens.length, 10)
2227
})
2328

2429
it('should extract plain strings', function () {
@@ -48,6 +53,21 @@ describe('UNIT: TextNode Parser', function () {
4853
assert.strictEqual(tokens[8].key, '>test')
4954
})
5055

56+
it('should extract triple mustache (html instead of text)', function () {
57+
assert.strictEqual(tokens[9].key, 'a + "<em>"')
58+
assert.ok(tokens[9].html)
59+
})
60+
61+
it('should deal with bad binding tags', function () {
62+
assert.strictEqual(badTokens.length, 4)
63+
assert.strictEqual(badTokens[0].key, 'a')
64+
assert.notOk(badTokens[0].html)
65+
assert.strictEqual(badTokens[1], '{{{{{')
66+
assert.strictEqual(badTokens[2].key, 'b')
67+
assert.ok(badTokens[2].html)
68+
assert.strictEqual(badTokens[3], '}')
69+
})
70+
5171
})
5272

5373
describe('.parseAttr()', function () {

0 commit comments

Comments
 (0)