From 177d2044f0003423b441220bf54e21ca4fe5ab25 Mon Sep 17 00:00:00 2001 From: Alexey Vasiliev Date: Thu, 25 Dec 2014 22:00:21 +0200 Subject: [PATCH 01/23] Fix warning for duplicated keys htmlcompressor-0.1.2/lib/htmlcompressor/compressor.rb:112: warning: duplicated key at line 132 ignored: :javascript_compressor htmlcompressor-0.1.2/lib/htmlcompressor/compressor.rb:113: warning: duplicated key at line 133 ignored: :css_compressor --- lib/htmlcompressor/compressor.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/htmlcompressor/compressor.rb b/lib/htmlcompressor/compressor.rb index e8a0275..38d2c33 100644 --- a/lib/htmlcompressor/compressor.rb +++ b/lib/htmlcompressor/compressor.rb @@ -128,9 +128,7 @@ class Compressor :preserve_line_breaks => false, :remove_surrounding_spaces => nil, - :preserve_patterns => nil, - :javascript_compressor => nil, - :css_compressor => nil + :preserve_patterns => nil } def initialize(options = {}) From 7436fd248e440829f606d460336c282c3c84311d Mon Sep 17 00:00:00 2001 From: Roland Moriz Date: Fri, 13 Mar 2015 11:39:43 +0100 Subject: [PATCH 02/23] disable remove_http_protocol by default --- README.md | 2 +- lib/htmlcompressor/rack.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9f933f7..94c6da6 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,7 @@ The middleware uses a little more aggressive options by default: :remove_form_attributes => false, :remove_input_attributes => true, :remove_javascript_protocol => true, - :remove_http_protocol => true, + :remove_http_protocol => false, :remove_https_protocol => false, :preserve_line_breaks => false, :simple_boolean_attributes => true diff --git a/lib/htmlcompressor/rack.rb b/lib/htmlcompressor/rack.rb index f72474b..706ae84 100644 --- a/lib/htmlcompressor/rack.rb +++ b/lib/htmlcompressor/rack.rb @@ -17,7 +17,7 @@ class Rack :remove_form_attributes => false, :remove_input_attributes => true, :remove_javascript_protocol => true, - :remove_http_protocol => true, + :remove_http_protocol => false, :remove_https_protocol => false, :preserve_line_breaks => false, :simple_boolean_attributes => true From 22cf66cf570556f4f864d80e7f5a36df2756a797 Mon Sep 17 00:00:00 2001 From: Andrew Kvalheim Date: Sun, 22 Mar 2015 19:04:09 -0700 Subject: [PATCH 03/23] Unindent fenced code blocks. --- README.md | 138 +++++++++++++++++++++++++++--------------------------- 1 file changed, 68 insertions(+), 70 deletions(-) diff --git a/README.md b/README.md index 9f933f7..88b6a4b 100644 --- a/README.md +++ b/README.md @@ -15,69 +15,69 @@ Please note that Htmlcompressor is still in alpha version and need some addition Using the compressor class is straightforward: ```ruby - compressor = HtmlCompressor::Compressor.new - compressor.compress('
') +compressor = HtmlCompressor::Compressor.new +compressor.compress('
') ``` The compressor ships with basic and safe default options that may be overwritten passing the options hash to the constructor: ```ruby - options = { - :enabled => true, - :remove_multi_spaces => true, - :remove_comments => true, - :remove_intertag_spaces => false, - :remove_quotes => false, - :compress_css => false, - :compress_javascript => false, - :simple_doctype => false, - :remove_script_attributes => false, - :remove_style_attributes => false, - :remove_link_attributes => false, - :remove_form_attributes => false, - :remove_input_attributes => false, - :remove_javascript_protocol => false, - :remove_http_protocol => false, - :remove_https_protocol => false, - :preserve_line_breaks => false, - :simple_boolean_attributes => false - } +options = { + :enabled => true, + :remove_multi_spaces => true, + :remove_comments => true, + :remove_intertag_spaces => false, + :remove_quotes => false, + :compress_css => false, + :compress_javascript => false, + :simple_doctype => false, + :remove_script_attributes => false, + :remove_style_attributes => false, + :remove_link_attributes => false, + :remove_form_attributes => false, + :remove_input_attributes => false, + :remove_javascript_protocol => false, + :remove_http_protocol => false, + :remove_https_protocol => false, + :preserve_line_breaks => false, + :simple_boolean_attributes => false +} ``` Using rack middleware is as easy as: ```ruby - config.middleware.use HtmlCompressor::Rack, options +config.middleware.use HtmlCompressor::Rack, options ``` The middleware uses a little more aggressive options by default: ```ruby - options = { - :enabled => true, - :remove_multi_spaces => true, - :remove_comments => true, - :remove_intertag_spaces => false, - :remove_quotes => true, - :compress_css => false, - :compress_javascript => false, - :simple_doctype => false, - :remove_script_attributes => true, - :remove_style_attributes => true, - :remove_link_attributes => true, - :remove_form_attributes => false, - :remove_input_attributes => true, - :remove_javascript_protocol => true, - :remove_http_protocol => true, - :remove_https_protocol => false, - :preserve_line_breaks => false, - :simple_boolean_attributes => true - } +options = { + :enabled => true, + :remove_multi_spaces => true, + :remove_comments => true, + :remove_intertag_spaces => false, + :remove_quotes => true, + :compress_css => false, + :compress_javascript => false, + :simple_doctype => false, + :remove_script_attributes => true, + :remove_style_attributes => true, + :remove_link_attributes => true, + :remove_form_attributes => false, + :remove_input_attributes => true, + :remove_javascript_protocol => true, + :remove_http_protocol => true, + :remove_https_protocol => false, + :preserve_line_breaks => false, + :simple_boolean_attributes => true +} ``` Rails 2.3 users may need to add ```ruby - require 'htmlcompressor' +require 'htmlcompressor' ``` ## CSS and JavaScript Compression @@ -87,48 +87,46 @@ In order to minify in page javascript and css, you need to supply a compressor i A compressor can be `:yui` or `:closure` or any object that responds to `:compress`. E.g.: `compressed = compressor.compress(source)` ```ruby +class MyCompressor - class MyCompressor - - def compress(source) - return 'minified' - end - + def compress(source) + return 'minified' end - options = { - :compress_css => true, - :css_compressor => MyCompressor.new, - :compress_javascript => true, - :javascript_compressor => MyCompressor.new - } +end +options = { + :compress_css => true, + :css_compressor => MyCompressor.new, + :compress_javascript => true, + :javascript_compressor => MyCompressor.new +} ``` Please note that in order to use yui or closure compilers you need to manually add them to the Gemfile ```ruby - gem 'yui-compressor' +gem 'yui-compressor' - ... +... - options = { - :compress_javscript => true, - :javascript_compressor => :yui, - :compress_css => true - :css_compressor => :yui - } +options = { + :compress_javscript => true, + :javascript_compressor => :yui, + :compress_css => true + :css_compressor => :yui +} ``` ```ruby - gem 'closure-compiler' +gem 'closure-compiler' - ... +... - options = { - :compress_javascript => true, - :javascript_compressor => :closure - } +options = { + :compress_javascript => true, + :javascript_compressor => :closure +} ``` ## Statistics From 08465107b5e42334b3ec6821ebd77e0c0122202b Mon Sep 17 00:00:00 2001 From: Andrew Kvalheim Date: Sun, 22 Mar 2015 19:13:21 -0700 Subject: [PATCH 04/23] Document the preserve_patterns option. --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index 88b6a4b..33b590c 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,16 @@ Rails 2.3 users may need to add require 'htmlcompressor' ``` +## Custom preservation rules + +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: + +```ruby +options = { + :preserve_patterns => [/<\?php.*?\?>/im] +} +``` + ## CSS and JavaScript Compression By default CSS/JS compression is disabled. From f376a8099abee39b46a44164fa8bc47092e53d9c Mon Sep 17 00:00:00 2001 From: Paolo Chiodi Date: Tue, 31 Mar 2015 18:31:33 +0200 Subject: [PATCH 05/23] compress custom js templates --- CHANGELOG | 2 ++ README.md | 13 +++++++++++-- lib/htmlcompressor/compressor.rb | 17 ++++++++++++++++- test/compressor_closure_test.rb | 3 ++- test/compressor_test.rb | 9 ++++++++- test/compressor_yui_test.rb | 3 ++- .../html/testCompressCustomHtmlTemplates.html | 1 + .../testCompressCustomHtmlTemplatesResult.html | 1 + 8 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 test/resources/html/testCompressCustomHtmlTemplates.html create mode 100644 test/resources/html/testCompressCustomHtmlTemplatesResult.html diff --git a/CHANGELOG b/CHANGELOG index 2a0cf57..004bef1 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,5 @@ +0.2.0 - TBD + Support for custom html templates 0.1.2 - 04/09/2014 Preserve custom attributes similar to simple attributes, such as ng-disabled (fixes #12) 0.1.1 - 08/05/2014 diff --git a/README.md b/README.md index 9f933f7..007b20f 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,8 @@ The compressor ships with basic and safe default options that may be overwritten :remove_http_protocol => false, :remove_https_protocol => false, :preserve_line_breaks => false, - :simple_boolean_attributes => false + :simple_boolean_attributes => false, + :compress_js_templates => false } ``` @@ -80,6 +81,14 @@ Rails 2.3 users may need to add require 'htmlcompressor' ``` +## Javascript template compression + +You can compress javascript templates that are present in the html. +Setting the `:compress_js_templates` options to `true` will by default compress the content of script tags marked with `type="text/x-jquery-tmpl"`. +For compressing other types of templates, you can pass a string (or an array of strings) containing the type: `:compress_js_templates => ['text/html']`. +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. + + ## CSS and JavaScript Compression By default CSS/JS compression is disabled. @@ -113,7 +122,7 @@ Please note that in order to use yui or closure compilers you need to manually a ... options = { - :compress_javscript => true, + :compress_javascript => true, :javascript_compressor => :yui, :compress_css => true :css_compressor => :yui diff --git a/lib/htmlcompressor/compressor.rb b/lib/htmlcompressor/compressor.rb index 38d2c33..d753086 100644 --- a/lib/htmlcompressor/compressor.rb +++ b/lib/htmlcompressor/compressor.rb @@ -135,6 +135,21 @@ def initialize(options = {}) @options = DEFAULT_OPTIONS.merge(options) + if @options[:compress_js_templates] + @options[:remove_quotes] = false + + js_template_types = [ 'text/x-jquery-tmpl' ] + + unless @options[:compress_js_templates].is_a? TrueClass + js_template_types << @options[:compress_js_templates] + js_template_types.flatten! + end + + @options[:js_template_types] = js_template_types + else + @options[:js_template_types] = [] + end + detect_external_compressors end @@ -312,7 +327,7 @@ def preserve_blocks(html, preBlocks, taBlocks, scriptBlocks, styleBlocks, eventB scriptBlocks << group_2 index += 1 group_1 + message_format(TEMP_SCRIPT_BLOCK, index) + group_3 - elsif type == 'text/x-jquery-tmpl' + elsif @options[:js_template_types].include?(type) # jquery template, ignore so it gets compressed with the rest of html match else diff --git a/test/compressor_closure_test.rb b/test/compressor_closure_test.rb index e174579..4c46201 100644 --- a/test/compressor_closure_test.rb +++ b/test/compressor_closure_test.rb @@ -11,7 +11,8 @@ def test_compress_java_script_closure compressor = Compressor.new( :compress_javascript => true, :javascript_compressor => :closure, - :remove_intertag_spaces => true + :remove_intertag_spaces => true, + :compress_js_templates => true ) assert_equal result, compressor.compress(source) diff --git a/test/compressor_test.rb b/test/compressor_test.rb index 50969ce..7705a55 100644 --- a/test/compressor_test.rb +++ b/test/compressor_test.rb @@ -82,7 +82,7 @@ def test_compress source = read_resource("testCompress.html") result = read_resource("testCompressResult.html") - compressor = Compressor.new + compressor = Compressor.new(:compress_js_templates => true) assert_equal result, compressor.compress(source) end @@ -207,6 +207,13 @@ def test_preserve_textarea assert_equal result, compressor.compress(source) end + def test_compress_custom_html_templates + source = read_resource("testCompressCustomHtmlTemplates.html") + result = read_resource("testCompressCustomHtmlTemplatesResult.html") + compressor = Compressor.new(:compress_js_templates => ['text/html'], :remove_quotes => true) + assert_equal result, compressor.compress(source) + end + end end diff --git a/test/compressor_yui_test.rb b/test/compressor_yui_test.rb index a832678..d669f83 100644 --- a/test/compressor_yui_test.rb +++ b/test/compressor_yui_test.rb @@ -11,7 +11,8 @@ def test_compress_javascript_yui compressor = Compressor.new( :compress_javascript => true, :javascript_compressor => :yui, - :remove_intertag_spaces => true + :remove_intertag_spaces => true, + :compress_js_templates => true ) assert_equal result, compressor.compress(source) diff --git a/test/resources/html/testCompressCustomHtmlTemplates.html b/test/resources/html/testCompressCustomHtmlTemplates.html new file mode 100644 index 0000000..7402a3f --- /dev/null +++ b/test/resources/html/testCompressCustomHtmlTemplates.html @@ -0,0 +1 @@ + diff --git a/test/resources/html/testCompressCustomHtmlTemplatesResult.html b/test/resources/html/testCompressCustomHtmlTemplatesResult.html new file mode 100644 index 0000000..04a1f13 --- /dev/null +++ b/test/resources/html/testCompressCustomHtmlTemplatesResult.html @@ -0,0 +1 @@ + \ No newline at end of file From fdcc0cdd24acdf0de9173c20c89f2f8edbbdb2ff Mon Sep 17 00:00:00 2001 From: Paolo Chiodi Date: Tue, 31 Mar 2015 18:46:19 +0200 Subject: [PATCH 06/23] bump version --- CHANGELOG | 4 +++- lib/htmlcompressor/version.rb | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 004bef1..1e2aa45 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,7 @@ -0.2.0 - TBD +0.2.0 - 32/03/2015 + Remove warnings in ruby 2.2 Support for custom html templates + Disable remove_http_protocol by default in the middleware 0.1.2 - 04/09/2014 Preserve custom attributes similar to simple attributes, such as ng-disabled (fixes #12) 0.1.1 - 08/05/2014 diff --git a/lib/htmlcompressor/version.rb b/lib/htmlcompressor/version.rb index 5f48a8c..a79216d 100644 --- a/lib/htmlcompressor/version.rb +++ b/lib/htmlcompressor/version.rb @@ -1,3 +1,3 @@ module HtmlCompressor - VERSION = "0.1.2" + VERSION = "0.2.0" end From a4ad78363f42cf35416b06a5aff2952c6938e6d8 Mon Sep 17 00:00:00 2001 From: Paolo Chiodi Date: Wed, 1 Apr 2015 08:55:34 +0200 Subject: [PATCH 07/23] add ruby 2.2 to travis build --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 75cc44e..34288d5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,6 @@ language: ruby rvm: + - 2.2.1 - 2.1.1 - 2.0.0 - 1.9.3 From 4935b4860e81ce394f32456ccd4237dc02bdea68 Mon Sep 17 00:00:00 2001 From: Joel Larsson Date: Sun, 5 Apr 2015 13:15:34 +0200 Subject: [PATCH 08/23] Update README.md Add how to use the rack middleware in sinatra --- README.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3175a7f..600133a 100644 --- a/README.md +++ b/README.md @@ -45,12 +45,18 @@ options = { } ``` -Using rack middleware is as easy as: +Using rack middleware (in rails) is as easy as: ```ruby config.middleware.use HtmlCompressor::Rack, options ``` +And in sinatra: + +```ruby +use HtmlCompressor::Rack, options +``` + The middleware uses a little more aggressive options by default: ```ruby From cf13ebc91a2c609d29889278a5bb5152f1c61d0c Mon Sep 17 00:00:00 2001 From: Gustavo Lobo Date: Thu, 2 Jul 2015 16:25:43 +0100 Subject: [PATCH 09/23] Place CDATA tags between comments to avoid confusing older browsers. --- lib/htmlcompressor/compressor.rb | 2 +- test/resources/html/testCompressJavaScriptClosureResult.html | 2 +- test/resources/html/testCompressJavaScriptYuiResult.html | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/htmlcompressor/compressor.rb b/lib/htmlcompressor/compressor.rb index d753086..ed75183 100644 --- a/lib/htmlcompressor/compressor.rb +++ b/lib/htmlcompressor/compressor.rb @@ -540,7 +540,7 @@ def compress_javascript(source) result = javascript_compressor.compress(source).strip if cdataWrapper - result = "" + result = "/**/" end result diff --git a/test/resources/html/testCompressJavaScriptClosureResult.html b/test/resources/html/testCompressJavaScriptClosureResult.html index 1cbc5a4..927ebaa 100644 --- a/test/resources/html/testCompressJavaScriptClosureResult.html +++ b/test/resources/html/testCompressJavaScriptClosureResult.html @@ -1,4 +1,4 @@ -
+
 	
