sinatra-tags 0.1.0

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.
@@ -0,0 +1,64 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{sinatra-tags}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["kematzy"]
12
+ s.date = %q{2010-03-02}
13
+ s.description = %q{A Sinatra Extension that provides easy creation of flexible HTML tags.}
14
+ s.email = %q{kematzy@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "CHANGES",
23
+ "LICENSE",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "lib/sinatra/tags.rb",
28
+ "sinatra-tags.gemspec",
29
+ "spec/sinatra/tags_spec.rb",
30
+ "spec/spec_helper.rb"
31
+ ]
32
+ s.homepage = %q{http://github.com/kematzy/sinatra-tags}
33
+ s.rdoc_options = ["--charset=UTF-8"]
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = %q{1.3.6}
36
+ s.summary = %q{A Sinatra Extension that provides easy creation of flexible HTML tags.}
37
+ s.test_files = [
38
+ "spec/sinatra/tags_spec.rb",
39
+ "spec/spec_helper.rb"
40
+ ]
41
+
42
+ if s.respond_to? :specification_version then
43
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
44
+ s.specification_version = 3
45
+
46
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
47
+ s.add_runtime_dependency(%q<sinatra>, [">= 1.0.a"])
48
+ s.add_runtime_dependency(%q<sinatra-outputbuffer>, [">= 0.1.0"])
49
+ s.add_development_dependency(%q<sinatra-tests>, [">= 0.1.6"])
50
+ s.add_development_dependency(%q<rspec>, [">= 1.3.0"])
51
+ else
52
+ s.add_dependency(%q<sinatra>, [">= 1.0.a"])
53
+ s.add_dependency(%q<sinatra-outputbuffer>, [">= 0.1.0"])
54
+ s.add_dependency(%q<sinatra-tests>, [">= 0.1.6"])
55
+ s.add_dependency(%q<rspec>, [">= 1.3.0"])
56
+ end
57
+ else
58
+ s.add_dependency(%q<sinatra>, [">= 1.0.a"])
59
+ s.add_dependency(%q<sinatra-outputbuffer>, [">= 0.1.0"])
60
+ s.add_dependency(%q<sinatra-tests>, [">= 0.1.6"])
61
+ s.add_dependency(%q<rspec>, [">= 1.3.0"])
62
+ end
63
+ end
64
+
@@ -0,0 +1,463 @@
1
+
2
+ require "#{File.dirname(File.dirname(File.expand_path(__FILE__)))}/spec_helper"
3
+
4
+ describe "Sinatra" do
5
+
6
+ describe "Tags" do
7
+
8
+ class MyTestApp
9
+ register(Sinatra::Tags)
10
+ end
11
+
12
+ class MyCustomTestApp < Sinatra::Base
13
+ register(Sinatra::Tags)
14
+ set :tags_add_newlines_after_tags, false
15
+ set :tags_output_format_is_xhtml, true
16
+ end
17
+
18
+ # convenience shared spec that sets up MyTestApp and tests it's OK,
19
+ # without it you will get "stack level too deep" errors
20
+ it_should_behave_like "MyTestApp"
21
+
22
+ describe "VERSION" do
23
+
24
+ it "should return the VERSION string" do
25
+ Sinatra::Tags::VERSION.should be_a_kind_of(String)
26
+ Sinatra::Tags::VERSION.should match(/\d\.\d+\.\d+(\.\d)?/)
27
+ end
28
+
29
+ end #/ VERSION
30
+
31
+ describe "#self.version" do
32
+
33
+ it "should return a version of the Sinatra::Tags VERSION string" do
34
+ Sinatra::Tags.version.should be_a_kind_of(String)
35
+ Sinatra::Tags.version.should match(/Sinatra::Tags v\d\.\d+\.\d+(\.\d)?/)
36
+ end
37
+
38
+ end #/ #self.version
39
+
40
+
41
+ describe "Configuration" do
42
+
43
+ describe "with Default settings" do
44
+
45
+ it "should set the :tags_add_newlines_after_tags to 'true'" do
46
+ MyTestApp.tags_add_newlines_after_tags.should == true
47
+ end
48
+
49
+ it "should set the :tags_output_format_is_xhtml to 'false'" do
50
+ MyTestApp.tags_output_format_is_xhtml.should == false
51
+ end
52
+
53
+ end #/ with Default settings
54
+
55
+ describe "with Custom Settings" do
56
+
57
+ it "should set the :tags_add_newlines_after_tags to 'true'" do
58
+ MyCustomTestApp.tags_add_newlines_after_tags.should == false
59
+ end
60
+
61
+ it "should set the :tags_output_format_is_xhtml to 'false'" do
62
+ MyCustomTestApp.tags_output_format_is_xhtml.should == true
63
+ end
64
+
65
+ end #/ with Custom Settings
66
+
67
+ end #/ Configuration
68
+
69
+
70
+ describe "Helpers" do
71
+
72
+ describe "#tag" do
73
+
74
+ it "should return nothing when block tag <% tag(:div) %> is used without a block" do
75
+ erb_app '<% tag(:div) %>'
76
+ body.should == ''
77
+
78
+ haml_app '- tag(:div)'
79
+ body.should == "\n"
80
+ end
81
+
82
+ describe "multi line tags " do
83
+
84
+ %w(
85
+ a address applet bdo big blockquote body button caption center
86
+ colgroup dd dir div dl dt fieldset form frameset head html iframe
87
+ map noframes noscript object ol optgroup pre script select small
88
+ style table tbody td textarea tfoot th thead title tr tt ul
89
+ ).each do |t|
90
+
91
+ describe "like <#{t}>" do
92
+
93
+ it "should have a '\\n' after the opening tag and before the closing tag" do
94
+ erb_app "<%= tag(:#{t},'contents') %>"
95
+ body.should == "<#{t}>\ncontents\n</#{t}>\n"
96
+
97
+ haml_app "= tag(:#{t},'contents')"
98
+ body.should == "<#{t}>\ncontents\n</#{t}>\n"
99
+ end
100
+
101
+ it "should work without contents passed in" do
102
+ erb_app "<%= tag(:#{t},nil) %>"
103
+ body.should == "<#{t}>\n\n</#{t}>\n"
104
+
105
+ haml_app "= tag(:#{t},nil)"
106
+ body.should == "<#{t}>\n\n</#{t}>\n"
107
+ end
108
+
109
+ it "should allow a hash of attributes to be passed" do
110
+ erb_app "<%= tag(:#{t},'contents', :id => 'tag-id', :class => 'tag-class') %>"
111
+ body.should have_tag("#{t}#tag-id.tag-class","\ncontents\n")
112
+
113
+ haml_app "= tag(:#{t},'contents', :id => 'tag-id', :class => 'tag-class')"
114
+ body.should have_tag("#{t}#tag-id.tag-class","\ncontents\n")
115
+ end
116
+
117
+ it "with ':newline => false' should NOT add '\\n' around the contents" do
118
+ erb_app "<%= tag(:#{t},'content', :id => 'tag-id', :newline => false) %>"
119
+ body.should == "<#{t} id=\"tag-id\">content</#{t}>\n"
120
+
121
+ haml_app "= tag(:#{t},'content', :id => 'tag-id', :newline => false)"
122
+ body.should == "<#{t} id=\"tag-id\">content</#{t}>\n"
123
+ end
124
+
125
+ end #/ like ##{t}
126
+
127
+ end #/ loop
128
+
129
+ end #/ multi line tags
130
+
131
+ describe "self-closing tags " do
132
+
133
+ %w( area base br col frame hr img input link meta param ).each do |t|
134
+
135
+ describe "like <#{t}>" do
136
+
137
+ it "should be self-closed and with a trailing '\\n'" do
138
+ erb_app "<%= tag(:#{t}) %>"
139
+ body.should == "<#{t}>\n"
140
+
141
+ haml_app "= tag(:#{t})"
142
+ body.should == "<#{t}>\n"
143
+ end
144
+
145
+ it "should ignore the content passed in" do
146
+ erb_app "<%= tag(:#{t},'content') %>"
147
+ body.should == "<#{t}>\n"
148
+
149
+ haml_app "= tag(:#{t},'content')"
150
+ body.should == "<#{t}>\n"
151
+ end
152
+
153
+ it "should allow a hash of attributes to be passed" do
154
+ erb_app "<%= tag(:#{t}, :id => 'tag-id', :class => 'tag-class') %>"
155
+ body.should have_tag("#{t}#tag-id.tag-class")
156
+
157
+ haml_app "= tag(:#{t}, :id => 'tag-id', :class => 'tag-class')"
158
+ body.should have_tag("#{t}#tag-id.tag-class")
159
+ end
160
+
161
+ it "with ':newline => false' ERB does NOT add a '\\n' after the tag" do
162
+ erb_app "<%= tag(:#{t},:id => 'tag-id', :newline => false) %>"
163
+ body.should == "<#{t} id=\"tag-id\">"
164
+ end
165
+
166
+ it "with ':newline => false' Haml DOES add a '\\n' after the tag" do
167
+ haml_app "= tag(:#{t},:id => 'tag-id', :newline => false)"
168
+ body.should == "<#{t} id=\"tag-id\">\n"
169
+ end
170
+
171
+ end #/ #{t}
172
+
173
+ end #/loop
174
+
175
+ end #/ self-closing tag
176
+
177
+ describe "single line tags " do
178
+
179
+ %w(
180
+ abbr acronym b cite code del dfn em h1 h2 h3 h4 h5 h6 i kbd
181
+ label legend li option p q samp span strong sub sup var
182
+ ).each do |t|
183
+
184
+ describe "like <#{t}>" do
185
+
186
+ it "should strip the '\\n' around the content" do
187
+ erb_app "<%= tag(:#{t}, 'content', :id => 'tag-id') %>"
188
+ body.should have_tag("#{t}#tag-id", 'content')
189
+ body.should == "<#{t} id=\"tag-id\">content</#{t}>\n"
190
+
191
+ haml_app "= tag(:#{t}, 'content', :id => 'tag-id')"
192
+ body.should have_tag("#{t}#tag-id", 'content')
193
+ body.should == "<#{t} id=\"tag-id\">content</#{t}>\n"
194
+ end
195
+
196
+ it "should work without contents passed in" do
197
+ erb_app "<%= tag(:#{t},nil) %>"
198
+ body.should == "<#{t}></#{t}>\n"
199
+
200
+ haml_app "= tag(:#{t},nil)"
201
+ body.should == "<#{t}></#{t}>\n"
202
+ end
203
+
204
+ it "should allow a hash of attributes to be passed" do
205
+ erb_app "<%= tag(:#{t},'content', :id => 'tag-id', :class => 'tag-class') %>"
206
+ body.should have_tag("#{t}#tag-id.tag-class","content")
207
+
208
+ haml_app "= tag(:#{t},'content', :id => 'tag-id', :class => 'tag-class') "
209
+ body.should have_tag("#{t}#tag-id.tag-class","content")
210
+ end
211
+
212
+ it "should be made multi-line tags with ':newline => true'" do
213
+ erb_app "<%= tag(:#{t},'content',:id => 'tag-id', :newline => true) %>"
214
+ body.should == "<#{t} id=\"tag-id\">\ncontent\n</#{t}>\n"
215
+
216
+ haml_app "= tag(:#{t},'content',:id => 'tag-id', :newline => true)"
217
+ body.should == "<#{t} id=\"tag-id\">\ncontent\n</#{t}>\n"
218
+ end
219
+
220
+ end #/ like #{t}
221
+
222
+ end #/loop
223
+
224
+ end #/ single line tags
225
+
226
+ describe "with Boolean attributes " do
227
+
228
+ %w(selected checked disabled readonly multiple).each do |attr|
229
+
230
+ describe "like ##{attr}" do
231
+
232
+ it "should set '#{attr}=\"#{attr}\"' when '#{attr} => true'" do
233
+ erb_app "<%= tag(:input, :type => :dummy, :#{attr} => true) %>"
234
+ body.should have_tag("input[@#{attr}=#{attr}]")
235
+
236
+ haml_app "= tag(:input, :type => :dummy, :#{attr} => true)"
237
+ body.should have_tag("input[@#{attr}=#{attr}]")
238
+ end
239
+
240
+ it "should NOT set '#{attr}=\"#{attr}\"' when '#{attr} => false'" do
241
+ erb_app "<%= tag(:input, :type => :dummy, :#{attr} => false) %>"
242
+ body.should_not have_tag("input[@#{attr}=#{attr}]")
243
+
244
+ haml_app "= tag(:input, :type => :dummy, :#{attr} => false) "
245
+ body.should_not have_tag("input[@#{attr}=#{attr}]")
246
+ end
247
+
248
+ it "should NOT set '#{attr}=\"#{attr}\"' when '#{attr} => nil'" do
249
+ erb_app "<%= tag(:input, :type => :dummy, :#{attr} => nil) %>"
250
+ body.should_not have_tag("input[@#{attr}=#{attr}]")
251
+
252
+ haml_app "= tag(:input, :type => :dummy, :#{attr} => nil)"
253
+ body.should_not have_tag("input[@#{attr}=#{attr}]")
254
+ end
255
+
256
+ it "should NOT set '#{attr}=\"#{attr}\"' when '#{attr} => [empty]'" do
257
+ erb_app "<%= tag(:input, :type => :dummy, :#{attr} => '') %>"
258
+ body.should_not have_tag("input[@#{attr}=#{attr}]")
259
+
260
+ haml_app "= tag(:input, :type => :dummy, :#{attr} => '')"
261
+ body.should_not have_tag("input[@#{attr}=#{attr}]")
262
+ end
263
+
264
+ end #/ ##{attr}
265
+
266
+ end #/ boolean loop
267
+
268
+ end #/ with Boolean attribute
269
+
270
+ describe "with Blocks" do
271
+
272
+ it "should buffer the tag contents within an ERB block" do
273
+ block = %Q[
274
+ <% tag(:div, :class => :list) do %>
275
+ <% tag(:ol) do %>
276
+ <%= tag(:li, 'A') %>
277
+ <%= tag(:li, 'B') %>
278
+ <% end %>
279
+ <% end %>]
280
+ erb_app block
281
+ body.should have_tag('div.list > ol > li', "A")
282
+ body.should have_tag('div.list > ol > li', "B")
283
+ end
284
+
285
+ it "should buffer the tag contents within a Haml block" do
286
+ block = %Q[
287
+ - tag(:div, :class => :list) do
288
+ - tag(:ol) do
289
+ = tag(:li, 'A')
290
+ = tag(:li, 'B')
291
+ ]
292
+ haml_app block
293
+ body.should have_tag('div.list > ol > li', "A")
294
+ body.should have_tag('div.list > ol > li', "B")
295
+ end
296
+
297
+ it "should buffer content within nested ERB blocks" do
298
+ block = %Q[<% tag( :div, :id => 'comments') do %>]
299
+ block << %Q[ <% tag(:fieldset) do %>]
300
+ block << %Q[ <%= tag( :legend, 'Comments') %>]
301
+ block << %Q[ <%= tag( :textarea, 'test', :id => 'field-comments') %>]
302
+ block << %Q[ <%= tag(:span, 'Some description') %>]
303
+ block << %Q[ <% end %>]
304
+ block << %Q[ <% tag(:fieldset, :id => 'form-details') do %>]
305
+ block << %Q[ <%= tag(:legend, 'Details') %>]
306
+ block << %Q[ <%= tag(:input, :type => :text, :value => 'City') %>]
307
+ block << %Q[ <% end %>]
308
+ block << %Q[<% end %>]
309
+
310
+ erb_app block
311
+ # body.should have_tag(:debug)
312
+ body.should have_tag('div#comments')
313
+ body.should have_tag('div#comments > fieldset > legend', 'Comments')
314
+ body.should have_tag('div#comments > fieldset > textarea#field-comments', "\ntest\n")
315
+ body.should have_tag('div#comments > fieldset > span', "Some description")
316
+ #
317
+ body.should have_tag('div#comments > fieldset#form-details')
318
+ body.should have_tag('div#comments > fieldset#form-details > legend', 'Details')
319
+ body.should have_tag('div#comments > fieldset#form-details > input[@type=text]')
320
+
321
+ end
322
+
323
+ it "should buffer content within nested Haml blocks" do
324
+ block = %Q[
325
+ - tag( :div, :id => 'comments') do
326
+ - tag(:fieldset) do
327
+ = tag( :legend, 'Comments')
328
+ = tag( :textarea, 'test', :id => 'field-comments')
329
+ = tag(:span, 'Some description')
330
+
331
+ - tag(:fieldset, :id => 'form-details') do
332
+ = tag(:legend, 'Details')
333
+ = tag(:input, :type => :text, :value => 'City')
334
+
335
+ ]
336
+
337
+ haml_app block
338
+ # body.should have_tag(:debug)
339
+ body.should have_tag('div#comments')
340
+ body.should have_tag('div#comments > fieldset > legend', 'Comments')
341
+ body.should have_tag('div#comments > fieldset > textarea#field-comments', "\ntest\n")
342
+ body.should have_tag('div#comments > fieldset > span', "Some description")
343
+ #
344
+ body.should have_tag('div#comments > fieldset#form-details')
345
+ body.should have_tag('div#comments > fieldset#form-details > legend', 'Details')
346
+ body.should have_tag('div#comments > fieldset#form-details > input[@type=text]')
347
+
348
+ end
349
+
350
+ it "should buffer simple HTML content within nested ERB blocks" do
351
+ block = %Q[<% tag( :div, :id => 'comments') do %>]
352
+ block << %Q[ <p>Just plain HTML content</p>\n]
353
+ block << %Q[ <% tag(:fieldset) do %>]
354
+ block << %Q[ <%= tag( :legend, 'Comments') %>]
355
+ block << %Q[ <p>Even more plain HTML content</p>\n]
356
+ block << %Q[ <%= tag( :textarea, 'test', :id => 'field-comments') %>]
357
+ block << %Q[ <%= tag(:span, 'Some description') %>]
358
+ block << %Q[ <% end %>]
359
+ block << %Q[ <p>Loads of plain HTML content</p>\n]
360
+ block << %Q[ <% tag(:fieldset, :id => 'form-details') do %>]
361
+ block << %Q[ <%= tag(:legend, 'Details') %>]
362
+ block << %Q[ <%= tag(:input, :type => :text, :value => 'City') %>]
363
+ block << %Q[ <% end %>]
364
+ block << %Q[<% end %>]
365
+
366
+ erb_app block
367
+ # body.should have_tag(:debug)
368
+ body.should have_tag('div#comments')
369
+ body.should have_tag('div#comments > p', 'Just plain HTML content')
370
+ body.should have_tag('div#comments > fieldset > legend', 'Comments')
371
+ body.should have_tag('div#comments > fieldset > p', 'Even more plain HTML content')
372
+ body.should have_tag('div#comments > fieldset > textarea#field-comments', "\ntest\n")
373
+ body.should have_tag('div#comments > fieldset > span', "Some description")
374
+ body.should have_tag('div#comments > p', 'Loads of plain HTML content')
375
+ #
376
+ body.should have_tag('div#comments > fieldset#form-details')
377
+ body.should have_tag('div#comments > fieldset#form-details > legend', 'Details')
378
+ body.should have_tag('div#comments > fieldset#form-details > input[@type=text]')
379
+
380
+ end
381
+
382
+ it "should buffer simple HTML content within nested Haml blocks" do
383
+ block = %Q[
384
+ - tag( :div, :id => 'comments') do
385
+ %p Just plain HTML content
386
+ - tag(:fieldset) do
387
+ = tag( :legend, 'Comments')
388
+ %p Even more plain HTML content
389
+ = tag( :textarea, 'test', :id => 'field-comments')
390
+ = tag(:span, 'Some description')
391
+ %p Loads of plain HTML content
392
+ - tag(:fieldset, :id => 'form-details') do
393
+ = tag(:legend, 'Details')
394
+ = tag(:input, :type => :text, :value => 'City')
395
+ ]
396
+
397
+ haml_app block
398
+ body.should have_tag('div#comments')
399
+ body.should have_tag('div#comments > p', 'Just plain HTML content')
400
+ body.should have_tag('div#comments > fieldset > legend', 'Comments')
401
+ body.should have_tag('div#comments > fieldset > p', 'Even more plain HTML content')
402
+ body.should have_tag('div#comments > fieldset > textarea#field-comments', "\ntest\n")
403
+ body.should have_tag('div#comments > fieldset > span', "Some description")
404
+ body.should have_tag('div#comments > p', 'Loads of plain HTML content')
405
+ #
406
+ body.should have_tag('div#comments > fieldset#form-details')
407
+ body.should have_tag('div#comments > fieldset#form-details > legend', 'Details')
408
+ body.should have_tag('div#comments > fieldset#form-details > input[@type=text]')
409
+
410
+ end
411
+
412
+ it "should NOT prepend contents when both contents and block are present" do
413
+ block = %Q[
414
+ <% tag(:div, 'tag-content') do %>
415
+ <%= tag(:label, 'Comments:', :for => :comments) %>
416
+ <%= tag(:textarea,'This works', :id => :comments) %>
417
+ <% end %>]
418
+
419
+ erb_app block
420
+ body.should_not have_tag('div', /tag-content/)
421
+ body.should have_tag('label[@for=comments]', 'Comments:')
422
+ body.should have_tag('textarea[@id=comments]',"\nThis works\n")
423
+
424
+ block = %Q[
425
+ - tag(:div, 'tag-content') do
426
+ = tag(:label, 'Comments:', :for => :comments)
427
+ = tag(:textarea,'This works', :id => :comments)
428
+ ]
429
+ haml_app block
430
+ body.should_not have_tag('div', /tag-content/)
431
+ body.should have_tag('label[@for=comments]', 'Comments:')
432
+ body.should have_tag('textarea[@id=comments]',"\nThis works\n")
433
+ end
434
+
435
+ end #/ with Blocks
436
+
437
+
438
+ describe "as XHTML" do
439
+
440
+ before(:each) do
441
+ MyTestApp.tags_output_format_is_xhtml = true
442
+ end
443
+ %w( area base br col frame hr img input link meta param ).each do |t|
444
+
445
+ it "should self-close <#{t} />" do
446
+ erb_app "<%= tag(:#{t}) %>"
447
+ body.should == "<#{t} />\n"
448
+
449
+ haml_app "= tag(:#{t})"
450
+ body.should == "<#{t} />\n"
451
+ end
452
+
453
+ end #/loop
454
+
455
+ end #/ as XHTML
456
+
457
+ end #/ #tag
458
+
459
+ end #/ Helpers
460
+
461
+ end #/ Tags
462
+
463
+ end #/ Sinatra