Skip to content

Commit 7a63cb0

Browse files
committed
allow configuring unsafeDelimiters separately
1 parent aa566da commit 7a63cb0

File tree

3 files changed

+37
-35
lines changed

3 files changed

+37
-35
lines changed

src/config.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,20 +90,32 @@ module.exports = {
9090
}
9191

9292
/**
93-
* Interpolation delimiters.
94-
* We need to mark the changed flag so that the text parser
95-
* knows it needs to recompile the regex.
93+
* Interpolation delimiters. Changing these would trigger
94+
* the text parser to re-compile the regular expressions.
9695
*
9796
* @type {Array<String>}
9897
*/
9998

10099
var delimiters = ['{{', '}}']
100+
var unsafeDelimiters = ['{{{', '}}}']
101+
var textParser = require('./parsers/text')
102+
101103
Object.defineProperty(module.exports, 'delimiters', {
102104
get: function () {
103105
return delimiters
104106
},
105107
set: function (val) {
106108
delimiters = val
107-
this._delimitersChanged = true
109+
textParser.compileRegex()
110+
}
111+
})
112+
113+
Object.defineProperty(module.exports, 'unsafeDelimiters', {
114+
get: function () {
115+
return unsafeDelimiters
116+
},
117+
set: function (val) {
118+
unsafeDelimiters = val
119+
textParser.compileRegex()
108120
}
109121
})

src/parsers/text.js

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var Cache = require('../cache')
22
var config = require('../config')
33
var dirParser = require('./directive')
44
var regexEscapeRE = /[-.*+?^${}()|[\]\/\\]/g
5-
var cache, tagRE, htmlRE, firstChar, lastChar
5+
var cache, tagRE, htmlRE
66

77
/**
88
* Escape a string so it can be used in a RegExp
@@ -15,32 +15,18 @@ function escapeRegex (str) {
1515
return str.replace(regexEscapeRE, '\\$&')
1616
}
1717

18-
/**
19-
* Compile the interpolation tag regex.
20-
*
21-
* @return {RegExp}
22-
*/
23-
24-
function compileRegex () {
25-
config._delimitersChanged = false
26-
var open = config.delimiters[0]
27-
var close = config.delimiters[1]
28-
firstChar = open.charAt(0)
29-
lastChar = close.charAt(close.length - 1)
30-
var firstCharRE = escapeRegex(firstChar)
31-
var lastCharRE = escapeRegex(lastChar)
32-
var openRE = escapeRegex(open)
33-
var closeRE = escapeRegex(close)
18+
exports.compileRegex = function () {
19+
var open = escapeRegex(config.delimiters[0])
20+
var close = escapeRegex(config.delimiters[1])
21+
var unsafeOpen = escapeRegex(config.unsafeDelimiters[0])
22+
var unsafeClose = escapeRegex(config.unsafeDelimiters[1])
3423
tagRE = new RegExp(
35-
firstCharRE + '?' + openRE +
36-
'(.+?)' +
37-
closeRE + lastCharRE + '?',
24+
unsafeOpen + '(.+?)' + unsafeClose + '|' +
25+
open + '(.+?)' + close,
3826
'g'
3927
)
4028
htmlRE = new RegExp(
41-
'^' + firstCharRE + openRE +
42-
'.*' +
43-
closeRE + lastCharRE + '$'
29+
'^' + unsafeOpen + '.*' + unsafeClose + '$'
4430
)
4531
// reset cache
4632
cache = new Cache(1000)
@@ -58,8 +44,8 @@ function compileRegex () {
5844
*/
5945

6046
exports.parse = function (text) {
61-
if (config._delimitersChanged) {
62-
compileRegex()
47+
if (!cache) {
48+
exports.compileRegex()
6349
}
6450
var hit = cache.get(text)
6551
if (hit) {
@@ -71,7 +57,7 @@ exports.parse = function (text) {
7157
}
7258
var tokens = []
7359
var lastIndex = tagRE.lastIndex = 0
74-
var match, index, value, first, oneTime, twoWay
60+
var match, index, html, value, first, oneTime, twoWay
7561
/* eslint-disable no-cond-assign */
7662
while (match = tagRE.exec(text)) {
7763
/* eslint-enable no-cond-assign */
@@ -83,16 +69,18 @@ exports.parse = function (text) {
8369
})
8470
}
8571
// tag token
86-
first = match[1].charCodeAt(0)
72+
html = htmlRE.test(match[0])
73+
value = html ? match[1] : match[2]
74+
first = value.charCodeAt(0)
8775
oneTime = first === 42 // *
8876
twoWay = first === 64 // @
8977
value = oneTime || twoWay
90-
? match[1].slice(1)
91-
: match[1]
78+
? value.slice(1)
79+
: value
9280
tokens.push({
9381
tag: true,
9482
value: value.trim(),
95-
html: htmlRE.test(match[0]),
83+
html: html,
9684
oneTime: oneTime,
9785
twoWay: twoWay
9886
})

test/unit/specs/parsers/text_spec.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,17 @@ describe('Text Parser', function () {
8282

8383
it('custom delimiters', function () {
8484
config.delimiters = ['[%', '%]']
85+
config.unsafeDelimiters = ['{!!', '!!}']
8586
assertParse({
86-
text: '[%* text %] and [[% html %]]',
87+
text: '[%* text %] and {!! html !!}',
8788
expected: [
8889
{ tag: true, value: 'text', html: false, oneTime: true },
8990
{ value: ' and ' },
9091
{ tag: true, value: 'html', html: true, oneTime: false }
9192
]
9293
})
9394
config.delimiters = ['{{', '}}']
95+
config.unsafeDelimiters = ['{{{', '}}}']
9496
})
9597

9698
it('tokens to expression', function () {

0 commit comments

Comments
 (0)