html_press 0.0.1 → 0.0.2

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.
@@ -1,8 +1,14 @@
1
1
  module HtmlPress
2
2
  class Html
3
3
 
4
- def self.compress text
5
- Html.new.compile text
4
+ def initialize (options = nil)
5
+ @options = options
6
+ end
7
+
8
+ def log (text)
9
+ if !@options.nil? && @options[:logger]
10
+ @options[:logger].warn text
11
+ end
6
12
  end
7
13
 
8
14
  def compile (html)
@@ -13,6 +19,15 @@ module HtmlPress
13
19
  @placeholders = []
14
20
  @strip_crlf = false
15
21
 
22
+ # IE conditional comments
23
+ out.gsub! /\s*(<!--\[[^\]]+\]>[\s\S]*?<!\[[^\]]+\]-->)\s*/ do |m|
24
+ m.gsub!(/^\s+|\s+$/, '')
25
+ comment = m.gsub(/\s*<!--\[[^\]]+\]>([\s\S]*?)<!\[[^\]]+\]-->\s*/, "\\1")
26
+ comment_compressed = Html.new.compile(comment)
27
+ m.gsub!(comment, comment_compressed)
28
+ reserve m
29
+ end
30
+
16
31
  # replace SCRIPTs (and minify) with placeholders
17
32
  out.gsub! /\s*(<script\b[^>]*?>[\s\S]*?<\/script>)\s*/i do |m|
18
33
  m.gsub!(/^\s+|\s+$/, '')
@@ -21,7 +36,7 @@ module HtmlPress
21
36
  js_compressed = HtmlPress.js_compressor js
22
37
  m.gsub!(js, js_compressed)
23
38
  rescue Exception => e
24
- # p e.message
39
+ log e.message
25
40
  end
26
41
  reserve m
27
42
  end
@@ -34,20 +49,11 @@ module HtmlPress
34
49
  css_compressed = HtmlPress.css_compressor css
35
50
  m.gsub!(css, css_compressed)
36
51
  rescue Exception => e
37
- # p e.message
52
+ log e.message
38
53
  end
39
54
  reserve m
40
55
  end
41
56
 
42
- # IE conditional comments
43
- out.gsub! /\s*(<!--\[[^\]]+\]>[\s\S]*?<!\[[^\]]+\]-->)\s*/ do |m|
44
- m.gsub!(/^\s+|\s+$/, '')
45
- comment = m.gsub(/\s*<!--\[[^\]]+\]>([\s\S]*?)<!\[[^\]]+\]-->\s*/, "\\1")
46
- comment_compressed = Html.new.compile(comment)
47
- m.gsub!(comment, comment_compressed)
48
- reserve m
49
- end
50
-
51
57
  # remove out comments (not containing IE conditional comments).
52
58
  out.gsub! /<!--([\s\S]*?)-->/ do |m|
53
59
  ''
@@ -72,7 +78,7 @@ module HtmlPress
72
78
 
73
79
  re = '\\s+(<\\/?(?:area|base(?:font)?|blockquote|body' +
74
80
  '|caption|center|cite|col(?:group)?|dd|dir|div|dl|dt|fieldset|form' +
75
- '|frame(?:set)?|h[1-6]|head|hr|out|legend|li|link|map|menu|meta' +
81
+ '|frame(?:set)?|h[1-6]|head|hr|html|legend|li|link|map|menu|meta' +
76
82
  '|ol|opt(?:group|ion)|p|param|t(?:able|body|head|d|h|r|foot|itle)' +
77
83
  '|ul)\\b[^>]*>)'
78
84
 
@@ -122,13 +128,13 @@ module HtmlPress
122
128
  end
123
129
 
124
130
  if attributes.size > 0
