hom 1.3.0 → 1.3.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: fd29d8108ab23030723dca12dcf54f727a8287bcb6b9c0834ef782f2481210c1
4
+ data.tar.gz: b34b94747fa553cc4ec05a3cca10bf7301b6c6bdf9f80c3089d4df532803fa2c
5
+ SHA512:
6
+ metadata.gz: 28b2940dbffc39193e63ff1fc8efcd5d1056da6feb43b9d4287a3b73adc3504d75f1c3f6de626c5a1e4aa2074361b24de2237d1ae7f2d3a47ac3bf4fa96f01c8
7
+ data.tar.gz: 15a99338cbb5d7d414c43f0b984646f9f8ff9adb0277d96de2c3c6a203ed1f17c5eb96d69ea6ee704b1b2deab181cfb8fba7aa5e6e1fbc4100b560c250aabb52
data/CHANGES.md ADDED
@@ -0,0 +1,61 @@
1
+ # 1.3.2
2
+
3
+ * Updated gemspec metadata
4
+
5
+ # 1.3.1
6
+
7
+ * Added metadata to gemspec
8
+
9
+ # 1.3.0
10
+
11
+ * HOM::NodeList#join now returns a new node list
12
+
13
+ * Added plus method to HOM::Element
14
+
15
+ * Added plus method to HOM::Entity
16
+
17
+ # 1.2.0
18
+
19
+ * Added html_safe? methods to HOM::Element and HOM::NodeList for compatibility with [erubis-auto](https://github.com/timcraft/erubis-auto)
20
+
21
+ # 1.1.0
22
+
23
+ * Added HOM::NodeList class
24
+
25
+ # 1.0.0
26
+
27
+ * Ruby 1.8.7 compatibility
28
+
29
+ * Removed support for rendering arbitrary objects by calling #html
30
+
31
+ # 0.4.0
32
+
33
+ * Deprecated support for rendering arbitrary objects by calling #html
34
+
35
+ * Added notion of undefined content, changing how elements with nil content are rendered:
36
+
37
+ With an element like this: `HOM::Element.new(:h1, {}, nil)`
38
+
39
+ Pre v0.4.0 rendering: `<h1>`
40
+
41
+ Post v0.4.0 rendering: `<h1></h1>`
42
+
43
+ # 0.3.0
44
+
45
+ * Added notion of undefined attribute values, nil is now rendered as an empty value:
46
+
47
+ With an element like this: `HOM::Element.new(:input, {:value => nil})`
48
+
49
+ Pre v0.3.0 rendering: `<input value>`
50
+
51
+ Post v0.3.0 rendering: `<input value="">`
52
+
53
+ * Removed Element#lookup method
54
+
55
+ # 0.2.0
56
+
57
+ * Replaced method missing attribute access with Element#lookup method
58
+
59
+ # 0.1.0
60
+
61
+ * First version!
data/LICENSE.txt ADDED
@@ -0,0 +1,4 @@
1
+ Copyright (c) 2011-2026 TIMCRAFT
2
+
3
+ This is an Open Source project licensed under the terms of the LGPLv3 license.
4
+ Please see <http://www.gnu.org/licenses/lgpl-3.0.html> for license text.
data/README.md CHANGED
@@ -1,26 +1,21 @@
1
- hom
2
- ===
3
-
1
+ # hom
4
2
 
5
3
  A straightforward API for generating HTML.
6
4
 
7
5
 
8
- Motivation
9
- ----------
6
+ ## Motivation
10
7
 
11
8
  HOM helps you implement HTML presentation logic in your code. Things like
12
9
  navigation links, select boxes, sets of checkboxes; anything with behaviour
13
10
  that is too complex for your templates.
14
11
 
15
12
 
16
- Installation
17
- ------------
13
+ ## Installation
18
14
 
19
15
  $ gem install hom
20
16
 
21
17
 
22
- Quick start
23
- -----------
18
+ ## Quick start
24
19
 
25
20
  ```ruby
26
21
  require 'hom'
@@ -29,8 +24,7 @@ puts HOM::Element.new(:h1, nil, 'hello world')
29
24
  ```
30
25
 
31
26
 
32
- Using HOM::Element
33
- ------------------
27
+ ## Using HOM::Element
34
28
 
35
29
  Create instances of HOM::Element to represent DOM elements. The first
36
30
  constructor argument is a symbol representing the tag name. For example,
@@ -73,8 +67,7 @@ generated HTML markup. HOM::Element objects are safe to use directly in Rails
73
67
  templates, all escaping is handled automatically.
74
68
 
75
69
 
76
- Using HOM::Entity
77
- -----------------
70
+ ## Using HOM::Entity
78
71
 
79
72
  Create instances of HOM::Entity to represent HTML entities. Use an integer
80
73
  argument for numeric entities and a symbol/string argument for named entities,
@@ -87,8 +80,7 @@ HOM::Entity.new(:nbsp)
87
80
  ```
88
81
 
89
82
 
90
- Using HOM::NodeList
91
- -------------------
83
+ ## Using HOM::NodeList
92
84
 
93
85
  Use HOM::NodeList to group nodes together without having to wrap them in an
94
86
  outer element. For example:
@@ -100,3 +92,37 @@ HOM::NodeList.new(['This is a ', HOM::Element.new(:strong, nil, 'Contrived'), '
100
92
  Calling #to_s on a HOM::NodeList object will return a string containing the
101
93
  generated HTML markup. Calling #join will insert separator nodes, a bit like
102
94
  Array#join, but returning HTML safe output.
95
+
96
+
97
+ ## XSS 101
98
+
99
+ Do you have helper methods that look like this:
100
+
101
+ ```ruby
102
+ def user_name(user)
103
+ "<strong>#{user.name}</strong>".html_safe
104
+ end
105
+ ```
106
+
107
+ Bzzzt, that's a security vulnerability right there. If you're using Rails
108
+ you should have code that looks like this:
109
+
110
+ ```ruby
111
+ def user_name(user)
112
+ content_tag(:strong, user.name)
113
+ end
114
+ ```
115
+
116
+ The content_tag helper will automatically escape content not explicitly
117
+ marked as safe. HOM will do very much the same thing, the equivalent helper
118
+ method would look like this:
119
+
120
+ ```ruby
121
+ def user_name(user)
122
+ HOM::Element.new(:strong, nil, user.name)
123
+ end
124
+ ```
125
+
126
+ Moral of the story: building up fragments of HTML using string interpolation
127
+ and concatenation is highly error prone. Solution: use content_tag or HOM to
128
+ safely build your content, and audit your usage of html_safe.
data/hom.gemspec CHANGED
@@ -1,18 +1,14 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'hom'
3
- s.version = '1.3.0'
3
+ s.version = '1.3.2'
4
+ s.license = 'LGPL-3.0'
4
5
  s.platform = Gem::Platform::RUBY
5
6
  s.authors = ['Tim Craft']
6
- s.email = ['mail@timcraft.com']
7
- s.homepage = 'http://github.com/timcraft/hom'
7
+ s.email = ['email@timcraft.com']
8
+ s.homepage = 'https://rubygems.org/gems/hom'
8
9
  s.description = 'A straightforward API for generating HTML'
9
- s.summary = 'See description'
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('activesupport', ['>= 3.0.3'])
10
+ s.summary = 'A straightforward API for generating HTML'
11
+ s.files = Dir.glob('lib/**/*.rb') + %w(LICENSE.txt README.md CHANGES.md hom.gemspec)
12
+ s.required_ruby_version = '>= 1.9.3'
13
13
  s.require_path = 'lib'
14
-
15
- if RUBY_VERSION == '1.8.7'
16
- s.add_development_dependency('minitest', '>= 4.2.0')
17
- end
18
14
  end
metadata CHANGED
@@ -1,86 +1,45 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hom
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
5
- prerelease:
4
+ version: 1.3.2
6
5
  platform: ruby
7
6
  authors:
8
7
  - Tim Craft
9
- autorequire:
10
8
  bindir: bin
11
9
  cert_chain: []
12
- date: 2013-06-10 00:00:00.000000000 Z
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: activesupport
32
- requirement: !ruby/object:Gem::Requirement
33
- none: false
34
- requirements:
35
- - - ! '>='
36
- - !ruby/object:Gem::Version
37
- version: 3.0.3
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: 3.0.3
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
46
12
  description: A straightforward API for generating HTML
47
13
  email:
48
- - mail@timcraft.com
14
+ - email@timcraft.com
49
15
  executables: []
50
16
  extensions: []
51
17
  extra_rdoc_files: []
52
18
  files:
53
- - lib/hom.rb
54
- - spec/element_spec.rb
55
- - spec/encoding_spec.rb
56
- - spec/entity_spec.rb
57
- - spec/node_list_spec.rb
58
- - Rakefile
19
+ - CHANGES.md
20
+ - LICENSE.txt
59
21
  - README.md
60
22
  - hom.gemspec
61
- homepage: http://github.com/timcraft/hom
62
- licenses: []
63
- post_install_message:
23
+ - lib/hom.rb
24
+ homepage: https://rubygems.org/gems/hom
25
+ licenses:
26
+ - LGPL-3.0
27
+ metadata: {}
64
28
  rdoc_options: []
65
29
  require_paths:
66
30
  - lib
67
31
  required_ruby_version: !ruby/object:Gem::Requirement
68
- none: false
69
32
  requirements:
70
- - - ! '>='
33
+ - - ">="
71
34
  - !ruby/object:Gem::Version
72
- version: '0'
35
+ version: 1.9.3
73
36
  required_rubygems_version: !ruby/object:Gem::Requirement
74
- none: false
75
37
  requirements:
76
- - - ! '>='
38
+ - - ">="
77
39
  - !ruby/object:Gem::Version
78
40
  version: '0'
79
41
  requirements: []
80
- rubyforge_project:
81
- rubygems_version: 1.8.25
82
- signing_key:
83
- specification_version: 3
84
- summary: See description
42
+ rubygems_version: 4.0.6
43
+ specification_version: 4
44
+ summary: A straightforward API for generating HTML
85
45
  test_files: []
86
- has_rdoc:
data/Rakefile DELETED
@@ -1,8 +0,0 @@
1
- require 'rake/testtask'
2
-
3
- task :default => :spec
4
-
5
- Rake::TestTask.new(:spec) do |t|
6
- t.test_files = FileList['spec/*_spec.rb']
7
- t.warning = true
8
- end
data/spec/element_spec.rb DELETED
@@ -1,42 +0,0 @@
1
- require 'minitest/autorun'
2
- require 'active_support/core_ext/string/output_safety'
3
-
4
- if defined? require_relative
5
- require_relative '../lib/hom'
6
- else
7
- require File.join(File.dirname(__FILE__), '../lib/hom')
8
- end
9
-
10
- describe 'HOM::Element' do
11
- describe 'content query method' do
12
- it 'returns false if the content is undefined' do
13
- HOM::Element.new(:br).content?.must_equal(false)
14
- end
15
-
16
- it 'returns true otherwise' do
17
- HOM::Element.new(:h1, nil, '').content?.must_equal(true)
18
- end
19
- end
20
-
21
- describe 'html_safe query method' do
22
- it 'returns true' do
23
- HOM::Element.new(:br).html_safe?.must_equal(true)
24
- end
25
- end
26
-
27
- describe 'to_s method' do
28
- it 'returns an html safe string containing the encoded element' do
29
- output = HOM::Element.new(:br).to_s
30
- output.html_safe?.must_equal(true)
31
- output.must_equal('<br>')
32
- end
33
- end
34
-
35
- describe 'addition of an element with another object' do
36
- it 'returns a node list containing the two nodes' do
37
- nodes = HOM::Element.new(:br) + "\n"
38
- nodes.must_be_instance_of(HOM::NodeList)
39
- nodes.to_s.must_equal("<br>\n")
40
- end
41
- end
42
- end
@@ -1,102 +0,0 @@
1
- require 'minitest/autorun'
2
- require 'active_support/core_ext/string/output_safety'
3
-
4
- if defined? require_relative
5
- require_relative '../lib/hom'
6
- else
7
- require File.join(File.dirname(__FILE__), '../lib/hom')
8
- end
9
-
10
- describe 'HOM::Encoding' do
11
- describe 'encode method' do
12
- it 'renders elements with a tag name' do
13
- element = HOM::Element.new(:br)
14
-
15
- HOM::Encoding.encode(element).must_equal('<br>')
16
- end
17
-
18
- it 'renders elements with a symbol attribute' do
19
- element = HOM::Element.new(:input, :disabled)
20
-
21
- HOM::Encoding.encode(element).must_equal('<input disabled>')
22
- end
23
-
24
- it 'renders elements with a hash of attributes' do
25
- element = HOM::Element.new(:input, {:type => :text, :size => 30, :value => nil})
26
-
27
- output = HOM::Encoding.encode(element)
28
- output.must_match(/<input .+>/)
29
- output.must_include(' type="text"')
30
- output.must_include(' size="30"')
31
- output.must_include(' value=""')
32
- end
33
-
34
- it 'renders elements with an array of mixed attributes' do
35
- element = HOM::Element.new(:input, [{:type => :text, :size => 30}, :disabled])
36
-
37
- output = HOM::Encoding.encode(element)
38
- output.must_match(/<input .+>/)
39
- output.must_include(' type="text"')
40
- output.must_include(' size="30"')
41
- output.must_include(' disabled')
42
- end
43
-
44
- it 'renders elements with empty content' do
45
- element = HOM::Element.new(:h1, nil, '')
46
-
47
- HOM::Encoding.encode(element).must_equal('<h1></h1>')
48
- end
49
-
50
- it 'renders elements with string content' do
51
- element = HOM::Element.new(:h2, nil, 'hello world')
52
-
53
- HOM::Encoding.encode(element).must_equal('<h2>hello world</h2>')
54
- end
55
-
56
- it 'renders elements with numeric content' do
57
- element = HOM::Element.new(:h4, nil, 1234567890)
58
-
59
- HOM::Encoding.encode(element).must_equal('<h4>1234567890</h4>')
60
- end
61
-
62
- it 'renders elements with a single child element' do
63
- element = HOM::Element.new(:h5, nil, HOM::Element.new(:b, nil, 'how bold'))
64
-
65
- HOM::Encoding.encode(element).must_equal('<h5><b>how bold</b></h5>')
66
- end
67
-
68
- it 'renders elements with nil content' do
69
- element = HOM::Element.new(:h6, nil, nil)
70
-
71
- HOM::Encoding.encode(element).must_equal('<h6></h6>')
72
- end
73
-
74
- it 'renders elements with multiple child elements' do
75
- element = HOM::Element.new(:ul, nil, (1..3).map { |n| HOM::Element.new(:li, nil, n) })
76
-
77
- HOM::Encoding.encode(element).must_equal('<ul><li>1</li><li>2</li><li>3</li></ul>')
78
- end
79
-
80
- it 'escapes string content' do
81
- element = HOM::Element.new(:h3, nil, 'a && b, x > y')
82
-
83
- HOM::Encoding.encode(element).must_equal('<h3>a &amp;&amp; b, x &gt; y</h3>')
84
- end
85
-
86
- it 'does not escape content that has been marked as html safe' do
87
- element = HOM::Element.new(:span, nil, '<br>'.html_safe)
88
-
89
- HOM::Encoding.encode(element).must_equal('<span><br></span>')
90
- end
91
- end
92
-
93
- describe 'safe_encode method' do
94
- it 'encodes the argument and marks it as html safe' do
95
- element = HOM::Element.new(:br)
96
-
97
- output = HOM::Encoding.safe_encode(element)
98
- output.must_equal('<br>')
99
- output.html_safe?.must_equal(true)
100
- end
101
- end
102
- end
data/spec/entity_spec.rb DELETED
@@ -1,67 +0,0 @@
1
- require 'minitest/autorun'
2
-
3
- if defined? require_relative
4
- require_relative '../lib/hom'
5
- else
6
- require File.join(File.dirname(__FILE__), '../lib/hom')
7
- end
8
-
9
- describe 'HOM::Entity' do
10
- before do
11
- @named_entity = HOM::Entity.new(:nbsp)
12
-
13
- @numeric_entity = HOM::Entity.new(160)
14
- end
15
-
16
- describe 'value method' do
17
- it 'returns the value passed to the constructor' do
18
- @named_entity.value.must_equal(:nbsp)
19
-
20
- @numeric_entity.value.must_equal(160)
21
- end
22
- end
23
-
24
- describe 'numeric query method' do
25
- it 'returns false for named entities' do
26
- @named_entity.numeric?.must_equal(false)
27
- end
28
-
29
- it 'returns true for numeric entities' do
30
- @numeric_entity.numeric?.must_equal(true)
31
- end
32
- end
33
-
34
- describe 'named query method' do
35
- it 'returns true for named entities' do
36
- @named_entity.named?.must_equal(true)
37
- end
38
-
39
- it 'returns false for numeric entities' do
40
- @numeric_entity.named?.must_equal(false)
41
- end
42
- end
43
-
44
- describe 'to_s method' do
45
- it 'returns the encoded html entity' do
46
- @named_entity.to_s.must_equal('&nbsp;')
47
-
48
- @numeric_entity.to_s.must_equal('&#160;')
49
- end
50
- end
51
-
52
- describe 'html_safe query method' do
53
- it 'returns true' do
54
- @named_entity.html_safe?.must_equal(true)
55
-
56
- @numeric_entity.html_safe?.must_equal(true)
57
- end
58
- end
59
-
60
- describe 'addition of an entity with another object' do
61
- it 'returns a node list containing the two nodes' do
62
- nodes = HOM::Entity.new(:pound) + '9.99'
63
- nodes.must_be_instance_of(HOM::NodeList)
64
- nodes.to_s.must_equal('&pound;9.99')
65
- end
66
- end
67
- end
@@ -1,59 +0,0 @@
1
- require 'minitest/autorun'
2
- require 'active_support/core_ext/string/output_safety'
3
-
4
- if defined? require_relative
5
- require_relative '../lib/hom'
6
- else
7
- require File.join(File.dirname(__FILE__), '../lib/hom')
8
- end
9
-
10
- describe 'HOM::NodeList' do
11
- describe 'html_safe query method' do
12
- it 'returns true' do
13
- HOM::NodeList.new([]).html_safe?.must_equal(true)
14
- end
15
- end
16
-
17
- describe 'to_s method' do
18
- it 'returns an html safe string containing the encoded nodes' do
19
- nodes = HOM::NodeList.new(['Hello', HOM::Entity.new(:nbsp), HOM::Element.new(:b, nil, 'World')])
20
-
21
- output = nodes.to_s
22
- output.must_equal('Hello&nbsp;<b>World</b>')
23
- output.html_safe?.must_equal(true)
24
- end
25
- end
26
-
27
- describe 'to_a method' do
28
- it 'returns the array of nodes passed to the constructor' do
29
- nodes = HOM::NodeList.new(%w(a b c))
30
- nodes.to_a.must_equal(%w(a b c))
31
- end
32
- end
33
-
34
- describe 'addition of two node lists' do
35
- it 'returns a new node list containing the items in each list' do
36
- nodes = HOM::NodeList.new(['one']) + HOM::NodeList.new(['two'])
37
- nodes.must_be_instance_of(HOM::NodeList)
38
- nodes.to_a.must_equal(%w(one two))
39
- end
40
- end
41
-
42
- describe 'addition of a node list with an element object' do
43
- it 'returns a new node list containing the items in the list and the additional element' do
44
- element = HOM::Element.new(:b, nil, 'World')
45
-
46
- nodes = HOM::NodeList.new(['Hello', HOM::Entity.new(:nbsp)]) + element
47
- nodes.must_be_instance_of(HOM::NodeList)
48
- nodes.to_s.must_equal('Hello&nbsp;<b>World</b>')
49
- end
50
- end
51
-
52
- describe 'join method' do
53
- it 'returns a new node list containing the items in the list seperated by the given node' do
54
- nodes = HOM::NodeList.new(%w(a b c)).join(HOM::Entity.new(:nbsp))
55
- nodes.must_be_instance_of(HOM::NodeList)
56
- nodes.to_s.must_equal('a&nbsp;b&nbsp;c')
57
- end
58
- end
59
- end