htmlmin 0.0.3 → 0.0.4

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.
data/README.md CHANGED
@@ -8,7 +8,7 @@ Please report issues on [Github] (https://github.com/aishek/htmlmin/issues).
8
8
 
9
9
  For feedback, suggestions, etc. write to <aishek@gmail.com>.
10
10
 
11
- **Important**: this project is in beta and may contain bugs. Current version: 0.0.2
11
+ **Important**: this project is in beta and may contain bugs. Current version: 0.0.4
12
12
 
13
13
  # Installation
14
14
 
@@ -23,6 +23,7 @@ Usage:
23
23
  require 'htmlmin'
24
24
  options = {
25
25
  :remove_comments => true,
26
+ :remove_conditional_comments => true, # only downlevel-hidden conditional comments e.g. <!--[if expression]> HTML <![endif]-->
26
27
  :minify_whitespaces => true,
27
28
  :max_line_len => 32768, # 0 - disable this safety feature
28
29
  }
@@ -41,8 +42,8 @@ Usage:
41
42
  * ~~Minify whitespaces after tag start and tag stop~~
42
43
  * ~~Minify whitespaces in text~~
43
44
  * ~~Remove comments~~
44
- * Do not remove conditional comments
45
- * Keep lines not longer that 30k (config option)
45
+ * ~~Do not remove conditional comments~~
46
+ * ~~Keep lines not longer that 30k (config option)~~
46
47
  * Do not perform minify in script and style tag's content
47
48
  * Do not perform minify in code, samp, pre tags's content
48
49
  * Replace entities with numeric entities for shorter strings
data/lib/htmlmin.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require "htmlmin/version"
2
2
  require "htmlmin/buffer"
3
+ require "htmlmin/state"
3
4
  require "htmlmin/max_line_len"
4
5
  require "htmlmin/slot_machines"
5
6
 
@@ -13,16 +14,18 @@ module HTMLMin
13
14
  END_OF_FILE = ''
14
15
 
15
16
  extend HTMLMin::Buffer
17
+ extend HTMLMin::State
16
18
  extend HTMLMin::MaxLineLen
17
19
  extend HTMLMin::SlotMachines::Common
18
20
  extend HTMLMin::SlotMachines::Comment
19
21
  extend HTMLMin::SlotMachines::Doctype
20
22
  extend HTMLMin::SlotMachines::Tag
21
-
22
- class << self
23
+
24
+ class << self
23
25
 
24
26
  DEFAULT_OPTIONS = {
25
27
  :remove_comments => true,
28
+ :preserve_conditional_comments => true,
26
29
 
27
30
  :minify_whitespaces => true,
28
31
 
@@ -30,9 +33,8 @@ module HTMLMin
30
33
 
31
34
  :debug => false
32
35
  }
33
-
34
-
35
- def minify(html, options = {})
36
+
37
+ def minify(html, options = {})
36
38
  init_settings options
37
39
 
38
40
  @result = ''
@@ -52,8 +54,9 @@ module HTMLMin
52
54
  def init_slot_machine
53
55
  init_max_line_len
54
56
  clear_buffer
55
- @state = :text_prespaced
56
- @prev_state = nil
57
+
58
+ reset_saved_state
59
+ @state = :start
57
60
  end
58
61
 
59
62
  def run_slot_machine(html)
@@ -73,18 +76,7 @@ module HTMLMin
73
76
  def debug_log(char)
74
77
  p "char: #{char}, new state: #{@state.to_s}, buffer: '#{@result_buffer.join}', result: '#{@result}'"
75
78
  end
76
-
77
- def save_state
78
- @prev_state = @state
79
- end
80
-
81
- def restore_state
82
- return false if @prev_state.nil?
83
-
84
- @state = @prev_state
85
- return true
86
- end
87
-
79
+
88
80
  end
89
81
 
90
82
 
@@ -8,54 +8,385 @@ module HTMLMin
8
8
 
9
9
  def comment_begin_1(char)
10
10
  case char
11
- when "\s", "\n", "\r", "\t", " "
12
- flush_buffer
13
- change_state_to_text_prespaced char
14
11
  when '-'
12
+ unshift_spaces_from_buffer if saved_state_unshiftable?
13
+ reset_saved_state
14
+
15
15
  add_to_buffer char
16
- change_state_to :comment_start_end
16
+ change_state_to :comment_or_cc
17
17
  when HTMLMin::Minifier::END_OF_FILE
18
18
  flush_buffer
19
19
  else
20
+ reset_saved_state
21
+
20
22
  flush_buffer
23
+
21
24
  add_to_result char
22
25
  change_state_to :start
23
26
  end
24
27
  end
28
+
29
+ def comment_or_cc(char)
30
+ case char
31
+ when '['
32
+ add_to_buffer char
33
+ change_state_to :cc
34
+ when '-'
35
+ add_to_buffer char
36
+ change_state_to :comment_end_1
37
+ when HTMLMin::Minifier::END_OF_FILE
38
+ clear_buffer if @settings[:remove_comments]
39
+ flush_buffer
40
+ else
41
+ add_to_buffer char
42
+ change_state_to :comment
43
+ end
44
+ end
45
+
46
+ def cc(char)
47
+ case char
48
+ when ']'
49
+ add_to_buffer char
50
+ change_state_to :cc_1
51
+ when '-'
52
+ add_to_buffer char
53
+ change_state_to :comment_end_1
54
+ when HTMLMin::Minifier::END_OF_FILE
55
+ clear_buffer if @settings[:remove_comments]
56
+ flush_buffer
57
+ else
58
+ add_to_buffer char
59
+ end
60
+ end
61
+
62
+ def cc_1(char)
63
+ case char
64
+ when '>'
65
+ add_to_buffer char
66
+ change_state_to :cc_begin_ended
67
+ when '-'
68
+ add_to_buffer char
69
+ change_state_to :comment_end_1
70
+ when HTMLMin::Minifier::END_OF_FILE
71
+ clear_buffer if @settings[:remove_comments]
72
+ flush_buffer
73
+ else
74
+ add_to_buffer char
75
+ change_state_to :comment
76
+ end
77
+ end
78
+
79
+ # wait for <![endif]-->,
80
+ # if --> then it just a simple comment
81
+ def cc_begin_ended(char)
82
+ case char
83
+ when '<'
84
+ add_to_buffer char
85
+ change_state_to :cc_begin_ended_1
86
+ when '-'
87
+ add_to_buffer char
88
+ change_state_to :cc_begin_ended_simple_1
89
+ when HTMLMin::Minifier::END_OF_FILE
90
+ clear_buffer if @settings[:remove_comments]
91
+ flush_buffer
92
+ else
93
+ add_to_buffer char
94
+ end
95
+ end
96
+
97
+ def cc_begin_ended_1(char)
98
+ case char
99
+ when '<'
100
+ add_to_buffer char
101
+ when '!'
102
+ add_to_buffer char
103
+ change_state_to :cc_begin_ended_2
104
+ when '-'
105
+ add_to_buffer char
106
+ change_state_to :cc_begin_ended_simple_1
107
+ when HTMLMin::Minifier::END_OF_FILE
108
+ clear_buffer if @settings[:remove_comments]
109
+ flush_buffer
110
+ else
111
+ add_to_buffer char
112
+ end
113
+ end
25
114
 
26
- def comment_start_end(char)
27
- return if char == HTMLMin::Minifier::END_OF_FILE
28
-
29
- add_to_buffer char
30
- change_state_to :comment
115
+ def cc_begin_ended_2(char)
116
+ case char
117
+ when '<'
118
+ add_to_buffer char
119
+ change_state_to :cc_begin_ended_1
120
+ when '['
121
+ add_to_buffer char
122
+ change_state_to :cc_begin_ended_3
123
+ when '-'
124
+ add_to_buffer char
125
+ change_state_to :cc_begin_ended_simple_1
126
+ when HTMLMin::Minifier::END_OF_FILE
127
+ clear_buffer if @settings[:remove_comments]
128
+ flush_buffer
129
+ else
130
+ add_to_buffer char
131
+ change_state_to :cc_begin_ended
132
+ end
31
133
  end
32
134
 
33
- def comment(char)
34
- return if char == HTMLMin::Minifier::END_OF_FILE
135
+ def cc_begin_ended_3(char)
136
+ case char
137
+ when '<'
138
+ add_to_buffer char
139
+ change_state_to :cc_begin_ended_1
140
+ when 'e'
141
+ add_to_buffer char
142
+ change_state_to :cc_begin_ended_4
143
+ when '-'
144
+ add_to_buffer char
145
+ change_state_to :cc_begin_ended_simple_1
146
+ when HTMLMin::Minifier::END_OF_FILE
147
+ clear_buffer if @settings[:remove_comments]
148
+ flush_buffer
149
+ else
150
+ add_to_buffer char
151
+ change_state_to :cc_begin_ended
152
+ end
153
+ end
154
+
155
+ def cc_begin_ended_4(char)
156
+ case char
157
+ when '<'
158
+ add_to_buffer char
159
+ change_state_to :cc_begin_ended_1
160
+ when 'n'
161
+ add_to_buffer char
162
+ change_state_to :cc_begin_ended_5
163
+ when '-'
164
+ add_to_buffer char
165
+ change_state_to :cc_begin_ended_simple_1
166
+ when HTMLMin::Minifier::END_OF_FILE
167
+ clear_buffer if @settings[:remove_comments]
168
+ flush_buffer
169
+ else
170
+ add_to_buffer char
171
+ change_state_to :cc_begin_ended
172
+ end
173
+ end
174
+
175
+ def cc_begin_ended_5(char)
176
+ case char
177
+ when '<'
178
+ add_to_buffer char
179
+ change_state_to :cc_begin_ended_1
180
+ when 'd'
181
+ add_to_buffer char
182
+ change_state_to :cc_begin_ended_6
183
+ when '-'
184
+ add_to_buffer char
185
+ change_state_to :cc_begin_ended_simple_1
186
+ when HTMLMin::Minifier::END_OF_FILE
187
+ clear_buffer if @settings[:remove_comments]
188
+ flush_buffer
189
+ else
190
+ add_to_buffer char
191
+ change_state_to :cc_begin_ended
192
+ end
193
+ end
194
+
195
+ def cc_begin_ended_6(char)
196
+ case char
197
+ when '<'
198
+ add_to_buffer char
199
+ change_state_to :cc_begin_ended_1
200
+ when 'i'
201
+ add_to_buffer char
202
+ change_state_to :cc_begin_ended_7
203
+ when '-'
204
+ add_to_buffer char
205
+ change_state_to :cc_begin_ended_simple_1
206
+ when HTMLMin::Minifier::END_OF_FILE
207
+ clear_buffer if @settings[:remove_comments]
208
+ flush_buffer
209
+ else
210
+ add_to_buffer char
211
+ change_state_to :cc_begin_ended
212
+ end
213
+ end
214
+
215
+ def cc_begin_ended_7(char)
216
+ case char
217
+ when '<'
218
+ add_to_buffer char
219
+ change_state_to :cc_begin_ended_1
220
+ when 'f'
221
+ add_to_buffer char
222
+ change_state_to :cc_begin_ended_8
223
+ when '-'
224
+ add_to_buffer char
225
+ change_state_to :cc_begin_ended_simple_1
226
+ when HTMLMin::Minifier::END_OF_FILE
227
+ clear_buffer if @settings[:remove_comments]
228
+ flush_buffer
229
+ else
230
+ add_to_buffer char
231
+ change_state_to :cc_begin_ended
232
+ end
233
+ end
234
+
235
+ def cc_begin_ended_8(char)
236
+ case char
237
+ when '<'
238
+ add_to_buffer char
239
+ change_state_to :cc_begin_ended_1
240
+ when ']'
241
+ add_to_buffer char
242
+ change_state_to :cc_begin_ended_9
243
+ when '-'
244
+ add_to_buffer char
245
+ change_state_to :cc_begin_ended_simple_1
246
+ when HTMLMin::Minifier::END_OF_FILE
247
+ clear_buffer if @settings[:remove_comments]
248
+ flush_buffer
249
+ else
250
+ add_to_buffer char
251
+ change_state_to :cc_begin_ended
252
+ end
253
+ end
35
254
 
36
- add_to_buffer char
37
- change_state_to :comment_end_1 if char == '-'
255
+ def cc_begin_ended_9(char)
256
+ case char
257
+ when '<'
258
+ add_to_buffer char
259
+ change_state_to :cc_begin_ended_1
260
+ when '-'
261
+ add_to_buffer char
262
+ change_state_to :cc_begin_ended_10
263
+ when HTMLMin::Minifier::END_OF_FILE
264
+ clear_buffer if @settings[:remove_comments]
265
+ flush_buffer
266
+ else
267
+ add_to_buffer char
268
+ change_state_to :cc_begin_ended
269
+ end
270
+ end
271
+
272
+ def cc_begin_ended_10(char)
273
+ case char
274
+ when '<'
275
+ add_to_buffer char
276
+ change_state_to :cc_begin_ended_1
277
+ when '-'
278
+ add_to_buffer char
279
+ change_state_to :cc_begin_ended_11
280
+ when HTMLMin::Minifier::END_OF_FILE
281
+ clear_buffer if @settings[:remove_comments]
282
+ flush_buffer
283
+ else
284
+ add_to_buffer char
285
+ change_state_to :cc_begin_ended
286
+ end
287
+ end
288
+
289
+ def cc_begin_ended_11(char)
290
+ case char
291
+ when '<'
292
+ add_to_buffer char
293
+ change_state_to :cc_begin_ended_1
294
+ when '>'
295
+ add_to_buffer char
296
+ clear_buffer if @settings[:remove_conditional_comments]
297
+ flush_buffer
298
+
299
+ change_state_to :entity_ended
300
+ when HTMLMin::Minifier::END_OF_FILE
301
+ clear_buffer if @settings[:remove_comments]
302
+ flush_buffer
303
+ else
304
+ add_to_buffer char
305
+ change_state_to :cc_begin_ended
306
+ end
307
+ end
308
+
309
+ def cc_begin_ended_simple_1(char)
310
+ case char
311
+ when '<'
312
+ add_to_buffer char
313
+ change_state_to :cc_begin_ended_1
314
+ when '-'
315
+ add_to_buffer char
316
+ change_state_to :cc_begin_ended_simple_2
317
+ when HTMLMin::Minifier::END_OF_FILE
318
+ clear_buffer if @settings[:remove_comments]
319
+ flush_buffer
320
+ else
321
+ add_to_buffer char
322
+ change_state_to :cc_begin_ended
323
+ end
324
+ end
325
+
326
+ def cc_begin_ended_simple_2(char)
327
+ case char
328
+ when '<'
329
+ add_to_buffer char
330
+ change_state_to :cc_begin_ended_1
331
+ when '>'
332
+ add_to_buffer char
333
+ clear_buffer if @settings[:remove_comments]
334
+ flush_buffer
335
+
336
+ change_state_to :entity_ended
337
+ when HTMLMin::Minifier::END_OF_FILE
338
+ clear_buffer if @settings[:remove_comments]
339
+ flush_buffer
340
+ else
341
+ add_to_buffer char
342
+ change_state_to :cc_begin_ended
343
+ end
344
+ end
345
+
346
+ def comment(char)
347
+ case char
348
+ when HTMLMin::Minifier::END_OF_FILE
349
+ clear_buffer if @settings[:remove_comments]
350
+ flush_buffer
351
+ when '-'
352
+ add_to_buffer char
353
+ change_state_to :comment_end_1
354
+ else
355
+ add_to_buffer char
356
+ end
38
357
  end
39
358
 
40
359
  def comment_end_1(char)
41
- return if char == HTMLMin::Minifier::END_OF_FILE
42
-
43
- add_to_buffer char
44
- change_state_to (char == '-' ? :comment_end_2 : :comment)
360
+ case char
361
+ when HTMLMin::Minifier::END_OF_FILE
362
+ clear_buffer if @settings[:remove_comments]
363
+ flush_buffer
364
+ when '-'
365
+ add_to_buffer char
366
+ change_state_to :comment_end_2
367
+ else
368
+ add_to_buffer char
369
+ change_state_to :comment
370
+ end
45
371
  end
46
372
 
47
373
  def comment_end_2(char)
48
- return if char == HTMLMin::Minifier::END_OF_FILE
49
-
50
- if char == '>'
51
- clear_buffer
52
- change_state_to :start unless restore_state
53
- else
54
- add_to_buffer char
55
- change_state_to :comment
374
+ case char
375
+ when HTMLMin::Minifier::END_OF_FILE
376
+ clear_buffer if @settings[:remove_comments]
377
+ flush_buffer
378
+ when '>'
379
+ add_to_buffer char
380
+ clear_buffer if @settings[:remove_comments]
381
+ flush_buffer
382
+
383
+ change_state_to :entity_ended
384
+ else
385
+ add_to_buffer char
386
+ change_state_to :comment
56
387
  end
57
388
  end
58
-
389
+
59
390
  end
60
391
 
61
392
  end
@@ -5,67 +5,61 @@ module HTMLMin
5
5
  module Common
6
6
 
7
7
  private
8
-
9
- def add_to_result(char)
10
- @result += char
11
-
12
- check_max_line_len(char) if @settings[:max_line_len] > 0
13
- end
14
-
15
- def change_state_to(new_state)
16
- @state = new_state
17
- end
18
-
8
+
19
9
  def tag_or_tagend_or_comment_or_doctype_begin(char)
20
10
  case char
21
- when "\s", "\n", "\r", "\t", " "
22
- flush_buffer
23
- change_state_to_text_prespaced char
24
- when "/"
25
- add_to_buffer char
26
- change_state_to :tag_end
27
11
  when "!"
28
12
  add_to_buffer char
29
13
  change_state_to :comment_or_doctype_begin
30
- when "a".."z", "A".."Z"
14
+ when "/"
15
+ add_to_buffer char
16
+ change_state_to :tagend_body_expected
17
+ when 'a'..'z', 'A'..'Z'
31
18
  add_to_buffer char
32
- change_state_to :tag_begin
19
+ change_state_to :tag
33
20
  when HTMLMin::Minifier::END_OF_FILE
34
21
  flush_buffer
35
22
  else
23
+ reset_saved_state
24
+
36
25
  flush_buffer
26
+
37
27
  add_to_result char
38
28
  change_state_to :start
39
29
  end
40
30
  end
41
-
31
+
42
32
  def comment_or_doctype_begin(char)
43
33
  case char
44
- when "\s", "\n", "\r", "\t", " "
45
- flush_buffer
46
- change_state_to_text_prespaced char
47
34
  when "-"
48
- if @settings[:remove_comments]
49
- add_to_buffer char
50
- change_state_to :comment_begin_1
51
- else
52
- flush_buffer
53
- add_to_result char
54
- change_state_to :start
55
- end
35
+ add_to_buffer char
36
+ change_state_to :comment_begin_1
56
37
  when 'D'
57
38
  add_to_buffer char
58
39
  change_state_to :doctype_begin_1
59
40
  when HTMLMin::Minifier::END_OF_FILE
60
41
  flush_buffer
61
42
  else
43
+ reset_saved_state
44
+
62
45
  flush_buffer
46
+
63
47
  add_to_result char
64
48
  change_state_to :start
65
49
  end
66
50
  end
67
-
68
- def text_prespaced(char)
51
+
52
+ def add_to_result(char)
53
+ @result += char
54
+
55
+ check_max_line_len(char) if @settings[:max_line_len] > 0
56
+ end
57
+
58
+ def change_state_to(new_state)
59
+ @state = new_state
60
+ end
61
+
62
+ def spaces(char)
69
63
  case char
70
64
  when "\s", "\n", "\r", "\t", " "
71
65
  if @settings[:minify_whitespaces]
@@ -74,28 +68,25 @@ module HTMLMin
74
68
  add_to_buffer char
75
69
  end
76
70
  when "<"
77
- flush_buffer unless @settings[:minify_whitespaces]
78
-
79
- unshift_spaces_from_buffer
80
- add_to_buffer char
81
- change_state_to :tag_or_tagend_or_comment_or_doctype_begin
71
+ flush_buffer if saved_state_empty?
72
+ change_state_to_tag_or_tagend_or_comment_or_doctype_begin char
82
73
  when HTMLMin::Minifier::END_OF_FILE
83
- unshift_spaces_from_buffer
84
74
  flush_buffer
85
75
  else
76
+ reset_saved_state
86
77
  flush_buffer
78
+
87
79
  add_to_result char
88
80
  change_state_to :start
89
81
  end
90
82
  end
91
-
83
+
92
84
  def start(char)
93
85
  case char
94
86
  when "\s", "\n", "\r", "\t"
95
- change_state_to_text_prespaced char
87
+ change_state_to_spaces char
96
88
  when "<"
97
- add_to_buffer char
98
- change_state_to :tag_or_tagend_or_comment_or_doctype_begin
89
+ change_state_to_tag_or_tagend_or_comment_or_doctype_begin char
99
90
  when HTMLMin::Minifier::END_OF_FILE
100
91
  flush_buffer
101
92
  else
@@ -103,11 +94,35 @@ module HTMLMin
103
94
  change_state_to :start
104
95
  end
105
96
  end
106
-
107
- def change_state_to_text_prespaced(char)
108
- add_to_buffer (@settings[:minify_whitespaces] ? ' ' : char)
109
- change_state_to :text_prespaced
97
+
98
+ def entity_ended(char)
99
+ return if char == HTMLMin::Minifier::END_OF_FILE
100
+
101
+ case char
102
+ when "\s", "\n", "\r", "\t", " "
103
+ save_state
104
+ change_state_to_spaces char
105
+ when "<"
106
+ change_state_to_tag_or_tagend_or_comment_or_doctype_begin char
107
+ else
108
+ add_to_result char
109
+ change_state_to :start
110
+ end
110
111
  end
112
+
113
+ def change_state_to_tag_or_tagend_or_comment_or_doctype_begin(char)
114
+ add_to_buffer char
115
+ change_state_to :tag_or_tagend_or_comment_or_doctype_begin
116
+ end
117
+
118
+ def change_state_to_spaces(char)
119
+ if @settings[:minify_whitespaces]
120
+ add_to_buffer ' '
121
+ else
122
+ add_to_result char
123
+ end
124
+ change_state_to :spaces
125
+ end
111
126
 
112
127
  end
113
128
 
@@ -12,46 +12,60 @@ module HTMLMin
12
12
  add_to_buffer char
13
13
  change_state_to :doctype_begin_2
14
14
  when "\s", "\n", "\r", "\t", " "
15
- change_state_to_text_prespaced char
15
+ reset_saved_state
16
+
17
+ flush_buffer
18
+
19
+ change_state_to_spaces char
16
20
  when HTMLMin::Minifier::END_OF_FILE
17
21
  flush_buffer
18
22
  else
23
+ reset_saved_state
24
+
19
25
  flush_buffer
20
26
 
21
27
  add_to_result char
22
28
  change_state_to :start
23
- end
29
+ end
24
30
  end
25
-
31
+
26
32
  def doctype_begin_2(char)
27
33
  case char
28
34
  when "C"
29
35
  add_to_buffer char
30
36
  change_state_to :doctype_begin_3
31
37
  when "\s", "\n", "\r", "\t", " "
38
+ reset_saved_state
39
+
32
40
  flush_buffer
33
- change_state_to_text_prespaced char
41
+ change_state_to_spaces char
34
42
  when HTMLMin::Minifier::END_OF_FILE
35
43
  flush_buffer
36
44
  else
45
+ reset_saved_state
46
+
37
47
  flush_buffer
38
48
 
39
49
  add_to_result char
40
50
  change_state_to :start
41
51
  end
42
52
  end
43
-
53
+
44
54
  def doctype_begin_3(char)
45
55
  case char
46
56
  when "T"
47
57
  add_to_buffer char
48
58
  change_state_to :doctype_begin_4
49
59
  when "\s", "\n", "\r", "\t", " "
60
+ reset_saved_state
61
+
50
62
  flush_buffer
51
- change_state_to_text_prespaced char
63
+ change_state_to_spaces char
52
64
  when HTMLMin::Minifier::END_OF_FILE
53
65
  flush_buffer
54
66
  else
67
+ reset_saved_state
68
+
55
69
  flush_buffer
56
70
 
57
71
  add_to_result char
@@ -65,11 +79,15 @@ module HTMLMin
65
79
  add_to_buffer char
66
80
  change_state_to :doctype_begin_5
67
81
  when "\s", "\n", "\r", "\t", " "
82
+ reset_saved_state
83
+
68
84
  flush_buffer
69
- change_state_to_text_prespaced char
85
+ change_state_to_spaces char
70
86
  when HTMLMin::Minifier::END_OF_FILE
71
87
  flush_buffer
72
88
  else
89
+ reset_saved_state
90
+
73
91
  flush_buffer
74
92
 
75
93
  add_to_result char
@@ -83,11 +101,15 @@ module HTMLMin
83
101
  add_to_buffer char
84
102
  change_state_to :doctype_begin_6
85
103
  when "\s", "\n", "\r", "\t", " "
104
+ reset_saved_state
105
+
86
106
  flush_buffer
87
- change_state_to_text_prespaced char
107
+ change_state_to_spaces char
88
108
  when HTMLMin::Minifier::END_OF_FILE
89
109
  flush_buffer
90
110
  else
111
+ reset_saved_state
112
+
91
113
  flush_buffer
92
114
 
93
115
  add_to_result char
@@ -101,31 +123,42 @@ module HTMLMin
101
123
  add_to_buffer char
102
124
  change_state_to :doctype_begin_7
103
125
  when "\s", "\n", "\r", "\t", " "
126
+ reset_saved_state
127
+
104
128
  flush_buffer
105
- change_state_to_text_prespaced char
129
+ change_state_to_spaces char
106
130
  when HTMLMin::Minifier::END_OF_FILE
107
131
  flush_buffer
108
132
  else
133
+ reset_saved_state
134
+
109
135
  flush_buffer
110
136
 
111
137
  add_to_result char
112
138
  change_state_to :start
113
139
  end
114
140
  end
115
-
141
+
116
142
  def doctype_begin_7(char)
117
143
  case char
118
144
  when " "
145
+ unshift_spaces_from_buffer if saved_state_unshiftable?
146
+ reset_saved_state
147
+
119
148
  add_to_buffer char
120
149
  change_state_to :doctype
121
150
  when "\s", "\n", "\r", "\t"
151
+ reset_saved_state
152
+
122
153
  flush_buffer
123
- change_state_to_text_prespaced char
154
+ change_state_to_spaces char
124
155
  when HTMLMin::Minifier::END_OF_FILE
125
156
  flush_buffer
126
157
  else
158
+ reset_saved_state
159
+
127
160
  flush_buffer
128
-
161
+
129
162
  add_to_result char
130
163
  change_state_to :start
131
164
  end
@@ -136,18 +169,18 @@ module HTMLMin
136
169
  when '>'
137
170
  add_to_buffer char
138
171
 
139
- unshift_spaces_from_buffer
140
172
  flush_buffer
141
173
 
142
174
  add_to_result "\n"
143
- change_state_to :start
175
+ change_state_to :entity_ended
144
176
  when HTMLMin::Minifier::END_OF_FILE
145
177
  flush_buffer
146
178
  else
179
+
147
180
  add_to_buffer char
148
181
  end
149
182
  end
150
-
183
+
151
184
  end
152
185
 
153
186
  end
@@ -6,90 +6,69 @@ module HTMLMin
6
6
 
7
7
  private
8
8
 
9
- def tag_end(char)
9
+ def tag(char)
10
10
  case char
11
- when "a".."z", "A".."Z"
12
- add_to_buffer char
13
- change_state_to :tag_end_body
14
- when HTMLMin::Minifier::END_OF_FILE
15
- flush_buffer
16
- else
17
- flush_buffer
18
- change_state_to :start
19
- end
20
- end
21
-
22
- def tag_end_body(char)
23
- case char
24
- when "a".."z", "A".."Z"
11
+ when 'a'..'z', 'A'..'Z'
25
12
  add_to_buffer char
26
13
  when '>'
14
+ unshift_spaces_from_buffer if saved_state_unshiftable?
15
+
27
16
  add_to_buffer char
28
-
29
- unshift_spaces_from_buffer
30
17
  flush_buffer
31
-
32
- change_state_to :tag_ended
18
+
19
+ change_state_to :entity_ended
33
20
  when HTMLMin::Minifier::END_OF_FILE
34
21
  flush_buffer
35
22
  else
23
+ reset_saved_state
24
+
36
25
  flush_buffer
26
+
27
+ add_to_result char
37
28
  change_state_to :start
38
29
  end
39
30
  end
40
31
 
41
- def tag_begin(char)
32
+ def tagend_body_expected(char)
42
33
  case char
43
- when "/"
44
- add_to_buffer char
45
- change_state_to :tag_end_expected
46
- when ">"
34
+ when 'a'..'z', 'A'..'Z'
47
35
  add_to_buffer char
48
- change_state_to :tag_ended
49
- when HTMLMin::Minifier::END_OF_FILE
50
- flush_buffer
51
- else
52
- add_to_buffer char
53
- end
54
- end
55
-
56
- def tag_end_expected(char)
57
- case char
58
- when '>'
59
- add_to_buffer char
60
- change_state_to :tag_ended
36
+ change_state_to :tagend_body
61
37
  when HTMLMin::Minifier::END_OF_FILE
62
38
  flush_buffer
63
39
  else
40
+ reset_saved_state
41
+
64
42
  flush_buffer
43
+
44
+ add_to_result char
65
45
  change_state_to :start
66
46
  end
67
47
  end
68
-
69
- def tag_ended(char)
48
+
49
+ def tagend_body(char)
70
50
  case char
71
- when "\s", "\n", "\r", "\t", " "
72
- add_to_buffer char unless @settings[:minify_whitespaces]
73
- when '<'
74
- save_state
75
-
76
- unshift_spaces_from_buffer
77
- flush_buffer
51
+ when 'a'..'z', 'A'..'Z'
52
+ add_to_buffer char
53
+ when '>'
54
+ unshift_spaces_from_buffer if saved_state_unshiftable?
78
55
 
79
56
  add_to_buffer char
80
- change_state_to :tag_or_tagend_or_comment_or_doctype_begin
57
+ flush_buffer
58
+
59
+ change_state_to :entity_ended
81
60
  when HTMLMin::Minifier::END_OF_FILE
82
- unshift_spaces_from_buffer
83
61
  flush_buffer
84
62
  else
85
- unshift_spaces_from_buffer
86
- flush_buffer
87
-
63
+ reset_saved_state
64
+
65
+ flush_buffer
66
+
88
67
  add_to_result char
89
68
  change_state_to :start
90
69
  end
91
70
  end
92
-
71
+
93
72
  end
94
73
 
95
74
  end
@@ -0,0 +1,25 @@
1
+ module HTMLMin
2
+
3
+ module State
4
+
5
+ private
6
+
7
+ def save_state
8
+ @prev_state = @state
9
+ end
10
+
11
+ def reset_saved_state
12
+ @prev_state = nil
13
+ end
14
+
15
+ def saved_state_empty?
16
+ @prev_state.nil?
17
+ end
18
+
19
+ def saved_state_unshiftable?
20
+ @prev_state == :entity_ended
21
+ end
22
+
23
+ end
24
+
25
+ end
@@ -1,3 +1,3 @@
1
1
  module Htmlmin
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
data/test/input/cc_1.txt CHANGED
@@ -5,4 +5,5 @@
5
5
  <!--[if gte IE 8]>
6
6
  <p>Internet Explorer 8 or later</p>
7
7
  <![endif]-->
8
+ <![if expression]> HTML <![endif]>
8
9
  </html>
@@ -0,0 +1,9 @@
1
+ <html>
2
+ <!--[if lte IE 7]>
3
+ <p>Internet Explorer 7 or older</p>
4
+ <![endif]-->
5
+ <!-- comment should be removed -->
6
+ <!--[if lte IE 7]>
7
+ <p>also should be removed</p>
8
+ -->
9
+ </html>
@@ -0,0 +1,6 @@
1
+
2
+ <!-- hello before doctype -->
3
+
4
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><!-- no way comment -->
5
+
6
+ <html>
@@ -0,0 +1,3 @@
1
+ <p> hello </p>
2
+ <p> how <em>are</em> you? </p>
3
+ <p> <small> <strong> nothing?</strong> </small> </p>
@@ -0,0 +1,8 @@
1
+ {
2
+ :remove_comments => true,
3
+ :remove_conditional_comments => true,
4
+
5
+ :max_line_len => 0,
6
+ :minify_whitespaces => true,
7
+ :debug => false
8
+ }
@@ -0,0 +1,8 @@
1
+ {
2
+ :remove_comments => true,
3
+ :remove_conditional_comments => false,
4
+
5
+ :max_line_len => 0,
6
+ :minify_whitespaces => true,
7
+ :debug => false
8
+ }
@@ -0,0 +1,8 @@
1
+ {
2
+ :remove_comments => true,
3
+ :preserve_conditional_comments => false,
4
+
5
+ :max_line_len => 0,
6
+ :minify_whitespaces => true,
7
+ :debug => false
8
+ }
@@ -0,0 +1,8 @@
1
+ {
2
+ :remove_comments => true,
3
+
4
+ :minify_whitespaces => true,
5
+ :max_line_len => 0,
6
+
7
+ :debug => false
8
+ }
@@ -2,6 +2,6 @@
2
2
  :remove_comments => false,
3
3
 
4
4
  :minify_whitespaces => true,
5
- :debug => false,
6
- :max_line_len => 0
5
+ :max_line_len => 0,
6
+ :debug => false
7
7
  }