125
- attributes_compressed = attributes.gsub(/\s*([a-z\-_]+(="[^"]*")?(='[^']*')?)\s*/i, " \\1")
131
+ attributes_compressed = attributes.gsub(/\s*([a-z\-_:]+(="[^"]*")?(='[^']*')?)\s*/i, " \\1")
126
132
 
127
- attributes_compressed.gsub! /([a-z\-_]+="[^"]*")/ do |k|
133
+ attributes_compressed.gsub! /([a-z\-_:]+="[^"]*")/i do |k|
128
134
  attr k, "\"", tag
129
135
  end
130
136
 
131
- attributes_compressed.gsub! /([a-z\-_]+='[^']*')/ do |k|
137
+ attributes_compressed.gsub! /([a-z\-_:]+='[^']*')/i do |k|
132
138
  attr k, "'", tag
133
139
  end
134
140
 
@@ -139,7 +145,7 @@ module HtmlPress
139
145
  end
140
146
 
141
147
  def attr(attribute, delimiter, tag)
142
- re = "([a-z\\-_]+)(=" + delimiter + "[^" + delimiter + "]*" + delimiter + ")?"
148
+ re = "([a-z\\-_:]+)(=" + delimiter + "[^" + delimiter + "]*" + delimiter + ")?"
143
149
  re = Regexp.new re
144
150
  value = attribute.gsub(re, "\\2")
145
151
 
@@ -1,3 +1,3 @@
1
1
  module HtmlPress
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/html_press.rb CHANGED
@@ -4,7 +4,7 @@ require "html_press/uglifier"
4
4
  require "html_press/html"
5
5
 
6
6
  module HtmlPress
7
- def self.compress(text)
8
- self::Html.new.compile text
7
+ def self.compress(text, options = nil)
8
+ self::Html.new(options).compile text
9
9
  end
10
10
  end
@@ -2,6 +2,16 @@
2
2
 
3
3
  require_relative "../lib/html_press"
4
4
 
5
+ class Lg
6
+ attr_accessor :warns
7
+ def initialize
8
+ @warns = []
9
+ end
10
+ def warn text
11
+ @warns.push text
12
+ end
13
+ end
14
+
5
15
  describe HtmlPress do
6
16
  before :each do
7
17
  end
@@ -12,6 +22,7 @@ describe HtmlPress do
12
22
 
13
23
  it "should leave no whitespaces between block tags" do
14
24
  HtmlPress.compress("<div></div> \t\r\n <div></div>").should eql "<div></div><div></div>"
25
+ HtmlPress.compress("<div> <div> \t\r\n </div> </div>").should eql "<div><div></div></div>"
15
26
  end
16
27
 
17
28
  it "should leave only one whitespace in text" do
@@ -80,6 +91,11 @@ describe HtmlPress do
80
91
  text = "<!--[if IE]>" + text + "<![endif]-->"
81
92
  text2 = "<!--[if IE]>" + text2 + "<![endif]-->"
82
93
  HtmlPress.compress(text).should eql text2
94
+ text = "<script> (function(undefined){ var a;}()) </script>"
95
+ text2 = HtmlPress.compress(text)
96
+ text = "<!--[if IE]>" + text + "<![endif]-->"
97
+ text2 = "<!--[if IE]>" + text2 + "<![endif]-->"
98
+ HtmlPress.compress(text).should eql text2
83
99
  end
84
100
 
85
101
  it "should remove unnecessary whitespaces in html attributes (class)" do
@@ -91,6 +107,25 @@ describe HtmlPress do
91
107
  HtmlPress.compress("<p style=\"display: none;\"></p>").should eql "<p style=\"display:none;\"></p>"
92
108
  end
93
109
 
110
+ it "should work with namespaces" do
111
+ text = "<html xmlns:og=\"http://ogp.me/ns#\" class=\"a b\"><og:like>like</og:like></html>"
112
+ HtmlPress.compress(text).should eql text
113
+ end
114
+
115
+ it "should not modify input value" do
116
+ text = "<div> </div>"
117
+ text1 = text + ""
118
+ HtmlPress.compress(text)
119
+ text.should eql text1
120
+ end
121
+
122
+ it "should not modify input value" do
123
+ script_with_error = "<script>function(){</script>"
124
+ l = Lg.new
125
+ l.warns.size.should eql 0
126
+ HtmlPress.compress(script_with_error, {:logger => l}).should eql script_with_error
127
+ l.warns.size.should eql 1
128
+ end
94
129
  # it "should remove unnecessary attributes" do
95
130
  # HtmlPress.compress("<script type=\"text/javascript\">var a;</script>").should eql "<script>var a;</script>"
96
131
  # end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: html_press
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-02-04 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &19832364 !ruby/object:Gem::Requirement
16
+ requirement: &25089276 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *19832364
24
+ version_requirements: *25089276
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &20531592 !ruby/object:Gem::Requirement
27
+ requirement: &25978872 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *20531592
35
+ version_requirements: *25978872
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rainpress
38
- requirement: &20756364 !ruby/object:Gem::Requirement
38
+ requirement: &26094612 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *20756364
46
+ version_requirements: *26094612
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: uglifier
49
- requirement: &21077148 !ruby/object:Gem::Requirement
49
+ requirement: &26094228 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: '0'
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *21077148
57
+ version_requirements: *26094228
58
58
  description: Ruby gem for compressing html
59
59
  email:
60
60
  - stereobooster@gmail.com