From bcecd71b4813193bfb88b421fd4a83b873454941 Mon Sep 17 00:00:00 2001 From: qiang Date: Fri, 17 Jul 2020 11:44:34 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=A7=A3=E5=86=B3=E5=B0=8F=E7=A8=8B?= =?UTF-8?q?=E5=BA=8F=E4=B8=8A=E5=85=B7=E5=90=8D=E6=8F=92=E6=A7=BDdefault?= =?UTF-8?q?=E5=92=8C=E9=BB=98=E8=AE=A4=E6=8F=92=E6=A7=BD=E4=B8=8D=E7=AD=89?= =?UTF-8?q?=E5=90=8C=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../__tests__/compiler-extra.spec.js | 6 +- .../__tests__/compiler-mp-alipay.spec.js | 4 +- .../__tests__/compiler-mp-baidu.spec.js | 98 +++++++++---------- .../__tests__/compiler.spec.js | 11 ++- .../lib/template/generate.js | 9 +- 5 files changed, 70 insertions(+), 58 deletions(-) diff --git a/packages/uni-template-compiler/__tests__/compiler-extra.spec.js b/packages/uni-template-compiler/__tests__/compiler-extra.spec.js index 11add830b..e8bc904d7 100644 --- a/packages/uni-template-compiler/__tests__/compiler-extra.spec.js +++ b/packages/uni-template-compiler/__tests__/compiler-extra.spec.js @@ -226,12 +226,12 @@ describe('mp:compiler-extra', () => { assertCodegen( 'text', - 'text' + 'text' ) assertCodegen( 'text123213', - 'text123213' + 'text123213' ) assertCodegen( '', @@ -676,4 +676,4 @@ describe('mp:compiler-extra', () => { '' ) }) -}) +}) diff --git a/packages/uni-template-compiler/__tests__/compiler-mp-alipay.spec.js b/packages/uni-template-compiler/__tests__/compiler-mp-alipay.spec.js index 83828d969..f1741249d 100644 --- a/packages/uni-template-compiler/__tests__/compiler-mp-alipay.spec.js +++ b/packages/uni-template-compiler/__tests__/compiler-mp-alipay.spec.js @@ -64,11 +64,11 @@ describe('mp:compiler-mp-alipay', () => { it('generate scoped slot', () => { assertCodegen( '', - '{{props.text}}' + '{{props.text}}' ) assertCodegen( '', - '{{__SCOPED__.text}}' + '{{__SCOPED__.text}}' ) }) diff --git a/packages/uni-template-compiler/__tests__/compiler-mp-baidu.spec.js b/packages/uni-template-compiler/__tests__/compiler-mp-baidu.spec.js index 42116261b..addfc54e7 100644 --- a/packages/uni-template-compiler/__tests__/compiler-mp-baidu.spec.js +++ b/packages/uni-template-compiler/__tests__/compiler-mp-baidu.spec.js @@ -1,7 +1,7 @@ const compiler = require('../lib') function assertCodegen (template, templateCode, renderCode = 'with(this){}', options = {}) { - const res = compiler.compile(template, { + const res = compiler.compile(template, { resourcePath: 'test.wxml', mp: Object.assign({ minified: true, @@ -24,11 +24,11 @@ describe('mp:compiler-mp-baidu', () => { it('generate scoped slot', () => { assertCodegen( '', - '{{foo}}' + '{{foo}}' ) assertCodegen( '{{ bar.foo }}', - '{{foo}}' + '{{foo}}' ) }) @@ -46,7 +46,7 @@ describe('mp:compiler-mp-baidu', () => { it('generate scoped slot with multiline v-if', () => { assertCodegen( '', - '{{foo}}' + '{{foo}}' ) assertCodegen( '{{ bar.foo }}', @@ -57,54 +57,54 @@ describe('mp:compiler-mp-baidu', () => { it('generate scoped slot', () => { assertCodegen( '{{ user.lastName }}', - '' + '' ) assertCodegen( '{{ user.lastName }}', '' ) - }) - - it('generate vue id', () => { - assertCodegen( - '', - '' - ) - assertCodegen( - '', - '' - ) - assertCodegen( - '', - '' - ) - assertCodegen( - '', - '' - ) - assertCodegen( - '', - '' - ) - }) - - it('generate text trim', () => { - assertCodegen( - '\nN: {{title}}\n′', - '{{\'N: \'+title+"\\\\n′"}}' - ) - assertCodegen( - '我是第一行1\n我的第二行', - '我是第一行1\n我的第二行' - ) - assertCodegen( - '我是第一行2\n我的第二行1{{title}}', - '{{"我是第一行2\\\\n我的第二行1"+title}}' - ) - assertCodegen( - `我是第一行3 - 我的第二行2{{title}}`, - '{{"我是第一行3\\\\n 我的第二行2"+title}}' - ) }) -}) + + it('generate vue id', () => { + assertCodegen( + '', + '' + ) + assertCodegen( + '', + '' + ) + assertCodegen( + '', + '' + ) + assertCodegen( + '', + '' + ) + assertCodegen( + '', + '' + ) + }) + + it('generate text trim', () => { + assertCodegen( + '\nN: {{title}}\n′', + '{{\'N: \'+title+"\\\\n′"}}' + ) + assertCodegen( + '我是第一行1\n我的第二行', + '我是第一行1\n我的第二行' + ) + assertCodegen( + '我是第一行2\n我的第二行1{{title}}', + '{{"我是第一行2\\\\n我的第二行1"+title}}' + ) + assertCodegen( + `我是第一行3 + 我的第二行2{{title}}`, + '{{"我是第一行3\\\\n 我的第二行2"+title}}' + ) + }) +}) diff --git a/packages/uni-template-compiler/__tests__/compiler.spec.js b/packages/uni-template-compiler/__tests__/compiler.spec.js index 0693d7d93..cccdc17a8 100644 --- a/packages/uni-template-compiler/__tests__/compiler.spec.js +++ b/packages/uni-template-compiler/__tests__/compiler.spec.js @@ -1,7 +1,7 @@ const compiler = require('../lib') function assertCodegen (template, templateCode, renderCode = 'with(this){}') { - const res = compiler.compile(template, { + const res = compiler.compile(template, { resourcePath: 'test.wxml', mp: { minified: true, @@ -172,6 +172,7 @@ describe('mp:compiler', () => { it('generate single slot', () => { assertCodegen('', '') + assertCodegen('', '') }) it('generate named slot', () => { @@ -193,6 +194,10 @@ describe('mp:compiler', () => { 'hello world', 'hello world' ) + assertCodegen( + 'hello world', + 'hello world' + ) }) // it('generate scoped slot', () => { @@ -486,7 +491,7 @@ describe('mp:compiler', () => { ) // normal named function assertCodegen( - '', + '', '', 'with(this){if(!_isMounted){e0=function fn(){current++}}}' ) @@ -665,4 +670,4 @@ describe('mp:compiler', () => { '' ) }) -}) +}) diff --git a/packages/uni-template-compiler/lib/template/generate.js b/packages/uni-template-compiler/lib/template/generate.js index 987130f1a..f78e04ee0 100644 --- a/packages/uni-template-compiler/lib/template/generate.js +++ b/packages/uni-template-compiler/lib/template/generate.js @@ -14,6 +14,13 @@ function processElement (ast, state, isRoot) { ast.type = 'view' } + // 由于小程序端 default 不等同于默认插槽,统一移除 default 命名 + if (ast.type === 'slot' && hasOwn(ast.attr, 'name') && ast.attr.name === 'default') { + delete ast.attr.name + } else if (hasOwn(ast.attr, 'slot') && ast.attr.slot === 'default') { + delete ast.attr.slot + } + if (hasOwn(ast.attr, 'textContent')) { ast.children = [ast.attr.textContent] delete ast.attr.textContent @@ -145,4 +152,4 @@ module.exports = function generate (ast, state) { } return code -} +} -- GitLab