html_attributes 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,24 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ html_attributes (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.1.3)
10
+ rspec (2.11.0)
11
+ rspec-core (~> 2.11.0)
12
+ rspec-expectations (~> 2.11.0)
13
+ rspec-mocks (~> 2.11.0)
14
+ rspec-core (2.11.1)
15
+ rspec-expectations (2.11.3)
16
+ diff-lcs (~> 1.1.3)
17
+ rspec-mocks (2.11.2)
18
+
19
+ PLATFORMS
20
+ ruby
21
+
22
+ DEPENDENCIES
23
+ html_attributes!
24
+ rspec
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 (Jan Sebastian Siwy, Martin Spickermann, Henning Staib)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # HTML Attributes
2
+
3
+ HTML Attributes provide helper methods to convert arrays and hashes to valid html attributes. If this gem is included in a Ruby on Rails project it will extend the view helpers to pass arrays or hashes for class or style attributes.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem "html_attributes"
10
+
11
+ And then execute:
12
+
13
+ $ bundle install
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install html_attributes
18
+
19
+ ## Usage
20
+
21
+ { :first => true, :second => false, :third => true }.to_class_attr
22
+ => "first third"
23
+
24
+ [:first, :second, :third].to_class_attr
25
+ => "first second third"
26
+
27
+ { :margin => { :top => "10px", :left => "5px" }, :font_size => "12pt" }.to_style_attr
28
+ => "margin-top: 10px; margin-left: 5px; font-size: 12pt;"
29
+
30
+ ["padding-top: 8px", "padding-right: 15px", "font-size: 12pt"].to_style_attr
31
+ => "padding-top: 8px; padding-right: 15px; font-size: 12pt;"
32
+
33
+ If gem is included in a Ruby on Rails project the view helpers are extended:
34
+
35
+ content_tag(:div, :class => { :first => true, :second => false, :third => true }) { ... }
36
+ => <div class="first third">...</div>
37
+
38
+ content_tag(:div, :style => { :margin => { :top => "10px", :left => "5px" }, :font_size => "12pt" }) { ... }
39
+ => <div style="margin-top: 10px; margin-left: 5px; font-size: 12pt;">...</div>
40
+
41
+ ## Extension
42
+
43
+ To support additional conversions of arrays, hashed, or any other objects to HTML attributes when using the Rails view helpers a `#to_#{attribute_name}_attr` method can be implemented.
44
+
45
+ class MyImageClass
46
+
47
+ # initializer ...
48
+
49
+ def to_width_attr
50
+ @width
51
+ end
52
+
53
+ def to_height_attr
54
+ @height
55
+ end
56
+
57
+ end
58
+
59
+ img = MyImageClass.new
60
+ tag(:img, :width => img, :height => img)
61
+ => <img width="30" height="20" />
62
+
63
+ ## Test
64
+
65
+ Checkout and cd into the repository and then run:
66
+
67
+ $ bundle install
68
+ $ bundle exec rspec spec
69
+
70
+ Copyright (c) 2012 Jan Sebastian Siwy, Martin Spickermann, Henning Staib, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path("../lib/html_attributes/version", __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+
6
+ gem.name = "html_attributes"
7
+ gem.authors = ["Jan Sebastian Siwy", "Martin Spickermann", "Henning Staib"]
8
+ gem.email = ["github@henning-staib.de"]
9
+ gem.summary = %q{html_attributes provide helper methods to convert arrays
10
+ and hashes to valid html attributes.}
11
+ gem.description = %q{html_attributes provide helper methods to convert arrays
12
+ and hashes to valid html attributes.}
13
+ gem.homepage = "https://github.com/serevaris/html_attributes"
14
+
15
+ gem.files = `git ls-files`.split($\)
16
+ gem.require_paths = ["lib"]
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+
19
+ gem.version = HtmlAttributes::VERSION
20
+ gem.platform = Gem::Platform::RUBY
21
+
22
+ gem.add_development_dependency "rspec"
23
+
24
+ end
@@ -0,0 +1,45 @@
1
+ class Array
2
+
3
+ # Returns a string representing the value of a HTML class attribute or nil (if empty).
4
+ #
5
+ # ==== Examples
6
+ #
7
+ # ["post", "comment", "spam"].to_class_attr
8
+ # # => "post comment spam"
9
+ #
10
+ # [].to_class_attr
11
+ # # => nil
12
+ #
13
+ def to_class_attr
14
+ empty? ? nil : join(' ')
15
+ end
16
+
17
+ # Returns a string representing the value of a HTML rel attribute or nil (if empty).
18
+ #
19
+ # ==== Examples
20
+ #
21
+ # ["nofollow", "me"].to_rel_attr
22
+ # # => "nofollow me"
23
+ #
24
+ # [].to_class_attr
25
+ # # => nil
26
+ #
27
+ def to_rel_attr
28
+ empty? ? nil : join(' ')
29
+ end
30
+
31
+ # Returns a string representing the value of a HTML style attribute or nil (if empty).
32
+ #
33
+ # ==== Examples
34
+ #
35
+ # ["padding-top: 8px", "padding-right: 15px", "font-size: 12pt"].to_style_attr
36
+ # # => "padding-top: 8px; padding-right: 15px; font-size: 12pt;"
37
+ #
38
+ # [].to_style_attr
39
+ # # => nil
40
+ #
41
+ def to_style_attr
42
+ empty? ? nil : join('; ').concat(';')
43
+ end
44
+
45
+ end
@@ -0,0 +1,98 @@
1
+ class Hash
2
+
3
+ # Returns a string containing the class names whose conditions are neighter <tt>false</tt> nor <tt>nil</tt>,
4
+ # or returns <tt>nil</tt> if no class names remains.
5
+ #
6
+ # ==== Examples
7
+ #
8
+ # { :first => true, :second => false, :third => true }.to_class_attr
9
+ # # => "first third"
10
+ #
11
+ # { :foo => false, :bar => false }.to_class_attr
12
+ # # => nil
13
+ #
14
+ def to_class_attr
15
+ self.select { |klass, condition| condition }. # remove classes with nil or false condition
16
+ map { |elem| elem.first.to_s }. # throw condition away
17
+ sort. # sort alphabetically
18
+ to_class_attr # join with ' ' or return nil if empty
19
+ end
20
+
21
+ # Returns a string containing the rel values whose conditions are neighter <tt>false</tt> nor <tt>nil</tt>,
22
+ # or returns <tt>nil</tt> if no rel values remains.
23
+ #
24
+ # ==== Examples
25
+ #
26
+ # { :nofollow => true, :other => false, :me => true }.to_rel_attr
27
+ # # => "nofollow me"
28
+ #
29
+ # { :foo => false, :bar => false }.to_rel_attr
30
+ # # => nil
31
+ #
32
+ def to_rel_attr
33
+ self.select { |klass, condition| condition }. # remove rel values with nil or false condition
34
+ map { |elem| elem.first.to_s }. # throw condition away
35
+ sort. # sort alphabetically
36
+ to_rel_attr # join with ' ' or return nil if empty
37
+ end
38
+
39
+ # Returns a string containing a style attribute, or returns <tt>nil</tt> if no values remains.
40
+ #
41
+ # ==== Examples
42
+ #
43
+ # { :padding => { :top => "8px", :right => "15px" }, :font_size => "12pt" }.to_style_attr
44
+ # # => "font-size: 12pt; padding-right: 15px; padding-top: 8px;"
45
+ #
46
+ # visible = true
47
+ # { :display => visible ? nil : 'none' }.to_style_attr
48
+ # # => nil
49
+ #
50
+ def to_style_attr
51
+ self.flatten { |keys| keys.join('_').gsub('_', '-') }. # build CSS like key names
52
+ select { |key, value| !value.nil? }. # remove properties with nil value
53
+ map { |key, value| "#{key}: #{value}" }. # build property
54
+ sort. # sort alphabetically
55
+ to_style_attr # join with '; ' or return nil if empty
56
+ end
57
+
58
+ # Returns a new hash flatting the values (recursively), i.e. if a value of <tt>self</tt> is a hash again
59
+ # 1) the keys are build by combining the key of <tt>self</tt> (the one to access that hash value) with the
60
+ # keys of that hash;
61
+ # 2) the values are the values of that hash.
62
+ #
63
+ # You can either specify a seperator to to build the keys joining them, or a block which gets an array of keys
64
+ # and returns the new key.
65
+ #
66
+ # ==== Examples
67
+ #
68
+ # styles = { :padding => { :top => "8px", :right => "15px" }, :font_size => "12pt" }
69
+ #
70
+ # styles.flatten("_")
71
+ # # => { "padding_top" => "8px", "padding_right" => "15px", "font_size" => "12pt" }
72
+ #
73
+ # styles.flatten { |keys| keys.join('-').gsub('_', '-') }
74
+ # # => { "padding-top" => "8px", "padding-right" => "15px", "font-size" => "12pt" }
75
+ #
76
+ def flatten(sep = $,, prefix_keys = [], &block) # :nodoc:
77
+ hash = self.class.new
78
+ self.each do |key, value|
79
+ prefix_keys.push(key)
80
+ case value
81
+ when ::Hash
82
+ value.flatten(sep, prefix_keys, &block).each do |k, v|
83
+ hash[k] = v
84
+ end
85
+ else
86
+ hash[block_given? ? yield(prefix_keys) : prefix_keys.join(sep)] = value
87
+ end
88
+ prefix_keys.pop
89
+ end
90
+ hash
91
+ end
92
+
93
+ # Flattens <tt>self</tt> in place.
94
+ def flatten!(sep = $,, prefix_keys = [], &block) # :nodoc:
95
+ replace(flatten(sep, prefix_keys, &block))
96
+ end
97
+
98
+ end
@@ -0,0 +1,21 @@
1
+ module HtmlAttributes #:nodoc:
2
+
3
+ BOOLEAN_ATTRIBUTES = %w(disabled readonly multiple checked autobuffer
4
+ autoplay controls loop selected hidden scoped async
5
+ defer reversed ismap seemless muted required
6
+ autofocus novalidate formnovalidate open pubdate).to_set
7
+
8
+ module TagHelper #:nodoc:
9
+
10
+ def self.included(base) #:nodoc:
11
+ base.alias_method_chain :tag_options, :html_attributes
12
+ end
13
+
14
+ def tag_options_with_html_attributes(options, escape = true) #:nodoc:
15
+ options = ::Hash[*options.map { |key, value| [key, value.respond_to?("to_#{key}_attr") ? value.send("to_#{key}_attr") : value] }.flatten] unless options.blank?
16
+ tag_options_without_html_attributes(options, escape)
17
+ end
18
+
19
+ end
20
+
21
+ end
@@ -0,0 +1,12 @@
1
+ module HtmlAttributes
2
+ VERSION = "0.1.0"
3
+
4
+ module Version
5
+ version = VERSION.to_s.split(".").map { |i| i.to_i }
6
+ MAJOR = version[0]
7
+ MINOR = version[1]
8
+ PATCH = version[2]
9
+ STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
10
+ end
11
+
12
+ end
@@ -0,0 +1,15 @@
1
+ require "html_attributes/array"
2
+ require "html_attributes/hash"
3
+
4
+ if defined?(ActionView::Helpers::TagHelper)
5
+
6
+ require "html_attributes/rails/tag_helper"
7
+
8
+ ActionView::Helpers::TagHelper.module_eval do
9
+ include HtmlAttributes::TagHelper
10
+ end
11
+
12
+ ActionView::Helpers::TagHelper::BOOLEAN_ATTRIBUTES.merge(::HtmlAttributes::BOOLEAN_ATTRIBUTES)
13
+ ActionView::Helpers::TagHelper::BOOLEAN_ATTRIBUTES.merge(ActionView::Helpers::TagHelper::BOOLEAN_ATTRIBUTES.map(&:to_sym))
14
+
15
+ end
@@ -0,0 +1,43 @@
1
+ require "spec_helper"
2
+
3
+ describe Array do
4
+
5
+ describe "#to_class_attr" do
6
+
7
+ it "should return nil if array is empty" do
8
+ classes = []
9
+ classes.to_class_attr.should be_nil
10
+ end
11
+
12
+ it "should return a space separated string if array contains strings" do
13
+ classes = ["first", "second", "third"]
14
+ classes.to_class_attr.should eql("first second third")
15
+ end
16
+
17
+ it "should preserve existing spaces" do
18
+ classes = ["first", "second third", "forth"]
19
+ classes.to_class_attr.should eql("first second third forth")
20
+ end
21
+
22
+ end
23
+
24
+ describe "#to_style_attr" do
25
+
26
+ it "should return nil if array is empty" do
27
+ styles = []
28
+ styles.to_style_attr.should be_nil
29
+ end
30
+
31
+ it "should return a semicolon separated string if array contains strings" do
32
+ styles = ["padding-top: 8px", "padding-right: 15px", "font-size: 12pt"]
33
+ styles.to_style_attr.should eql("padding-top: 8px; padding-right: 15px; font-size: 12pt;")
34
+ end
35
+
36
+ it "should preserve existing semicolon" do
37
+ styles = ["padding-top: 8px; padding-right: 15px", "font-size: 12pt"]
38
+ styles.to_style_attr.should eql("padding-top: 8px; padding-right: 15px; font-size: 12pt;")
39
+ end
40
+
41
+ end
42
+
43
+ end
@@ -0,0 +1,89 @@
1
+ require "spec_helper"
2
+
3
+ describe Hash do
4
+
5
+ describe "#to_class_attr" do
6
+
7
+ it "should return nil if hash is empty" do
8
+ classes = {}
9
+ classes.to_class_attr.should be_nil
10
+ end
11
+
12
+ it "should return nil if hash only contains false values" do
13
+ classes = { :foo => false, :bar => false }
14
+ classes.to_class_attr.should be_nil
15
+ end
16
+
17
+ it "should return space separated string" do
18
+ classes = { :first => true, :second => true, :third => true }
19
+ classes.to_class_attr.should eql("first second third")
20
+ end
21
+
22
+ it "should return space separated string only for true values" do
23
+ classes = { :first => true, :second => false, :third => true }
24
+ classes.to_class_attr.should eql("first third")
25
+ end
26
+
27
+ end
28
+
29
+ describe "#to_style_attr" do
30
+
31
+ it "should return nil if hash is empty" do
32
+ styles = {}
33
+ styles.to_style_attr.should be_nil
34
+ end
35
+
36
+ it "should return nil if hash only contains nil values" do
37
+ styles = { :display => nil }
38
+ styles.to_style_attr.should be_nil
39
+ end
40
+
41
+ it "should return semicolon separated string" do
42
+ styles = { :display => "none", :visibility => "hidden" }
43
+ styles.to_style_attr.should eql("display: none; visibility: hidden;")
44
+ end
45
+
46
+ it "should replace '_' in keys with '-'" do
47
+ styles = { :font_size => "12pt" }
48
+ styles.to_style_attr.should eql("font-size: 12pt;")
49
+ end
50
+
51
+ it "should flatten hash to construct style statements" do
52
+ styles = { :padding => { :top => "8px", :right => "15px" }, :font_size => "12pt" }
53
+ styles.to_style_attr.should eql("font-size: 12pt; padding-right: 15px; padding-top: 8px;")
54
+ end
55
+
56
+ end
57
+
58
+ describe "#flatten" do
59
+
60
+ it "should do nothing for emtpy hash" do
61
+ {}.flatten.should eql({})
62
+ end
63
+
64
+ it "should do nothing for unnested hash" do
65
+ { "padding" => "8px", "font_size" => "12pt" }.flatten.should eql({ "padding" => "8px", "font_size" => "12pt" })
66
+ end
67
+
68
+ it "should convert symbolized keys to stringified keys" do
69
+ { :padding => "8px", :font_size => "12pt" }.flatten.should eql({ "padding" => "8px", "font_size" => "12pt" })
70
+ end
71
+
72
+ it "should flatten nested hash" do
73
+ styles = { :padding => { :top => "8px", :right => "15px" } }
74
+ styles.flatten.should eql({ "paddingtop" => "8px", "paddingright" => "15px" })
75
+ end
76
+
77
+ it "should flatten nested hash using given separator" do
78
+ styles = { :padding => { :top => "8px", :right => "15px" } }
79
+ styles.flatten("-").should eql({ "padding-top" => "8px", "padding-right" => "15px" })
80
+ end
81
+
82
+ it "should pass array of nested keys if block is given" do
83
+ styles = { :padding => { :top => "8px", :right => "15px" } }
84
+ styles.flatten { |keys| keys.join('-') }.should eql({ "padding-top" => "8px", "padding-right" => "15px" })
85
+ end
86
+
87
+ end
88
+
89
+ end
@@ -0,0 +1 @@
1
+ require "html_attributes"
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: html_attributes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jan Sebastian Siwy
9
+ - Martin Spickermann
10
+ - Henning Staib
11
+ autorequire:
12
+ bindir: bin
13
+ cert_chain: []
14
+ date: 2012-09-12 00:00:00.000000000 Z
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rspec
18
+ requirement: !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ! '>='
22
+ - !ruby/object:Gem::Version
23
+ version: '0'
24
+ type: :development
25
+ prerelease: false
26
+ version_requirements: !ruby/object:Gem::Requirement
27
+ none: false
28
+ requirements:
29
+ - - ! '>='
30
+ - !ruby/object:Gem::Version
31
+ version: '0'
32
+ description: ! "html_attributes provide helper methods to convert arrays\n and
33
+ hashes to valid html attributes."
34
+ email:
35
+ - github@henning-staib.de
36
+ executables: []
37
+ extensions: []
38
+ extra_rdoc_files: []
39
+ files:
40
+ - Gemfile
41
+ - Gemfile.lock
42
+ - MIT-LICENSE
43
+ - README.md
44
+ - Rakefile
45
+ - html_attributes.gemspec
46
+ - lib/html_attributes.rb
47
+ - lib/html_attributes/array.rb
48
+ - lib/html_attributes/hash.rb
49
+ - lib/html_attributes/rails/tag_helper.rb
50
+ - lib/html_attributes/version.rb
51
+ - spec/html_attributes/array_spec.rb
52
+ - spec/html_attributes/hash_spec.rb
53
+ - spec/spec_helper.rb
54
+ homepage: https://github.com/serevaris/html_attributes
55
+ licenses: []
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ segments:
67
+ - 0
68
+ hash: -89085853264897424
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ segments:
76
+ - 0
77
+ hash: -89085853264897424
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 1.8.23
81
+ signing_key:
82
+ specification_version: 3
83
+ summary: html_attributes provide helper methods to convert arrays and hashes to valid
84
+ html attributes.
85
+ test_files:
86
+ - spec/html_attributes/array_spec.rb
87
+ - spec/html_attributes/hash_spec.rb
88
+ - spec/spec_helper.rb