iq-html 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,4 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ doc
19
+ .yardoc
20
+ pkg
21
+
22
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Jamie Hill, SonicIQ Ltd.
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.rdoc ADDED
@@ -0,0 +1,47 @@
1
+ = IQ::HTML
2
+
3
+ A simple gem containing a series of helper methods for creating html markup including the escaping of strings. The aim was to create the simplest possible gem for creating HTML snippets for when the alternatives are overkill.
4
+
5
+ ==Install
6
+ gem install iq-html
7
+
8
+ ==Usage
9
+
10
+ ===Tag examples:
11
+ IQ::HTML.tag('br') #=> "<br />"
12
+ IQ::HTML.tag('strong', 'Bill & Ben') #=> "<strong>Bill &amp; Ben</strong>"
13
+ IQ::HTML.tag('strong', 'Bill & Ben', false) #=> "<strong>Bill & Ben</strong>"
14
+ IQ::HTML.tag('strong', 'B&B', { :id => 'bb' }, false) #=> "<strong id="bb">Bill & Ben</strong>"
15
+ IQ::HTML.tag('input', :title => 'B&B') #=> '<input type="text" title="B&amp;B" />'
16
+ IQ::HTML.tag('strong', 'B&B', :title => 'Bill & Ben') #=> "<strong title="Bill &amp; Ben">B&amp;B</strong>"
17
+
18
+ ===Escaping strings:
19
+ IQ::HTML.escape('Letters, Num83r5, & -- &amp; > -- &gt; < -- &lt; " -- &quot; &#1234;')
20
+ #=> 'Letters, Num83r5, &amp; -- &amp;amp; &gt; -- &amp;gt; &lt; -- &amp;lt; &quot; -- &amp;quot; &amp;#1234;'
21
+
22
+ IQ::HTML.escape_once('Letters, Num83r5, & -- &amp; > -- &gt; < -- &lt; " -- &quot; &#1234;')
23
+ #=> 'Letters, Num83r5, &amp; -- &amp; &gt; -- &gt; &lt; -- &lt; &quot; -- &quot; &#1234;'
24
+
25
+ ===Sanitising a string for use in an HTML "id" attribute:
26
+ IQ::HTML.sanitize_as_dom_id('product[variants][0][stock]')
27
+ #=> 'product-variants-0-stock'
28
+
29
+ == Documentation
30
+
31
+ Documentation is handled with YARD[http://github.com/lsegal/yard]. You can view the docs at http://rdoc.info/projects/soniciq/iq-html or alternatively, if you have the yard gem installed, run:
32
+
33
+ rake yard
34
+
35
+ == Note on Patches/Pull Requests
36
+
37
+ * Fork the project.
38
+ * Make your feature addition or bug fix.
39
+ * Add tests for it. This is important so I don't break it in a
40
+ future version unintentionally.
41
+ * Commit, do not mess with rakefile, version, or history.
42
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
43
+ * Send me a pull request. Bonus points for topic branches.
44
+
45
+ == Copyright
46
+
47
+ Copyright (c) 2010 Jamie Hill, SonicIQ Ltd. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "iq-html"
8
+ gem.summary = %Q{Library for creating snippets of html.}
9
+ gem.description = %Q{Library containing a series of helper methods for creating html markup including the escaping of strings.}
10
+ gem.email = "jamie@soniciq.com"
11
+ gem.homepage = "http://github.com/soniciq/iq-html"
12
+ gem.authors = ["Jamie Hill"]
13
+ gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
14
+ gem.add_development_dependency "yard", ">= 0"
15
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
+ end
21
+
22
+ require 'rake/testtask'
23
+ Rake::TestTask.new(:test) do |test|
24
+ test.libs << 'lib' << 'test'
25
+ test.pattern = 'test/**/test_*.rb'
26
+ test.verbose = true
27
+ end
28
+
29
+ begin
30
+ require 'rcov/rcovtask'
31
+ Rcov::RcovTask.new do |test|
32
+ test.libs << 'test'
33
+ test.pattern = 'test/**/test_*.rb'
34
+ test.verbose = true
35
+ end
36
+ rescue LoadError
37
+ task :rcov do
38
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
39
+ end
40
+ end
41
+
42
+ task :test => :check_dependencies
43
+
44
+ task :default => :test
45
+
46
+ begin
47
+ require 'yard'
48
+ YARD::Rake::YardocTask.new
49
+ rescue LoadError
50
+ task :yardoc do
51
+ abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
52
+ end
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/iq-html.gemspec ADDED
@@ -0,0 +1,58 @@
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{iq-html}
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 = ["Jamie Hill"]
12
+ s.date = %q{2010-01-13}
13
+ s.description = %q{Library containing a series of helper methods for creating html markup including the escaping of strings.}
14
+ s.email = %q{jamie@soniciq.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "iq-html.gemspec",
27
+ "lib/iq-html.rb",
28
+ "lib/iq/html.rb",
29
+ "test/helper.rb",
30
+ "test/iq/html_test.rb"
31
+ ]
32
+ s.homepage = %q{http://github.com/soniciq/iq-html}
33
+ s.rdoc_options = ["--charset=UTF-8"]
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = %q{1.3.5}
36
+ s.summary = %q{Library for creating snippets of html.}
37
+ s.test_files = [
38
+ "test/helper.rb",
39
+ "test/iq/html_test.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_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
48
+ s.add_development_dependency(%q<yard>, [">= 0"])
49
+ else
50
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
51
+ s.add_dependency(%q<yard>, [">= 0"])
52
+ end
53
+ else
54
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
55
+ s.add_dependency(%q<yard>, [">= 0"])
56
+ end
57
+ end
58
+
data/lib/iq/html.rb ADDED
@@ -0,0 +1,102 @@
1
+ module IQ
2
+ module HTML
3
+ HTML_ESCAPE = { '&' => '&amp;', '>' => '&gt;', '<' => '&lt;', '"' => '&quot;' }.freeze
4
+
5
+ # Returns HTML escaped version of supplied string.
6
+ #
7
+ # @example
8
+ # IQ::HTML.escape('Letters, Num83r5, & -- &amp; > -- &gt; < -- &lt; " -- &quot; &#1234;')
9
+ # #=> 'Letters, Num83r5, &amp; -- &amp;amp; &gt; -- &amp;gt; &lt; -- &amp;lt; &quot; -- &amp;quot; &amp;#1234;'
10
+ #
11
+ # @param [String]
12
+ # @return [String] the escaped string
13
+ def self.escape(value)
14
+ raise ArgumentError, 'Must supply a string' unless value.is_a?(String)
15
+ value.gsub(/[&"><]/) { |special| HTML_ESCAPE[special] }
16
+ end
17
+
18
+ # Returns HTML escaped version of supplied string leaving any existing
19
+ # entities intact.
20
+ #
21
+ # @example
22
+ # IQ::HTML.escape_once('Letters, Num83r5, & -- &amp; > -- &gt; < -- &lt; " -- &quot; &#1234;')
23
+ # #=> 'Letters, Num83r5, &amp; -- &amp; &gt; -- &gt; &lt; -- &lt; &quot; -- &quot; &#1234;'
24
+ #
25
+ # @param [String]
26
+ # @return [String] the escaped string (with existing entities intact)
27
+ def self.escape_once(value)
28
+ raise ArgumentError, 'Must supply a string' unless value.is_a?(String)
29
+ value.gsub(/[\"><]|&(?!([a-zA-Z]+|(#\d+));)/) { |special| HTML_ESCAPE[special] }
30
+ end
31
+
32
+ # Takes a string and returns a new hyphenated string that can safely be
33
+ # used as a dom id.
34
+ #
35
+ # @example
36
+ # IQ::HTML.sanitize_as_dom_id('product[variants][0][stock]')
37
+ # #=> 'product-variants-0-stock'
38
+ #
39
+ # @param [String]
40
+ # @return [String] the escaped string (leaving existing entities intact)
41
+ def self.sanitize_as_dom_id(string_to_sanitize)
42
+ raise ArgumentError, 'Argument must be a string' unless string_to_sanitize.is_a?(String)
43
+
44
+ # see http://www.w3.org/TR/html4/types.html#type-name
45
+ string_to_sanitize.to_s.gsub(']','').gsub(/[^-a-zA-Z0-9:.]/, "-")
46
+ end
47
+
48
+ # Helper method for creating HTML tags of a specified name, along with
49
+ # optional content and list of attributes. All attribute values and content
50
+ # will be escaped, however content escaping may be dissabled by supplying
51
+ # +false+ as the last argument.
52
+ #
53
+ # @example
54
+ # IQ::HTML.tag('br') #=> "<br />"
55
+ # IQ::HTML.tag('strong', 'Bill & Ben') #=> "<strong>Bill &amp; Ben</strong>"
56
+ # IQ::HTML.tag('strong', 'Bill & Ben', false) #=> "<strong>Bill & Ben</strong>"
57
+ # IQ::HTML.tag('strong', 'B&B', { :id => 'bb' }, false) #=> "<strong id="bb">Bill & Ben</strong>"
58
+ # IQ::HTML.tag('input', :title => 'B&B') #=> '<input type="text" title="B&amp;B" />'
59
+ # IQ::HTML.tag('strong', 'B&B', :title => 'Bill & Ben') #=> "<strong title="Bill &amp; Ben">B&amp;B</strong>"
60
+ #
61
+ # @param [String, Symbol] name
62
+ # @param [String] content
63
+ # @param [Hash] attributes
64
+ # @param [true, false] escape
65
+ #
66
+ # @return [String]
67
+ def self.tag(name, *args)
68
+ raise ArgumentError, 'Name must be a symbol or string' unless name.is_a?(Symbol) || name.is_a?(String)
69
+
70
+ raise ArgumentError, 'Too many arguments' if args.size > 3
71
+ case args.size
72
+ when 3 then content, attributes, escape = *args
73
+ when 2
74
+ case args.last
75
+ when Hash then escape, content, attributes = true, *args
76
+ when true, false then content, escape = *args
77
+ else
78
+ raise ArgumentError, 'Third argument must be an attribute hash or boolean escape value'
79
+ end
80
+ when 1
81
+ case args.last
82
+ when String then escape, content = true, *args
83
+ when Hash then attributes = *args
84
+ else
85
+ raise ArgumentError, 'Second argument must be a content string or an attributes hash'
86
+ end
87
+ end
88
+
89
+ raise ArgumentError, 'Content must be in the form of a string' unless content.nil? || content.is_a?(String)
90
+ raise ArgumentError, 'Attributes must be in the form of a hash' unless attributes.nil? || attributes.is_a?(Hash)
91
+ raise ArgumentError, 'Escape argument must be a boolean' unless escape.nil? || escape == true || escape == false
92
+ raise ArgumentError, 'Escape option supplied, but no content to escape' if escape && content.nil?
93
+
94
+ tag = "<#{name}"
95
+ if attributes
96
+ attributes.reject! { |key, value| value.nil? }
97
+ tag << attributes.map { |key, value| %( #{key}="#{escape(value.to_s)}") }.sort.join
98
+ end
99
+ tag << (content ? ">#{escape ? escape(content) : content}</#{name}>" : ' />')
100
+ end
101
+ end
102
+ end
data/lib/iq-html.rb ADDED
@@ -0,0 +1 @@
1
+ require 'iq/html'
data/test/helper.rb ADDED
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require 'mocha'
5
+
6
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
8
+ require 'iq/html'
9
+
10
+ class Test::Unit::TestCase
11
+ end
@@ -0,0 +1,248 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'helper')
2
+
3
+ class IQ::HTMLTest < Test::Unit::TestCase
4
+ context "escape" do
5
+ should "respond" do
6
+ assert_respond_to IQ::HTML, :escape
7
+ end
8
+
9
+ should "raise when supplied argument is not a string" do
10
+ not_a_string = stub_everything
11
+ not_a_string.stubs(:is_a?).with(String).returns(false)
12
+ assert_raise(ArgumentError) do
13
+ IQ::HTML.escape(not_a_string)
14
+ end
15
+ end
16
+
17
+ should "return value escaped with html entities" do
18
+ assert_equal(
19
+ 'Letters, Num83r5, &amp; -- &amp;amp; &gt; -- &amp;gt; &lt; -- &amp;lt; &quot; -- &amp;quot; &amp;#1234;',
20
+ IQ::HTML.escape('Letters, Num83r5, & -- &amp; > -- &gt; < -- &lt; " -- &quot; &#1234;')
21
+ )
22
+ end
23
+ end
24
+
25
+ context "escape_once" do
26
+ should "escape respond" do
27
+ assert_respond_to IQ::HTML, :escape_once
28
+ end
29
+
30
+ should "raise when supplied argument is not a string" do
31
+ not_a_string = stub_everything
32
+ not_a_string.stubs(:is_a?).with(String).returns(false)
33
+ assert_raise(ArgumentError) { IQ::HTML.escape_once(not_a_string) }
34
+ end
35
+
36
+ should "return value escaped with html entities ignoring already escaped entities" do
37
+ assert_equal(
38
+ 'Letters, Num83r5, &amp; -- &amp; &gt; -- &gt; &lt; -- &lt; &quot; -- &quot; &#1234;',
39
+ IQ::HTML.escape_once('Letters, Num83r5, & -- &amp; > -- &gt; < -- &lt; " -- &quot; &#1234;')
40
+ )
41
+ end
42
+ end
43
+
44
+ context "sanitize_as_dom_id" do
45
+ should "respond" do
46
+ assert_respond_to IQ::HTML, :sanitize_as_dom_id
47
+ end
48
+
49
+ should "accept name argument" do
50
+ assert_nothing_raised(ArgumentError) do
51
+ IQ::HTML.sanitize_as_dom_id('the string')
52
+ end
53
+ end
54
+
55
+ should "raise when name is not a string" do
56
+ not_a_string = stub_everything
57
+ not_a_string.stubs(:is_a?).with(String).returns(false)
58
+ assert_raise(ArgumentError) do
59
+ IQ::HTML.sanitize_as_dom_id(not_a_string)
60
+ end
61
+ end
62
+
63
+ should "return sanitized name" do
64
+ assert_equal(
65
+ '----LeTTers:--Num83r--B0-7h-Plu5.AnoTh3r',
66
+ IQ::HTML.sanitize_as_dom_id('$&% LeTTers: Num83r$[B0-7h][Plu5.AnoTh3r]')
67
+ )
68
+ end
69
+ end
70
+
71
+ context "tag" do
72
+ should "respond" do
73
+ assert_respond_to IQ::HTML, :tag
74
+ end
75
+
76
+ should "raise when there are too many arguments" do
77
+ assert_raise(ArgumentError) do
78
+ IQ::HTML.tag('strong', 'Hello', { :title => 'Howdy' }, false, stub)
79
+ end
80
+ end
81
+
82
+ should "raise when name is not a string or a symbol" do
83
+ not_a_string_or_symbol = stub
84
+ not_a_string_or_symbol.stubs(:is_a?).with(String).returns(false)
85
+ not_a_string_or_symbol.stubs(:is_a?).with(Symbol).returns(false)
86
+ assert_raise(ArgumentError) do
87
+ IQ::HTML.tag(not_a_string_or_symbol)
88
+ end
89
+ end
90
+
91
+ context "with 2 argument usage" do
92
+ should "raise when 2nd argument is not a string or hash" do
93
+ not_a_string_or_hash = stub
94
+ not_a_string_or_hash.stubs(:is_a?).with(String).returns(false)
95
+ not_a_string_or_hash.stubs(:is_a?).with(Hash).returns(false)
96
+ assert_raise(ArgumentError) { IQ::HTML.tag('the_name', not_a_string_or_hash) }
97
+ end
98
+ end
99
+
100
+ context "with 3 argument usage" do
101
+ should "raise when 2nd argument is not a string or hash" do
102
+ not_a_string_or_hash = stub
103
+ not_a_string_or_hash.stubs(:is_a?).with(String).returns(false)
104
+ not_a_string_or_hash.stubs(:is_a?).with(Hash).returns(false)
105
+ assert_raise(ArgumentError) { IQ::HTML.tag('the_name', not_a_string_or_hash, stub_everything) }
106
+ end
107
+
108
+ should "raise when 2nd argument is a string and 3rd argument is not a hash or boolean" do
109
+ not_a_string_or_boolean = stub
110
+ not_a_string_or_boolean.stubs(:is_a?).with(String).returns(false)
111
+ not_a_string_or_boolean.stubs(:==).with(true).returns(false)
112
+ not_a_string_or_boolean.stubs(:==).with(false).returns(false)
113
+ assert_raise(ArgumentError) { IQ::HTML.tag('the_name', 'the content', not_a_string_or_boolean) }
114
+ end
115
+
116
+ should "raise when 2nd argument is a hash and 3rd argument supplied" do
117
+ assert_raise(ArgumentError) { IQ::HTML.tag('the_name', { :title => 'The title' }, stub_everything) }
118
+ end
119
+
120
+ should "raise when setting escape to true but not supplying content" do
121
+ assert_raise(ArgumentError) { IQ::HTML.tag('the_name', { :title => 'The title' }, true) }
122
+ end
123
+ end
124
+
125
+ context "with 4 argument usage" do
126
+ should "raise when 2nd argument is not a string" do
127
+ not_a_string = stub
128
+ not_a_string.stubs(:is_a?).with(String).returns(false)
129
+ assert_raise(ArgumentError) { IQ::HTML.tag('the_name', not_a_string, { :title => 'Title' }, true) }
130
+ end
131
+
132
+ should "raise when 3rd argument is not a hash" do
133
+ not_a_hash = stub
134
+ not_a_hash.stubs(:is_a?).with(Hash).returns(false)
135
+ assert_raise(ArgumentError) { IQ::HTML.tag('the_name', 'the content', not_a_hash, true) }
136
+ end
137
+
138
+ should "raise when 4th argument is not a boolean" do
139
+ not_a_boolean = stub
140
+ not_a_boolean.stubs(:===).with(true).returns(false)
141
+ not_a_boolean.stubs(:===).with(false).returns(false)
142
+ assert_raise(ArgumentError) { IQ::HTML.tag('the_name', 'the content', { :title => 'Title' }, not_a_boolean) }
143
+ end
144
+ end
145
+
146
+ should "return self closing tag of specified name" do
147
+ assert_equal '<the_tag />', IQ::HTML.tag('the_tag')
148
+ end
149
+
150
+ should "return self closing tag of specified name with attributes when attributes supplied" do
151
+ assert_equal(
152
+ '<the_tag id="foo" name="bar" />',
153
+ IQ::HTML.tag('the_tag', :id => 'foo', :name => 'bar')
154
+ )
155
+ end
156
+
157
+ should "return content tag of specified name" do
158
+ assert_equal '<the_tag>the content</the_tag>',
159
+ IQ::HTML.tag('the_tag', 'the content')
160
+ end
161
+
162
+ should "return content tag of specified name with attributes when content and attribute options supplied" do
163
+ assert_equal(
164
+ '<the_tag for="bar" value="foo">the content</the_tag>',
165
+ IQ::HTML.tag('the_tag', 'the content', :value => 'foo', :for => 'bar')
166
+ )
167
+ end
168
+
169
+ should "apply attributes in alphabetical order" do
170
+ assert_equal(
171
+ '<the_tag a="foo" b="bar" c="baz" d="yum" />',
172
+ IQ::HTML.tag('the_tag', :a => 'foo', :b => 'bar', :c => 'baz', :d => 'yum')
173
+ )
174
+ end
175
+
176
+ should "escape attributes in self closing tag by default" do
177
+ IQ::HTML.stubs(:escape).with('the value').returns('the value [escaped]')
178
+ assert_equal(
179
+ '<the_tag value="the value [escaped]" />',
180
+ IQ::HTML.tag('the_tag', :value => 'the value')
181
+ )
182
+ end
183
+
184
+ should "escape attributes in content tag by default" do
185
+ IQ::HTML.stubs(:escape).with('the content').returns('the content [escaped]')
186
+ IQ::HTML.stubs(:escape).with('the value').returns('the value [escaped]')
187
+ assert_equal(
188
+ '<the_tag value="the value [escaped]">the content [escaped]</the_tag>',
189
+ IQ::HTML.tag('the_tag', 'the content', :value => 'the value')
190
+ )
191
+ end
192
+
193
+ should "escape attributes in content tag even when escape turned off for content" do
194
+ IQ::HTML.stubs(:escape).with('the value').returns('the value [escaped]')
195
+ assert_equal(
196
+ '<the_tag value="the value [escaped]">the content</the_tag>',
197
+ IQ::HTML.tag('the_tag', 'the content', { :value => 'the value' }, false)
198
+ )
199
+ end
200
+
201
+ should "ignore attributes with nil values in self closing tag" do
202
+ assert_equal(
203
+ '<the_tag value="not nil" />',
204
+ IQ::HTML.tag('the_tag', :value => 'not nil', :foo => nil, :bar => nil)
205
+ )
206
+ end
207
+
208
+ should "ignore attributes with nil values in content tag" do
209
+ assert_equal(
210
+ '<the_tag value="not nil">the content</the_tag>',
211
+ IQ::HTML.tag('the_tag', 'the content', :value => 'not nil', :foo => nil, :bar => nil)
212
+ )
213
+ end
214
+
215
+ should "stringify attributes in self closing tag" do
216
+ assert_equal(
217
+ '<the_tag name="321" value="a_symbol" />',
218
+ IQ::HTML.tag('the_tag', :name => 321, :value => :a_symbol)
219
+ )
220
+ end
221
+
222
+ should "stringify attributes in content tag" do
223
+ assert_equal(
224
+ '<the_tag name="321" value="a_symbol">the content</the_tag>',
225
+ IQ::HTML.tag('the_tag', 'the content', :name => 321, :value => :a_symbol)
226
+ )
227
+ end
228
+
229
+ should "escape content by default" do
230
+ IQ::HTML.stubs(:escape).with('the content').returns('the content [escaped]')
231
+ assert_equal('<the_tag>the content [escaped]</the_tag>', IQ::HTML.tag('the_tag', 'the content'))
232
+ end
233
+
234
+ should "escape content when escape option is true" do
235
+ IQ::HTML.stubs(:escape).with('the content').returns('the content [escaped]')
236
+ assert_equal(
237
+ '<the_tag>the content [escaped]</the_tag>', IQ::HTML.tag('the_tag', 'the content', true)
238
+ )
239
+ end
240
+
241
+ should "not escape content when escape option is false" do
242
+ IQ::HTML.stubs(:escape).with('the content').returns('the content [escaped]')
243
+ assert_equal(
244
+ '<the_tag>the content</the_tag>', IQ::HTML.tag('the_tag', 'the content', false)
245
+ )
246
+ end
247
+ end
248
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: iq-html
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jamie Hill
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-13 00:00:00 +00:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: thoughtbot-shoulda
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: yard
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ description: Library containing a series of helper methods for creating html markup including the escaping of strings.
36
+ email: jamie@soniciq.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - LICENSE
43
+ - README.rdoc
44
+ files:
45
+ - .document
46
+ - .gitignore
47
+ - LICENSE
48
+ - README.rdoc
49
+ - Rakefile
50
+ - VERSION
51
+ - iq-html.gemspec
52
+ - lib/iq-html.rb
53
+ - lib/iq/html.rb
54
+ - test/helper.rb
55
+ - test/iq/html_test.rb
56
+ has_rdoc: true
57
+ homepage: http://github.com/soniciq/iq-html
58
+ licenses: []
59
+
60
+ post_install_message:
61
+ rdoc_options:
62
+ - --charset=UTF-8
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: "0"
70
+ version:
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: "0"
76
+ version:
77
+ requirements: []
78
+
79
+ rubyforge_project:
80
+ rubygems_version: 1.3.5
81
+ signing_key:
82
+ specification_version: 3
83
+ summary: Library for creating snippets of html.
84
+ test_files:
85
+ - test/helper.rb
86
+ - test/iq/html_test.rb