Skip to content

Commit 94d5e1b

Browse files
committed
allow configuring unsafeDelimiters separately
1 parent 315f9a7 commit 94d5e1b

File tree

3 files changed

+37
-35
lines changed

3 files changed

+37
-35
lines changed

src/config.js

+16-4
Original file line numberDiff line numberDiff line change
@@ -116,20 +116,32 @@ Object.defineProperty(module.exports, 'prefix', {
116116
})
117117

118118
/**
119-
* Interpolation delimiters.
120-
* We need to mark the changed flag so that the text parser
121-
* knows it needs to recompile the regex.
119+
* Interpolation delimiters. Changing these would trigger
120+
* the text parser to re-compile the regular expressions.
122121
*
123122
* @type {Array<String>}
124123
*/
125124

126125
var delimiters = ['{{', '}}']
126+
var unsafeDelimiters = ['{{{', '}}}']
127+
var textParser = require('./parsers/text')
128+
127129
Object.defineProperty(module.exports, 'delimiters', {
128130
get: function () {
129131
return delimiters
130132
},
131133
set: function (val) {
132134
delimiters = val
133-
this._delimitersChanged = true
135+
textParser.compileRegex()
136+
}
137+
})
138+
139+
Object.defineProperty(module.exports, 'unsafeDelimiters', {
140+
get: function () {
141+
return unsafeDelimiters
142+
},
143+
set: function (val) {
144+
unsafeDelimiters = val
145+
textParser.compileRegex()
134146
}
135147
})

src/parsers/text.js

+18-30
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

+3-1
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,17 @@ describe('Text Parser', function () {
8383

8484
it('custom delimiters', function () {
8585
config.delimiters = ['[%', '%]']
86+
config.unsafeDelimiters = ['{!!', '!!}']
8687
assertParse({
87-
text: '[%* text %] and [[% html %]]',
88+
text: '[%* text %] and {!! html !!}',
8889
expected: [
8990
{ tag: true, value: 'text', html: false, oneTime: true },
9091
{ value: ' and ' },
9192
{ tag: true, value: 'html', html: true, oneTime: false }
9293
]
9394
})
9495
config.delimiters = ['{{', '}}']
96+
config.unsafeDelimiters = ['{{{', '}}}']
9597
})
9698

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

0 commit comments

Comments
 (0)