hom 0.4.0 → 1.0.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.
- data/README.md +20 -2
- data/hom.gemspec +7 -1
- data/lib/hom.rb +0 -4
- data/spec/element_spec.rb +11 -57
- data/spec/encoding_spec.rb +97 -0
- data/spec/entity_spec.rb +13 -14
- metadata +35 -3
data/README.md
CHANGED
@@ -1,5 +1,6 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
A straightforward API for generating HTML
|
2
|
+
=========================================
|
3
|
+
|
3
4
|
|
4
5
|
Motivation
|
5
6
|
----------
|
@@ -8,6 +9,23 @@ HOM helps you implement HTML presentation logic in your code. Things like
|
|
8
9
|
navigation links, select boxes, sets of checkboxes; anything with behaviour
|
9
10
|
that is too complex for your templates.
|
10
11
|
|
12
|
+
|
13
|
+
Installation
|
14
|
+
------------
|
15
|
+
|
16
|
+
$ gem install hom
|
17
|
+
|
18
|
+
|
19
|
+
Quick start
|
20
|
+
-----------
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
require 'hom'
|
24
|
+
|
25
|
+
puts HOM::Element.new(:h1, nil, 'hello world')
|
26
|
+
```
|
27
|
+
|
28
|
+
|
11
29
|
Usage
|
12
30
|
-----
|
13
31
|
|
data/hom.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'hom'
|
3
|
-
s.version = '0.
|
3
|
+
s.version = '1.0.0'
|
4
4
|
s.platform = Gem::Platform::RUBY
|
5
5
|
s.authors = ['Tim Craft']
|
6
6
|
s.email = ['mail@timcraft.com']
|
@@ -8,6 +8,12 @@ Gem::Specification.new do |s|
|
|
8
8
|
s.description = 'A straightforward API for generating HTML'
|
9
9
|
s.summary = 'See description'
|
10
10
|
s.files = Dir.glob('{lib,spec}/**/*') + %w(Rakefile README.md hom.gemspec)
|
11
|
+
s.add_development_dependency('rake', '~> 10.0.3')
|
12
|
+
s.add_development_dependency('mocha', '~> 0.13.2')
|
11
13
|
s.add_development_dependency('activesupport', ['>= 3.0.3'])
|
12
14
|
s.require_path = 'lib'
|
15
|
+
|
16
|
+
if RUBY_VERSION == '1.8.7'
|
17
|
+
s.add_development_dependency('minitest', '>= 4.2.0')
|
18
|
+
end
|
13
19
|
end
|
data/lib/hom.rb
CHANGED
@@ -82,10 +82,6 @@ module HOM
|
|
82
82
|
object.map { |item| encode(item) }.join
|
83
83
|
elsif object.is_a?(Element)
|
84
84
|
encode_element(object)
|
85
|
-
elsif object.respond_to?(:html) # TODO: REMOVE ME
|
86
|
-
Kernel.warn '[hom] defining #html on custom objects is deprecated, map the objects to elements instead'
|
87
|
-
|
88
|
-
object.html
|
89
85
|
elsif object.respond_to?(:html_safe?) && object.html_safe?
|
90
86
|
object.to_s
|
91
87
|
else
|
data/spec/element_spec.rb
CHANGED
@@ -1,71 +1,25 @@
|
|
1
1
|
require 'minitest/autorun'
|
2
|
-
require '
|
3
|
-
|
4
|
-
require_relative '../lib/hom'
|
5
|
-
|
6
|
-
describe 'Element' do
|
7
|
-
before do
|
8
|
-
@br = HOM::Element.new(:br)
|
9
|
-
@i1 = HOM::Element.new(:input, :disabled)
|
10
|
-
@i2 = HOM::Element.new(:input, {type: :text, size: 30, value: nil})
|
11
|
-
@i3 = HOM::Element.new(:input, [{type: :text, size: 30}, :disabled])
|
12
|
-
@h1 = HOM::Element.new(:h1, nil, '')
|
13
|
-
@h2 = HOM::Element.new(:h2, nil, 'hello world')
|
14
|
-
@h3 = HOM::Element.new(:h3, nil, 'a && b, x > y')
|
15
|
-
@h4 = HOM::Element.new(:h4, nil, 1234567890)
|
16
|
-
@h5 = HOM::Element.new(:h5, nil, HOM::Element.new(:b, nil, 'how bold'))
|
17
|
-
@h6 = HOM::Element.new(:h6, nil, nil)
|
18
|
-
@ul = HOM::Element.new(:ul, nil, (1..3).map { |n| HOM::Element.new(:li, nil, n) })
|
19
|
-
end
|
2
|
+
require 'mocha/setup'
|
3
|
+
require 'hom'
|
20
4
|
|
5
|
+
describe 'HOM::Element' do
|
21
6
|
describe 'content query method' do
|
22
|
-
it '
|
23
|
-
|
7
|
+
it 'returns false if the content is undefined' do
|
8
|
+
HOM::Element.new(:br).content?.must_equal(false)
|
24
9
|
end
|
25
10
|
|
26
|
-
it '
|
27
|
-
|
11
|
+
it 'returns true otherwise' do
|
12
|
+
HOM::Element.new(:h1, nil, '').content?.must_equal(true)
|
28
13
|
end
|
29
14
|
end
|
30
15
|
|
31
16
|
describe 'to_s method' do
|
32
|
-
it '
|
33
|
-
|
34
|
-
@i1.to_s.must_equal('<input disabled>')
|
35
|
-
@i2.to_s.must_equal('<input type="text" size="30" value="">')
|
36
|
-
@i3.to_s.must_equal('<input type="text" size="30" disabled>')
|
37
|
-
@h1.to_s.must_equal('<h1></h1>')
|
38
|
-
@h2.to_s.must_equal('<h2>hello world</h2>')
|
39
|
-
@h3.to_s.must_equal('<h3>a && b, x > y</h3>')
|
40
|
-
@h4.to_s.must_equal('<h4>1234567890</h4>')
|
41
|
-
@h5.to_s.must_equal('<h5><b>how bold</b></h5>')
|
42
|
-
@h6.to_s.must_equal('<h6></h6>')
|
43
|
-
@ul.to_s.must_equal('<ul><li>1</li><li>2</li><li>3</li></ul>')
|
44
|
-
end
|
45
|
-
|
46
|
-
it 'should not encode content that has been marked as html safe' do
|
47
|
-
HOM::Element.new(:span, nil, '<br>').to_s.must_equal('<span><br></span>')
|
48
|
-
HOM::Element.new(:span, nil, '<br>'.html_safe).to_s.must_equal('<span><br></span>')
|
49
|
-
end
|
50
|
-
|
51
|
-
it 'should return content that is marked as html safe' do
|
52
|
-
@br.to_s.html_safe?.must_equal(true)
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
describe 'encoding objects with an html method' do # TODO: REMOVE ME
|
57
|
-
it 'should emit a deprecation warning' do
|
58
|
-
require 'mocha'
|
59
|
-
|
60
|
-
object = Object.new
|
61
|
-
|
62
|
-
def object.html; HOM::Element.new(:span, nil, 'html') end
|
63
|
-
|
64
|
-
div = HOM::Element.new(:div, nil, object)
|
17
|
+
it 'safely encodes itself using the encoding module' do
|
18
|
+
element = HOM::Element.new(:br)
|
65
19
|
|
66
|
-
|
20
|
+
HOM::Encoding.expects(:safe_encode).with(element)
|
67
21
|
|
68
|
-
|
22
|
+
element.to_s
|
69
23
|
end
|
70
24
|
end
|
71
25
|
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'active_support/core_ext/string/output_safety'
|
3
|
+
require 'hom'
|
4
|
+
|
5
|
+
describe 'HOM::Encoding' do
|
6
|
+
describe 'encode method' do
|
7
|
+
it 'renders elements with a tag name' do
|
8
|
+
element = HOM::Element.new(:br)
|
9
|
+
|
10
|
+
HOM::Encoding.encode(element).must_equal('<br>')
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'renders elements with a symbol attribute' do
|
14
|
+
element = HOM::Element.new(:input, :disabled)
|
15
|
+
|
16
|
+
HOM::Encoding.encode(element).must_equal('<input disabled>')
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'renders elements with a hash of attributes' do
|
20
|
+
element = HOM::Element.new(:input, {:type => :text, :size => 30, :value => nil})
|
21
|
+
|
22
|
+
output = HOM::Encoding.encode(element)
|
23
|
+
output.must_match(/<input .+>/)
|
24
|
+
output.must_include(' type="text"')
|
25
|
+
output.must_include(' size="30"')
|
26
|
+
output.must_include(' value=""')
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'renders elements with an array of mixed attributes' do
|
30
|
+
element = HOM::Element.new(:input, [{:type => :text, :size => 30}, :disabled])
|
31
|
+
|
32
|
+
output = HOM::Encoding.encode(element)
|
33
|
+
output.must_match(/<input .+>/)
|
34
|
+
output.must_include(' type="text"')
|
35
|
+
output.must_include(' size="30"')
|
36
|
+
output.must_include(' disabled')
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'renders elements with empty content' do
|
40
|
+
element = HOM::Element.new(:h1, nil, '')
|
41
|
+
|
42
|
+
HOM::Encoding.encode(element).must_equal('<h1></h1>')
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'renders elements with string content' do
|
46
|
+
element = HOM::Element.new(:h2, nil, 'hello world')
|
47
|
+
|
48
|
+
HOM::Encoding.encode(element).must_equal('<h2>hello world</h2>')
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'renders elements with numeric content' do
|
52
|
+
element = HOM::Element.new(:h4, nil, 1234567890)
|
53
|
+
|
54
|
+
HOM::Encoding.encode(element).must_equal('<h4>1234567890</h4>')
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'renders elements with a single child element' do
|
58
|
+
element = HOM::Element.new(:h5, nil, HOM::Element.new(:b, nil, 'how bold'))
|
59
|
+
|
60
|
+
HOM::Encoding.encode(element).must_equal('<h5><b>how bold</b></h5>')
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'renders elements with nil content' do
|
64
|
+
element = HOM::Element.new(:h6, nil, nil)
|
65
|
+
|
66
|
+
HOM::Encoding.encode(element).must_equal('<h6></h6>')
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'renders elements with multiple child elements' do
|
70
|
+
element = HOM::Element.new(:ul, nil, (1..3).map { |n| HOM::Element.new(:li, nil, n) })
|
71
|
+
|
72
|
+
HOM::Encoding.encode(element).must_equal('<ul><li>1</li><li>2</li><li>3</li></ul>')
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'escapes string content' do
|
76
|
+
element = HOM::Element.new(:h3, nil, 'a && b, x > y')
|
77
|
+
|
78
|
+
HOM::Encoding.encode(element).must_equal('<h3>a && b, x > y</h3>')
|
79
|
+
end
|
80
|
+
|
81
|
+
it 'does not escape content that has been marked as html safe' do
|
82
|
+
element = HOM::Element.new(:span, nil, '<br>'.html_safe)
|
83
|
+
|
84
|
+
HOM::Encoding.encode(element).must_equal('<span><br></span>')
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
describe 'safe_encode method' do
|
89
|
+
it 'encodes the argument and marks it as html safe' do
|
90
|
+
element = HOM::Element.new(:br)
|
91
|
+
|
92
|
+
output = HOM::Encoding.safe_encode(element)
|
93
|
+
output.must_equal('<br>')
|
94
|
+
output.html_safe?.must_equal(true)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
data/spec/entity_spec.rb
CHANGED
@@ -1,74 +1,73 @@
|
|
1
1
|
require 'minitest/autorun'
|
2
|
+
require 'hom'
|
2
3
|
|
3
|
-
|
4
|
-
|
5
|
-
describe 'Named entity' do
|
4
|
+
describe 'HOM::Entity initialized with a symbol' do
|
6
5
|
before do
|
7
6
|
@entity = HOM::Entity.new(:nbsp)
|
8
7
|
end
|
9
8
|
|
10
9
|
describe 'value method' do
|
11
|
-
it '
|
10
|
+
it 'returns the symbol value' do
|
12
11
|
@entity.value.must_equal(:nbsp)
|
13
12
|
end
|
14
13
|
end
|
15
14
|
|
16
15
|
describe 'numeric query method' do
|
17
|
-
it '
|
16
|
+
it 'returns false' do
|
18
17
|
@entity.numeric?.must_equal(false)
|
19
18
|
end
|
20
19
|
end
|
21
20
|
|
22
21
|
describe 'named query method' do
|
23
|
-
it '
|
22
|
+
it 'returns true' do
|
24
23
|
@entity.named?.must_equal(true)
|
25
24
|
end
|
26
25
|
end
|
27
26
|
|
28
27
|
describe 'to_s method' do
|
29
|
-
it '
|
28
|
+
it 'returns the encoded html entity' do
|
30
29
|
@entity.to_s.must_equal(' ')
|
31
30
|
end
|
32
31
|
end
|
33
32
|
|
34
33
|
describe 'html_safe query method' do
|
35
|
-
it '
|
34
|
+
it 'returns true' do
|
36
35
|
@entity.html_safe?.must_equal(true)
|
37
36
|
end
|
38
37
|
end
|
39
38
|
end
|
40
39
|
|
41
|
-
describe '
|
40
|
+
describe 'HOM::Entity initialized with an integer' do
|
42
41
|
before do
|
43
42
|
@entity = HOM::Entity.new(160)
|
44
43
|
end
|
45
44
|
|
46
45
|
describe 'value method' do
|
47
|
-
it '
|
46
|
+
it 'returns the integer value' do
|
48
47
|
@entity.value.must_equal(160)
|
49
48
|
end
|
50
49
|
end
|
51
50
|
|
52
51
|
describe 'numeric query method' do
|
53
|
-
it '
|
52
|
+
it 'returns true' do
|
54
53
|
@entity.numeric?.must_equal(true)
|
55
54
|
end
|
56
55
|
end
|
57
56
|
|
58
57
|
describe 'named query method' do
|
59
|
-
it '
|
58
|
+
it 'returns false' do
|
60
59
|
@entity.named?.must_equal(false)
|
61
60
|
end
|
62
61
|
end
|
63
62
|
|
64
63
|
describe 'to_s method' do
|
65
|
-
it '
|
64
|
+
it 'returns the encoded html entity' do
|
66
65
|
@entity.to_s.must_equal(' ')
|
67
66
|
end
|
68
67
|
end
|
69
68
|
|
70
69
|
describe 'html_safe query method' do
|
71
|
-
it '
|
70
|
+
it 'returns true' do
|
72
71
|
@entity.html_safe?.must_equal(true)
|
73
72
|
end
|
74
73
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hom
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,8 +9,40 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-02-16 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 10.0.3
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 10.0.3
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: mocha
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 0.13.2
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 0.13.2
|
14
46
|
- !ruby/object:Gem::Dependency
|
15
47
|
name: activesupport
|
16
48
|
requirement: !ruby/object:Gem::Requirement
|
@@ -36,6 +68,7 @@ extra_rdoc_files: []
|
|
36
68
|
files:
|
37
69
|
- lib/hom.rb
|
38
70
|
- spec/element_spec.rb
|
71
|
+
- spec/encoding_spec.rb
|
39
72
|
- spec/entity_spec.rb
|
40
73
|
- Rakefile
|
41
74
|
- README.md
|
@@ -65,4 +98,3 @@ signing_key:
|
|
65
98
|
specification_version: 3
|
66
99
|
summary: See description
|
67
100
|
test_files: []
|
68
|
-
has_rdoc:
|