Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add XML.build_fragment #8813

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions spec/std/xml/xml_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -373,4 +373,25 @@ describe XML do
res = root.delete("biz")
res.should be_nil
end

it ".build" do
XML.build do |builder|
builder.element "foo" { }
end.should eq %[<?xml version="1.0"?>\n<foo/>\n]
end

describe ".build_fragment" do
it "builds fragment without XML declaration" do
XML.build_fragment do |builder|
builder.element "foo" { }
end.should eq %[<foo/>\n]
end

it "closes open elements" do
XML.build_fragment do |builder|
builder.start_element "foo"
builder.start_element "bar"
end.should eq %[<foo><bar/></foo>\n]
end
end
end
49 changes: 45 additions & 4 deletions src/xml/builder.cr
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,8 @@ end
module XML
# Returns the resulting `String` of writing XML to the yielded `XML::Builder`.
#
# Builds an XML document (see `#document`) including XML declaration (`<?xml?>`).
#
# ```
# require "xml"
#
Expand All @@ -320,14 +322,53 @@ module XML
end
end

# Writes XML into the given `IO`. An `XML::Builder` is yielded to the block.
# Returns the resulting `String` of writing XML to the yielded `XML::Builder`.
#
# Builds an XML fragment without XML declaration (`<?xml?>`).
#
# ```
# require "xml"
#
# string = XML.build_fragment(indent: " ") do |xml|
# xml.element("person", id: 1) do
# xml.element("firstname") { xml.text "Jane" }
# xml.element("lastname") { xml.text "Doe" }
# end
# end
#
# string # => "<person id=\"1\">\n <firstname>Jane</firstname>\n <lastname>Doe</lastname>\n</person>\n"
# ```
def self.build_fragment(*, indent = nil, quote_char = nil)
String.build do |str|
build_fragment(str, indent: indent, quote_char: quote_char) do |xml|
yield xml
end
end
end

# Writes XML document into the given `IO`. An `XML::Builder` is yielded to the block.
#
# Builds an XML document (see `#document`) including XML declaration (`<?xml?>`).
def self.build(io : IO, version : String? = nil, encoding : String? = nil, indent = nil, quote_char = nil)
build_fragment(io, indent: indent, quote_char: quote_char) do |xml|
xml.start_document version, encoding
yield xml
# omit end_document because it is called in build_fragment
end
end

# Writes XML fragment into the given `IO`. An `XML::Builder` is yielded to the block.
#
# Builds an XML fragment without XML declaration (`<?xml?>`).
def self.build_fragment(io : IO, *, indent = nil, quote_char = nil)
xml = XML::Builder.new(io)
xml.indent = indent if indent
xml.quote_char = quote_char if quote_char
v = xml.document(version, encoding) do
yield xml
end
v = yield xml

# EndDocument is still necessary to ensure all all elements are closed, even
# when StartDocument is omitted.
xml.end_document
xml.flush
v
end
Expand Down