• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Weex `<style>` Transformer
2
3[![NPM version][npm-image]][npm-url]
4[![Build status][circle-image]][circle-url]
5[![Downloads][downloads-image]][downloads-url]
6
7[npm-image]: https://img.shields.io/npm/v/weex-styler.svg?style=flat-square
8[npm-url]: https://npmjs.org/package/weex-styler
9[circle-image]: https://circleci.com/gh/alibaba/weex_toolchain.svg?style=svg
10[circle-url]: https://circleci.com/gh/alibaba/weex_toolchain/tree/master
11[downloads-image]: https://img.shields.io/npm/dm/weex-styler.svg?style=flat-square
12[downloads-url]: https://npmjs.org/package/weex-styler
13
14## Features
15
16- convert a `<style>` element to JSON format
17- autofix common mistakes
18- friendly warnings
19
20## API
21
22- `parse(code, done)`
23- `validate(json, done)`
24- `validateItem(name, value)`
25
26### util api
27
28- `util.hyphenedToCamelCase(value)`
29- `util.camelCaseToHyphened(value)`
30
31```javascript
32/**
33 * Parse `<style>` code to a JSON Object and log errors & warnings
34 *
35 * @param {string} code
36 * @param {function} done
37 */
38function parse(code, done) {}
39
40/**
41 * Validate a JSON Object and log errors & warnings
42 *
43 * @param {object} json
44 * @param {function} done
45 */
46function validate(json, done) {}
47
48/**
49 * Result callback
50 *
51 * data
52 * - jsonStyle{}: `classname.propname.value`-like object
53 * - log[{line, column, reason}]
54 *
55 * @param {Error} err
56 * @param {object} data
57 */
58function done(err, data) {}
59
60/**
61 * Validate a single name-value pair
62 *
63 * @param  {string} name  camel cased
64 * @param  {string} value
65 * @return {object}
66 * - value
67 * - log{reason}
68 */
69function validateItem(name, value) {}
70```
71
72## Validation
73
74- rule check: only common rule type supported, othres will be ignored
75- selector check: only single-classname selector is supported, others will be ignored
76- prop name check: out-of-defined prop name will be warned but preserved
77- prop value check: common prop value mistakes will be autofixed or ignored
78    + color type: keywords, `#xxx` -> warning: `#xxxxxx`
79    + color type: `transparent` -> error: not supported
80    + length type: `100px` -> warning: `100`
81
82## Demo
83
84```javascript
85var styler = require('weex-styler')
86
87var code = 'html {color: #000000;} .foo {color: red; -webkit-transform: rotate(90deg); width: 200px;}'
88
89styler.parse(code, function (err, data) {
90  // syntax error
91  // format: {line, column, reason, ...}
92  err
93  // result
94  // {foo: {color: '#ff0000', webkitTransform: 'rotate(90deg)', width: 200}}
95  data.jsonStyle
96  // format: {line, column, reason}
97  // - Error: Selector `html` is not supported. Weex only support single-classname selector
98  // - Warning: prop value `red` is autofixed to `#ff0000`
99  // - Warning: prop name `-webkit-transform` is not supported
100  // - Warning: prop value `200px` is autofixed to `200`
101  data.log[]
102})
103
104var jsonStyle = {
105  foo: {
106    color: 'red',
107    webkitTransform: 'rotate(90deg)',
108    width: '200px'
109  }
110}
111
112styler.validate(json, function (err, data) {
113  // syntax error
114  err
115  // result
116  // {foo: {color: '#ff0000', webkitTransform: 'rotate(90deg)', width: 200}}
117  data.jsonStyle
118  // format: {reason}
119  // - Warning: prop value `red` is autofixed to `#ff0000`
120  // - Warning: prop name `-webkit-transform` is not supported
121  // - Warning: prop value `200px` is autofixed to `200`
122  data.log[]
123})
124```
125