@@ -2,6 +2,5 @@
2
2
  :remove_comments => true,
3
3
 
4
4
  :minify_whitespaces => false,
5
- :debug => false,
6
5
  :max_line_len => 0
7
6
  }
@@ -0,0 +1,8 @@
1
+ {
2
+ :remove_comments => true,
3
+ :preserve_conditional_comments => false,
4
+
5
+ :max_line_len => 0,
6
+ :minify_whitespaces => true,
7
+ :debug => false
8
+ }
@@ -0,0 +1,8 @@
1
+ {
2
+ :minify_whitespaces => true,
3
+
4
+ :max_line_len => 0,
5
+ :remove_comments => true,
6
+ :preserve_conditional_comments => false,
7
+ :debug => false
8
+ }
@@ -0,0 +1,8 @@
1
+ {
2
+ :remove_comments => true,
3
+ :preserve_conditional_comments => false,
4
+
5
+ :max_line_len => 0,
6
+ :minify_whitespaces => true,
7
+ :debug => false
8
+ }
data/test/output/cc_1.txt CHANGED
@@ -1 +1 @@
1
- <html></html>
1
+ <html> <![if expression]> HTML <![endif]> </html>
@@ -0,0 +1,3 @@
1
+ <html><!--[if lte IE 7]>
2
+ <p>Internet Explorer 7 or older</p>
3
+ <![endif]--></html>
@@ -1 +1 @@
1
- <p>Fix me</p>
1
+ <p> Fix me </p>
@@ -1 +1,2 @@
1
- <!-- this comment should stay here --><!-- this should also -- because this is not comment<!-- and no whitespaces! -->
1
+ <!-- this comment should stay here --><!-- this should also -- because this is not comment
2
+ <!-- and no whitespaces! -->
@@ -0,0 +1,2 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2
+ <html>
@@ -1 +1 @@
1
- <p><em>nothing at all</em></p>
1
+ <p><em> nothing at all</em></p>
@@ -0,0 +1 @@
1
+ <p> hello </p><p> how <em>are</em> you? </p><p><small><strong> nothing?</strong></small></p>
@@ -1 +1 @@
1
- <p>please, stay with me</p>
1
+ <p> please, stay with me </p>
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: htmlmin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-08-24 00:00:00.000000000Z
12
+ date: 2011-08-26 00:00:00.000000000Z
13
13
  dependencies: []
