Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 16 additions & 17 deletions packages/hap-compiler/src/template/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,27 +75,27 @@ function traverse(node, output, previousNode, conditionList, options) {
switch (name) {
case 'id':
// 保留checkId为兼容原有:新打的RPK包兼容原来的APK平台
validator.checkId(value, output)
validator.checkAttr(name, value, output, node.tagName, locationInfo)
validator.checkId(value, output, options)
validator.checkAttr(name, value, output, node.tagName, locationInfo, options)
break
case 'class':
validator.checkClass(value, output)
validator.checkClass(value, output, options)
break
case 'style':
validator.checkStyle(value, output, locationInfo, options)
break
case 'if':
if (!node._isroot) {
validator.checkIf(value, output, false, locationInfo, conditionList)
validator.checkIf(value, output, false, locationInfo, conditionList, options)
}
break
case 'is':
validator.checkIs(value, output, locationInfo)
validator.checkIs(value, output, locationInfo, options)
break
case 'else':
if (!node._isroot) {
if (previousNode && previousNode.__cond__) {
validator.checkElse(previousNode.__cond__, output, locationInfo, conditionList)
validator.checkElse(previousNode.__cond__, output, locationInfo, conditionList, options)
}
}
break
Expand All @@ -107,14 +107,15 @@ function traverse(node, output, previousNode, conditionList, options) {
previousNode.__cond__,
output,
locationInfo,
conditionList
conditionList,
options
)
}
}
break
case 'for':
if (!node._isroot) {
validator.checkFor(value, output, locationInfo)
validator.checkFor(value, output, locationInfo, options)
}
break
case 'tree':
Expand All @@ -123,14 +124,14 @@ function traverse(node, output, previousNode, conditionList, options) {
default:
if (name.match(/^(on|@)/)) {
// 事件以on或@开头
validator.checkEvent(name, value, output)
validator.checkEvent(name, value, output, options)
} else {
if (name.match(/^model:/)) {
// 解析model指令,model指令格式:model:name="{{youName}}"
validator.checkModel(name, value, output, node, locationInfo, options)
} else if (name.match(/^dir:/)) {
// 解析自定义指令,自定义指令格式:dir:指令名称="{{data}}"
validator.checkCustomDirective(name, value, output, node)
validator.checkCustomDirective(name, value, output, node, options)
} else {
// 其余为普通属性
validator.checkAttr(name, value, output, node.tagName, locationInfo, options)
Expand Down Expand Up @@ -195,17 +196,17 @@ function traverse(node, output, previousNode, conditionList, options) {
column: node.__location.col,
reason: `WARN: 文本和span标签并行存在,编译时将文本节点:"${child.value}" 用span包裹(关于span嵌套的使用,请参考官方文档"span嵌套")`
})
validator.checkAttr('value', child.value, output)
validator.checkAttr('value', child.value, output, null, null, options)
}

// 如果父节点是option, 处理value和content属性
if (node.tagName === 'option') {
const tempResult = output.result
output.result = originResult
if (!originResult.attr.hasOwnProperty('value')) {
validator.checkAttr('value', child.value, output)
validator.checkAttr('value', child.value, output, null, null, options)
}
validator.checkAttr('content', child.value, output)
validator.checkAttr('content', child.value, output, null, null, options)
output.result = tempResult
return
}
Expand All @@ -217,7 +218,7 @@ function traverse(node, output, previousNode, conditionList, options) {
) {
const tempResult = output.result // 备份当前result
output.result = originResult
validator.checkAttr('value', child.value, output)
validator.checkAttr('value', child.value, output, null, null, options)
output.result = tempResult
}
}
Expand Down Expand Up @@ -365,9 +366,7 @@ function parse(source, options) {
const output = {
result: {},
log: [],
depFiles: [],
isNewJSCard: !!options.newJSCard,
isLite: !!options.lite
depFiles: []
}

// 模板为空或解析失败
Expand Down
42 changes: 21 additions & 21 deletions packages/hap-compiler/src/template/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ export default function model(name, value, output, node, locationInfo, options)
/* eslint-disable no-eval */
if (!isDynamicType) {
if (tag === 'select') {
genSelectModel(value, output)
genSelectModel(value, output, options)
} else if (tag === 'input' && type === 'checkbox') {
genCheckboxModel(node, attrName, value, output)
genCheckboxModel(node, attrName, value, output, options)
} else if (tag === 'input' && type === 'radio') {
genRadioModel(node, attrName, value, output)
genRadioModel(node, attrName, value, output, options)
} else if (tag === 'input' || tag === 'textarea') {
genDefaultModel(attrName, value, output)
genDefaultModel(attrName, value, output, options)
} else if (
tag === 'component' ||
!validator.isReservedTag(tag) ||
Expand All @@ -64,7 +64,7 @@ export default function model(name, value, output, node, locationInfo, options)
genComponentModel(node, attrName, value, output, locationInfo, options)
}
} else {
genDynamicModel(node, attrName, value, output, type)
genDynamicModel(node, attrName, value, output, type, options)
}
}

