Преглед изворни кода

remove literal directive concept

Evan You пре 10 година
родитељ
комит
737a85de88

+ 3 - 3
src/compiler/compile-props.js

@@ -6,7 +6,6 @@ var propBindingModes = require('../config')._propBindingModes
 // regexes
 var identRE = require('../parsers/path').identRE
 var settablePathRE = /^[A-Za-z_$][\w$]*(\.[A-Za-z_$][\w$]*|\[[^\[\]]+\])*$/
-var literalValueRE = /^(true|false)$|^\d.*|^'[^']*'$|^"[^"]*"$/
 
 /**
  * Compile props on a root element and return
@@ -28,6 +27,7 @@ module.exports = function compileProps (el, propOptions) {
     if (process.env.NODE_ENV !== 'production' && name === '$data') {
       _.warn('Do not use $data as prop.')
       el.removeAttribute('$data')
+      el.removeAttribute(':$data')
       el.removeAttribute('bind-$data')
       continue
     }
@@ -66,7 +66,7 @@ module.exports = function compileProps (el, propOptions) {
         value = parsed.expression
         prop.filters = parsed.filters
         // check binding type
-        if (literalValueRE.test(value)) {
+        if (_.isLiteral(value)) {
           // for bind- literals such as numbers and booleans,
           // there's no need to setup a prop binding, so we
           // can optimize them as a one-time set.
@@ -160,7 +160,7 @@ function makePropsLinkFn (props) {
         }
       } else if (prop.optimizedLiteral) {
         // optimized literal, cast it and just set once
-        raw = _.stripQuotes(raw) || raw
+        raw = _.stripQuotes(raw)
         value = _.toBoolean(_.toNumber(raw))
         _.initProp(vm, prop, value)
       } else {

+ 3 - 11
src/compiler/compile.js

@@ -9,7 +9,7 @@ var templateParser = require('../parsers/template')
 var resolveAsset = _.resolveAsset
 
 // special binding prefixes
-var bindRE = /^bind-/
+var bindRE = /^bind-|^:/
 var onRE = /^on-/
 
 // terminal directives
@@ -552,21 +552,13 @@ function compileDirectives (attrs, options) {
     // Core directive
     if (name.indexOf(config.prefix) === 0) {
       dirName = name.slice(config.prefix.length)
-
-      // check literal directive
-      if (dirName.charAt(dirName.length - 1) === ':') {
-        dirName = dirName.slice(0, -1)
-        isLiteral = true
-      } else {
-        isLiteral = false
-      }
-
       dirDef = resolveAsset(options, 'directives', dirName)
-
       if (process.env.NODE_ENV !== 'production') {
         _.assertAsset(dirDef, 'directive', dirName)
       }
       if (dirDef) {
+        isLiteral = _.isLiteral(value)
+        if (isLiteral) value = _.stripQuotes(value)
         pushDir(dirName, dirDef, {
           literal: isLiteral
         })

+ 1 - 1
src/parsers/directive.js

@@ -44,7 +44,7 @@ function processFilterArg (arg) {
   var stripped = reservedArgRE.test(arg)
     ? arg
     : _.stripQuotes(arg)
-  var dynamic = stripped === false
+  var dynamic = stripped === arg
   return {
     value: dynamic ? arg : stripped,
     dynamic: dynamic

+ 13 - 1
src/util/lang.js

@@ -1,5 +1,17 @@
 var Dep = require('../observer/dep')
 
+/**
+ * Check if an expression is a literal value.
+ *
+ * @param {String} exp
+ * @return {Boolean}
+ */
+
+var literalValueRE = /^\s?(true|false|[\d\.]+|'[^']*'|"[^"]*")\s?$/
+exports.isLiteral = function (exp) {
+  return literalValueRE.test(exp)
+}
+
 /**
  * Check is a string starts with $ or _
  *
@@ -72,7 +84,7 @@ exports.stripQuotes = function (str) {
   var b = str.charCodeAt(str.length - 1)
   return a === b && (a === 0x22 || a === 0x27)
     ? str.slice(1, -1)
-    : false
+    : str
 }
 
 /**

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

@@ -49,7 +49,7 @@ if (_.inBrowser) {
 
     it('normal directives', function () {
       el.setAttribute('v-a', 'b')
-      el.innerHTML = '<p v-a="a" v-b="b">hello</p><div v-b:="b"></div>'
+      el.innerHTML = '<p v-a="a" v-b="b">hello</p><div v-b="1"></div>'
       var defA = { priority: 1 }
       var defB = { priority: 2 }
       var options = _.mergeOptions(Vue.options, {
@@ -89,7 +89,7 @@ if (_.inBrowser) {
       // 4
       args = vm._bindDir.calls.argsFor(3)
       expect(args[0].name).toBe('b')
-      expect(args[0].expression).toBe('b')
+      expect(args[0].expression).toBe('1')
       expect(args[0].def).toBe(defB)
       expect(args[0].literal).toBe(true)
       expect(args[1]).toBe(el.lastChild)

+ 12 - 1
test/unit/specs/util/lang_spec.js

@@ -2,6 +2,17 @@ var _ = require('../../../../src/util')
 
 describe('Util - Language Enhancement', function () {
 
+  it('isLiteral', function () {
+    expect(_.isLiteral('123')).toBe(true)
+    expect(_.isLiteral('12.3')).toBe(true)
+    expect(_.isLiteral('true')).toBe(true)
+    expect(_.isLiteral(' false ')).toBe(true)
+    expect(_.isLiteral('"hi"')).toBe(true)
+    expect(_.isLiteral(" 'whatt' ")).toBe(true)
+    expect(_.isLiteral('a.b.c')).toBe(false)
+    expect(_.isLiteral('1 + 1')).toBe(false)
+  })
+
   it('toString', function () {
     expect(_.toString('hi')).toBe('hi')
     expect(_.toString(1.234)).toBe('1.234')
@@ -21,7 +32,7 @@ describe('Util - Language Enhancement', function () {
   it('strip quotes', function () {
     expect(_.stripQuotes('"123"')).toBe('123')
     expect(_.stripQuotes("'fff'")).toBe('fff')
-    expect(_.stripQuotes("'fff")).toBe(false)
+    expect(_.stripQuotes("'fff")).toBe("'fff")
   })
 
   it('camelize', function () {