14
14
  description: HTMLMin is a HTML-code minification tool implemented in Ruby. It works
15
15
  like a slot machine.
@@ -32,8 +32,10 @@ files:
32
32
  - lib/htmlmin/slot_machines/common.rb
33
33
  - lib/htmlmin/slot_machines/doctype.rb
34
34
  - lib/htmlmin/slot_machines/tag.rb
35
+ - lib/htmlmin/state.rb
35
36
  - lib/htmlmin/version.rb
36
37
  - test/input/cc_1.txt
38
+ - test/input/cc_2.txt
37
39
  - test/input/comment_1.txt
38
40
  - test/input/comment_2.txt
39
41
  - test/input/comment_3.txt
@@ -44,22 +46,32 @@ files:
44
46
  - test/input/comment_8.txt
45
47
  - test/input/doctype_1.txt
46
48
  - test/input/doctype_2.txt
49
+ - test/input/doctype_3.txt
47
50
  - test/input/line_length_1.txt
48
51
  - test/input/line_length_2.txt
49
52
  - test/input/line_length_3.txt
50
53
  - test/input/line_length_4.txt
51
54
  - test/input/tag_1.txt
52
55
  - test/input/tag_2.txt
56
+ - test/input/tag_3.txt
53
57
  - test/input/text_1.txt
