Skip to content

Commit

Permalink
Merge branch 'fix_issue_33' of https://github.com/hderms/builder into…
Browse files Browse the repository at this point in the history
… nil

* 'fix_issue_33' of https://github.com/hderms/builder:
  Update lib/builder/xmlbase.rb
  Squashed commit of the following:
  • Loading branch information
jimweirich committed Feb 6, 2013
2 parents 9bc4568 + 76d389d commit 183d740
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/builder/xmlbase.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ def initialize(indent=0, initial=0, encoding='utf-8')
@level = initial
@encoding = encoding.downcase
end
def explicit_nil_handling?
if @explicit_nil_handling == true
true
else
false
end
end

# Create a tag named +sym+. Other than the first argument which
# is the tag name, the arguments are the same as the tags
Expand All @@ -45,7 +52,8 @@ def tag!(sym, *args, &block)
attrs ||= {}
attrs.merge!(arg)
when nil
# do nothing
attrs ||= {}
attrs.merge!({nil: true}) if explicit_nil_handling?
else
text ||= ''
text << arg.to_s
Expand Down
1 change: 1 addition & 0 deletions lib/builder/xmlmarkup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ class XmlMarkup < XmlBase
def initialize(options={})
indent = options[:indent] || 0
margin = options[:margin] || 0
@explicit_nil_handling = options[:explicit_nil_handling] || false
super(indent, margin)
@target = options[:target] || ""
end
Expand Down
16 changes: 16 additions & 0 deletions test/test_markupbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,22 @@ def test_escaping
@xml.div { |x| x.text! "<hi>"; x.em("H&R Block") }
assert_equal %{<div>&lt;hi&gt;<em>H&amp;R Block</em></div>}, @xml.target!
end
def test_explicit_nil_handling
b = Builder::XmlMarkup.new explicit_nil_handling: true
b.tag! "foo", nil
assert_equal %{<foo nil="true"/>}, b.target!

b = Builder::XmlMarkup.new explicit_nil_handling: true
b.div("ns:xml"=>:"&xml;") { |x| x << "<h&i>"; x.em("H&R Block") }
assert_equal %{<div ns:xml="&xml;"><h&i><em>H&amp;R Block</em></div>}, b.target!

b = Builder::XmlMarkup.new explicit_nil_handling: true
n = 3
b.ol { |x| n.times { x.li(n) } }
assert_equal "<ol><li>3</li><li>3</li><li>3</li></ol>", b.target!


end

def test_non_escaping
@xml.div("ns:xml"=>:"&xml;") { |x| x << "<h&i>"; x.em("H&R Block") }
Expand Down

0 comments on commit 183d740

Please sign in to comment.