+
 	
+
\ No newline at end of file
diff --git a/test/resources/html/testCompressCustomHtmlTemplatesResult.html b/test/resources/html/testCompressCustomHtmlTemplatesResult.html
index 04a1f13..b47cb0a 100644
--- a/test/resources/html/testCompressCustomHtmlTemplatesResult.html
+++ b/test/resources/html/testCompressCustomHtmlTemplatesResult.html
@@ -1 +1 @@
-
\ No newline at end of file
+
\ No newline at end of file

From 3e739ae91d1ba1fcd41aff25cfc9868d085e4d65 Mon Sep 17 00:00:00 2001
From: Paolo Chiodi 
Date: Thu, 28 Dec 2017 17:29:28 +0100
Subject: [PATCH 20/23] add warning about performances

---
 README.md | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/README.md b/README.md
index 0218221..ab4a9d9 100644
--- a/README.md
+++ b/README.md
@@ -47,6 +47,10 @@ options = {
 }
 ```
 
+Htmlcompressor also ships a rack middleware that can be used with rails, sinatra or rack.
+
+** However keep in mind that compression is slow (especially when also compressing javascript or css) and can negatively impact the performance of your website. Take your measurements and adopt a different strategy if necessary **
+
 Using rack middleware (in rails) is as easy as:
 
 ```ruby