54
58
  - test/input/text_2.txt
55
59
  - test/input/text_3.txt
60
+ - test/options/cc_1.rb
61
+ - test/options/cc_2.rb
62
+ - test/options/comment_1.rb
63
+ - test/options/comment_6.rb
56
64
  - test/options/comment_7.rb
57
65
  - test/options/comment_8.rb
66
+ - test/options/doctype_3.rb
58
67
  - test/options/line_length_1.rb
59
68
  - test/options/line_length_2.rb
60
69
  - test/options/line_length_3.rb
61
70
  - test/options/line_length_4.rb
71
+ - test/options/tag_2.rb
72
+ - test/options/tag_3.rb
62
73
  - test/output/cc_1.txt
74
+ - test/output/cc_2.txt
63
75
  - test/output/comment_1.txt
64
76
  - test/output/comment_2.txt
65
77
  - test/output/comment_3.txt
@@ -70,12 +82,14 @@ files:
70
82
  - test/output/comment_8.txt
71
83
  - test/output/doctype_1.txt
72
84
  - test/output/doctype_2.txt
85
+ - test/output/doctype_3.txt
73
86
  - test/output/line_length_1.txt
74
87
  - test/output/line_length_2.txt
75
88
  - test/output/line_length_3.txt
76
89
  - test/output/line_length_4.txt
