Browse Source

more refactor

dev
Evan You 9 years ago
parent
commit
9999e95161
  1. 20
      src/compiler/codegen/directives/ref.js
  2. 12
      src/compiler/codegen/directives/show.js
  3. 8
      src/compiler/codegen/directives/text.js
  4. 2
      src/compiler/codegen/index.js
  5. 17
      src/compiler/helpers.js
  6. 3
      src/runtime/instance/proxy.js

20
src/compiler/codegen/directives/ref.js

@ -1,7 +1,6 @@
import { addHook } from '../../helpers'
export function ref (el, dir) {
if (!el.hooks) {
el.hooks = {}
}
// go up and check if this node is inside a v-for
let isFor = false
let parent = el
@ -12,16 +11,7 @@ export function ref (el, dir) {
parent = parent.parent
}
// __registerRef__(name, ref, vFor?, remove?)
const code = `__registerRef__("${dir.arg}", vnode.data.child || vnode.elm, ${isFor ? 'true' : 'false'}`
patchHook(el.hooks, 'insert', `${code})`)
patchHook(el.hooks, 'destroy', `${code}, true)`)
}
const replaceRE = /^function\(vnode\)\{(.*)\}$/
function patchHook (hooks, name, code) {
if (hooks[name]) {
hooks[name] = hooks[name].replace(replaceRE, `function(vnode){$1;${code}}`)
} else {
hooks[name] = `function(vnode){${code}}`
}
const code = `__registerRef__("${dir.arg}", n1.data.child || n1.elm, ${isFor ? 'true' : 'false'}`
addHook(el, 'insert', `${code})`)
addHook(el, 'destroy', `${code}, true)`)
}

12
src/compiler/codegen/directives/show.js

@ -1,12 +1,8 @@
import { addStyleBinding } from '../../helpers'
export function show (el, dir) {
patchNode(el, `display:(${dir.value}?'':'none')`)
addStyleBinding(el, 'display', `(${dir.value}?'':'none')`)
if (el.elseBlock) {
patchNode(el.elseBlock, `display:(${dir.value}?'none':'')`)
addStyleBinding(el.elseBlock, 'display', `(${dir.value}?'none':'')`)
}
}
function patchNode (el, code) {
el.styleBinding = el.styleBinding
? el.styleBinding.replace(/}\s?$/, `${code},}`)
: `{${code}}`
}

8
src/compiler/codegen/directives/text.js

@ -1,8 +1,6 @@
import { addProp } from '../../helpers'
export function text (el, dir) {
if (!dir.value) return
if (!el.props) el.props = []
el.props.push({
name: 'textContent',
value: `__toString__(${dir.value})`
})
addProp(el, 'textContent', `__toString__(${dir.value})`)
}

2
src/compiler/codegen/index.js

@ -159,7 +159,7 @@ function genProps (props) {
function genHooks (hooks) {
let res = ''
for (let key in hooks) {
res += `"${key}":${hooks[key]},`
res += `"${key}":function(n1,n2){${hooks[key].join(';')}},`
}
return res.slice(0, -1)
}

17
src/compiler/helpers.js

@ -23,7 +23,7 @@ export function getAndRemoveAttr (el, name) {
}
export function addHandler (el, name, value, modifiers) {
const events = (el.events || (el.events = {}))
const events = el.events || (el.events = {})
// check capture modifier
if (modifiers && modifiers.capture) {
delete modifiers.capture
@ -52,6 +52,19 @@ export function addDirective (el, name, value, arg, modifiers) {
(el.directives || (el.directives = [])).push({ name, value, arg, modifiers })
}
export function addStyle (el, name, value) {
export function addStyleBinding (el, name, value) {
const code = `"${name}":${value}`
el.styleBinding = el.styleBinding
? el.styleBinding.replace(/}\s?$/, `${code},}`)
: `{${code}}`
}
export function addHook (el, name, code) {
const hooks = el.hooks || (el.hooks = {})
const hook = hooks[name]
if (hook) {
hook.push(code)
} else {
hooks[name] = [code]
}
}

3
src/runtime/instance/proxy.js

@ -1,9 +1,8 @@
import { warn, inBrowser, makeMap } from '../util/index'
import { warn, makeMap } from '../util/index'
let hasProxy, proxyHandlers, initProxy
if (process.env.NODE_ENV !== 'production') {
const context = inBrowser ? window : global
const allowedGlobals = makeMap(
'Infinity,undefined,NaN,isFinite,isNaN,' +
'parseFloat,parseInt,decodeURI,decodeURIComponent,encodeURI,encodeURIComponent,' +

Loading…
Cancel
Save