mh-two-thousand-and-two
2024-04-12 3d2ec2fd0578d3ba0a414b0cc4e4a2ae60878596
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
var chai = require('chai')
var sinon = require('sinon')
var sinonChai = require('sinon-chai')
var expect = chai.expect
chai.use(sinonChai)
 
var styler = require('../')
 
describe('parse', function () {
 
  it('parse normal style code', function (done) {
    var code = 'html {color: #000000;}\n\n.foo {color: red; background-color: rgba(255,255,255,0.6); -webkit-transform: rotate(90deg); width: 200px; left: 0; right: 0px; border-width: 1pt; font-weight: 100}\n\n.bar {background: red}'
    styler.parse(code, function (err, data) {
      expect(err).is.undefined
      expect(data).is.an.object
      expect(data.jsonStyle).eql({foo: {color: '#FF0000', backgroundColor: 'rgba(255,255,255,0.6)', WebkitTransform: 'rotate(90deg)', width: '200px', left: 0, right: '0px', borderWidth: '1pt', fontWeight: '100'}, bar: {background: 'red'}})
      expect(data.log).eql([
        {line: 1, column: 1, reason: 'ERROR: Selector `html` is not supported. Weex only support single-classname selector'},
        {line: 3, column: 7, reason: 'NOTE: property value `red` is autofixed to `#FF0000`'},
        {line: 3, column: 60, reason: 'WARNING: `-webkit-transform` is not a standard property name (may not be supported)'},
        {line: 5, column: 7, reason: 'WARNING: `background` is not a standard property name (may not be supported), suggest `background-color`'}
      ])
      done()
    })
  })
 
  it('parse and fix prop value', function (done) {
    var code = '.foo {font-size: 200px;}'
    styler.parse(code, function (err, data) {
      expect(err).is.undefined
      expect(data).is.an.object
      expect(data.jsonStyle).eql({foo: {fontSize: '200px'}})
      done()
    })
  })
 
  it('parse and ensure number type value', function (done) {
    var code = '.foo {line-height: 40;}\n\n .bar {line-height: 20px;}'
    styler.parse(code, function (err, data) {
      expect(err).is.undefined
      expect(data).is.an.object
      expect(data.jsonStyle).eql({foo: {lineHeight: 40}, bar: {lineHeight: '20px'}})
      done()
    })
  })
 
  it('handle complex class definition', function (done) {
    var code = '.foo, .bar {font-size: 20;}\n\n .foo {color: #ff5000;}\n\n .bar {color: #000000;}'
    styler.parse(code, function (err, data) {
      expect(err).is.undefined
      expect(data).is.an.object
      expect(data.jsonStyle).eql({
        foo: {fontSize: 20, color: '#ff5000'},
        bar: {fontSize: 20, color: '#000000'}
      })
      done()
    })
  })
 
  it('handle more complex class definition', function (done) {
    var code = '.foo, .bar {font-size: 20; color: #000000}\n\n .foo, .bar, .baz {color: #ff5000; height: 30;}'
    styler.parse(code, function (err, data) {
      expect(err).is.undefined
      expect(data).is.an.object
      expect(data.jsonStyle).eql({
        foo: {fontSize: 20, color: '#ff5000', height: 30},
        bar: {fontSize: 20, color: '#ff5000', height: 30},
        baz: {color: '#ff5000', height: 30}
      })
      done()
    })
  })
 
  it('parse transition', function (done) {
    var code = '.foo {transition-property: margin-top; transition-duration: 300ms; transition-delay: 0.2s; transition-timing-function: ease-in;}'
    styler.parse(code, function (err, data) {
      expect(err).is.undefined
      expect(data).is.an.object
      expect(data.jsonStyle['@TRANSITION']).eql({foo: {property: 'marginTop', duration: 300, delay: 200, timingFunction: 'ease-in'}})
      expect(data.jsonStyle.foo).eql({
        transitionDelay: 200,
        transitionDuration: 300,
        transitionProperty: "marginTop",
        transitionTimingFunction: "ease-in"
      })
      expect(data.log).eql([
        {line: 1, column: 40, reason: 'NOTE: property value `300ms` is autofixed to `300`'},
        {line: 1, column: 68, reason: 'NOTE: property value `0.2s` is autofixed to `200`'}
      ])
      done()
    })
  })
 
  it('parse transition transform', function (done) {
    var code = '.foo {transition-property: transform; transition-duration: 300ms; transition-delay: 0.2s; transition-timing-function: ease-in-out;}'
    styler.parse(code, function (err, data) {
      expect(err).is.undefined
      expect(data).is.an.object
      expect(data.jsonStyle['@TRANSITION']).eql({foo: {property: 'transform', duration: 300, delay: 200, timingFunction: 'ease-in-out'}})
      expect(data.jsonStyle.foo).eql({
        transitionDelay: 200,
        transitionDuration: 300,
        transitionProperty: "transform",
        transitionTimingFunction: "ease-in-out"
      })
      done()
    })
  })
 
  it('parse multi transition properties', function (done) {
    var code = '.foo {transition-property: margin-top, height; transition-duration: 300ms; transition-delay: 0.2s; transition-timing-function: ease-in-out;}'
    styler.parse(code, function (err, data) {
      expect(err).is.undefined
      expect(data).is.an.object
      expect(data.jsonStyle['@TRANSITION']).eql({foo: {property: 'marginTop,height', duration: 300, delay: 200, timingFunction: 'ease-in-out'}})
      expect(data.jsonStyle.foo).eql({
        transitionDelay: 200,
        transitionDuration: 300,
        transitionProperty: "marginTop,height",
        transitionTimingFunction: "ease-in-out"
      })
      done()
    })
  })
 
  it('parse complex transition', function (done) {
    var code = '.foo {font-size: 20; color: #000000}\n\n .foo, .bar {color: #ff5000; height: 30; transition-property: margin-top; transition-duration: 300ms; transition-delay: 0.2s; transition-timing-function: ease-in;}'
    styler.parse(code, function (err, data) {
      expect(err).is.undefined
      expect(data).is.an.object
      expect(data.jsonStyle['@TRANSITION']).eql({
        foo: {property: 'marginTop', duration: 300, delay: 200, timingFunction: 'ease-in'},
        bar: {property: 'marginTop', duration: 300, delay: 200, timingFunction: 'ease-in'}
      })
      expect(data.jsonStyle.foo).eql({
        fontSize: 20, color: '#ff5000', height: 30,
        transitionDelay: 200,
        transitionDuration: 300,
        transitionProperty: "marginTop",
        transitionTimingFunction: "ease-in"
      })
      expect(data.jsonStyle.bar).eql({
        color: '#ff5000', height: 30,
        transitionDelay: 200,
        transitionDuration: 300,
        transitionProperty: "marginTop",
        transitionTimingFunction: "ease-in"
      })
      expect(data.log).eql([
        {line: 3, column: 75, reason: 'NOTE: property value `300ms` is autofixed to `300`'},
        {line: 3, column: 103, reason: 'NOTE: property value `0.2s` is autofixed to `200`'}
      ])
      done()
    })
  })
 
  it('parse transition shorthand', function (done) {
    var code = '.foo {font-size: 20; transition: margin-top 500ms ease-in-out 1s}'
    styler.parse(code, function (err, data) {
      expect(err).is.undefined
      expect(data).is.an.object
      expect(data.jsonStyle['@TRANSITION']).eql({foo: {property: 'marginTop', duration: 500, delay: 1000, timingFunction: 'ease-in-out' }})
      expect(data.jsonStyle.foo).eql({
        fontSize: 20,
        transitionDelay: 1000,
        transitionDuration: 500,
        transitionProperty: "marginTop",
        transitionTimingFunction: "ease-in-out"
      })
      expect(data.log).eql([
        {line: 1, column: 22, reason: 'NOTE: property value `500ms` is autofixed to `500`'},
        {line: 1, column: 22, reason: 'NOTE: property value `1s` is autofixed to `1000`'}
      ])
      done()
    })
  })
 
  it.skip('override transition shorthand', function (done) {
    var code = '.foo {font-size: 32px; transition: margin-top 500ms ease-in-out 1s; transition-duration: 300ms}'
    styler.parse(code, function (err, data) {
      expect(err).is.undefined
      expect(data).is.an.object
      expect(data.jsonStyle['@TRANSITION']).eql({foo: {property: 'marginTop', duration: 300, delay: 1000, timingFunction: 'ease-in-out' }})
      expect(data.jsonStyle.foo).eql({
        fontSize: 32,
        transitionDelay: 1000,
        transitionDuration: 300,
        transitionProperty: "marginTop",
        transitionTimingFunction: "ease-in-out"
      })
      done()
    })
  })
 
  it('parse padding & margin shorthand', function (done) {
    var code = '.foo { padding: 20px; margin: 30px 40; } .bar { margin: 10px 20 30; padding: 10 20px 30px 40;}'
    styler.parse(code, function (err, data) {
      expect(err).is.undefined
      expect(data).is.an.object
      expect(data.jsonStyle.foo).eql({
        paddingTop: '20px',
        paddingRight: '20px',
        paddingBottom: '20px',
        paddingLeft: '20px',
        marginTop: '30px',
        marginRight: 40,
        marginBottom: '30px',
        marginLeft: 40
      })
      expect(data.jsonStyle.bar).eql({
        paddingTop: 10,
        paddingRight: '20px',
        paddingBottom: '30px',
        paddingLeft: 40,
        marginTop: '10px',
        marginRight: 20,
        marginBottom: 30,
        marginLeft: 20
      })
      done()
    })
  })
 
  it('override padding & margin shorthand', function (done) {
    var code = '.foo { padding: 20px; padding-left: 30px; } .bar { margin: 10px 20; margin-bottom: 30px;}'
    styler.parse(code, function (err, data) {
      expect(err).is.undefined
      expect(data).is.an.object
      expect(data.jsonStyle.foo).eql({
        paddingTop: '20px',
        paddingRight: '20px',
        paddingBottom: '20px',
        paddingLeft: '30px'
      })
      expect(data.jsonStyle.bar).eql({
        marginTop: '10px',
        marginRight: 20,
        marginBottom: '30px',
        marginLeft: 20
      })
      done()
    })
  })
 
  it('handle pseudo class', function (done) {
    var code = '.class-a {color: #0000ff;} .class-a:last-child:focus {color: #ff0000;}'
    styler.parse(code, function (err, data) {
      expect(err).is.undefined
      expect(data).is.an.object
      expect(data.jsonStyle).eql({
        'class-a': {
          color: '#0000ff',
          'color:last-child:focus': '#ff0000'
        }
      })
      done()
    })
  })
 
  it('handle iconfont', function (done) {
    var code = '@font-face {font-family: "font-family-name-1"; src: url("font file url 1-1") format("truetype");} @font-face {font-family: "font-family-name-2"; src: url("font file url 2-1") format("truetype"), url("font file url 2-2") format("woff");}'
    styler.parse(code, function (err, data) {
      expect(err).is.undefined
      expect(data).is.an.object
      expect(data.jsonStyle).eql({
        '@FONT-FACE': [
          {fontFamily: 'font-family-name-1', src: 'url("font file url 1-1") format("truetype")'},
          {fontFamily: 'font-family-name-2', src: 'url("font file url 2-1") format("truetype"), url("font file url 2-2") format("woff")'}
        ]
      })
      done()
    })
  })
 
  it('handle syntax error', function (done) {
    var code = 'asdf'
    styler.parse(code, function (err, data) {
      expect(err).is.an.array
      expect(err[0].toString()).eql('Error: undefined:1:5: missing \'{\'')
      expect(err[0].reason).eql('missing \'{\'')
      expect(err[0].filename).eql(undefined)
      expect(err[0].line).eql(1)
      expect(err[0].column).eql(5)
      expect(err[0].source).eql('')
      expect(data.log).eql([{line: 1, column: 5, reason: 'ERROR: undefined:1:5: missing \'{\''}])
      done()
    })
  })
})