Skip to content

Commit 5800e0b

Browse files
authored
fix: compatible with old js widget and add manufacturer media query (#100)
* fix: compatible with older jscard Signed-off-by: lileirjyb <lileirjyb@vivo.com> * feat: add manufacturer media query Signed-off-by: lileirjyb <lileirjyb@vivo.com> * fix: fix bug for const expression Signed-off-by: lileirjyb <lileirjyb@vivo.com> --------- Signed-off-by: lileirjyb <lileirjyb@vivo.com>
1 parent 8002037 commit 5800e0b

File tree

18 files changed

+274
-190
lines changed

18 files changed

+274
-190
lines changed

packages/hap-compiler/src/style/mediaquery.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ const featureValidatorMap = {
4747
'prefers-color-scheme': 'preferColorScheme',
4848
scene: 'scene',
4949
'widget-size': 'widgetSize',
50-
'device-type': 'deviceType'
50+
'device-type': 'deviceType',
51+
manufacturer: 'manufacturer'
5152
}
5253

5354
/**
@@ -168,6 +169,23 @@ const featureValidator = {
168169
}
169170
}
170171
},
172+
manufacturer(value) {
173+
const reg = /^(xiaomi|vivo|OPPO|honor)$/
174+
if (reg.test(value)) {
175+
return { value }
176+
}
177+
return {
178+
reason: function (feature) {
179+
return (
180+
'WARN: 媒体特征 `' +
181+
feature +
182+
'` 的值 `' +
183+
value +
184+
'` 不正确, 必须为 `xiaomi | vivo | OPPO| honor`'
185+
)
186+
}
187+
}
188+
},
171189
ratio(value) {
172190
const reg = /^(\d+\s*\/\s*\d+|\d+\.\d+|\d+)$/
173191
if (reg.test(value)) {

packages/hap-compiler/src/template/exp.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function trimhtml(str) {
3535
* @param isLite is lite card
3636
* @returns {*}
3737
*/
38-
function transExpr(expContent, toFunc, isLite, isCard) {
38+
function transExpr(expContent, toFunc, isLite, isNewJSCard) {
3939
let ret
4040
const trimExpContent = expContent.trim()
4141
if (!textParser.isExpr(trimExpContent)) {
@@ -64,7 +64,7 @@ function transExpr(expContent, toFunc, isLite, isCard) {
6464
ret = ret.join(' + ')
6565
if (toFunc !== false) {
6666
try {
67-
if (isCard) {
67+
if (isNewJSCard) {
6868
ret = 'function () {return ' + ret + '}'
6969
} else {
7070
/* eslint-disable no-eval */

packages/hap-compiler/src/template/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ function parse(source, options) {
366366
result: {},
367367
log: [],
368368
depFiles: [],
369-
isCard: !!options.card,
369+
isNewJSCard: !!options.newJSCard,
370370
isLite: !!options.lite
371371
}
372372

packages/hap-compiler/src/template/model.js

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,8 @@ function genCheckboxModel(node, attrName, value, output) {
102102
${expValue} = checked ? ${trueValueBinding} : ${falseValueBinding}
103103
}`
104104

105-
const isCard = output.isCard
106-
const isLite = output.isLite
107-
if (isCard && !isLite) {
105+
const isNewJSCard = output.isNewJSCard
106+
if (isNewJSCard) {
108107
addAttr(output.result, attrName, `(function() {${attrCheckedCode}})`)
109108
addAttr(output.result, attrName + 'Raw', value)
110109
addHandler(output.result, 'change', `function(evt) {${eventChangeCode}}`)
@@ -132,9 +131,8 @@ function genRadioModel(node, attrName, value, output) {
132131
const attrCheckedCode = `return ${exp(value, false)} === ${valueBinding}`
133132
const eventChangeCode = `${exp(value, false)} = ${valueBinding}`
134133

135-
const isCard = output.isCard
136-
const isLite = output.isLite
137-
if (isCard && !isLite) {
134+
const isNewJSCard = output.isNewJSCard
135+
if (isNewJSCard) {
138136
addAttr(output.result, attrName, `(function() {${attrCheckedCode}})`)
139137
addAttr(output.result, attrName + 'Raw', value)
140138
addHandler(output.result, 'change', `function(evt) {${eventChangeCode}}`)
@@ -155,9 +153,8 @@ function genRadioModel(node, attrName, value, output) {
155153
* @param {object} output 构建的输出结果
156154
*/
157155
function genSelectModel(value, output) {
158-
const isCard = output.isCard
159-
const isLite = output.isLite
160-
if (isCard && !isLite) {
156+
const isNewJSCard = output.isNewJSCard
157+
if (isNewJSCard) {
161158
addHandler(output.result, 'change', `function(evt) { ${exp(value, false)} = evt.newValue}`)
162159
} else {
163160
addHandler(
@@ -177,10 +174,10 @@ function genSelectModel(value, output) {
177174
*/
178175
function genDefaultModel(attrName, value, output) {
179176
const eventChangeCode = `${exp(value, false)} = evt.target.value`
180-
const isCard = output.isCard
177+
const isNewJSCard = output.isNewJSCard
181178
const isLite = output.isLite
182-
if (isCard && !isLite) {
183-
addAttr(output.result, attrName, exp(value, true, isLite, isCard))
179+
if (isNewJSCard) {
180+
addAttr(output.result, attrName, exp(value, true, isLite, isNewJSCard))
184181
addAttr(output.result, attrName + 'Raw', value)
185182
addHandler(output.result, 'change', `function(evt) {${eventChangeCode}}`)
186183
} else {
@@ -203,9 +200,8 @@ function genComponentModel(node, attrName, value, output, locationInfo, options)
203200
// 自定义组件model指令绑定的属性,依然作为普通属性处理
204201
validator.checkAttr(attrName, value, output, node.tagName, locationInfo, options)
205202

206-
const isCard = output.isCard
207-
const isLite = output.isLite
208-
if (isCard && !isLite) {
203+
const isNewJSCard = output.isNewJSCard
204+
if (isNewJSCard) {
209205
addHandler(
210206
output.result,
211207
`update:${attrName}`,
@@ -233,10 +229,10 @@ function genDynamicModel(node, attrName, value, output, expType) {
233229
const checkboxCode = genCheckboxModel(node, attrName, value, output)
234230
const radioCode = genRadioModel(node, attrName, value, output)
235231
const textCode = genDefaultModel(attrName, value, output)
236-
const isCard = output.isCard
232+
const isNewJSCard = output.isNewJSCard
237233
const isLite = output.isLite
238-
if (isCard && !isLite) {
239-
addAttr(output.result, attrName, exp(value, true, isLite, isCard))
234+
if (isNewJSCard) {
235+
addAttr(output.result, attrName, exp(value, true, isLite, isNewJSCard))
240236
addAttr(output.result, attrName + 'Raw', value)
241237

242238
addAttr(

packages/hap-compiler/src/template/validator.js

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,9 +1421,9 @@ function checkTagName(node, output, options = {}) {
14211421
function checkId(id, output) {
14221422
if (id) {
14231423
const isLite = output.isLite
1424-
const isCard = output.isCard
1425-
output.result.id = exp.isExpr(id) ? exp(id, true, isLite, isCard) : id
1426-
if (isCard && !isLite) {
1424+
const isNewJSCard = output.isNewJSCard
1425+
output.result.id = exp.isExpr(id) ? exp(id, true, isLite, isNewJSCard) : id
1426+
if (isNewJSCard) {
14271427
output.result.idRaw = id
14281428
}
14291429
}
@@ -1462,7 +1462,7 @@ function checkClass(className, output) {
14621462

14631463
className = className.trim()
14641464
const isLite = output.isLite
1465-
const isCard = output.isCard
1465+
const isNewJSCard = output.isNewJSCard
14661466
if (className) {
14671467
let start = 0
14681468
let end = 0
@@ -1509,7 +1509,7 @@ function checkClass(className, output) {
15091509
} else if (hasBinding) {
15101510
try {
15111511
let code = ''
1512-
if (isCard) {
1512+
if (isNewJSCard) {
15131513
code = 'function () {return [' + classList.join(', ') + ']}'
15141514
output.result.classList = code
15151515
} else {
@@ -1530,11 +1530,9 @@ function checkClass(className, output) {
15301530
)
15311531
}
15321532
}
1533-
if (isCard) {
1533+
if (isNewJSCard) {
15341534
output.result.class = className
1535-
if (!isLite) {
1536-
output.result.classListRaw = className
1537-
}
1535+
output.result.classListRaw = className
15381536
}
15391537
}
15401538

@@ -1552,7 +1550,7 @@ function checkStyle(cssText, output, locationInfo, options) {
15521550
const log = output.log
15531551
if (cssText) {
15541552
const isLite = output.isLite
1555-
const isCard = output.isCard
1553+
const isNewJSCard = output.isNewJSCard
15561554
if (exp.singleExpr(cssText)) {
15571555
// 检测是否嵌套{{}}
15581556
const incText = exp.removeExprffix(cssText)
@@ -1563,10 +1561,10 @@ function checkStyle(cssText, output, locationInfo, options) {
15631561
reason: 'ERROR: style 属性不能嵌套多层{{}}'
15641562
})
15651563
} else {
1566-
style = exp(cssText, true, isLite, isCard)
1564+
style = exp(cssText, true, isLite, isNewJSCard)
15671565
}
15681566
output.result.style = style
1569-
if (isCard && !isLite) {
1567+
if (isNewJSCard) {
15701568
output.result.styleRaw = cssText
15711569
}
15721570
return
@@ -1586,14 +1584,14 @@ function checkStyle(cssText, output, locationInfo, options) {
15861584
v = pair[1].trim()
15871585

15881586
const valueRaw = v
1589-
v = exp(v, true, isLite, isCard) // 处理值表达式
1587+
v = exp(v, true, isLite, isNewJSCard) // 处理值表达式
15901588
vResult = styler.validateDelaration(k, v, options)
15911589
v = vResult.value
15921590
v.forEach((t) => {
15931591
// 如果校验成功,则保存转换后的属性值
15941592
if (isValidValue(t.v) || typeof t.v === 'function') {
15951593
style[t.n] = t.v
1596-
if (isCard && !isLite) {
1594+
if (isNewJSCard) {
15971595
styleRaw[t.n] = valueRaw
15981596
}
15991597
}
@@ -1616,7 +1614,7 @@ function checkStyle(cssText, output, locationInfo, options) {
16161614
}
16171615
}
16181616
output.result.style = style
1619-
if (isCard && !isLite) {
1617+
if (isNewJSCard) {
16201618
output.result.styleRaw = styleRaw
16211619
}
16221620
}
@@ -1634,11 +1632,11 @@ function checkIs(value, output, locationInfo) {
16341632
// 如果没有,补充上{{}}
16351633
value = exp.addExprffix(value)
16361634
const isLite = output.isLite
1637-
const isCard = output.isCard
1635+
const isNewJSCard = output.isNewJSCard
16381636

16391637
// 将表达式转换为function
1640-
output.result.is = exp(value, true, isLite, isCard)
1641-
if (isCard && !isLite) {
1638+
output.result.is = exp(value, true, isLite, isNewJSCard)
1639+
if (isNewJSCard) {
16421640
output.result.isRaw = value
16431641
}
16441642
} else {
@@ -1662,7 +1660,7 @@ function checkIf(value, output, not, locationInfo, conditionList) {
16621660
// 如果没有,补充上{{}}
16631661
value = exp.addExprffix(value)
16641662
const isLite = output.isLite
1665-
const isCard = output.isCard
1663+
const isNewJSCard = output.isNewJSCard
16661664
if (not) {
16671665
value = '{{' + buildConditionExp(conditionList) + '}}'
16681666
} else {
@@ -1671,8 +1669,8 @@ function checkIf(value, output, not, locationInfo, conditionList) {
16711669
conditionList.push(`${value.substr(2, value.length - 4)}`)
16721670
}
16731671
// 将表达式转换为function
1674-
output.result.shown = isLite ? value : exp(value, true, isLite, isCard)
1675-
if (isCard && !isLite) {
1672+
output.result.shown = isLite ? value : exp(value, true, isLite, isNewJSCard)
1673+
if (isNewJSCard) {
16761674
output.result.shownRaw = value
16771675
}
16781676
} else {
@@ -1712,13 +1710,13 @@ function checkElif(value, cond, output, locationInfo, conditionList) {
17121710
value = exp.addExprffix(value)
17131711
cond = exp.addExprffix(cond)
17141712
const isLite = output.isLite
1715-
const isCard = output.isCard
1713+
const isNewJSCard = output.isNewJSCard
17161714
newcond =
17171715
'{{(' + value.substr(2, value.length - 4) + ') && ' + buildConditionExp(conditionList) + '}}'
17181716

17191717
// 将表达式转换为function
1720-
output.result.shown = isLite ? newcond : exp(newcond, true, isLite, isCard)
1721-
if (isCard && !isLite) {
1718+
output.result.shown = isLite ? newcond : exp(newcond, true, isLite, isNewJSCard)
1719+
if (isNewJSCard) {
17221720
output.result.shownRaw = newcond
17231721
}
17241722
conditionList.push(`${value.substr(2, value.length - 4)}`)
@@ -1761,14 +1759,14 @@ function checkFor(value, output, locationInfo) {
17611759
value = '{{' + value + '}}'
17621760

17631761
const isLite = output.isLite
1764-
const isCard = output.isCard
1762+
const isNewJSCard = output.isNewJSCard
17651763
let repeat, repeatRaw
17661764
if (!key && !val) {
1767-
repeat = exp(value, true, isLite, isCard)
1765+
repeat = exp(value, true, isLite, isNewJSCard)
17681766
repeatRaw = value
17691767
} else {
17701768
// 如果指定key,value
1771-
repeat = { exp: exp(value, true, isLite, isCard) }
1769+
repeat = { exp: exp(value, true, isLite, isNewJSCard) }
17721770
repeatRaw = { expRaw: value }
17731771
if (key) {
17741772
repeat.key = key
@@ -1778,7 +1776,7 @@ function checkFor(value, output, locationInfo) {
17781776
}
17791777
}
17801778
output.result.repeat = repeat
1781-
if (isCard && !isLite) {
1779+
if (isNewJSCard) {
17821780
output.result.repeatRaw = repeatRaw
17831781
}
17841782
} else {
@@ -1829,7 +1827,7 @@ function checkEvent(name, value, output) {
18291827
value = '{{' + funcName + '(' + params.join(',') + ')}}'
18301828
try {
18311829
// 将事件转换为函数对象
1832-
if (output.isCard && !output.isLite) {
1830+
if (output.isNewJSCard) {
18331831
value = 'function (evt) { return ' + exp(value, false).replace('this.evt', 'evt') + '}'
18341832
} else {
18351833
/* eslint-disable no-eval */
@@ -1863,22 +1861,21 @@ function checkCustomDirective(name, value, output, node) {
18631861
colorconsole.warn(`\`${node.tagName}\` 组件自定义指令名称不能为空`)
18641862
return false
18651863
}
1866-
const isCard = output.isCard
1867-
const isLite = output.isLite
1864+
const isNewJSCard = output.isNewJSCard
18681865
output.result.directives = output.result.directives || []
1869-
if (isCard && !isLite) {
1866+
if (isNewJSCard) {
18701867
// 补全绑定值的双花括号,如:dir:指令名称="data"补全为dir:指令名称="{{data}}"
18711868
value = exp.addExprffix(value)
18721869

18731870
output.result.directives.push({
18741871
name: dirName,
1875-
value: exp.isExpr(value) ? exp(value, true, output.isLite, output.isCard) : value,
1872+
value: exp.isExpr(value) ? exp(value, true, output.isLite, output.isNewJSCard) : value,
18761873
valueRaw: value
18771874
})
18781875
} else {
18791876
output.result.directives.push({
18801877
name: dirName,
1881-
value: exp.isExpr(value) ? exp(value, true, output.isLite, output.isCard) : value
1878+
value: exp.isExpr(value) ? exp(value, true, output.isLite, output.isNewJSCard) : value
18821879
})
18831880
}
18841881
}
@@ -1911,10 +1908,10 @@ function checkAttr(name, value, output, tagName, locationInfo, options) {
19111908
output.depFiles.push(value)
19121909
}
19131910
const isLite = output.isLite
1914-
const isCard = output.isCard
1911+
const isNewJSCard = output.isNewJSCard
19151912
output.result.attr = output.result.attr || {}
1916-
output.result.attr[hyphenedToCamelCase(name)] = exp(value, true, isLite, isCard)
1917-
if (isCard && !isLite) {
1913+
output.result.attr[hyphenedToCamelCase(name)] = exp(value, true, isLite, isNewJSCard)
1914+
if (isNewJSCard) {
19181915
output.result.attr[hyphenedToCamelCase(name) + 'Raw'] = value
19191916
}
19201917
if (name === 'value' && tagName === 'text') {

packages/hap-dsl-xvm/src/loaders/style-loader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export default function styleLoader(code) {
4141
query: loaderQuery
4242
})
4343

44-
if (compileOptionsObject.enableExtractCss && !options.card) {
44+
if (compileOptionsObject.enableExtractCss && !options.newJSCard) {
4545
componentId.add(resourcePath)
4646
if (jsonStyle) {
4747
jsonStyle[`@info`] = {

0 commit comments

Comments
 (0)