Quellcode durchsuchen

allow mixed interpolation in attributes

Evan You vor 11 Jahren
Ursprung
Commit
ef80f11a50
5 geänderte Dateien mit 45 neuen und 46 gelöschten Zeilen
  1. 14 23
      src/compile/compile.js
  2. 4 13
      src/directive.js
  3. 19 0
      src/parse/text.js
  4. 0 10
      test/unit/specs/directive_spec.js
  5. 8 0
      test/unit/specs/parse/text_spec.js

+ 14 - 23
src/compile/compile.js

@@ -299,10 +299,10 @@ function compileParamAttributes (el, attrs, options) {
       if (tokens) {
         if (tokens.length > 1) {
           _.warn(
-            'Invalid attribute binding: "' +
+            'Invalid param attribute binding: "' +
             name + '="' + value + '"' +
             '\nDon\'t mix binding tags with plain text ' +
-            'in attribute bindings.'
+            'in param attribute bindings.'
           )
         } else {
           param.dynamic = true
@@ -462,28 +462,19 @@ function collectDirectives (el, options) {
 function collectAttrDirective (el, name, value, options) {
   var tokens = textParser.parse(value)
   if (tokens) {
-    if (tokens.length > 1) {
-      _.warn(
-        'Invalid attribute binding: "' +
-        name + '="' + value + '"' +
-        '\nDon\'t mix binding tags with plain text ' +
-        'in attribute bindings.'
-      )
+    if (tokens.length === 1 && tokens[0].oneTime) {
+      return {
+        name: name,
+        value: tokens[0].value,
+        def: options.directives.attr,
+        oneTime: true
+      }
     } else {
-      if (tokens[0].oneTime) {
-        return {
-          name: name,
-          value: tokens[0].value,
-          def: options.directives.attr,
-          oneTime: true
-        }
-      } else {
-        value = name + ':' + tokens[0].value
-        return {
-          name: 'attr',
-          def: options.directives.attr,
-          descriptors: dirParser.parse(value)
-        }
+      value = textParser.tokensToExp(tokens)
+      return {
+        name: 'attr',
+        def: options.directives.attr,
+        descriptors: dirParser.parse(name + ':' + value)
       }
     }
   }

+ 4 - 13
src/directive.js

@@ -103,19 +103,10 @@ p._checkDynamicLiteral = function () {
   if (expression && this.isLiteral) {
     var tokens = textParser.parse(expression)
     if (tokens) {
-      if (tokens.length > 1) {
-        _.warn(
-          'Invalid literal directive: ' +
-          this.name + '="' + expression + '"' +
-          '\nDon\'t mix binding tags with plain text ' +
-          'in literal directives.'
-        )
-      } else {
-        var exp = tokens[0].value
-        this.expression = this.vm.$get(exp)
-        this._watcherExp = exp
-        this._isDynamicLiteral = true
-      }
+      var exp = textParser.tokensToExp(tokens)
+      this.expression = this.vm.$get(exp)
+      this._watcherExp = exp
+      this._isDynamicLiteral = true
     }
   }
 }

+ 19 - 0
src/parse/text.js

@@ -102,4 +102,23 @@ exports.parse = function (text) {
   }
   cache.put(text, tokens)
   return tokens
+}
+
+/**
+ * Format a list of tokens into an expression.
+ *
+ * @param {Array} tokens
+ * @return {String}
+ */
+
+exports.tokensToExp = function (tokens) {
+  return tokens.length > 1
+    ? tokens.map(formatToken).join('+')
+    : formatToken(tokens[0])
+}
+
+function formatToken (token) {
+  return token.tag
+    ? token.value
+    : '"' + token.value + '"'
 }

+ 0 - 10
test/unit/specs/directive_spec.js

@@ -136,14 +136,4 @@ describe('Directive', function () {
     })
   })
 
-  it('invalid dynamic literal', function () {
-    var _ = Vue.util
-    spyOn(_, 'warn')    
-    def.isLiteral = true
-    new Directive('test', el, vm, {
-      expression: 'abc {{a}}'
-    }, def)
-    expect(_.warn).toHaveBeenCalled()
-  })
-
 })

+ 8 - 0
test/unit/specs/parse/text_spec.js

@@ -83,6 +83,14 @@ describe('Text Parser', function () {
         { tag: true, value: 'html', html: true, oneTime: false },
       ]
     })
+    config.delimiters = ['{{', '}}']
+  })
+
+  it('tokens to expression', function () {
+    var tokens = textParser.parse('view-{{test}}-test-{{ok}}')
+    console.log(tokens)
+    var exp = textParser.tokensToExp(tokens)
+    expect(exp).toBe('"view-"+test+"-test-"+ok')
   })
 
 })