提交 af4a2304 编写于 作者: fxy060608's avatar fxy060608

refactor: boolean attribute

上级 fd2cbbf1
......@@ -250,6 +250,16 @@ describe('codegen', () => {
'<view> {{text}} text text {{text}} </view>',
`with(this){return _c('view',[_v((_$s(0,'t0-0',_s(text)))+(_$s(0,'t0-1',_s(text))))])}`
)
})
it('generate bool attr', () => {
assertCodegen(
'<video controls/>',
`with(this){return _c('video',{attrs:{"_i":0}})}`
)
assertCodegen(
'<video controls=""/>',
`with(this){return _c('video',{})}`
)
})
})
/* eslint-enable quotes */
......@@ -189,6 +189,16 @@ describe('codegen', () => {
'<view> {{text}} text text {{text}} </view>',
`with(this){return _c('v-uni-view',{attrs:{"_i":0}},[_v((_$g(0,'t0-0'))+" text text "+(_$g(0,'t0-1')))])}`
)
})
it('generate bool attr', () => {
assertCodegen(
'<video controls/>',
`with(this){return _c('v-uni-video',{attrs:{"controls":true,"_i":0}})}`
)
assertCodegen(
'<video controls=""/>',
`with(this){return _c('v-uni-video',{attrs:{"controls":"","_i":0}})}`
)
})
})
/* eslint-enable quotes */
......@@ -646,5 +646,15 @@ describe('mp:compiler-extra', () => {
'<view v-for="item in list" @click="test(item)">{{ item }}</view>',
'<block wx:for="{{list}}" wx:for-item="item" wx:for-index="__i0__"><view data-event-opts="{{[[\'tap\',[[\'test\',[\'$0\'],[[[\'list\',\'\',__i0__]]]]]]]}}" bindtap="__e">{{item}}</view></block>'
)
})
it('generate bool attr', () => {
assertCodegen(
'<video controls/>',
'<video controls="{{true}}"></video>'
)
assertCodegen(
'<video controls=""/>',
'<video controls></video>'
)
})
})
......@@ -19,7 +19,7 @@ const scopedPath = path.resolve(__dirname, '../../')
const compiler = require('../lib')
const res = compiler.compile(
`
<view> {{text}} text text {{text}} </view>
<video controls=""/>
`, {
miniprogram: true,
resourcePath: '/User/fxy/Documents/test.wxml',
......@@ -32,9 +32,9 @@ const res = compiler.compile(
mp: {
platform: 'mp-weixin'
},
filterModules: ['swipe'],
filterModules: ['swipe']
// service: true,
view: true
// view: true
})
console.log(require('util').inspect(res, {
......
......@@ -4,32 +4,10 @@ const {
addRawAttr
} = require('./util')
const dirRE = /^v-|^@|^:|^#/
/**
* 兼容小程序Boolean属性的怪异行为(<custom loading/>为true,<custom loading=""/>为false)
* @param {Object} el
*/
function fixBooleanAttribute (el) {
if (!el.attrsList) {
return
}
el.attrsList.forEach(attr => {
if (attr.bool) { // <custom loading/> => <custom :loading="true"/>
if (!dirRE.test(attr.name) && attr.name !== 'inline-template') {
delete el.attrsMap[attr.name]
attr.name = ':' + attr.name
attr.value = 'true'
el.attrsMap[attr.name] = attr.value
}
}
})
}
module.exports = function preTransformNode (el, options) {
if (!hasOwn(options, 'nid')) {
options.nid = 0
}
fixBooleanAttribute(el)
addRawAttr(el, ID, options.nid++)
if (el.attrsMap['v-for']) {
el.forId = el.attrsMap[ID]
......
const dirRE = /^v-|^@|^:|^#/
module.exports = {
preTransformNode (el) {
if (!el.attrsList) {
return
}
el.attrsList.forEach(attr => {
if (attr.bool) {
if (!dirRE.test(attr.name) && attr.name !== 'inline-template') {
delete el.attrsMap[attr.name]
attr.name = ':' + attr.name
attr.value = 'true'
el.attrsMap[attr.name] = attr.value
}
}
})
}
}
......@@ -44,9 +44,12 @@ module.exports = {
) {
(options.modules || (options.modules = [])).push(autoComponentsModule)
}
if (!options.modules) {
options.modules = []
}
// transformAssetUrls
(options.modules || (options.modules = [])).push(require('./asset-url'))
options.modules.push(require('./asset-url'))
options.modules.push(require('./bool-attr'))
options.isUnaryTag = isUnaryTag
// 将 autoComponents 挂在 isUnaryTag 上边
......@@ -54,7 +57,7 @@ module.exports = {
options.preserveWhitespace = false
if (options.service) {
(options.modules || (options.modules = [])).push(require('./app/service'))
options.modules.push(require('./app/service'))
options.optimize = false // 启用 staticRenderFns
// domProps => attrs
options.mustUseProp = () => false
......@@ -68,7 +71,7 @@ module.exports = {
throw e
}
} else if (options.view) {
(options.modules || (options.modules = [])).push(require('./app/view'))
options.modules.push(require('./app/view'))
options.optimize = false // 暂不启用 staticRenderFns
options.isUnaryTag = isUnaryTag
options.isReservedTag = (tagName) => false // 均为组件
......@@ -80,14 +83,14 @@ module.exports = {
}
} else if (options['quickapp-native']) {
// 后续改版,应统一由具体包实现
(options.modules || (options.modules = [])).push(require('@dcloudio/uni-quickapp-native/lib/compiler-module'))
options.modules.push(require('@dcloudio/uni-quickapp-native/lib/compiler-module'))
}
if (!options.mp) { // h5,quickapp-native
return compileTemplate(source, options, compile)
}
(options.modules || (options.modules = [])).push(compilerModule)
options.modules.push(compilerModule)
if (options.mp.platform === 'mp-alipay') {
options.modules.push(compilerAlipayModule)
......@@ -147,7 +150,7 @@ module.exports = {
delete state.files
// resolve scoped slots
res.generic = state.generic || []
res.generic = state.generic || []
delete state.generic
// define scoped slots
......
......@@ -105,7 +105,8 @@ const oldCompile = compiler.compile
compiler.compile = function (source, options = {}) {
(options.modules || (options.modules = [])).push(autoComponentsModule)
options.modules.push(require('@dcloudio/uni-template-compiler/lib/asset-url'))
options.modules.push(require('@dcloudio/uni-template-compiler/lib/asset-url'))
options.modules.push(require('@dcloudio/uni-template-compiler/lib/bool-attr'))
options.isUnaryTag = isUnaryTag
// 将 autoComponents 挂在 isUnaryTag 上边
......
......@@ -479,7 +479,9 @@ function parseHTML (html, options) {
: options.shouldDecodeNewlines;
attrs[i] = {
name: args[1],
value: decodeAttr(value, shouldDecodeNewlines)
value: decodeAttr(value, shouldDecodeNewlines),
// fixed by xxxxxx 标记 Boolean Attribute
bool: args[3] === undefined && args[4] === undefined && args[5] === undefined
};
if (process.env.NODE_ENV !== 'production' && options.outputSourceRange) {
attrs[i].start = args.start + args[0].match(/^\s*/).length;
......
......@@ -40,32 +40,10 @@ function addTag (tag) {
process.UNI_TAGS.add(tag)
}
const dirRE = /^v-|^@|^:/
/**
* 兼容小程序Boolean属性的怪异行为(<custom loading/>为true,<custom loading=""/>为false)
* @param {Object} el
*/
function fixBooleanAttribute (el) {
if (!el.attrsList) {
return
}
el.attrsList.forEach(attr => {
if (attr.bool) { // <custom loading/> => <custom :loading="true"/>
if (!dirRE.test(attr.name) && attr.name !== 'inline-template') {
delete el.attrsMap[attr.name]
attr.name = ':' + attr.name
attr.value = 'true'
el.attrsMap[attr.name] = attr.value
}
}
})
}
module.exports = {
h5: true,
modules: [require('../format-text'), {
preTransformNode (el, options) {
fixBooleanAttribute(el)
if (el.tag.indexOf('v-uni-') === 0) {
addTag(el.tag.replace('v-uni-', ''))
} else if (hasOwn(tags, el.tag)) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册