From 8e1c04dd85cb7fb3a47dbe11a73365e2938300e8 Mon Sep 17 00:00:00 2001
From: Paolo Chiodi 
Date: Thu, 28 Dec 2017 17:29:46 +0100
Subject: [PATCH 21/23] bump version

---
 lib/htmlcompressor/version.rb | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/htmlcompressor/version.rb b/lib/htmlcompressor/version.rb
index eab50bb..4a4f47c 100644
--- a/lib/htmlcompressor/version.rb
+++ b/lib/htmlcompressor/version.rb
@@ -1,3 +1,3 @@
 module HtmlCompressor
-  VERSION = "0.3.1"
+  VERSION = "0.4.0"
 end

From 5f20fd1b53263520886d8b942aa33b3185713e7e Mon Sep 17 00:00:00 2001
From: Paolo Chiodi 
Date: Thu, 28 Dec 2017 17:33:35 +0100
Subject: [PATCH 22/23] add recent ruby versions for travis

---
 .travis.yml | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/.travis.yml b/.travis.yml
index 34288d5..2322a17 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,8 +1,11 @@
 language: ruby
 rvm:
-  - 2.2.1
-  - 2.1.1
-  - 2.0.0
+  - 2.5
+  - 2.4
+  - 2.3
+  - 2.2
+  - 2.1
+  - 2.0
   - 1.9.3
   - jruby-18mode # JRuby in 1.8 mode
   - jruby-19mode # JRuby in 1.9 mode

From 4c8849418ed51f390a509cc93a7fd1631c42ed82 Mon Sep 17 00:00:00 2001
From: Paolo Chiodi 
Date: Thu, 28 Dec 2017 17:36:36 +0100
Subject: [PATCH 23/23] Fix readme style

---
 README.md | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/README.md b/README.md
index ab4a9d9..0eca79f 100644
--- a/README.md
+++ b/README.md
@@ -49,7 +49,7 @@ options = {
 
 Htmlcompressor also ships a rack middleware that can be used with rails, sinatra or rack.
 
-** However keep in mind that compression is slow (especially when also compressing javascript or css) and can negatively impact the performance of your website. Take your measurements and adopt a different strategy if necessary **
+**However keep in mind that compression is slow (especially when also compressing javascript or css) and can negatively impact the performance of your website. Take your measurements and adopt a different strategy if necessary**
 
 Using rack middleware (in rails) is as easy as: