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

refactor: boolean attribute

上级 fd2cbbf1
...@@ -251,5 +251,15 @@ describe('codegen', () => { ...@@ -251,5 +251,15 @@ describe('codegen', () => {
`with(this){return _c('view',[_v((_$s(0,'t0-0',_s(text)))+(_$s(0,'t0-1',_s(text))))])}` `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 */ /* eslint-enable quotes */
...@@ -190,5 +190,15 @@ describe('codegen', () => { ...@@ -190,5 +190,15 @@ describe('codegen', () => {
`with(this){return _c('v-uni-view',{attrs:{"_i":0}},[_v((_$g(0,'t0-0'))+" text text "+(_$g(0,'t0-1')))])}` `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 */ /* eslint-enable quotes */
...@@ -647,4 +647,14 @@ describe('mp:compiler-extra', () => { ...@@ -647,4 +647,14 @@ describe('mp:compiler-extra', () => {
'<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>' '<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, '../../') ...@@ -19,7 +19,7 @@ const scopedPath = path.resolve(__dirname, '../../')
const compiler = require('../lib') const compiler = require('../lib')
const res = compiler.compile( const res = compiler.compile(
` `
<view> {{text}} text text {{text}} </view> <video controls=""/>
`, { `, {
miniprogram: true, miniprogram: true,
resourcePath: '/User/fxy/Documents/test.wxml', resourcePath: '/User/fxy/Documents/test.wxml',
...@@ -32,9 +32,9 @@ const res = compiler.compile( ...@@ -32,9 +32,9 @@ const res = compiler.compile(
mp: { mp: {
platform: 'mp-weixin' platform: 'mp-weixin'
}, },
filterModules: ['swipe'], filterModules: ['swipe']
// service: true, // service: true,
view: true // view: true
}) })
console.log(require('util').inspect(res, { console.log(require('util').inspect(res, {
......
...@@ -4,32 +4,10 @@ const { ...@@ -4,32 +4,10 @@ const {
addRawAttr addRawAttr
} = require('./util') } = 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) { module.exports = function preTransformNode (el, options) {
if (!hasOwn(options, 'nid')) { if (!hasOwn(options, 'nid')) {
options.nid = 0 options.nid = 0
} }
fixBooleanAttribute(el)
addRawAttr(el, ID, options.nid++) addRawAttr(el, ID, options.nid++)
if (el.attrsMap['v-for']) { if (el.attrsMap['v-for']) {
el.forId = el.attrsMap[ID] 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 = { ...@@ -44,9 +44,12 @@ module.exports = {
) { ) {
(options.modules || (options.modules = [])).push(autoComponentsModule) (options.modules || (options.modules = [])).push(autoComponentsModule)
} }
if (!options.modules) {
options.modules = []
}
// transformAssetUrls // transformAssetUrls
(options.modules || (options.modules = [])).push(require('./asset-url')) options.modules.push(require('./asset-url'))
options.modules.push(require('./bool-attr'))
options.isUnaryTag = isUnaryTag options.isUnaryTag = isUnaryTag
// 将 autoComponents 挂在 isUnaryTag 上边 // 将 autoComponents 挂在 isUnaryTag 上边
...@@ -54,7 +57,7 @@ module.exports = { ...@@ -54,7 +57,7 @@ module.exports = {
options.preserveWhitespace = false options.preserveWhitespace = false
if (options.service) { if (options.service) {
(options.modules || (options.modules = [])).push(require('./app/service')) options.modules.push(require('./app/service'))
options.optimize = false // 启用 staticRenderFns options.optimize = false // 启用 staticRenderFns
// domProps => attrs // domProps => attrs
options.mustUseProp = () => false options.mustUseProp = () => false
...@@ -68,7 +71,7 @@ module.exports = { ...@@ -68,7 +71,7 @@ module.exports = {
throw e throw e
} }
} else if (options.view) { } else if (options.view) {
(options.modules || (options.modules = [])).push(require('./app/view')) options.modules.push(require('./app/view'))
options.optimize = false // 暂不启用 staticRenderFns options.optimize = false // 暂不启用 staticRenderFns
options.isUnaryTag = isUnaryTag options.isUnaryTag = isUnaryTag
options.isReservedTag = (tagName) => false // 均为组件 options.isReservedTag = (tagName) => false // 均为组件
...@@ -80,14 +83,14 @@ module.exports = { ...@@ -80,14 +83,14 @@ module.exports = {
} }
} else if (options['quickapp-native']) { } 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 if (!options.mp) { // h5,quickapp-native
return compileTemplate(source, options, compile) return compileTemplate(source, options, compile)
} }
(options.modules || (options.modules = [])).push(compilerModule) options.modules.push(compilerModule)
if (options.mp.platform === 'mp-alipay') { if (options.mp.platform === 'mp-alipay') {
options.modules.push(compilerAlipayModule) options.modules.push(compilerAlipayModule)
......
...@@ -106,6 +106,7 @@ compiler.compile = function (source, options = {}) { ...@@ -106,6 +106,7 @@ compiler.compile = function (source, options = {}) {
(options.modules || (options.modules = [])).push(autoComponentsModule) (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 options.isUnaryTag = isUnaryTag
// 将 autoComponents 挂在 isUnaryTag 上边 // 将 autoComponents 挂在 isUnaryTag 上边
......
...@@ -479,7 +479,9 @@ function parseHTML (html, options) { ...@@ -479,7 +479,9 @@ function parseHTML (html, options) {
: options.shouldDecodeNewlines; : options.shouldDecodeNewlines;
attrs[i] = { attrs[i] = {
name: args[1], 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) { if (process.env.NODE_ENV !== 'production' && options.outputSourceRange) {
attrs[i].start = args.start + args[0].match(/^\s*/).length; attrs[i].start = args.start + args[0].match(/^\s*/).length;
......
...@@ -40,32 +40,10 @@ function addTag (tag) { ...@@ -40,32 +40,10 @@ function addTag (tag) {
process.UNI_TAGS.add(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 = { module.exports = {
h5: true, h5: true,
modules: [require('../format-text'), { modules: [require('../format-text'), {
preTransformNode (el, options) { preTransformNode (el, options) {
fixBooleanAttribute(el)
if (el.tag.indexOf('v-uni-') === 0) { if (el.tag.indexOf('v-uni-') === 0) {
addTag(el.tag.replace('v-uni-', '')) addTag(el.tag.replace('v-uni-', ''))
} else if (hasOwn(tags, el.tag)) { } else if (hasOwn(tags, el.tag)) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册