Przeglądaj źródła

simplify text regex

Evan You 12 lat temu
rodzic
commit
f2113266d7
2 zmienionych plików z 6 dodań i 17 usunięć
  1. 3 3
      src/text-parser.js
  2. 3 14
      test/unit/specs/text-parser.js

+ 3 - 3
src/text-parser.js

@@ -1,5 +1,5 @@
-var BINDING_RE = /{{{?([^{}]+?)}?}}/,
-    TRIPLE_RE = /{{{[^{}]+}}}/
+var BINDING_RE = /{{{?(.+?)}?}}/,
+    TRIPLE_RE = /{{{.+}}}/
 
 /**
  *  Parse a piece of text, return an array of tokens
@@ -12,7 +12,7 @@ function parse (text) {
         i = m.index
         if (i > 0) tokens.push(text.slice(0, i))
         token = { key: m[1].trim() }
-        if (TRIPLE_RE.test(m[0])) token.html = true
+        token.html = TRIPLE_RE.test(m[0])
         tokens.push(token)
         text = text.slice(i + m[0].length)
     }

+ 3 - 14
test/unit/specs/text-parser.js

@@ -4,11 +4,10 @@ describe('Text Parser', function () {
 
     describe('.parse()', function () {
 
-        var tokens, badTokens
+        var tokens
 
         before(function () {
-            tokens = TextParser.parse('hello {{a}}! {{ bcd }}{{d.e.f}} {{a + (b || c) ? d : e}} {{>test}}{{{ a + "<em>" }}}')
-            badTokens = TextParser.parse('{{{a}}{{{{{{{{b}}}}')
+            tokens = TextParser.parse('hello {{a}}! {{ {bcd} }}{{d.e.f}} {{a + (b || c) ? d : e}} {{>test}}{{{ a + "<em>" }}}')
         })
         
         it('should return null if no interpolate tags are present', function () {
@@ -38,7 +37,7 @@ describe('Text Parser', function () {
         })
 
         it('should trim extracted keys', function () {
-            assert.strictEqual(tokens[3].key, 'bcd')
+            assert.strictEqual(tokens[3].key, '{bcd}')
         })
 
         it('should extract nested keys', function () {
@@ -58,16 +57,6 @@ describe('Text Parser', function () {
             assert.ok(tokens[9].html)
         })
 
-        it('should deal with bad binding tags', function () {
-            assert.strictEqual(badTokens.length, 4)
-            assert.strictEqual(badTokens[0].key, 'a')
-            assert.notOk(badTokens[0].html)
-            assert.strictEqual(badTokens[1], '{{{{{')
-            assert.strictEqual(badTokens[2].key, 'b')
-            assert.ok(badTokens[2].html)
-            assert.strictEqual(badTokens[3], '}')
-        })
-
     })
 
     describe('.parseAttr()', function () {