Expand All @@ -76,7 +76,7 @@ export default function model(name, value, output, node, locationInfo, options)
* @param {object} output 构建的输出结果
* @returns {object} 构建过程中生成的代码字符串组成的对象,供 genDynamicModel 使用
*/
function genCheckboxModel(node, attrName, value, output) {
function genCheckboxModel(node, attrName, value, output, options) {
const expValue = exp(value, false)
const valueBinding = getBindingAttr(node, 'value', true) || 'null'
const trueValueBinding = getBindingAttr(node, 'true-value', true) || 'true'
Expand All @@ -102,7 +102,7 @@ function genCheckboxModel(node, attrName, value, output) {
${expValue} = checked ? ${trueValueBinding} : ${falseValueBinding}
}`

const isNewJSCard = output.isNewJSCard
const isNewJSCard = options.newJSCard
if (isNewJSCard) {
addAttr(output.result, attrName, `(function() {${attrCheckedCode}})`)
addAttr(output.result, attrName + 'Raw', value)
Expand All @@ -125,13 +125,13 @@ function genCheckboxModel(node, attrName, value, output) {
* @param {object} output 构建的输出结果
* @returns {object} 构建过程中生成的代码字符串组成的对象,供 genDynamicModel 使用
*/
function genRadioModel(node, attrName, value, output) {
function genRadioModel(node, attrName, value, output, options) {
const valueBinding = getBindingAttr(node, 'value', true) || 'null'

const attrCheckedCode = `return ${exp(value, false)} === ${valueBinding}`
const eventChangeCode = `${exp(value, false)} = ${valueBinding}`

const isNewJSCard = output.isNewJSCard
const isNewJSCard = options.newJSCard
if (isNewJSCard) {
addAttr(output.result, attrName, `(function() {${attrCheckedCode}})`)
addAttr(output.result, attrName + 'Raw', value)
Expand All @@ -152,8 +152,8 @@ function genRadioModel(node, attrName, value, output) {
* @param {string} value model 绑定的值
* @param {object} output 构建的输出结果
*/
function genSelectModel(value, output) {
const isNewJSCard = output.isNewJSCard
function genSelectModel(value, output, options) {
const isNewJSCard = options.newJSCard
if (isNewJSCard) {
addHandler(output.result, 'change', `function(evt) { ${exp(value, false)} = evt.newValue}`)
} else {
Expand All @@ -172,10 +172,10 @@ function genSelectModel(value, output) {
* @param {object} output 构建的输出结果
* @returns {object} 构建过程中生成的代码字符串组成的对象,供 genDynamicModel 使用
*/
function genDefaultModel(attrName, value, output) {
function genDefaultModel(attrName, value, output, options) {
const eventChangeCode = `${exp(value, false)} = evt.target.value`
const isNewJSCard = output.isNewJSCard
const isLite = output.isLite
const isNewJSCard = options.newJSCard
const isLite = options.lite
if (isNewJSCard) {
addAttr(output.result, attrName, exp(value, true, isLite, isNewJSCard))
addAttr(output.result, attrName + 'Raw', value)
Expand All @@ -200,7 +200,7 @@ function genComponentModel(node, attrName, value, output, locationInfo, options)
// 自定义组件model指令绑定的属性,依然作为普通属性处理
validator.checkAttr(attrName, value, output, node.tagName, locationInfo, options)

const isNewJSCard = output.isNewJSCard
const isNewJSCard = options.newJSCard
if (isNewJSCard) {
addHandler(
output.result,
Expand All @@ -225,12 +225,12 @@ function genComponentModel(node, attrName, value, output, locationInfo, options)
* @param {object} output 构建的输出结果
* @param {string} expType type 属性绑定的值
*/
function genDynamicModel(node, attrName, value, output, expType) {
const checkboxCode = genCheckboxModel(node, attrName, value, output)
const radioCode = genRadioModel(node, attrName, value, output)
const textCode = genDefaultModel(attrName, value, output)
const isNewJSCard = output.isNewJSCard
const isLite = output.isLite
function genDynamicModel(node, attrName, value, output, expType, options) {
const checkboxCode = genCheckboxModel(node, attrName, value, output, options)
const radioCode = genRadioModel(node, attrName, value, output, options)
const textCode = genDefaultModel(attrName, value, output, options)
const isNewJSCard = options.newJSCard
const isLite = options.lite
if (isNewJSCard) {
addAttr(output.result, attrName, exp(value, true, isLite, isNewJSCard))
addAttr(output.result, attrName + 'Raw', value)
Expand Down
Loading