iron-web 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,58 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
+
3
+ SAMPLE_LIBRARY = {
4
+ :red => '#FF0000',
5
+ :purplish => '#FF20EE',
6
+ :orange => '#F0FF00'
7
+ }
8
+
9
+ describe Color do
10
+
11
+ it 'should default to white' do
12
+ Color.new.r.should == 255
13
+ Color.new.g.should == 255
14
+ Color.new.b.should == 255
15
+ Color.new.a.should == 255
16
+ end
17
+
18
+ it 'should convert to string as a web string' do
19
+ Color.parse(1,2,3).to_s.should == '#010203'
20
+ end
21
+
22
+ it 'should parse web colors' do
23
+ Color.new('#f8019c').should == Color.new(248,1,156)
24
+ end
25
+
26
+ it 'should make the global rgb() method available' do
27
+ defined?(:rgb).should be_true
28
+ end
29
+
30
+ it 'should parse values with rgb()' do
31
+ ['#fa8', Color::BLACK].each do |test|
32
+ rgb(test).should == Color.parse(test)
33
+ end
34
+ end
35
+
36
+ it 'should support alpha' do
37
+ c = rgb('abc')
38
+ c.a.should == 255
39
+ c.trans?.should be_false
40
+ c.opaque?.should be_true
41
+ c.to_s.should == '#AABBCC'
42
+
43
+ c.a = 38
44
+ c.a.should == 38
45
+ c.trans?.should be_true
46
+ c.opaque?.should be_false
47
+ c.to_s.should == '#AABBCC26'
48
+ end
49
+
50
+ it 'should support a definable callback for symbols' do
51
+ Color.lookup_callback = lambda{|val| SAMPLE_LIBRARY[val]}
52
+ SAMPLE_LIBRARY.each_pair do |k,v|
53
+ rgb(k).should == rgb(v)
54
+ end
55
+ Color.parse(:purplish).should == SAMPLE_LIBRARY[:purplish]
56
+ end
57
+
58
+ end
@@ -0,0 +1,69 @@
1
+ describe Html::Element do
2
+
3
+ # Helper to shortcut a lot of typing...
4
+ def build(*args, &block)
5
+ Html::Element.build(*args, &block)
6
+ end
7
+
8
+ it 'should build standard tags' do
9
+ Html::Element.new('b').render.should == '<b></b>'
10
+ Html::Element.build('script').should include('<script></script>')
11
+ end
12
+
13
+ it 'should build singleton tags' do
14
+ build('img').should == '<img>'
15
+ build('input').should == '<input>'
16
+ end
17
+
18
+ it 'should build attributes' do
19
+ build('img', :src => '/foo.jpg').should == '<img src="/foo.jpg">'
20
+ build('span', :id => 'alpha', :class => 'bold').should == '<span id="alpha" class="bold"></span>'
21
+ end
22
+
23
+ it 'should support basic text' do
24
+ build('span', 'heya').should == '<span>heya</span>'
25
+ end
26
+
27
+ it 'should html escape basic text' do
28
+ build('span', '"boo!"').should == '<span>&quot;boo!&quot;</span>'
29
+ end
30
+
31
+ it 'should html escape attr values' do
32
+ build('a', :alt => 'We had "fun"').should == '<a alt="We had &quot;fun&quot;"></a>'
33
+ end
34
+
35
+ it 'should support indenting' do
36
+ Html::Element.new('div', 'indent!').render.should == "\n<div>\n indent!\n</div>\n"
37
+ end
38
+
39
+ it 'should support block indenting by skipping initial newline' do
40
+ Html::Element.new('div', 'indent!').render(0,true).should == "<div>\n indent!\n</div>\n"
41
+ end
42
+
43
+ it 'should add attributes when called as setters' do
44
+ span = Html::Element.new(:span)
45
+ span.class = 'dynamic'
46
+ span.id = 'header'
47
+ span.attrs.keys.should include(:class, :id)
48
+ span.render.should == '<span class="dynamic" id="header"></span>'
49
+ end
50
+
51
+ it 'should yield a block for customization on creation' do
52
+ Html::Element.new(:span) {|span|
53
+ span.id = 'fun'
54
+ }.render.should == '<span id="fun"></span>'
55
+ end
56
+
57
+ it 'should contain HTML' do
58
+ Html::Element.new(:span).html.should be_a(Html)
59
+ end
60
+
61
+ it 'should allow setting the inner html' do
62
+ span = Html::Element.new(:span)
63
+ span.html = 'some text'
64
+ span.render.should == '<span>some text</span>'
65
+ span.html = Html.new.strong('more text!')
66
+ span.render.should == '<span><strong>more text!</strong></span>'
67
+ end
68
+
69
+ end
@@ -0,0 +1,25 @@
1
+ # Don't run specs if HtmlSafeString is not present, eg when running in a rails environment
2
+ if defined?(HtmlSafeString)
3
+
4
+ describe HtmlSafeString do
5
+
6
+ it 'should be html safe at creation' do
7
+ HtmlSafeString.new.should be_html_safe
8
+ end
9
+
10
+ it 'should stay html safe after concatenation' do
11
+ s = HtmlSafeString.new('alpha')
12
+ s += 'beta'
13
+ s.should == 'alphabeta'
14
+ s.should be_html_safe
15
+ end
16
+
17
+ it 'should safely escape dangerous chars on concatenation' do
18
+ s = HtmlSafeString.new('alpha')
19
+ s += 'b&ta'
20
+ s.should == 'alphab&amp;ta'
21
+ end
22
+
23
+ end
24
+
25
+ end
@@ -0,0 +1,20 @@
1
+ describe Html do
2
+
3
+ it 'should generate simple HTML' do
4
+ Html.build do |html|
5
+ html.span('hello wwworld')
6
+ end.should == '<span>hello wwworld</span>'
7
+ end
8
+
9
+ it 'should escape unsafe characters' do
10
+ Html.escape_once('"hi&<guy>"').should == '&quot;hi&amp;&lt;guy&gt;&quot;'
11
+ end
12
+
13
+ it 'should support comments' do
14
+ Html.build do |html|
15
+ html.comment! 'Important stuff'
16
+ html.h1 'Da header'
17
+ end.should == "<!-- Important stuff -->\n\n<h1>\n Da header\n</h1>\n"
18
+ end
19
+
20
+ end
@@ -0,0 +1,16 @@
1
+ describe String do
2
+
3
+ it 'should not be html safe' do
4
+ String.new.should_not be_html_safe
5
+ 'bob'.should_not be_html_safe
6
+ end
7
+
8
+ it 'should become a safe string on calls to #html_safe' do
9
+ 'abc'.html_safe.should be_a(HtmlSafeString)
10
+ end
11
+
12
+ it 'should not change contents on calls to #html_safe' do
13
+ '<span class="fun">'.html_safe.should == '<span class="fun">'
14
+ end
15
+
16
+ end
@@ -0,0 +1,101 @@
1
+ describe Url do
2
+
3
+ it 'should parse url paths' do
4
+ {
5
+ '/' => '/',
6
+ 'http://google.com' => '',
7
+ '/site/main-bar?foo=123bar#some_bit' => '/site/main-bar'
8
+ }.each_pair do |url, path|
9
+ Url.parse(url).path.should == path
10
+ end
11
+ end
12
+
13
+ it 'should parse servers' do
14
+ {
15
+ '/' => nil,
16
+ 'google.com' => nil,
17
+ 'http://google.com' => 'google.com',
18
+ 'http://google.com:8080' => 'google.com',
19
+ 'http://www3.text-mates.org.uk:1234?foo' => 'www3.text-mates.org.uk',
20
+ 'http://abc.com/yes.com/no.com#fun' => 'abc.com'
21
+ }.each_pair do |url, server|
22
+ Url.parse(url).server.should == server
23
+ end
24
+ end
25
+
26
+ it 'should parse a really complex url' do
27
+ u = Url.parse('sftp://mondo122.goober.com:819/this/is-a-bunch.jpg?of=crazy&free[]=55#now')
28
+ u.scheme.should == 'sftp'
29
+ u.server.should == 'mondo122.goober.com'
30
+ u.port.should == 819
31
+ u.path.should == '/this/is-a-bunch.jpg'
32
+ u.params['of'].should == 'crazy'
33
+ u.params['free[]'].should == '55'
34
+ u.fragment.should == 'now'
35
+ end
36
+
37
+ it 'should construct a simple url' do
38
+ u = Url.new
39
+ u.path = '/bob.jpg'
40
+ u.to_s.should == '/bob.jpg'
41
+ end
42
+
43
+ it 'should construct a complex url' do
44
+ u = Url.new
45
+ u.server = 'jimbo.net.org'
46
+ u.port = 1044
47
+ u.path = '/south-west'
48
+ u.add_param('site', 'http://goober.com?123')
49
+ u.fragment = 'boggle-noggle'
50
+ u.to_s.should == 'http://jimbo.net.org:1044/south-west?site=http%3A%2F%2Fgoober.com%3F123#boggle-noggle'
51
+ end
52
+
53
+ it 'should allow adding params' do
54
+ u = Url.parse('http://irongaze.com')
55
+ u.params['foo'] = 'bar'
56
+ u.to_s.should == 'http://irongaze.com?foo=bar'
57
+ end
58
+
59
+ it 'should allow multiple values for a single param' do
60
+ u = Url.new('/scores')
61
+ u.add_param('soccer', '123')
62
+ u.add_param('soccer', '456')
63
+ u = Url.parse(u.to_s)
64
+ val = u.params['soccer']
65
+ val.should be_an(Array)
66
+ val.length.should == 2
67
+ val[1].should == '456'
68
+ end
69
+
70
+ it 'should allow resetting params' do
71
+ u = Url.parse('/?a=1&b=2')
72
+ u.params.length.should == 2
73
+ u.reset_params
74
+ u.should_not have_params
75
+ end
76
+
77
+ it 'should allow removing a param' do
78
+ u = Url.parse('/?a=1&b=2')
79
+ u.remove_param('a')
80
+ u.params.length.should == 1
81
+ u.to_s.should == '/?b=2'
82
+ end
83
+
84
+ it 'should support adding to the path' do
85
+ u = Url.parse('/')
86
+ u += 'main/site'
87
+ u.to_s.should == '/main/site'
88
+ end
89
+
90
+ it 'should make relative urls absolute' do
91
+ u = Url.parse('/bar')
92
+ u.relative?.should be_true
93
+ Url.default_server = 'irongaze.com'
94
+ u.make_absolute.should == 'http://irongaze.com/bar'
95
+ end
96
+
97
+ it 'should escape params correctly' do
98
+ Url.build('/foo', 'one:two' => 'http://other.com').should == '/foo?one%3Atwo=http%3A%2F%2Fother.com'
99
+ end
100
+
101
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: iron-web
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Rob Morris
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-05 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: iron-extensions
16
+ requirement: &2152615480 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *2152615480
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &2152608900 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '2.6'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *2152608900
36
+ description: Html builder, color helper class and url parser/builder class for use
37
+ in Rails projects or other web-based content generation.
38
+ email:
39
+ - rob@irongaze.com
40
+ executables: []
41
+ extensions: []
42
+ extra_rdoc_files: []
43
+ files:
44
+ - lib/iron/web/color.rb
45
+ - lib/iron/web/html/element.rb
46
+ - lib/iron/web/html.rb
47
+ - lib/iron/web/string.rb
48
+ - lib/iron/web/url.rb
49
+ - lib/iron/web.rb
50
+ - spec/spec_helper.rb
51
+ - spec/web/color_spec.rb
52
+ - spec/web/element_spec.rb
53
+ - spec/web/html_safe_string_spec.rb
54
+ - spec/web/html_spec.rb
55
+ - spec/web/string_spec.rb
56
+ - spec/web/url_spec.rb
57
+ - LICENSE
58
+ - History.txt
59
+ - Version.txt
60
+ - README.rdoc
61
+ - .rspec
62
+ homepage: http://irongaze.com
63
+ licenses:
64
+ - MIT
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: 1.9.2
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 1.8.10
84
+ signing_key:
85
+ specification_version: 3
86
+ summary: Helper classes for generating web content
87
+ test_files: []