77
90
  - test/output/tag_1.txt
78
91
  - test/output/tag_2.txt
92
+ - test/output/tag_3.txt
79
93
  - test/output/text_1.txt
80
94
  - test/output/text_2.txt
81
95
  - test/output/text_3.txt
@@ -107,6 +121,7 @@ specification_version: 3
107
121
  summary: HTMLMin is a HTML-code minification tool
108
122
  test_files:
109
123
  - test/input/cc_1.txt
124
+ - test/input/cc_2.txt
110
125
  - test/input/comment_1.txt
111
126
  - test/input/comment_2.txt
112
127
  - test/input/comment_3.txt
@@ -117,22 +132,32 @@ test_files:
117
132
  - test/input/comment_8.txt
118
133
  - test/input/doctype_1.txt
119
134
  - test/input/doctype_2.txt
135
+ - test/input/doctype_3.txt
120
136
  - test/input/line_length_1.txt
121
137
  - test/input/line_length_2.txt
122
138
  - test/input/line_length_3.txt
123
139
  - test/input/line_length_4.txt
124
140
  - test/input/tag_1.txt
125
141
  - test/input/tag_2.txt
142
+ - test/input/tag_3.txt
126
143
  - test/input/text_1.txt
127
144
  - test/input/text_2.txt
128
145
  - test/input/text_3.txt
