Skip to content

Commit

Permalink
feat(wx-to-taro): 处理特殊键值
Browse files Browse the repository at this point in the history
  • Loading branch information
yuche authored and luckyadam committed Nov 19, 2018
1 parent 7617fe5 commit 0a8475d
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions packages/wx-to-taro/src/wxml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ type AllKindNode = Element | Comment | Text
type Node = Element | Text

export function parseWXML (wxml: string) {
const nodes = (parse(wxml) as AllKindNode[]).filter(node => node.type !== NodeType.Comment) as Node[]
const a = nodes.map(parseNode)
const nodes = (parse(wxml.trim()) as AllKindNode[]).filter(node => node.type !== NodeType.Comment) as Node[]
return nodes.map(parseNode).find(node => t.isJSXElement(node))
}

Expand Down Expand Up @@ -112,12 +111,38 @@ function parseContent (content: string) {
function parseAttribute (attr: Attribute) {
const { key, value } = attr

const jsxKey = handleAttrKey(key)
let jsxValue: null | t.JSXExpressionContainer | t.StringLiteral = null

if (value) {
const { type, content } = parseContent(value)
jsxValue = type === 'raw' ? t.stringLiteral(content) : t.jsxExpressionContainer(buildTemplate(content))
}

return t.jsxAttribute(t.jsxIdentifier(key), jsxValue)
return t.jsxAttribute(t.jsxIdentifier(jsxKey), jsxValue)
}

function handleAttrKey (key: string) {
if (key.startsWith('wx:') || key.startsWith('wx-')) {
return key
}

let jsxKey = camelCase(key)

switch (jsxKey) {
case 'onClick':
jsxKey = 'onClick'
break
case 'class':
jsxKey = 'className'
break
default:
jsxKey = jsxKey.replace(/^(bind|catch)/, 'on')
if (jsxKey.startsWith('on') && jsxKey.length > 2) {
jsxKey = jsxKey.substr(0, 2) + jsxKey[2].toUpperCase() + jsxKey.substr(3)
}
break
}

return jsxKey
}

0 comments on commit 0a8475d

Please sign in to comment.