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

Issue #53: Allow to specify :enconding as a #parse option, when Happy… #202

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 3 additions & 1 deletion lib/happymapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ def with_nokogiri_config(&blk)
# to retuning an array of multiple items.
# :xpath information where to start the parsing
# :namespace is the namespace to use for additional information.
# :encoding explicitly set the encoding to Nokogiri::XML document
#
def parse(xml, options = {})
# Capture any provided namespaces and merge in any namespaces that have
Expand All @@ -309,8 +310,9 @@ def parse(xml, options = {})
unless xml.is_a?(Nokogiri::XML::Document)
# Attempt to parse the xml value with Nokogiri XML as a document
# and select the root element
encoding = options[:encoding]
amseledka marked this conversation as resolved.
Show resolved Hide resolved
xml = Nokogiri::XML(
xml, nil, nil,
xml, nil, encoding,
Nokogiri::XML::ParseOptions::STRICT,
&nokogiri_config_callback
)
Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/address.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
<city>Oldenburg</city>
<country code="de">Germany</country>
<state>Lower Saxony</state>
</address>
</address>
2 changes: 1 addition & 1 deletion spec/fixtures/optional_attributes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
<address street=""/>
<address street="Milchstrasse"/>
<address />
</addresses>
</addresses>
8 changes: 8 additions & 0 deletions spec/happymapper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1208,4 +1208,12 @@ class Thing
end
end
end

context 'encoding' do
it 'allows passing :encoding option to fix special characters when parsing xml' do
xml = %(<address><street>Milchstrasse ÄÖÜ</street><housenumber>23</housenumber></address>)
address = Address.parse(xml, encoding: 'UTF-8')
expect(address.street).to eq('Milchstrasse ÄÖÜ')
end
end
end