Skip to content

Commit

Permalink
Issue mvz#53: Allow to specify :enconding as a #parse option, when Ha…
Browse files Browse the repository at this point in the history
…ppyMapper is included within a class
  • Loading branch information
amseledka committed Mar 21, 2022
1 parent 662049f commit 314dc71
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
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]
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

0 comments on commit 314dc71

Please sign in to comment.