html2haml 1.0.0.beta.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,323 @@
1
+ require 'test_helper'
2
+
3
+ class Html2HamlTest < MiniTest::Unit::TestCase
4
+ def test_empty_render_should_remain_empty
5
+ assert_equal '', render('')
6
+ end
7
+
8
+ def test_doctype
9
+ assert_equal '!!!', render("<!DOCTYPE html>")
10
+ assert_equal '!!! 1.1', render('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">')
11
+ assert_equal '!!! Strict', render('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">')
12
+ assert_equal '!!! Frameset', render('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">')
13
+ assert_equal '!!! Mobile 1.2', render('<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.2//EN" "http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd">')
14
+ assert_equal '!!! Basic 1.1', render('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd">')
15
+ assert_equal '!!!', render('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">')
16
+ assert_equal '!!! Strict', render('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">')
17
+ assert_equal '!!! Frameset', render('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">')
18
+ assert_equal '!!!', render('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">')
19
+ end
20
+
21
+ def test_id_and_class_should_be_removed_from_hash
22
+ assert_equal '%span#foo.bar', render('<span id="foo" class="bar"> </span>')
23
+ end
24
+
25
+ def test_no_tag_name_for_div_if_class_or_id_is_present
26
+ assert_equal '#foo', render('<div id="foo"> </div>')
27
+ assert_equal '.foo', render('<div class="foo"> </div>')
28
+ end
29
+
30
+ def test_multiple_class_names
31
+ assert_equal '.foo.bar.baz', render('<div class=" foo bar baz "> </div>')
32
+ end
33
+
34
+ def test_should_have_pretty_attributes
35
+ assert_equal('%input{:name => "login", :type => "text"}/',
36
+ render('<input type="text" name="login" />'))
37
+ assert_equal('%meta{:content => "text/html", "http-equiv" => "Content-Type"}/',
38
+ render('<meta http-equiv="Content-Type" content="text/html" />'))
39
+ end
40
+
41
+ def test_should_have_html_style_attributes
42
+ assert_equal('%input(name="login" type="text")/',
43
+ render('<input type="text" name="login" />', :html_style_attributes => true))
44
+ assert_equal('%meta(content="text/html" http-equiv="Content-Type")/',
45
+ render('<meta http-equiv="Content-Type" content="text/html" />', :html_style_attributes => true))
46
+ end
47
+
48
+ def test_class_with_dot_and_hash
49
+ assert_equal('%div{:class => "foo.bar"}', render("<div class='foo.bar'></div>"))
50
+ assert_equal('%div{:class => "foo#bar"}', render("<div class='foo#bar'></div>"))
51
+ assert_equal('.foo.bar{:class => "foo#bar foo.bar"}', render("<div class='foo foo#bar bar foo.bar'></div>"))
52
+ end
53
+
54
+ def test_id_with_dot_and_hash
55
+ assert_equal('%div{:id => "foo.bar"}', render("<div id='foo.bar'></div>"))
56
+ assert_equal('%div{:id => "foo#bar"}', render("<div id='foo#bar'></div>"))
57
+ end
58
+
59
+ def test_interpolation
60
+ assert_equal('Foo \#{bar} baz', render('Foo #{bar} baz'))
61
+ end
62
+
63
+ def test_interpolation_in_attrs
64
+ assert_equal('%p{:foo => "\#{bar} baz"}', render('<p foo="#{bar} baz"></p>'))
65
+ end
66
+
67
+ def test_cdata
68
+ assert_equal(<<HAML.strip, render(<<HTML))
69
+ %p
70
+ :cdata
71
+ <a foo="bar" baz="bang">
72
+ <div id="foo">flop</div>
73
+ </a>
74
+ HAML
75
+ <p><![CDATA[
76
+ <a foo="bar" baz="bang">
77
+ <div id="foo">flop</div>
78
+ </a>
79
+ ]]></p>
80
+ HTML
81
+ end
82
+
83
+ def test_self_closing_tag
84
+ assert_equal("%foo/", render("<foo />"))
85
+ end
86
+
87
+ def test_inline_text
88
+ assert_equal("%p foo", render("<p>foo</p>"))
89
+ end
90
+
91
+ def test_inline_comment
92
+ assert_equal("/ foo", render("<!-- foo -->"))
93
+ assert_equal(<<HAML.strip, render(<<HTML))
94
+ / foo
95
+ %p bar
96
+ HAML
97
+ <!-- foo -->
98
+ <p>bar</p>
99
+ HTML
100
+ end
101
+
102
+ def test_non_inline_comment
103
+ assert_equal(<<HAML.rstrip, render(<<HTML))
104
+ /
105
+ Foo
106
+ Bar
107
+ HAML
108
+ <!-- Foo
109
+ Bar -->
110
+ HTML
111
+ end
112
+
113
+ def test_non_inline_text
114
+ assert_equal(<<HAML.rstrip, render(<<HTML))
115
+ %p
116
+ foo
117
+ HAML
118
+ <p>
119
+ foo
120
+ </p>
121
+ HTML
122
+ assert_equal(<<HAML.rstrip, render(<<HTML))
123
+ %p
124
+ foo
125
+ HAML
126
+ <p>
127
+ foo</p>
128
+ HTML
129
+ assert_equal(<<HAML.rstrip, render(<<HTML))
130
+ %p
131
+ foo
132
+ HAML
133
+ <p>foo
134
+ </p>
135
+ HTML
136
+ end
137
+
138
+ def test_script_tag
139
+ assert_equal(<<HAML.rstrip, render(<<HTML))
140
+ :javascript
141
+ function foo() {
142
+ return "12" & "13";
143
+ }
144
+ HAML
145
+ <script type="text/javascript">
146
+ function foo() {
147
+ return "12" &amp; "13";
148
+ }
149
+ </script>
150
+ HTML
151
+ end
152
+
153
+ def test_script_tag_with_cdata
154
+ assert_equal(<<HAML.rstrip, render(<<HTML))
155
+ :javascript
156
+ function foo() {
157
+ return "&amp;";
158
+ }
159
+ HAML
160
+ <script type="text/javascript">
161
+ <![CDATA[
162
+ function foo() {
163
+ return "&amp;";
164
+ }
165
+ ]]>
166
+ </script>
167
+ HTML
168
+ end
169
+
170
+ def test_pre
171
+ assert_equal(<<HAML.rstrip, render(<<HTML))
172
+ %pre
173
+ :preserve
174
+ foo
175
+ bar
176
+ baz
177
+ HAML
178
+ <pre>foo
179
+ bar
180
+ baz</pre>
181
+ HTML
182
+ end
183
+
184
+ def test_pre_code
185
+ assert_equal(<<HAML.rstrip, render(<<HTML))
186
+ %pre
187
+ %code
188
+ :preserve
189
+ foo
190
+ bar
191
+ baz
192
+ HAML
193
+ <pre><code>foo
194
+ bar
195
+ baz</code></pre>
196
+ HTML
197
+ end
198
+
199
+ def test_code_without_pre
200
+ assert_equal(<<HAML.rstrip, render(<<HTML))
201
+ %code
202
+ foo
203
+ bar
204
+ baz
205
+ HAML
206
+ <code>foo
207
+ bar
208
+ baz</code>
209
+ HTML
210
+ end
211
+
212
+ def test_conditional_comment
213
+ assert_equal(<<HAML.rstrip, render(<<HTML))
214
+ /[if foo]
215
+ bar
216
+ baz
217
+ HAML
218
+ <!--[if foo]>
219
+ bar
220
+ baz
221
+ <![endif]-->
222
+ HTML
223
+ end
224
+
225
+ def test_style_to_css_filter
226
+ assert_equal(<<HAML.rstrip, render(<<HTML))
227
+ :css
228
+ foo {
229
+ bar: baz;
230
+ }
231
+ HAML
232
+ <style type="text/css">
233
+ foo {
234
+ bar: baz;
235
+ }
236
+ </style>
237
+ HTML
238
+ end
239
+
240
+ def test_inline_conditional_comment
241
+ assert_equal(<<HAML.rstrip, render(<<HTML))
242
+ /[if foo] bar baz
243
+ HAML
244
+ <!--[if foo]> bar baz <![endif]-->
245
+ HTML
246
+ end
247
+
248
+ def test_minus_in_tag
249
+ assert_equal("%p - foo bar -", render("<p>- foo bar -</p>"))
250
+ end
251
+
252
+ def test_equals_in_tag
253
+ assert_equal("%p = foo bar =", render("<p>= foo bar =</p>"))
254
+ end
255
+
256
+ def test_hash_in_tag
257
+ assert_equal("%p # foo bar #", render("<p># foo bar #</p>"))
258
+ end
259
+
260
+ def test_comma_post_tag
261
+ assert_equal(<<HAML.rstrip, render(<<HTML))
262
+ #foo
263
+ %span> Foo
264
+ ,
265
+ %span bar
266
+ Foo
267
+ %span> bar
268
+ ,
269
+ %span baz
270
+ HAML
271
+ <div id="foo">
272
+ <span>Foo</span>, <span>bar</span>
273
+ Foo<span>bar</span>, <span>baz</span>
274
+ </div>
275
+ HTML
276
+ end
277
+
278
+ def test_comma_post_tag_with_text_before
279
+ assert_equal(<<HAML.rstrip, render(<<HTML))
280
+ #foo
281
+ Batch
282
+ = succeed "," do
283
+ %span Foo
284
+ %span Bar
285
+ HAML
286
+ <div id="foo">
287
+ Batch
288
+ <span>Foo</span>, <span>Bar</span>
289
+ </div>
290
+ HTML
291
+ end
292
+
293
+ # Encodings
294
+
295
+ unless RUBY_VERSION < "1.9"
296
+ def test_encoding_error
297
+ render("foo\nbar\nb\xFEaz".force_encoding("utf-8"))
298
+ assert(false, "Expected exception")
299
+ rescue Haml::Error => e
300
+ assert_equal(3, e.line)
301
+ assert_equal('Invalid UTF-8 character "\xFE"', e.message)
302
+ end
303
+
304
+ def test_ascii_incompatible_encoding_error
305
+ template = "foo\nbar\nb_z".encode("utf-16le")
306
+ template[9] = "\xFE".force_encoding("utf-16le")
307
+ render(template)
308
+ assert(false, "Expected exception")
309
+ rescue Haml::Error => e
310
+ assert_equal(3, e.line)
311
+ assert_equal('Invalid UTF-16LE character "\xFE"', e.message)
312
+ end
313
+ end
314
+
315
+ # Regression Tests
316
+
317
+ def test_xhtml_strict_doctype
318
+ assert_equal('!!! Strict', render(<<HTML))
319
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
320
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
321
+ HTML
322
+ end
323
+ end
@@ -0,0 +1,22 @@
1
+ require "rubygems"
2
+ if ENV["COVERAGE"]
3
+ require "simplecov"
4
+ SimpleCov.start
5
+ end
6
+
7
+ require "bundler/setup"
8
+ require "minitest/autorun"
9
+ require "html2haml"
10
+ require 'html2haml/html'
11
+ require 'html2haml/html/erb'
12
+
13
+ class MiniTest::Unit::TestCase
14
+ protected
15
+ def render(text, options = {})
16
+ Haml::HTML.new(text, options).render.rstrip
17
+ end
18
+
19
+ def render_erb(text)
20
+ render(text, :erb => true)
21
+ end
22
+ end
metadata ADDED
@@ -0,0 +1,180 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: html2haml
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0.beta.1
5
+ prerelease: 6
6
+ platform: ruby
7
+ authors:
8
+ - Norman Clarke
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-27 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: hpricot
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: haml
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 3.2.0.beta.1
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 3.2.0.beta.1
46
+ - !ruby/object:Gem::Dependency
47
+ name: erubis
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: ruby_parser
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: simplecov
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: rake
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: minitest
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ description: Converts HTML into Haml
127
+ email:
128
+ - norman@njclarke.com
129
+ executables:
130
+ - html2haml
131
+ extensions: []
132
+ extra_rdoc_files: []
133
+ files:
134
+ - .gitignore
135
+ - .travis.yml
136
+ - Gemfile
137
+ - MIT-LICENSE
138
+ - README.md
139
+ - Rakefile
140
+ - bin/html2haml
141
+ - html2haml.gemspec
142
+ - lib/html2haml.rb
143
+ - lib/html2haml/exec.rb
144
+ - lib/html2haml/html.rb
145
+ - lib/html2haml/html/erb.rb
146
+ - lib/html2haml/version.rb
147
+ - test/erb_test.rb
148
+ - test/html2haml_test.rb
149
+ - test/test_helper.rb
150
+ homepage: http://haml.info
151
+ licenses: []
152
+ post_install_message:
153
+ rdoc_options: []
154
+ require_paths:
155
+ - lib
156
+ required_ruby_version: !ruby/object:Gem::Requirement
157
+ none: false
158
+ requirements:
159
+ - - ! '>='
160
+ - !ruby/object:Gem::Version
161
+ version: '0'
162
+ segments:
163
+ - 0
164
+ hash: 3443034388567895554
165
+ required_rubygems_version: !ruby/object:Gem::Requirement
166
+ none: false
167
+ requirements:
168
+ - - ! '>'
169
+ - !ruby/object:Gem::Version
170
+ version: 1.3.1
171
+ requirements: []
172
+ rubyforge_project:
173
+ rubygems_version: 1.8.24
174
+ signing_key:
175
+ specification_version: 3
176
+ summary: Converts HTML into Haml
177
+ test_files:
178
+ - test/erb_test.rb
179
+ - test/html2haml_test.rb
180
+ - test/test_helper.rb