Просмотр исходного кода

allow configuring unsafeDelimiters separately

Evan You 10 лет назад
Родитель
Сommit
7a63cb0317
3 измененных файлов с 37 добавлено и 35 удалено
  1. 16 4
      src/config.js
  2. 18 30
      src/parsers/text.js
  3. 3 1
      test/unit/specs/parsers/text_spec.js

+ 16 - 4
src/config.js

@@ -90,20 +90,32 @@ module.exports = {
 }
 
 /**
- * Interpolation delimiters.
- * We need to mark the changed flag so that the text parser
- * knows it needs to recompile the regex.
+ * Interpolation delimiters. Changing these would trigger
+ * the text parser to re-compile the regular expressions.
  *
  * @type {Array<String>}
  */
 
 var delimiters = ['{{', '}}']
+var unsafeDelimiters = ['{{{', '}}}']
+var textParser = require('./parsers/text')
+
 Object.defineProperty(module.exports, 'delimiters', {
   get: function () {
     return delimiters
   },
   set: function (val) {
     delimiters = val
-    this._delimitersChanged = true
+    textParser.compileRegex()
+  }
+})
+
+Object.defineProperty(module.exports, 'unsafeDelimiters', {
+  get: function () {
+    return unsafeDelimiters
+  },
+  set: function (val) {
+    unsafeDelimiters = val
+    textParser.compileRegex()
   }
 })

+ 18 - 30
src/parsers/text.js

@@ -2,7 +2,7 @@ var Cache = require('../cache')
 var config = require('../config')
 var dirParser = require('./directive')
 var regexEscapeRE = /[-.*+?^${}()|[\]\/\\]/g
-var cache, tagRE, htmlRE, firstChar, lastChar
+var cache, tagRE, htmlRE
 
 /**
  * Escape a string so it can be used in a RegExp
@@ -15,32 +15,18 @@ function escapeRegex (str) {
   return str.replace(regexEscapeRE, '\\$&')
 }
 
-/**
- * Compile the interpolation tag regex.
- *
- * @return {RegExp}
- */
-
-function compileRegex () {
-  config._delimitersChanged = false
-  var open = config.delimiters[0]
-  var close = config.delimiters[1]
-  firstChar = open.charAt(0)
-  lastChar = close.charAt(close.length - 1)
-  var firstCharRE = escapeRegex(firstChar)
-  var lastCharRE = escapeRegex(lastChar)
-  var openRE = escapeRegex(open)
-  var closeRE = escapeRegex(close)
+exports.compileRegex = function () {
+  var open = escapeRegex(config.delimiters[0])
+  var close = escapeRegex(config.delimiters[1])
+  var unsafeOpen = escapeRegex(config.unsafeDelimiters[0])
+  var unsafeClose = escapeRegex(config.unsafeDelimiters[1])
   tagRE = new RegExp(
-    firstCharRE + '?' + openRE +
-    '(.+?)' +
-    closeRE + lastCharRE + '?',
+    unsafeOpen + '(.+?)' + unsafeClose + '|' +
+    open + '(.+?)' + close,
     'g'
   )
   htmlRE = new RegExp(
-    '^' + firstCharRE + openRE +
-    '.*' +
-    closeRE + lastCharRE + '$'
+    '^' + unsafeOpen + '.*' + unsafeClose + '$'
   )
   // reset cache
   cache = new Cache(1000)
@@ -58,8 +44,8 @@ function compileRegex () {
  */
 
 exports.parse = function (text) {
-  if (config._delimitersChanged) {
-    compileRegex()
+  if (!cache) {
+    exports.compileRegex()
   }
   var hit = cache.get(text)
   if (hit) {
@@ -71,7 +57,7 @@ exports.parse = function (text) {
   }
   var tokens = []
   var lastIndex = tagRE.lastIndex = 0
-  var match, index, value, first, oneTime, twoWay
+  var match, index, html, value, first, oneTime, twoWay
   /* eslint-disable no-cond-assign */
   while (match = tagRE.exec(text)) {
   /* eslint-enable no-cond-assign */
@@ -83,16 +69,18 @@ exports.parse = function (text) {
       })
     }
     // tag token
-    first = match[1].charCodeAt(0)
+    html = htmlRE.test(match[0])
+    value = html ? match[1] : match[2]
+    first = value.charCodeAt(0)
     oneTime = first === 42 // *
     twoWay = first === 64  // @
     value = oneTime || twoWay
-      ? match[1].slice(1)
-      : match[1]
+      ? value.slice(1)
+      : value
     tokens.push({
       tag: true,
       value: value.trim(),
-      html: htmlRE.test(match[0]),
+      html: html,
       oneTime: oneTime,
       twoWay: twoWay
     })

+ 3 - 1
test/unit/specs/parsers/text_spec.js

@@ -82,8 +82,9 @@ describe('Text Parser', function () {
 
   it('custom delimiters', function () {
     config.delimiters = ['[%', '%]']
+    config.unsafeDelimiters = ['{!!', '!!}']
     assertParse({
-      text: '[%* text %] and [[% html %]]',
+      text: '[%* text %] and {!! html !!}',
       expected: [
         { tag: true, value: 'text', html: false, oneTime: true },
         { value: ' and ' },
@@ -91,6 +92,7 @@ describe('Text Parser', function () {
       ]
     })
     config.delimiters = ['{{', '}}']
+    config.unsafeDelimiters = ['{{{', '}}}']
   })
 
   it('tokens to expression', function () {