146
+ - test/options/cc_1.rb
147
+ - test/options/cc_2.rb
148
+ - test/options/comment_1.rb
149
+ - test/options/comment_6.rb
129
150
  - test/options/comment_7.rb
130
151
  - test/options/comment_8.rb
152
+ - test/options/doctype_3.rb
131
153
  - test/options/line_length_1.rb
132
154
  - test/options/line_length_2.rb
133
155
  - test/options/line_length_3.rb
134
156
  - test/options/line_length_4.rb
157
+ - test/options/tag_2.rb
158
+ - test/options/tag_3.rb
135
159
  - test/output/cc_1.txt
160
+ - test/output/cc_2.txt
136
161
  - test/output/comment_1.txt
137
162
  - test/output/comment_2.txt
138
163
  - test/output/comment_3.txt
@@ -143,12 +168,14 @@ test_files:
143
168
  - test/output/comment_8.txt
144
169
  - test/output/doctype_1.txt
145
170
  - test/output/doctype_2.txt
171
+ - test/output/doctype_3.txt
146
172
  - test/output/line_length_1.txt
147
173
  - test/output/line_length_2.txt
148
174
  - test/output/line_length_3.txt
149
175
  - test/output/line_length_4.txt
150
176
  - test/output/tag_1.txt
151
177
  - test/output/tag_2.txt
178
+ - test/output/tag_3.txt
152
179
  - test/output/text_1.txt
153
180
  - test/output/text_2.txt
154
181
  - test/output/text_3.txt