提交 f46520cf 编写于 作者: Q qiang

fix(mp-baidu, mp-alipay): scoped slot root element use block

上级 344e1ccd
......@@ -92,7 +92,7 @@ module.exports = {
normalizeChildren
}, state) {
const node = {
type: 'view',
type: 'block',
attr: {
slot: slotName
}
......
......@@ -24,7 +24,7 @@ module.exports = {
normalizeChildren
}, state) {
const node = {
type: 'view',
type: 'block',
attr: {
slot: slotName
},
......
......@@ -90,18 +90,18 @@ describe('mp:compiler-mp-alipay', () => {
)
assertCodegen(
'<component1>text<block slot="right"></block></component1>',
'<component1 vue-id="551070e6-1" onVueInit="__l" vue-slots="{{[\'default\',\'right\']}}">text<view slot="right"></view></component1>'
'<component1 vue-id="551070e6-1" onVueInit="__l" vue-slots="{{[\'default\',\'right\']}}">text<block slot="right"></block></component1>'
)
})
it('generate scoped slot', () => {
assertCodegen(
'<component1 :text="\'text\'"><template v-slot="props"><view :class="{text:props.text}">{{props.text}}</view></template></component1>',
'<component1 vue-id="551070e6-1" text="text" onVueInit="__l" vue-slots="{{[\'default\']}}"><view slot-scope="props"><view class="{{((props.text)?\'text\':\'\')}}">{{props.text}}</view></view></component1>'
'<component1 vue-id="551070e6-1" text="text" onVueInit="__l" vue-slots="{{[\'default\']}}"><block slot-scope="props"><view class="{{((props.text)?\'text\':\'\')}}">{{props.text}}</view></block></component1>'
)
assertCodegen(
'<component1 :text="\'text\'"><template v-slot="{text}"><view :class="{text:text}">{{text}}</view></template></component1>',
'<component1 vue-id="551070e6-1" text="text" onVueInit="__l" vue-slots="{{[\'default\']}}"><view slot-scope="__SCOPED__"><view class="{{((__SCOPED__.text)?\'text\':\'\')}}">{{__SCOPED__.text}}</view></view></component1>'
'<component1 vue-id="551070e6-1" text="text" onVueInit="__l" vue-slots="{{[\'default\']}}"><block slot-scope="__SCOPED__"><view class="{{((__SCOPED__.text)?\'text\':\'\')}}">{{__SCOPED__.text}}</view></block></component1>'
)
})
......@@ -119,7 +119,7 @@ describe('mp:compiler-mp-alipay', () => {
it('generate scoped slot with scopedSlotsCompiler: auto', () => {
assertCodegen(
'<my-component><template v-slot="{item}">{{item}}<template></my-component>',
'<my-component vue-id="551070e6-1" onVueInit="__l" vue-slots="{{[\'default\']}}"><view slot-scope="__SCOPED__">{{__SCOPED__.item}}</view></my-component>',
'<my-component vue-id="551070e6-1" onVueInit="__l" vue-slots="{{[\'default\']}}"><block slot-scope="__SCOPED__">{{__SCOPED__.item}}</block></my-component>',
'with(this){}',
{
scopedSlotsCompiler: 'auto'
......@@ -135,7 +135,7 @@ describe('mp:compiler-mp-alipay', () => {
)
assertCodegen(
'<my-component><template v-slot="{item}">{{item}}{{title}}<template></my-component>',
'<my-component vue-id="551070e6-1" onVueInit="__l" vue-slots="{{[\'default\']}}"><view slot-scope="__SCOPED__">{{__SCOPED__.item+title}}</view></my-component>',
'<my-component vue-id="551070e6-1" onVueInit="__l" vue-slots="{{[\'default\']}}"><block slot-scope="__SCOPED__">{{__SCOPED__.item+title}}</block></my-component>',
'with(this){}',
{
scopedSlotsCompiler: 'auto'
......@@ -143,7 +143,7 @@ describe('mp:compiler-mp-alipay', () => {
)
assertCodegen(
'<my-component><template v-slot="{item}">{{item}}{{getValue(title)}}<template></my-component>',
'<my-component vue-id="551070e6-1" onVueInit="__l" vue-slots="{{[\'default\']}}"><view slot-scope="__SCOPED__">{{__SCOPED__.item+$root.m0}}</view></my-component>',
'<my-component vue-id="551070e6-1" onVueInit="__l" vue-slots="{{[\'default\']}}"><block slot-scope="__SCOPED__">{{__SCOPED__.item+$root.m0}}</block></my-component>',
'with(this){var m0=getValue(title);$mp.data=Object.assign({},{$root:{m0:m0}})}',
{
scopedSlotsCompiler: 'auto'
......
......@@ -36,33 +36,33 @@ describe('mp:compiler-mp-baidu', () => {
it('generate scoped slot', () => {
assertCodegen(
'<foo><template slot-scope="bar">{{ bar.foo }}</template></foo>',
'<foo vue-id="551070e6-1" vue-slots="{{[\'default\']}}"><view>{{foo}}</view></foo>'
'<foo vue-id="551070e6-1" vue-slots="{{[\'default\']}}"><block>{{foo}}</block></foo>'
)
assertCodegen(
'<foo><view slot-scope="bar">{{ bar.foo }}</view></foo>',
'<foo vue-id="551070e6-1" vue-slots="{{[\'default\']}}"><view><view>{{foo}}</view></view></foo>'
'<foo vue-id="551070e6-1" vue-slots="{{[\'default\']}}"><block><view>{{foo}}</view></block></foo>'
)
})
it('generate named scoped slot', () => {
assertCodegen(
'<foo><template slot="foo" slot-scope="bar">{{ bar.foo }}</template></foo>',
'<foo vue-id="551070e6-1" vue-slots="{{[\'foo\']}}"><view slot="foo">{{foo}}</view></foo>'
'<foo vue-id="551070e6-1" vue-slots="{{[\'foo\']}}"><block slot="foo">{{foo}}</block></foo>'
)
assertCodegen(
'<foo><view slot="foo" slot-scope="bar">{{ bar.foo }}</view></foo>',
'<foo vue-id="551070e6-1" vue-slots="{{[\'foo\']}}"><view slot="foo"><view>{{foo}}</view></view></foo>'
'<foo vue-id="551070e6-1" vue-slots="{{[\'foo\']}}"><block slot="foo"><view>{{foo}}</view></block></foo>'
)
})
it('generate scoped slot with multiline v-if', () => {
assertCodegen(
'<foo><template v-if="\nshow\n" slot-scope="bar">{{ bar.foo }}</template></foo>',
'<foo vue-id="551070e6-1" vue-slots="{{[\'default\']}}"><view><block s-if="{{show}}">{{foo}}</block><block s-else><block></block></block></view></foo>'
'<foo vue-id="551070e6-1" vue-slots="{{[\'default\']}}"><block><block s-if="{{show}}">{{foo}}</block><block s-else><block></block></block></block></foo>'
)
assertCodegen(
'<foo><view v-if="\nshow\n" slot="foo" slot-scope="bar">{{ bar.foo }}</view></foo>',
'<foo vue-id="551070e6-1" vue-slots="{{[\'foo\']}}"><view slot="foo"><block s-if="{{show}}"><view>{{foo}}</view></block></view></foo>'
'<foo vue-id="551070e6-1" vue-slots="{{[\'foo\']}}"><block slot="foo"><block s-if="{{show}}"><view>{{foo}}</view></block></block></foo>'
)
})
......@@ -88,14 +88,14 @@ describe('mp:compiler-mp-baidu', () => {
)
assertCodegen(
'<foo><template v-slot:[test]="{user}"><view>{{user}}</view></template></foo>',
'<foo vue-id="551070e6-1" vue-slots="{{[test]}}"><view slot="{{test}}"><view>{{user}}</view></view></foo>'
'<foo vue-id="551070e6-1" vue-slots="{{[test]}}"><block slot="{{test}}"><view>{{user}}</view></block></foo>'
)
})
it('generate scoped slot with scopedSlotsCompiler: auto', () => {
assertCodegen(
'<my-component><template v-slot="{item}">{{item}}<template></my-component>',
'<my-component vue-id="551070e6-1" vue-slots="{{[\'default\']}}"><view>{{item}}</view></my-component>',
'<my-component vue-id="551070e6-1" vue-slots="{{[\'default\']}}"><block>{{item}}</block></my-component>',
'with(this){}',
{
scopedSlotsCompiler: 'auto'
......@@ -111,7 +111,7 @@ describe('mp:compiler-mp-baidu', () => {
)
assertCodegen(
'<my-component><template v-slot="{item}">{{item}}{{title}}<template></my-component>',
'<my-component vue-id="551070e6-1" vue-slots="{{[\'default\']}}"><view>{{item+title}}</view></my-component>',
'<my-component vue-id="551070e6-1" vue-slots="{{[\'default\']}}"><block>{{item+title}}</block></my-component>',
'with(this){}',
{
scopedSlotsCompiler: 'auto'
......@@ -119,7 +119,7 @@ describe('mp:compiler-mp-baidu', () => {
)
assertCodegen(
'<my-component><template v-slot="{item}">{{item}}{{getValue(title)}}<template></my-component>',
'<my-component vue-id="551070e6-1" vue-slots="{{[\'default\']}}"><view>{{item+$root.m0}}</view></my-component>',
'<my-component vue-id="551070e6-1" vue-slots="{{[\'default\']}}"><block>{{item+$root.m0}}</block></my-component>',
'with(this){var m0=getValue(title);$mp.data=Object.assign({},{$root:{m0:m0}})}',
{
scopedSlotsCompiler: 'auto'
......
......@@ -73,14 +73,14 @@ function processElement (ast, state, isRoot) {
for (let i = children.length - 1; i >= 0; i--) {
const childElement = children[i]
/**
* 仅百度、字节支持使用 block 作为命名插槽根节点
* 百度、字节、支付宝小程序支持使用 block 作为命名插槽根节点,支付宝不支持在 block 使用动态插槽名
* <block slot="left"></block> => <view slot="left"></view>
*/
const attr = typeof childElement !== 'string' && childElement.attr
const slot = attr && (attr[ATTR_SLOT_ORIGIN] || attr.slot)
if (slot) {
delete attr[ATTR_SLOT_ORIGIN]
if (!['mp-baidu', 'mp-toutiao'].includes(platformName) && attr.slot && attr.slot !== 'default' && childElement.type === 'block') {
if (!['mp-baidu', 'mp-toutiao'].includes(platformName) && !(platformName === 'mp-alipay' && !/\{\{.+?\}\}/.test(attr.slot)) && attr.slot && attr.slot !== 'default' && childElement.type === 'block') {
childElement.type = 'view'
}
if (!slots.includes(slot)) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册