Browse Source

fix literal casting in compilation

Evan You 10 years ago
parent
commit
7244c6997b

+ 4 - 2
src/compiler/compile-props.js

@@ -165,8 +165,10 @@ function makePropsLinkFn (props) {
         }
       } else if (prop.optimizedLiteral) {
         // optimized literal, cast it and just set once
-        raw = _.stripQuotes(raw)
-        value = _.toBoolean(_.toNumber(raw))
+        var stripped = _.stripQuotes(raw)
+        value = stripped === raw
+          ? _.toBoolean(_.toNumber(raw))
+          : stripped
         _.initProp(vm, prop, value)
       } else {
         // string literal, but we need to cater for

+ 0 - 4
src/compiler/compile.js

@@ -662,10 +662,6 @@ function compileDirectives (attrs, options) {
       }
 
       if (dirDef) {
-        if (_.isLiteral(value)) {
-          value = _.stripQuotes(value)
-          modifiers.literal = true
-        }
         pushDir(dirName, dirDef)
       }
     }

+ 1 - 4
src/directives/public/for.js

@@ -495,11 +495,8 @@ module.exports = {
       }
       return res
     } else {
-      var type = typeof value
-      if (type === 'number') {
+      if (typeof value === 'number') {
         value = range(value)
-      } else if (type === 'string') {
-        value = _.toArray(value)
       }
       return value || []
     }

+ 5 - 2
test/unit/specs/compiler/compile_spec.js

@@ -90,7 +90,6 @@ if (_.inBrowser) {
       expect(args[0].name).toBe('b')
       expect(args[0].expression).toBe('1')
       expect(args[0].def).toBe(defB)
-      expect(args[0].modifiers.literal).toBe(true)
       expect(args[1]).toBe(el.firstChild)
       // 4 (explicit literal)
       args = vm._bindDir.calls.argsFor(3)
@@ -245,12 +244,14 @@ if (_.inBrowser) {
         testTwoWay: null,
         twoWayWarn: null,
         testOneTime: null,
-        optimizeLiteral: null
+        optimizeLiteral: null,
+        optimizeLiteralStr: null
       }
       el.innerHTML = '<div ' +
         'v-bind:test-normal="a" ' +
         'test-literal="1" ' +
         ':optimize-literal="1" ' +
+        ':optimize-literal-str="\'true\'"' +
         ':test-two-way.sync="a" ' +
         ':two-way-warn.sync="a + 1" ' +
         ':test-one-time.once="a"></div>'
@@ -261,6 +262,8 @@ if (_.inBrowser) {
       expect(vm._data.testLiteral).toBe('1')
       expect(vm.optimizeLiteral).toBe(1)
       expect(vm._data.optimizeLiteral).toBe(1)
+      expect(vm.optimizeLiteralStr).toBe('true')
+      expect(vm._data.optimizeLiteralStr).toBe('true')
       // one time
       expect(vm.testOneTime).toBe('from parent: a')
       expect(vm._data.testOneTime).toBe('from parent: a')