Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(weex): stop trim css units in richtext component #6927

Merged
merged 1 commit into from Oct 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 12 additions & 25 deletions src/platforms/weex/runtime/components/richtext.js
@@ -1,55 +1,44 @@
function getVNodeType (vnode) {
/* @flow */

function getVNodeType (vnode: VNode): string {
if (!vnode.tag) {
return ''
}
return vnode.tag.replace(/vue\-component\-(\d+\-)?/, '')
}

function isSimpleSpan (vnode) {
return vnode.children && vnode.children.length === 1 && !vnode.children[0].tag
function isSimpleSpan (vnode: VNode): boolean {
return vnode.children &&
vnode.children.length === 1 &&
!vnode.children[0].tag
}

const cssLengthRE = /^([+-]?[0-9]+(\.[0-9]+)?)(px|em|ex|%|in|cm|mm|pt|pc)$/i
function trimCSSUnit (prop) {
const res = String(prop).match(cssLengthRE)
if (res) {
return Number(res[1])
}
return prop
}

function parseStyle (vnode) {
function parseStyle (vnode: VNode): Object | void {
if (!vnode || !vnode.data) {
return
}

const { staticStyle, staticClass } = vnode.data
if (vnode.data.style || vnode.data.class || staticStyle || staticClass) {
const styles = Object.assign({}, staticStyle, vnode.data.style)

const cssMap = vnode.context.$options.style || {}
const classList = [].concat(staticClass, vnode.data.class)
classList.forEach(name => {
if (name && cssMap[name]) {
Object.assign(styles, cssMap[name])
}
})

for (const key in styles) {
styles[key] = trimCSSUnit(styles[key])
}
return styles
}
}

function convertVNodeChildren (children) {
function convertVNodeChildren (children: Array<VNode>): Array<VNode> | void {
if (!children.length) {
return
}

return children.map(vnode => {
const type = getVNodeType(vnode)
const props = { type }
const type: string = getVNodeType(vnode)
const props: Object = { type }

// convert raw text node
if (!type) {
Expand All @@ -65,7 +54,6 @@ function convertVNodeChildren (children) {
props.events = vnode.data.on
}
}

if (type === 'span' && isSimpleSpan(vnode)) {
props.attr = props.attr || {}
props.attr.value = vnode.children[0].text.trim()
Expand All @@ -83,8 +71,7 @@ function convertVNodeChildren (children) {

export default {
name: 'richtext',
// abstract: true,
render (h) {
render (h: Function) {
return h('weex:richtext', {
on: this._events,
attrs: {
Expand Down
10 changes: 5 additions & 5 deletions test/weex/runtime/components/richtext.spec.js
Expand Up @@ -199,7 +199,7 @@ describe('richtext component', () => {
attr: {
value: [{
type: 'image',
style: { width: 150, height: 150 },
style: { width: '150px', height: '150px' },
attr: { src: 'path/to/profile.png' }
}]
}
Expand Down Expand Up @@ -293,11 +293,11 @@ describe('richtext component', () => {
attr: {
value: [{
type: 'span',
style: { fontSize: 16, color: '#FF6600' },
style: { fontSize: '16px', color: '#FF6600' },
attr: { value: 'ABCD' }
}, {
type: 'image',
style: { width: 33.33, height: 66.67 },
style: { width: '33.33px', height: '66.67px' },
attr: { src: 'path/to/A.png' }
}]
}
Expand Down Expand Up @@ -471,7 +471,7 @@ describe('richtext component', () => {
attr: {
value: [{
type: 'span',
style: { fontSize: 32, color: '#F6F660' },
style: { fontSize: '32px', color: '#F6F660' },
attr: { value: 'ABCD' }
}, {
type: 'span',
Expand Down Expand Up @@ -543,7 +543,7 @@ describe('richtext component', () => {
attr: {
value: [{
type: 'span',
style: { fontSize: 24, color: '#ABCDEF' },
style: { fontSize: '24px', color: '#ABCDEF' },
attr: { value: 'ABCD' }
}, {
type: 'span',
Expand Down