Skip to content

Commit d530396

Browse files
committed
Closes #2. Parsing multiple expressions in binding style to inject _px2rem.
1 parent 527f753 commit d530396

File tree

14 files changed

+517
-127
lines changed

14 files changed

+517
-127
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "weex-vue-precompiler",
3-
"version": "0.1.17",
3+
"version": "0.1.18",
44
"description": "a precompiler for weex-vue-render.",
55
"main": "src/index.js",
66
"scripts": {

src/components/index.js

+19-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
1+
const div = require('./div')
2+
const image = require('./image')
3+
const text = require('./text')
4+
const a = require('./a')
5+
const cell = require('./cell')
6+
7+
const cmpMaps = { div, image, text, a, cell }
8+
19
module.exports = {
2-
div: require('./div').processDiv,
3-
figure: require('./image').processImage,
4-
p: require('./text').processText,
5-
a: require('./a').processA,
6-
section: require('./cell').processCell
10+
div: div.processDiv,
11+
figure: image.processImage,
12+
p: text.processText,
13+
a: a.processA,
14+
section: cell.processCell,
15+
// get ast compiler for binding styles.
16+
getCompiler: function (tag) {
17+
const cmp = cmpMaps[tag]
18+
const compile = cmp && cmp.compile
19+
return compile ? { compile } : undefined
20+
}
721
}

src/components/text.js

+29
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const util = require('../util')
22
const {
3+
ast,
34
extend,
45
getStaticStyleObject
56
} = util
@@ -43,3 +44,31 @@ exports.processText = function (
4344
delete el.ns
4445
el.plain = false
4546
}
47+
48+
// deal with binding-styles ast node.
49+
exports.compile = function (objNode, px2remTags, rootValue, transformNode) {
50+
const props = objNode.properties
51+
let hasLines = false
52+
for (let i = 0, l = props.length; i < l; i++) {
53+
const propNode = props[i]
54+
const keyNode = propNode.key
55+
const keyType = keyNode.type
56+
const keyNodeValStr = keyType === 'Literal' ? 'value' : 'name'
57+
const keyName = keyNode[keyNodeValStr]
58+
const valNode = propNode.value
59+
if (keyName === 'lines') {
60+
hasLines = true
61+
keyNode[keyNodeValStr] = 'webkitLineClamp'
62+
}
63+
else if (px2remTags.indexOf(keyName) > -1) {
64+
propNode.value = transformNode(propNode.value, 'text', rootValue, true/*asPropValue*/)
65+
}
66+
}
67+
if (hasLines) {
68+
objNode.properties = props.concat([
69+
ast.genPropNode('overflow', 'hidden'),
70+
ast.genPropNode('textOverflow', 'ellipsis')
71+
])
72+
}
73+
return objNode
74+
}

src/config.js

+19-23
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
const util = require('./util')
2+
3+
const vendorReg = /webkit|moz/i
4+
function hyphen (key) {
5+
return util.hyphenate(key.replace(vendorReg, function ($0) {
6+
return `-${$0.toLowerCase()}-`
7+
}))
8+
}
9+
10+
function getAllStyles (scaleStyles) {
11+
return Object.keys(scaleStyles.reduce(function (pre, key) {
12+
pre[key] = 1
13+
pre[hyphen(key)] = 1
14+
return pre
15+
}, {}))
16+
}
17+
118
const config = {
219
eventMap: {
320
click: 'weex$tap',
@@ -81,35 +98,14 @@ const config = {
8198
'finish',
8299
'fail'
83100
],
84-
preservedTags: [
85-
'a',
86-
'container',
87-
'div',
88-
'image',
89-
'img',
90-
'text',
91-
'input',
92-
'switch',
93-
'list',
94-
'scroller',
95-
'waterfall',
96-
'slider',
97-
'indicator',
98-
'loading-indicator',
99-
'loading',
100-
'refresh',
101-
'textarea',
102-
'video',
103-
'web'
104-
],
105101
autoprefixer: {
106102
browsers: ['> 0.1%', 'ios >= 8', 'not ie < 12']
107103
},
108104
px2rem: {
109105
rootValue: 75,
110106
minPixelValue: 1.01
111107
},
112-
bindingStyleNamesForPx2Rem: [
108+
bindingStyleNamesForPx2Rem: getAllStyles([
113109
'width',
114110
'height',
115111
'left',
@@ -145,7 +141,7 @@ const config = {
145141
'mozTransform',
146142
'MozTransform',
147143
'itemSize'
148-
]
144+
])
149145
}
150146

151147
module.exports = config

0 commit comments

Comments
 (0)