Hanks
7 years ago
committed by
Evan You
4 changed files with 42 additions and 16 deletions
@ -1,31 +1,26 @@ |
|||||
/* @flow */ |
/* @flow */ |
||||
|
|
||||
|
import { camelize } from 'shared/util' |
||||
import { getAndRemoveAttr, addAttr } from 'compiler/helpers' |
import { getAndRemoveAttr, addAttr } from 'compiler/helpers' |
||||
|
|
||||
function isBindingAttr (name) { |
function isBindingAttr (name: string): boolean { |
||||
return /^(v\-bind)?\:/.test(name) |
return /^(v\-bind)?\:/.test(name) |
||||
} |
} |
||||
|
|
||||
function parseRealName (name: string): string { |
function parseAttrName (name: string): string { |
||||
return name.replace(/^(v\-bind)?\:/, '') |
return camelize(name.replace(/^(v\-bind)?\:/, '')) |
||||
} |
} |
||||
|
|
||||
export function transformVBind (el: ASTElement) { |
export function transformVBind (el: ASTElement) { |
||||
if (!el.attrsList.length) { |
if (!el.attrsList || !el.attrsList.length) { |
||||
return |
return |
||||
} |
} |
||||
el.attrsList.forEach(attr => { |
el.attrsList.forEach(attr => { |
||||
// console.log('is binding attr:', attr.name, isBindingAttr(attr.name))
|
|
||||
if (isBindingAttr(attr.name)) { |
if (isBindingAttr(attr.name)) { |
||||
const realName: string = parseRealName(attr.name) |
const name: string = parseAttrName(attr.name) |
||||
const binding = getAndRemoveAttr(el, attr.name) |
const binding = getAndRemoveAttr(el, attr.name) |
||||
if (el.attrs) { |
addAttr(el, name, { '@binding': binding }) |
||||
el.attrs = el.attrs.filter(at => at.name !== realName) // omit duplicated
|
|
||||
} |
|
||||
getAndRemoveAttr(el, realName) |
|
||||
addAttr(el, realName, { '@binding': binding }) |
|
||||
} |
} |
||||
}) |
}) |
||||
el.hasBindings = false |
el.hasBindings = false |
||||
// el.plain = true
|
|
||||
} |
} |
||||
|
@ -0,0 +1,29 @@ |
|||||
|
/* @flow */ |
||||
|
|
||||
|
import { getAndRemoveAttr, addAttr } from 'compiler/helpers' |
||||
|
|
||||
|
function isConditionAttr (name: string): boolean { |
||||
|
return /^v\-if|v\-else|v\-else\-if/.test(name) |
||||
|
} |
||||
|
|
||||
|
export function transformVIf (el: ASTElement) { |
||||
|
for (const attr in el.attrsMap) { |
||||
|
if (!isConditionAttr(attr)) { |
||||
|
continue |
||||
|
} |
||||
|
const binding = getAndRemoveAttr(el, attr) |
||||
|
switch (attr) { |
||||
|
case 'v-if': { |
||||
|
addAttr(el, '[[match]]', binding) |
||||
|
el.attrsMap['[[match]]'] = binding |
||||
|
el.attrsList.push({ name: '[[match]]', value: binding }) |
||||
|
delete el.attrsMap[attr] |
||||
|
delete el.if |
||||
|
delete el.ifConditions |
||||
|
break |
||||
|
} |
||||
|
|
||||
|
// TODO: support v-else and v-else-if
|
||||
|
} |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue