front-compiler 1.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,111 @@
1
+ require File.dirname(__FILE__)+"/../../spec_helper"
2
+
3
+ describe FrontCompiler::CssSource do
4
+ def css(src)
5
+ FrontCompiler::CssSource.new(src)
6
+ end
7
+
8
+ it "should remove comments" do
9
+ css(%{
10
+ /* some styleshit here */
11
+ html, body {
12
+ font-size: 9pt;
13
+ }
14
+ /* some more styleshit */
15
+ p {
16
+ padding: 10pt;
17
+ background: url("/*/*/image.png");
18
+ }
19
+
20
+ a:after {
21
+ content: '/* */';
22
+ }
23
+ }).remove_comments!.should == %{
24
+
25
+ html, body {
26
+ font-size: 9pt;
27
+ }
28
+
29
+ p {
30
+ padding: 10pt;
31
+ background: url("/*/*/image.png");
32
+ }
33
+
34
+ a:after {
35
+ content: '/* */';
36
+ }
37
+ }
38
+ end
39
+
40
+ it "should remove all the empty lines out of the code" do
41
+ css(%{
42
+ html, body {
43
+ font-weight: bold;
44
+
45
+
46
+ color: red;
47
+ }
48
+
49
+
50
+ label {
51
+
52
+
53
+
54
+ display: block;
55
+ }
56
+
57
+ }).remove_empty_lines!.should == %{
58
+ html, body {
59
+ font-weight: bold;
60
+ color: red;
61
+ }
62
+ label {
63
+ display: block;
64
+ }
65
+ }
66
+ end
67
+
68
+ it "should remove all the trailing spaces out of the code" do
69
+ css(%{
70
+ html, body {
71
+ cusor : pointer;
72
+ color: red;
73
+ }
74
+
75
+
76
+ div > p ~ label:after {
77
+ content: ' ';
78
+ text-decoration: underline;
79
+ }
80
+
81
+ form p label {
82
+ display: block;
83
+ }
84
+ }).remove_trailing_spaces!.should == ""\
85
+ "html,body{cusor:pointer;color:red}div>p~label:after{"\
86
+ "content:' ';text-decoration:underline}"\
87
+ "form p label{display:block}"
88
+ end
89
+
90
+ it "should apply all the minimizations to the code" do
91
+ css(%{
92
+ /* some comment */
93
+ div ,
94
+ p { padding: 10pt; }
95
+ }).compact!.should == %{div,p{padding:10pt}}
96
+ end
97
+
98
+ it "should convert the stylesheet into a embedded javascript code" do
99
+ css(%{
100
+ /* some comment */
101
+ div ,
102
+ p { padding: 10pt;
103
+ background: url('something');
104
+ content: "something";
105
+ }
106
+ }).to_javascript.should ==
107
+ 'document.write("<style type=\\"text/css\\">'+
108
+ 'div,p{padding:10pt;background:url(\'something\');content:\"something\"}'+
109
+ '</style>");'
110
+ end
111
+ end
@@ -0,0 +1,41 @@
1
+ require File.dirname(__FILE__)+"/../../spec_helper"
2
+
3
+ describe FrontCompiler::HTMLCompactor do
4
+ before :all do
5
+ @c = FrontCompiler::HTMLCompactor.new
6
+ end
7
+
8
+ it "should remove all the comments out of the code" do
9
+ @c.remove_comments(%{
10
+ <ul><!-- main menu -->
11
+ <li><!-- main label -->
12
+ Main</li>
13
+ </ul>
14
+ }).should == %{
15
+ <ul>
16
+ <li>
17
+ Main</li>
18
+ </ul>
19
+ }
20
+ end
21
+
22
+ it "should remove all the trailing spaces out of the code" do
23
+ @c.remove_trailing_spaces(%{
24
+ <ul class="bla">
25
+ <li>
26
+ <a href="" id="main"> Home </a>
27
+ </li>
28
+ </ul>
29
+ }).should == %{<ul class="bla"><li><a href="" id="main">Home</a></li></ul>}
30
+ end
31
+
32
+ it "should apply all the compactings" do
33
+ @c.minimize(%{ <!-- some comment -->
34
+ <ul class="bla">
35
+ <li> <!-- some another comment -->
36
+ <a href="" id="main"> Home </a>
37
+ </li>
38
+ </ul>
39
+ }).should == %{<ul class="bla"><li><a href="" id="main">Home</a></li></ul>}
40
+ end
41
+ end
@@ -0,0 +1,363 @@
1
+ require File.dirname(__FILE__)+"/../../../spec_helper"
2
+
3
+ describe FrontCompiler::JavaScript::LogicCompactor do
4
+ def compact(src)
5
+ FrontCompiler::JavaScript.new(src).compact_logic!
6
+ end
7
+
8
+ it "should have the compact_logic method" do
9
+ FrontCompiler::JavaScript.new('').should respond_to(:compact_logic!)
10
+ end
11
+
12
+ it "should convert multi-lined if/else short constructions" do
13
+ compact(%{
14
+ var a = b ?
15
+ c : d;
16
+ var a = b
17
+ ? c : d;
18
+ var a = b ?
19
+ c :
20
+ d;
21
+ var a = b
22
+ ? c
23
+ : d;
24
+ }).should == %{
25
+ var a = b ? c : d;
26
+ var a = b ? c : d;
27
+ var a = b ? c : d;
28
+ var a = b ? c : d;
29
+ }
30
+ end
31
+
32
+ it "should convert multi-lined assignments" do
33
+ compact(%{
34
+ var a =
35
+ b + c;
36
+ var a = b -
37
+ c;
38
+ var a = b
39
+ % c;
40
+ }).should == %{
41
+ var a = b + c;
42
+ var a = b - c;
43
+ var a = b % c;
44
+ }
45
+ end
46
+
47
+ it "should convert mutli-lined method-calls chain" do
48
+ compact(%{
49
+ var a = b
50
+ .c()
51
+ .d();
52
+ }).should == %{
53
+ var a = b.c().d();
54
+ }
55
+ end
56
+
57
+ it "should convert multi-lined method calls" do
58
+ compact(%{
59
+ var a = b(
60
+ c,
61
+ d
62
+ );
63
+ var a = b(
64
+ c, d
65
+ );
66
+ }).should == %{
67
+ var a = b(c, d);
68
+ var a = b(c, d);
69
+ }
70
+ end
71
+
72
+ it "should convert multi-lined logical expressions" do
73
+ compact(%{
74
+ if (a
75
+ && b ||
76
+ c)
77
+ var d = e
78
+ | f &
79
+ g;
80
+ }).should == %{
81
+ if (a && b || c)
82
+ var d = e | f & g;
83
+ }
84
+ end
85
+
86
+ it "should not touch multilined logic constructions" do
87
+ compact(%{
88
+ if (something) {
89
+ bla;
90
+ foo;
91
+ }
92
+ for (var k in o) {
93
+ bla; foo;
94
+ }
95
+ while (something) { bla; foo; }
96
+ }).should == %{
97
+ if (something) {
98
+ bla;
99
+ foo;
100
+ }
101
+ for (var k in o) {
102
+ bla; foo;
103
+ }
104
+ while (something) { bla; foo; }
105
+ }
106
+ end
107
+
108
+ it "should not break try/catch/finally constructions" do
109
+ compact(%{
110
+ try { bla; bla
111
+ }
112
+ catch(e) {
113
+ bla; bla; bla
114
+ }
115
+ finally {
116
+ bla; bla; bla
117
+ }
118
+ }).should == %{
119
+ try { bla; bla
120
+ }
121
+ catch(e) {
122
+ bla; bla; bla
123
+ }
124
+ finally {
125
+ bla; bla; bla
126
+ }
127
+ }
128
+ end
129
+
130
+ it "should convert simple multilined constructions" do
131
+ compact(%{
132
+ if (something) {
133
+ foo;
134
+ } else { foo }
135
+ for (var k in o) { foo; }
136
+ do {
137
+ something;
138
+ } while(needed)
139
+ while (something()) { foo }
140
+ }).should == %{
141
+ if (something)
142
+ foo;
143
+ else foo;
144
+ for (var k in o) foo;
145
+ do
146
+ something;
147
+ while(needed)
148
+ while (something()) foo;
149
+ }
150
+ end
151
+
152
+ it "should keep internal multilined constructions safe" do
153
+ compact(%{
154
+ if (something) {
155
+ for (var i=0; i<something.length; i++) {
156
+ var boo = something[i];
157
+ var foo = boo % i;
158
+ }
159
+ }
160
+ }).should == %{
161
+ if (something)
162
+ for (var i=0; i<something.length; i++) {
163
+ var boo = something[i];
164
+ var foo = boo % i;
165
+ }
166
+
167
+ }
168
+ end
169
+
170
+ it "should handle nested constructions" do
171
+ compact(%{
172
+ if (something) {
173
+ for (var k in o) {
174
+ while(something) {
175
+ var o[k] = something;
176
+ }
177
+ }
178
+ } else {
179
+ while (something) {
180
+ for (var k in o) {
181
+ something = o[k];
182
+ }
183
+ }
184
+ }
185
+ }).should == %{
186
+ if (something)
187
+ for (var k in o)
188
+ while(something)
189
+ var o[k] = something;
190
+
191
+
192
+ else
193
+ while (something)
194
+ for (var k in o)
195
+ something = o[k];
196
+
197
+
198
+
199
+ }
200
+ end
201
+
202
+ it "should keep doubleifs alive" do
203
+ compact(%{
204
+ if (something) {
205
+ if (something_else) {
206
+ bla; bla;
207
+ }
208
+ }
209
+ }).should == %{
210
+ if (something) {
211
+ if (something_else) {
212
+ bla; bla;
213
+ }
214
+ }
215
+ }
216
+ end
217
+
218
+ it "should remove semicolons after functions which before the 'else' keyword" do
219
+ compact(%{
220
+ if (something) {
221
+ function() {
222
+ };
223
+ } else {
224
+ something;
225
+ }
226
+ }).should == %{
227
+ if (something)
228
+ function() {
229
+ }
230
+ else
231
+ something;
232
+
233
+ }
234
+ end
235
+
236
+ it "should calculate try/catch constructions as code lines" do
237
+ compact(%{
238
+ if (something) {
239
+ try {something} catch(MyException e) {}
240
+ try {another} finally {}
241
+ } else {
242
+ do_nothing;
243
+ }
244
+ }).should == %{
245
+ if (something) {
246
+ try {something} catch(MyException e) {}
247
+ try {another} finally {}
248
+ } else
249
+ do_nothing;
250
+
251
+ }
252
+ end
253
+
254
+ it "should whatch if/else conditions intersections" do
255
+ compact(%{
256
+ if (something) {
257
+ for (something) {
258
+ if (something) {
259
+ return something;
260
+ }
261
+ }
262
+ } else {
263
+ return null;
264
+ }
265
+ }).should == %{
266
+ if (something) {
267
+ for (something)
268
+ if (something)
269
+ return something;
270
+
271
+
272
+ } else
273
+ return null;
274
+
275
+ }
276
+ end
277
+
278
+ it "should calculate number of constructions correctly in such case" do
279
+ compact(%{
280
+ if (a) {
281
+ for (k in a) {
282
+ if (a)
283
+ t;
284
+ }
285
+
286
+ if (a) {
287
+ each;
288
+ }
289
+ }
290
+ }).should == %{
291
+ if (a) {
292
+ for (k in a)
293
+ if (a)
294
+ t;
295
+
296
+
297
+ if (a)
298
+ each;
299
+
300
+ }
301
+ }
302
+ end
303
+
304
+ it "should fix missed semicolons before return|function|if|while|do" do
305
+ %w(return function if while do try).each do |command|
306
+ compact(%{
307
+ var a = 2
308
+ #{command};
309
+ var b = 4;
310
+ #{command};
311
+ }).should == %{
312
+ var a = 2;
313
+ #{command};
314
+ var b = 4;
315
+ #{command};
316
+ }
317
+ end
318
+ end
319
+
320
+ it "should fix temporary lambdas calls" do
321
+ %w(return function if while do try).each do |command|
322
+ compact(%{
323
+ var c = (function() {
324
+
325
+ })(1,2,3,4)
326
+ #{command} (c)
327
+ }).should == %(
328
+ var c = (function() {
329
+
330
+ })(1,2,3,4);
331
+ #{command} (c)
332
+ )
333
+ end
334
+
335
+ end
336
+
337
+ it "should not break weird else if constructions" do
338
+ compact(%{
339
+ if(somethind) {
340
+ } else
341
+ if (another) {
342
+ }
343
+ }).should == %{
344
+ if(somethind) {
345
+ } else
346
+ if (another) {
347
+ }
348
+ }
349
+ end
350
+
351
+ it "should not put missed semicolons in fron of opened lists" do
352
+ compact(%{
353
+ var flips = [
354
+ function() {},
355
+ function() {}
356
+ ];
357
+ }).should == %{
358
+ var flips = [
359
+ function() {}, function() {}
360
+ ];
361
+ }
362
+ end
363
+ end
@@ -0,0 +1,219 @@
1
+ require File.dirname(__FILE__)+"/../../../spec_helper"
2
+
3
+ describe FrontCompiler::JavaScript::NamesCompactor do
4
+ def compact(src)
5
+ FrontCompiler::JavaScript.new(src).compact_names!
6
+ end
7
+
8
+ it "should have the compact_logic method" do
9
+ FrontCompiler::JavaScript.new('').should respond_to(:compact_names!)
10
+ end
11
+
12
+ it "should not touch any local variables" do
13
+ compact(%{
14
+ var something = funny;
15
+ }).should == %{
16
+ var something = funny;
17
+ }
18
+ end
19
+
20
+ it "should handle simple cases" do
21
+ compact(%{
22
+ function(something, another) {
23
+ var more = something / another;
24
+ another = more * something;
25
+ something = another - more;
26
+ }
27
+ function (something, another) {
28
+ var more = something / another;
29
+ }
30
+ function asdf(something, another) {
31
+ var more = something / another;
32
+ }
33
+ }).should == %{
34
+ function(s, a) {
35
+ var m = s / a;
36
+ a = m * s;
37
+ s = a - m;
38
+ }
39
+ function (s, a) {
40
+ var m = s / a;
41
+ }
42
+ function asdf(s, a) {
43
+ var m = s / a;
44
+ }
45
+ }
46
+ end
47
+
48
+ it "should catch up local function names" do
49
+ compact(%{
50
+ function() {
51
+ function boo() {
52
+ };
53
+ var foo = function() {};
54
+ var moo = function ( ) {};
55
+
56
+ boo(foo(moo());
57
+ }
58
+ }).should == %{
59
+ function() {
60
+ function b() {
61
+ };
62
+ var f = function() {};
63
+ var m = function ( ) {};
64
+
65
+ b(f(m());
66
+ }
67
+ }
68
+ end
69
+
70
+ it "should catch up several vars in a line" do
71
+ compact(%{
72
+ function() {
73
+ var boo = {}, foo = [], moo = new String;
74
+ foo.push(moo);
75
+ boo[moo] = foo;
76
+ }
77
+ }).should == %{
78
+ function() {
79
+ var b = {}, f = [], m = new String;
80
+ f.push(m);
81
+ b[m] = f;
82
+ }
83
+ }
84
+ end
85
+
86
+ it "should keep safe the logical constructions and external variables" do
87
+ compact(%{
88
+ function(something) {
89
+ try {
90
+ if (something) {
91
+ for (var key in something) {
92
+ var object = something[key];
93
+ }
94
+ } else if (limit) {
95
+ for (var i=0; i < limit; i++) {
96
+ var object = limit + i;
97
+ }
98
+ } else {
99
+ while (running) {
100
+ var object = running;
101
+ }
102
+ }
103
+ } catch(MyException e) {
104
+ handle(e);
105
+ }
106
+ }
107
+ }).should == %{
108
+ function(s) {
109
+ try {
110
+ if (s) {
111
+ for (var k in s) {
112
+ var o = s[k];
113
+ }
114
+ } else if (limit) {
115
+ for (var i=0; i < limit; i++) {
116
+ var o = limit + i;
117
+ }
118
+ } else {
119
+ while (running) {
120
+ var o = running;
121
+ }
122
+ }
123
+ } catch(MyException e) {
124
+ handle(e);
125
+ }
126
+ }
127
+ }
128
+ end
129
+
130
+ it "should keep safe the objects members calls" do
131
+ compact(%{
132
+ function(something) {
133
+ var object = something.important ?
134
+ something.important(something.more()) : nil;
135
+ }
136
+ }).should == %{
137
+ function(s) {
138
+ var o = s.important ?
139
+ s.important(s.more()) : nil;
140
+ }
141
+ }
142
+ end
143
+
144
+ it "should resolve similar names issues" do
145
+ compact(%{
146
+ function(aaron, abba, alisa) {
147
+ var ann = aaron * abba + alisa;
148
+ }
149
+ }).should == %{
150
+ function(a, b, c) {
151
+ var d = a * b + c;
152
+ }
153
+ }
154
+ end
155
+
156
+ it "should handle a nested functions construction" do
157
+ compact(%{
158
+ function(boo) {
159
+ function moo() {
160
+ function foo() {
161
+ var moo = boo * 2;
162
+ boo(moo);
163
+ }
164
+ }
165
+ var foo = moo(boo);
166
+ }
167
+ }).should == %{
168
+ function(b) {
169
+ function c() {
170
+ function f() {
171
+ var m = b * 2;
172
+ b(m);
173
+ }
174
+ }
175
+ var a = c(b);
176
+ }
177
+ }
178
+ end
179
+
180
+ it "should not break object keys definitions" do
181
+ compact(%{
182
+ function(foo, boo) {
183
+ var moo = {foo: foo, boo : boo};
184
+ moo = foo ? boo : [foo, boo];
185
+ boo = { mooboo: moo + boo }
186
+ }
187
+ }).should == %{
188
+ function(f, b) {
189
+ var m = {foo: f, boo : b};
190
+ m = f ? b : [f, b];
191
+ b = { mooboo: m + b }
192
+ }
193
+ }
194
+ end
195
+
196
+ it "should not break similar external variables" do
197
+ compact(%{
198
+ function(foo, boo) {
199
+ var moo = foo_boo;
200
+ }
201
+ }).should == %{
202
+ function(f, b) {
203
+ var m = foo_boo;
204
+ }
205
+ }
206
+ end
207
+
208
+ it "should handle the case when there are two slashes but no regular expression" do
209
+ compact(%{
210
+ function(i) {
211
+ var y = (cell.y/3).floor() * 3 + (i/3).floor();
212
+ }
213
+ }).should == %{
214
+ function(a) {
215
+ var y = (cell.y/3).floor() * 3 + (a/3).floor();
216
+ }
217
+ }
218
+ end
219
+ end