Przeglądaj źródła

mvoe text parser into codegen helper

Evan You 10 lat temu
rodzic
commit
e577a1aa3c

+ 45 - 0
src/compiler/codegen/helpers.js

@@ -11,3 +11,48 @@ export function getAndRemoveAttr (el, attr) {
   }
   return val
 }
+
+const modifierRE = /\.[^\.]+/g
+
+export function parseModifiers (name) {
+  var res = Object.create(null)
+  var match = name.match(modifierRE)
+  if (match) {
+    var i = match.length
+    while (i--) {
+      res[match[i].slice(1)] = true
+    }
+  }
+  return res
+}
+
+export function removeModifiers (name) {
+  return name.replace(modifierRE, '')
+}
+
+const tagRE = /\{\{((?:.|\\n)+?)\}\}/g
+export function parseText (text) {
+  if (!tagRE.test(text)) {
+    return null
+  }
+  var tokens = []
+  var lastIndex = tagRE.lastIndex = 0
+  var match, index, value
+  /* eslint-disable no-cond-assign */
+  while (match = tagRE.exec(text)) {
+  /* eslint-enable no-cond-assign */
+    index = match.index
+    // push text token
+    if (index > lastIndex) {
+      tokens.push(JSON.stringify(text.slice(lastIndex, index)))
+    }
+    // tag token
+    value = match[1]
+    tokens.push('(' + match[1].trim() + ')')
+    lastIndex = index + match[0].length
+  }
+  if (lastIndex < text.length) {
+    tokens.push(JSON.stringify(text.slice(lastIndex)))
+  }
+  return tokens.join('+')
+}

+ 18 - 8
src/compiler/codegen/index.js

@@ -1,8 +1,12 @@
 import config from '../../config'
-import { parseText } from '../text-parser'
 import { genEvents, addHandler } from './events'
 import { genModel } from './model'
-import { getAndRemoveAttr } from './helpers'
+import {
+  parseText,
+  parseModifiers,
+  removeModifiers,
+  getAndRemoveAttr
+} from './helpers'
 
 const bindRE = /^:|^v-bind:/
 const onRE = /^@|^v-on:/
@@ -51,10 +55,21 @@ function genData (el, key) {
   if (!el.attrs.length) {
     return '{}'
   }
+
   let data = '{'
+  let attrs = `attrs:{`
+  let props = `props:{`
+  let events = {}
+  let hasAttrs = false
+  let hasProps = false
+  let hasEvents = false
+
+  // key
   if (key) {
     data += `key:${key},`
   }
+
+  // class
   const classBinding = getAndRemoveAttr(el, ':class') || getAndRemoveAttr(el, 'v-bind:class')
   if (classBinding) {
     data += `class: ${classBinding},`
@@ -63,12 +78,6 @@ function genData (el, key) {
   if (staticClass) {
     data += `staticClass: "${staticClass}",`
   }
-  let attrs = `attrs:{`
-  let props = `props:{`
-  let events = {}
-  let hasAttrs = false
-  let hasProps = false
-  let hasEvents = false
 
   // parent elements my need to add props to children
   if (el.props) {
@@ -76,6 +85,7 @@ function genData (el, key) {
     props += el.props + ','
   }
 
+  // loop attributes
   for (let i = 0, l = el.attrs.length; i < l; i++) {
     let attr = el.attrs[i]
     let name = attr.name

+ 0 - 27
src/compiler/text-parser.js

@@ -1,27 +0,0 @@
-const tagRE = /\{\{((?:.|\\n)+?)\}\}/g
-
-export function parseText (text) {
-  if (!tagRE.test(text)) {
-    return null
-  }
-  var tokens = []
-  var lastIndex = tagRE.lastIndex = 0
-  var match, index, value
-  /* eslint-disable no-cond-assign */
-  while (match = tagRE.exec(text)) {
-  /* eslint-enable no-cond-assign */
-    index = match.index
-    // push text token
-    if (index > lastIndex) {
-      tokens.push(JSON.stringify(text.slice(lastIndex, index)))
-    }
-    // tag token
-    value = match[1]
-    tokens.push('(' + match[1].trim() + ')')
-    lastIndex = index + match[0].length
-  }
-  if (lastIndex < text.length) {
-    tokens.push(JSON.stringify(text.slice(lastIndex)))
-  }
-  return tokens.join('+')
-}