Skip to content

Commit

Permalink
feat($compiler): compile weex native directives in preTransformNode
Browse files Browse the repository at this point in the history
  • Loading branch information
Hanks10100 authored and yyx990803 committed Dec 19, 2017
1 parent 9bd1483 commit 2d09ee3
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 67 deletions.
2 changes: 1 addition & 1 deletion src/compiler/parser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const forIteratorRE = /,([^,\}\]]*)(?:,([^,\}\]]*))?$/
const stripParensRE = /^\(|\)$/g

const argRE = /:(.*)$/
const bindRE = /^:|^v-bind:/
export const bindRE = /^:|^v-bind:/
const modifierRE = /\.[^.]+/g

const literalValueRE = /^(\{.*\}|\[.*\])$/
Expand Down
9 changes: 6 additions & 3 deletions src/platforms/weex/compiler/modules/recycle-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ function preTransformNode (el: ASTElement) {
if (el.tag === 'recycle-list') {
currentRecycleList = el
}
if (currentRecycleList) {
// TODO
transformVBind(el)
transformVIf(el)
transformVFor(el)
}
}

function transformNode (el: ASTElement) {
if (currentRecycleList) {
// TODO
transformVIf(el)
transformVFor(el)
transformVBind(el)
}
}

Expand Down
31 changes: 15 additions & 16 deletions src/platforms/weex/compiler/modules/recycle-list/v-bind.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
/* @flow */

import { camelize } from 'shared/util'
import { getAndRemoveAttr, addAttr } from 'compiler/helpers'

function isBindingAttr (name: string): boolean {
return /^(v\-bind)?\:/.test(name)
}
import { bindRE } from 'compiler/parser/index'
import { getAndRemoveAttr } from 'compiler/helpers'

function parseAttrName (name: string): string {
return camelize(name.replace(/^(v\-bind)?\:/, ''))
return camelize(name.replace(bindRE, ''))
}

export function transformVBind (el: ASTElement) {
if (!el.attrsList || !el.attrsList.length) {
return
}
el.attrsList.forEach(attr => {
if (isBindingAttr(attr.name)) {
const name: string = parseAttrName(attr.name)
const binding = getAndRemoveAttr(el, attr.name)
addAttr(el, name, { '@binding': binding })
for (const attr in el.attrsMap) {
if (bindRE.test(attr)) {
const name: string = parseAttrName(attr)
const value = {
'@binding': getAndRemoveAttr(el, attr)
}
delete el.attrsMap[attr]
el.attrsMap[name] = value
el.attrsList.push({ name, value })
// addAttr(el, name, value)
// el.hasBindings = false
}
})
el.hasBindings = false
}
}
44 changes: 20 additions & 24 deletions src/platforms/weex/compiler/modules/recycle-list/v-for.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,31 @@
/* @flow */

import { addAttr } from 'compiler/helpers'

function isVForAttr (name: string): boolean {
return /^v\-for/.test(name)
}
import { forAliasRE, forIteratorRE } from 'compiler/parser/index'
import { getAndRemoveAttr } from 'compiler/helpers'

export function transformVFor (el: ASTElement) {
for (const attr in el.attrsMap) {
if (!isVForAttr(attr)) {
continue
}
const exp = getAndRemoveAttr(el, 'v-for')
if (!exp) {
return
}
const inMatch = exp.match(forAliasRE)
if (inMatch) {
const alias = inMatch[1].trim()
const desc: Object = {
'@expression': el.for,
'@alias': el.alias
}
if (el.iterator1) {
desc['@index'] = el.iterator1
'@expression': inMatch[2].trim(),
'@alias': alias
}
if (el.iterator2) {
desc['@key'] = el.iterator1
desc['@index'] = el.iterator2
const iteratorMatch = alias.match(forIteratorRE)
if (iteratorMatch) {
desc['@alias'] = iteratorMatch[1].trim()
desc['@index'] = iteratorMatch[2].trim()
if (iteratorMatch[3]) {
desc['@key'] = iteratorMatch[2].trim()
desc['@index'] = iteratorMatch[3].trim()
}
}
addAttr(el, '[[repeat]]', desc)
delete el.attrsMap['v-for']
el.attrsMap['[[repeat]]'] = desc
el.attrsList.push({ name: '[[repeat]]', value: desc })
delete el.attrsMap[attr]
delete el.for
delete el.alias
delete el.key
delete el.iterator1
delete el.iterator2
}
}
30 changes: 7 additions & 23 deletions src/platforms/weex/compiler/modules/recycle-list/v-if.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,13 @@
/* @flow */

import { getAndRemoveAttr, addAttr } from 'compiler/helpers'

function isConditionAttr (name: string): boolean {
return /^v\-if|v\-else|v\-else\-if/.test(name)
}
import { getAndRemoveAttr } from 'compiler/helpers'

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
}
const exp = getAndRemoveAttr(el, 'v-if')
if (exp) {
el.attrsMap['[[match]]'] = exp
el.attrsList.push({ name: '[[match]]', value: exp })
delete el.attrsMap['v-if']
}
// TODO: support v-else and v-else-if
}

0 comments on commit 2d09ee3

Please sign in to comment.