Skip to content

Commit 8a784d8

Browse files
Hanks10100yyx990803
authored andcommitted
fix(weex): stop trim css units in richtext component (#6927)
+ Remove the `trimCSSUnit` method. + Modify the test cases to support css units. + Add flow type annotations.
1 parent 6f6e5c8 commit 8a784d8

File tree

2 files changed

+17
-30
lines changed

2 files changed

+17
-30
lines changed

src/platforms/weex/runtime/components/richtext.js

+12-25
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,44 @@
1-
function getVNodeType (vnode) {
1+
/* @flow */
2+
3+
function getVNodeType (vnode: VNode): string {
24
if (!vnode.tag) {
35
return ''
46
}
57
return vnode.tag.replace(/vue\-component\-(\d+\-)?/, '')
68
}
79

8-
function isSimpleSpan (vnode) {
9-
return vnode.children && vnode.children.length === 1 && !vnode.children[0].tag
10+
function isSimpleSpan (vnode: VNode): boolean {
11+
return vnode.children &&
12+
vnode.children.length === 1 &&
13+
!vnode.children[0].tag
1014
}
1115

12-
const cssLengthRE = /^([+-]?[0-9]+(\.[0-9]+)?)(px|em|ex|%|in|cm|mm|pt|pc)$/i
13-
function trimCSSUnit (prop) {
14-
const res = String(prop).match(cssLengthRE)
15-
if (res) {
16-
return Number(res[1])
17-
}
18-
return prop
19-
}
20-
21-
function parseStyle (vnode) {
16+
function parseStyle (vnode: VNode): Object | void {
2217
if (!vnode || !vnode.data) {
2318
return
2419
}
25-
2620
const { staticStyle, staticClass } = vnode.data
2721
if (vnode.data.style || vnode.data.class || staticStyle || staticClass) {
2822
const styles = Object.assign({}, staticStyle, vnode.data.style)
29-
3023
const cssMap = vnode.context.$options.style || {}
3124
const classList = [].concat(staticClass, vnode.data.class)
3225
classList.forEach(name => {
3326
if (name && cssMap[name]) {
3427
Object.assign(styles, cssMap[name])
3528
}
3629
})
37-
38-
for (const key in styles) {
39-
styles[key] = trimCSSUnit(styles[key])
40-
}
4130
return styles
4231
}
4332
}
4433

45-
function convertVNodeChildren (children) {
34+
function convertVNodeChildren (children: Array<VNode>): Array<VNode> | void {
4635
if (!children.length) {
4736
return
4837
}
4938

5039
return children.map(vnode => {
51-
const type = getVNodeType(vnode)
52-
const props = { type }
40+
const type: string = getVNodeType(vnode)
41+
const props: Object = { type }
5342

5443
// convert raw text node
5544
if (!type) {
@@ -65,7 +54,6 @@ function convertVNodeChildren (children) {
6554
props.events = vnode.data.on
6655
}
6756
}
68-
6957
if (type === 'span' && isSimpleSpan(vnode)) {
7058
props.attr = props.attr || {}
7159
props.attr.value = vnode.children[0].text.trim()
@@ -83,8 +71,7 @@ function convertVNodeChildren (children) {
8371

8472
export default {
8573
name: 'richtext',
86-
// abstract: true,
87-
render (h) {
74+
render (h: Function) {
8875
return h('weex:richtext', {
8976
on: this._events,
9077
attrs: {

test/weex/runtime/components/richtext.spec.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ describe('richtext component', () => {
199199
attr: {
200200
value: [{
201201
type: 'image',
202-
style: { width: 150, height: 150 },
202+
style: { width: '150px', height: '150px' },
203203
attr: { src: 'path/to/profile.png' }
204204
}]
205205
}
@@ -293,11 +293,11 @@ describe('richtext component', () => {
293293
attr: {
294294
value: [{
295295
type: 'span',
296-
style: { fontSize: 16, color: '#FF6600' },
296+
style: { fontSize: '16px', color: '#FF6600' },
297297
attr: { value: 'ABCD' }
298298
}, {
299299
type: 'image',
300-
style: { width: 33.33, height: 66.67 },
300+
style: { width: '33.33px', height: '66.67px' },
301301
attr: { src: 'path/to/A.png' }
302302
}]
303303
}
@@ -471,7 +471,7 @@ describe('richtext component', () => {
471471
attr: {
472472
value: [{
473473
type: 'span',
474-
style: { fontSize: 32, color: '#F6F660' },
474+
style: { fontSize: '32px', color: '#F6F660' },
475475
attr: { value: 'ABCD' }
476476
}, {
477477
type: 'span',
@@ -543,7 +543,7 @@ describe('richtext component', () => {
543543
attr: {
544544
value: [{
545545
type: 'span',
546-
style: { fontSize: 24, color: '#ABCDEF' },
546+
style: { fontSize: '24px', color: '#ABCDEF' },
547547
attr: { value: 'ABCD' }
548548
}, {
549549
type: 'span',

0 commit comments

Comments
 (0)