From 4d285abb6bcd50d494c69d97d7ff8489da4b2df9 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Thu, 23 Apr 2020 09:57:29 +0800 Subject: [PATCH] fix(v3): text trim --- .../compiler-app-plus-extra.service.spec.js | 68 +++++++++++++++++++ .../compiler-app-plus-extra.view.spec.js | 68 +++++++++++++++++++ .../uni-template-compiler/__tests__/demo.js | 7 +- .../uni-template-compiler/lib/app/service.js | 4 +- .../uni-template-compiler/lib/app/view.js | 4 +- 5 files changed, 143 insertions(+), 8 deletions(-) diff --git a/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.service.spec.js b/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.service.spec.js index 0cbb25116..aea890b05 100644 --- a/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.service.spec.js +++ b/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.service.spec.js @@ -182,6 +182,74 @@ describe('codegen', () => { '', `with(this){return _c('image',{attrs:{"_i":0}})}` ) + }) + it('generate text trim', () => { + assertCodegen( + 'text', + `with(this){return _c('view')}` + ) + + assertCodegen( + ' text ', + `with(this){return _c('view')}` + ) + + assertCodegen( + '{{line_one_cn+\' \'}}', + `with(this){return _c('text',[_v((_$s(0,'t0-0',_s(line_one_cn+' '))))])}` + ) + + assertCodegen( + '{{" "+line_one_cn}}', + `with(this){return _c('text',[_v((_$s(0,'t0-0',_s(" "+line_one_cn))))])}` + ) + + assertCodegen( + '\nN: {{title}}\n′', + `with(this){return _c('text',[_v((_$s(0,'t0-0',_s(title))))])}` + ) + assertCodegen( + '我是第一行\n我的第二行', + `with(this){return _c('text')}` + ) + assertCodegen( + '我是第一行\n我的第二行1{{title}}', + `with(this){return _c('text',[_v((_$s(0,'t0-0',_s(title))))])}` + ) + assertCodegen( + `我是第一行 + 我的第二行2{{title}}`, + `with(this){return _c('text',[_v((_$s(0,'t0-0',_s(title))))])}` + ) + + assertCodegen( + ' text text ', + `with(this){return _c('view')}` + ) + assertCodegen( + 'text {{text}} text', + `with(this){return _c('view',[_v((_$s(0,'t0-0',_s(text))))])}` + ) + assertCodegen( + ' text {{text}} 文本 ', + `with(this){return _c('view',[_v((_$s(0,'t0-0',_s(text))))])}` + ) + assertCodegen( + '{{text}} text text ', + `with(this){return _c('view',[_v((_$s(0,'t0-0',_s(text))))])}` + ) + assertCodegen( + ' {{text}} text text ', + `with(this){return _c('view',[_v((_$s(0,'t0-0',_s(text))))])}` + ) + assertCodegen( + '{{text}} text text {{text}}', + `with(this){return _c('view',[_v((_$s(0,'t0-0',_s(text)))+(_$s(0,'t0-1',_s(text))))])}` + ) + assertCodegen( + ' {{text}} text text {{text}} ', + `with(this){return _c('view',[_v((_$s(0,'t0-0',_s(text)))+(_$s(0,'t0-1',_s(text))))])}` + ) }) }) /* eslint-enable quotes */ diff --git a/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.view.spec.js b/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.view.spec.js index 261a67392..a34429112 100644 --- a/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.view.spec.js +++ b/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.view.spec.js @@ -122,5 +122,73 @@ describe('codegen', () => { `with(this){return _c('v-uni-image',{attrs:{"src":"/static/logo.png","_i":0}})}` ) }) + it('generate text trim', () => { + assertCodegen( + 'text', + `with(this){return _c('v-uni-view',{attrs:{"_i":0}},[_v("text")])}` + ) + + assertCodegen( + ' text ', + `with(this){return _c('v-uni-view',{attrs:{"_i":0}},[_v("text")])}` + ) + + assertCodegen( + '{{line_one_cn+\' \'}}', + `with(this){return _c('v-uni-text',{attrs:{"_i":0}},[_v((_$g(0,'t0-0')))])}` + ) + + assertCodegen( + '{{" "+line_one_cn}}', + `with(this){return _c('v-uni-text',{attrs:{"_i":0}},[_v((_$g(0,'t0-0')))])}` + ) + + assertCodegen( + '\nN: {{title}}\n′', + `with(this){return _c('v-uni-text',{attrs:{"_i":0}},[_v("N: "+(_$g(0,'t0-0'))+"\\n′")])}` + ) + assertCodegen( + '我是第一行\n我的第二行', + `with(this){return _c('v-uni-text',{attrs:{"_i":0}},[_v("我是第一行\\n我的第二行")])}` + ) + assertCodegen( + '我是第一行\n我的第二行1{{title}}', + `with(this){return _c('v-uni-text',{attrs:{"_i":0}},[_v("我是第一行\\n我的第二行1"+(_$g(0,'t0-0')))])}` + ) + assertCodegen( + `我是第一行 + 我的第二行2{{title}}`, + `with(this){return _c('v-uni-text',{attrs:{"_i":0}},[_v("我是第一行\\n 我的第二行2"+(_$g(0,'t0-0')))])}` + ) + + assertCodegen( + ' text text ', + `with(this){return _c('v-uni-view',{attrs:{"_i":0}},[_v("text text")])}` + ) + assertCodegen( + 'text {{text}} text', + `with(this){return _c('v-uni-view',{attrs:{"_i":0}},[_v("text "+(_$g(0,'t0-0'))+" text")])}` + ) + assertCodegen( + ' text {{text}} 文本 ', + `with(this){return _c('v-uni-view',{attrs:{"_i":0}},[_v("text "+(_$g(0,'t0-0'))+" 文本")])}` + ) + assertCodegen( + '{{text}} text text ', + `with(this){return _c('v-uni-view',{attrs:{"_i":0}},[_v((_$g(0,'t0-0'))+" text text")])}` + ) + assertCodegen( + ' {{text}} text text ', + `with(this){return _c('v-uni-view',{attrs:{"_i":0}},[_v((_$g(0,'t0-0'))+" text text")])}` + ) + assertCodegen( + '{{text}} text text {{text}}', + `with(this){return _c('v-uni-view',{attrs:{"_i":0}},[_v((_$g(0,'t0-0'))+" text text "+(_$g(0,'t0-1')))])}` + ) + assertCodegen( + ' {{text}} text text {{text}} ', + `with(this){return _c('v-uni-view',{attrs:{"_i":0}},[_v((_$g(0,'t0-0'))+" text text "+(_$g(0,'t0-1')))])}` + ) + }) }) /* eslint-enable quotes */ diff --git a/packages/uni-template-compiler/__tests__/demo.js b/packages/uni-template-compiler/__tests__/demo.js index 39d59d4f8..75b8c58c5 100644 --- a/packages/uni-template-compiler/__tests__/demo.js +++ b/packages/uni-template-compiler/__tests__/demo.js @@ -19,12 +19,7 @@ const scopedPath = path.resolve(__dirname, '../../') const compiler = require('../lib') const res = compiler.compile( ` - - -123 -456 - - + {{text}} text text {{text}} `, { miniprogram: true, resourcePath: '/User/fxy/Documents/test.wxml', diff --git a/packages/uni-template-compiler/lib/app/service.js b/packages/uni-template-compiler/lib/app/service.js index 766af8348..21f691df3 100644 --- a/packages/uni-template-compiler/lib/app/service.js +++ b/packages/uni-template-compiler/lib/app/service.js @@ -105,7 +105,9 @@ function checkAutoFill (el) { } function transformNode (el, parent, state, isScopedSlot) { - if (el.type === 3) { + if (el.type === 3) { + // fixed by xxxxxx 注意:保持平台一致性,trim 一下,理论上service不需要,保险起见也处理一遍 + el.text = el.text.trim() return } parseBlock(el, parent) diff --git a/packages/uni-template-compiler/lib/app/view.js b/packages/uni-template-compiler/lib/app/view.js index 319613bfc..b0ab64641 100644 --- a/packages/uni-template-compiler/lib/app/view.js +++ b/packages/uni-template-compiler/lib/app/view.js @@ -100,7 +100,9 @@ const includeDirs = [ const ignoreDirs = ['model'] function transformNode (el, parent, state, isScopedSlot) { - if (el.type === 3) { + if (el.type === 3) { + // fixed by xxxxxx 注意:保持平台一致性,trim 一下 + el.text = el.text.trim() return } parseBlock(el, parent) -- GitLab