htmlcompressor 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +1 -0
- data/CHANGELOG +4 -0
- data/README.md +85 -68
- data/lib/htmlcompressor/compressor.rb +17 -4
- data/lib/htmlcompressor/rack.rb +1 -1
- data/lib/htmlcompressor/version.rb +1 -1
- data/test/compressor_closure_test.rb +2 -1
- data/test/compressor_test.rb +8 -1
- data/test/compressor_yui_test.rb +2 -1
- data/test/resources/html/testCompressCustomHtmlTemplates.html +1 -0
- data/test/resources/html/testCompressCustomHtmlTemplatesResult.html +1 -0
- metadata +8 -4
data/.travis.yml
CHANGED
data/CHANGELOG
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
0.2.0 - 32/03/2015
|
2
|
+
Remove warnings in ruby 2.2
|
3
|
+
Support for custom html templates
|
4
|
+
Disable remove_http_protocol by default in the middleware
|
1
5
|
0.1.2 - 04/09/2014
|
2
6
|
Preserve custom attributes similar to simple attributes, such as ng-disabled (fixes #12)
|
3
7
|
0.1.1 - 08/05/2014
|
data/README.md
CHANGED
@@ -22,62 +22,81 @@ Using the compressor class is straightforward:
|
|
22
22
|
The compressor ships with basic and safe default options that may be overwritten passing the options hash to the constructor:
|
23
23
|
|
24
24
|
```ruby
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
25
|
+
options = {
|
26
|
+
:enabled => true,
|
27
|
+
:remove_multi_spaces => true,
|
28
|
+
:remove_comments => true,
|
29
|
+
:remove_intertag_spaces => false,
|
30
|
+
:remove_quotes => false,
|
31
|
+
:compress_css => false,
|
32
|
+
:compress_javascript => false,
|
33
|
+
:simple_doctype => false,
|
34
|
+
:remove_script_attributes => false,
|
35
|
+
:remove_style_attributes => false,
|
36
|
+
:remove_link_attributes => false,
|
37
|
+
:remove_form_attributes => false,
|
38
|
+
:remove_input_attributes => false,
|
39
|
+
:remove_javascript_protocol => false,
|
40
|
+
:remove_http_protocol => false,
|
41
|
+
:remove_https_protocol => false,
|
42
|
+
:preserve_line_breaks => false,
|
43
|
+
:simple_boolean_attributes => false,
|
44
|
+
:compress_js_templates => false
|
45
|
+
}
|
45
46
|
```
|
46
47
|
|
47
48
|
Using rack middleware is as easy as:
|
48
49
|
|
49
50
|
```ruby
|
50
|
-
|
51
|
+
config.middleware.use HtmlCompressor::Rack, options
|
51
52
|
```
|
52
53
|
|
53
54
|
The middleware uses a little more aggressive options by default:
|
54
55
|
|
55
56
|
```ruby
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
57
|
+
options = {
|
58
|
+
:enabled => true,
|
59
|
+
:remove_multi_spaces => true,
|
60
|
+
:remove_comments => true,
|
61
|
+
:remove_intertag_spaces => false,
|
62
|
+
:remove_quotes => true,
|
63
|
+
:compress_css => false,
|
64
|
+
:compress_javascript => false,
|
65
|
+
:simple_doctype => false,
|
66
|
+
:remove_script_attributes => true,
|
67
|
+
:remove_style_attributes => true,
|
68
|
+
:remove_link_attributes => true,
|
69
|
+
:remove_form_attributes => false,
|
70
|
+
:remove_input_attributes => true,
|
71
|
+
:remove_javascript_protocol => true,
|
72
|
+
:remove_http_protocol => false,
|
73
|
+
:remove_https_protocol => false,
|
74
|
+
:preserve_line_breaks => false,
|
75
|
+
:simple_boolean_attributes => true
|
76
|
+
}
|
76
77
|
```
|
77
78
|
|
78
79
|
Rails 2.3 users may need to add
|
79
80
|
```ruby
|
80
|
-
|
81
|
+
require 'htmlcompressor'
|
82
|
+
```
|
83
|
+
|
84
|
+
## Javascript template compression
|
85
|
+
|
86
|
+
You can compress javascript templates that are present in the html.
|
87
|
+
Setting the `:compress_js_templates` options to `true` will by default compress the content of script tags marked with `type="text/x-jquery-tmpl"`.
|
88
|
+
For compressing other types of templates, you can pass a string (or an array of strings) containing the type: `:compress_js_templates => ['text/html']`.
|
89
|
+
Please note that activating template compression will disable the removal of quotes from attributes values, as this could lead to unexpected errors with compiled templates.
|
90
|
+
|
91
|
+
|
92
|
+
## Custom preservation rules
|
93
|
+
|
94
|
+
If you need to define custom preservation rules, you can list regular expressions in the `preserve_patterns` option. For example, to preserve PHP blocks you might want to define:
|
95
|
+
|
96
|
+
```ruby
|
97
|
+
options = {
|
98
|
+
:preserve_patterns => [/<\?php.*?\?>/im]
|
99
|
+
}
|
81
100
|
```
|
82
101
|
|
83
102
|
## CSS and JavaScript Compression
|
@@ -87,48 +106,46 @@ In order to minify in page javascript and css, you need to supply a compressor i
|
|
87
106
|
A compressor can be `:yui` or `:closure` or any object that responds to `:compress`. E.g.: `compressed = compressor.compress(source)`
|
88
107
|
|
89
108
|
```ruby
|
109
|
+
class MyCompressor
|
90
110
|
|
91
|
-
|
92
|
-
|
93
|
-
def compress(source)
|
94
|
-
return 'minified'
|
95
|
-
end
|
96
|
-
|
111
|
+
def compress(source)
|
112
|
+
return 'minified'
|
97
113
|
end
|
98
114
|
|
99
|
-
|
100
|
-
:compress_css => true,
|
101
|
-
:css_compressor => MyCompressor.new,
|
102
|
-
:compress_javascript => true,
|
103
|
-
:javascript_compressor => MyCompressor.new
|
104
|
-
}
|
115
|
+
end
|
105
116
|
|
117
|
+
options = {
|
118
|
+
:compress_css => true,
|
119
|
+
:css_compressor => MyCompressor.new,
|
120
|
+
:compress_javascript => true,
|
121
|
+
:javascript_compressor => MyCompressor.new
|
122
|
+
}
|
106
123
|
```
|
107
124
|
|
108
125
|
Please note that in order to use yui or closure compilers you need to manually add them to the Gemfile
|
109
126
|
|
110
127
|
```ruby
|
111
|
-
|
128
|
+
gem 'yui-compressor'
|
112
129
|
|
113
|
-
|
130
|
+
...
|
114
131
|
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
132
|
+
options = {
|
133
|
+
:compress_javascript => true,
|
134
|
+
:javascript_compressor => :yui,
|
135
|
+
:compress_css => true
|
136
|
+
:css_compressor => :yui
|
137
|
+
}
|
121
138
|
```
|
122
139
|
|
123
140
|
```ruby
|
124
|
-
|
141
|
+
gem 'closure-compiler'
|
125
142
|
|
126
|
-
|
143
|
+
...
|
127
144
|
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
145
|
+
options = {
|
146
|
+
:compress_javascript => true,
|
147
|
+
:javascript_compressor => :closure
|
148
|
+
}
|
132
149
|
```
|
133
150
|
|
134
151
|
## Statistics
|
@@ -128,15 +128,28 @@ module HtmlCompressor
|
|
128
128
|
:preserve_line_breaks => false,
|
129
129
|
:remove_surrounding_spaces => nil,
|
130
130
|
|
131
|
-
:preserve_patterns => nil
|
132
|
-
:javascript_compressor => nil,
|
133
|
-
:css_compressor => nil
|
131
|
+
:preserve_patterns => nil
|
134
132
|
}
|
135
133
|
|
136
134
|
def initialize(options = {})
|
137
135
|
|
138
136
|
@options = DEFAULT_OPTIONS.merge(options)
|
139
137
|
|
138
|
+
if @options[:compress_js_templates]
|
139
|
+
@options[:remove_quotes] = false
|
140
|
+
|
141
|
+
js_template_types = [ 'text/x-jquery-tmpl' ]
|
142
|
+
|
143
|
+
unless @options[:compress_js_templates].is_a? TrueClass
|
144
|
+
js_template_types << @options[:compress_js_templates]
|
145
|
+
js_template_types.flatten!
|
146
|
+
end
|
147
|
+
|
148
|
+
@options[:js_template_types] = js_template_types
|
149
|
+
else
|
150
|
+
@options[:js_template_types] = []
|
151
|
+
end
|
152
|
+
|
140
153
|
detect_external_compressors
|
141
154
|
end
|
142
155
|
|
@@ -314,7 +327,7 @@ module HtmlCompressor
|
|
314
327
|
scriptBlocks << group_2
|
315
328
|
index += 1
|
316
329
|
group_1 + message_format(TEMP_SCRIPT_BLOCK, index) + group_3
|
317
|
-
elsif type
|
330
|
+
elsif @options[:js_template_types].include?(type)
|
318
331
|
# jquery template, ignore so it gets compressed with the rest of html
|
319
332
|
match
|
320
333
|
else
|
data/lib/htmlcompressor/rack.rb
CHANGED
@@ -17,7 +17,7 @@ module HtmlCompressor
|
|
17
17
|
:remove_form_attributes => false,
|
18
18
|
:remove_input_attributes => true,
|
19
19
|
:remove_javascript_protocol => true,
|
20
|
-
:remove_http_protocol =>
|
20
|
+
:remove_http_protocol => false,
|
21
21
|
:remove_https_protocol => false,
|
22
22
|
:preserve_line_breaks => false,
|
23
23
|
:simple_boolean_attributes => true
|
@@ -11,7 +11,8 @@ module HtmlCompressor
|
|
11
11
|
compressor = Compressor.new(
|
12
12
|
:compress_javascript => true,
|
13
13
|
:javascript_compressor => :closure,
|
14
|
-
:remove_intertag_spaces => true
|
14
|
+
:remove_intertag_spaces => true,
|
15
|
+
:compress_js_templates => true
|
15
16
|
)
|
16
17
|
|
17
18
|
assert_equal result, compressor.compress(source)
|
data/test/compressor_test.rb
CHANGED
@@ -82,7 +82,7 @@ module HtmlCompressor
|
|
82
82
|
source = read_resource("testCompress.html")
|
83
83
|
result = read_resource("testCompressResult.html")
|
84
84
|
|
85
|
-
compressor = Compressor.new
|
85
|
+
compressor = Compressor.new(:compress_js_templates => true)
|
86
86
|
|
87
87
|
assert_equal result, compressor.compress(source)
|
88
88
|
end
|
@@ -207,6 +207,13 @@ module HtmlCompressor
|
|
207
207
|
assert_equal result, compressor.compress(source)
|
208
208
|
end
|
209
209
|
|
210
|
+
def test_compress_custom_html_templates
|
211
|
+
source = read_resource("testCompressCustomHtmlTemplates.html")
|
212
|
+
result = read_resource("testCompressCustomHtmlTemplatesResult.html")
|
213
|
+
compressor = Compressor.new(:compress_js_templates => ['text/html'], :remove_quotes => true)
|
214
|
+
assert_equal result, compressor.compress(source)
|
215
|
+
end
|
216
|
+
|
210
217
|
end
|
211
218
|
|
212
219
|
end
|
data/test/compressor_yui_test.rb
CHANGED
@@ -11,7 +11,8 @@ module HtmlCompressor
|
|
11
11
|
compressor = Compressor.new(
|
12
12
|
:compress_javascript => true,
|
13
13
|
:javascript_compressor => :yui,
|
14
|
-
:remove_intertag_spaces => true
|
14
|
+
:remove_intertag_spaces => true,
|
15
|
+
:compress_js_templates => true
|
15
16
|
)
|
16
17
|
|
17
18
|
assert_equal result, compressor.compress(source)
|
@@ -0,0 +1 @@
|
|
1
|
+
<script type="text/html"> <a attribute="value"> <!-- comment --> <b> </script>
|
@@ -0,0 +1 @@
|
|
1
|
+
<script type="text/html"> <a attribute="value"> <b> </script>
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: htmlcompressor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
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:
|
12
|
+
date: 2015-04-01 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: yui-compressor
|
@@ -156,6 +156,8 @@ files:
|
|
156
156
|
- test/resources/html/testCompress.html
|
157
157
|
- test/resources/html/testCompressCss.html
|
158
158
|
- test/resources/html/testCompressCssResult.html
|
159
|
+
- test/resources/html/testCompressCustomHtmlTemplates.html
|
160
|
+
- test/resources/html/testCompressCustomHtmlTemplatesResult.html
|
159
161
|
- test/resources/html/testCompressJavaScript.html
|
160
162
|
- test/resources/html/testCompressJavaScriptClosureResult.html
|
161
163
|
- test/resources/html/testCompressJavaScriptYuiResult.html
|
@@ -233,7 +235,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
233
235
|
version: '0'
|
234
236
|
segments:
|
235
237
|
- 0
|
236
|
-
hash:
|
238
|
+
hash: 472079073096694576
|
237
239
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
238
240
|
none: false
|
239
241
|
requirements:
|
@@ -242,7 +244,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
242
244
|
version: '0'
|
243
245
|
segments:
|
244
246
|
- 0
|
245
|
-
hash:
|
247
|
+
hash: 472079073096694576
|
246
248
|
requirements: []
|
247
249
|
rubyforge_project:
|
248
250
|
rubygems_version: 1.8.25
|
@@ -311,6 +313,8 @@ test_files:
|
|
311
313
|
- test/resources/html/testCompress.html
|
312
314
|
- test/resources/html/testCompressCss.html
|
313
315
|
- test/resources/html/testCompressCssResult.html
|
316
|
+
- test/resources/html/testCompressCustomHtmlTemplates.html
|
317
|
+
- test/resources/html/testCompressCustomHtmlTemplatesResult.html
|
314
318
|
- test/resources/html/testCompressJavaScript.html
|
315
319
|
- test/resources/html/testCompressJavaScriptClosureResult.html
|
316
320
|
- test/resources/html/testCompressJavaScriptYuiResult.html
|