From 314dc716faf6580f2f96838d8fa4997ea5397413 Mon Sep 17 00:00:00 2001 From: Daria Grishchenko Date: Mon, 21 Mar 2022 12:58:04 +0200 Subject: [PATCH] Issue #53: Allow to specify :enconding as a #parse option, when HappyMapper is included within a class --- lib/happymapper.rb | 4 +++- spec/fixtures/address.xml | 2 +- spec/fixtures/optional_attributes.xml | 2 +- spec/happymapper_spec.rb | 8 ++++++++ 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/happymapper.rb b/lib/happymapper.rb index 0f5c75eb..754a4114 100644 --- a/lib/happymapper.rb +++ b/lib/happymapper.rb @@ -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 @@ -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 ) diff --git a/spec/fixtures/address.xml b/spec/fixtures/address.xml index 1460877f..29077133 100644 --- a/spec/fixtures/address.xml +++ b/spec/fixtures/address.xml @@ -6,4 +6,4 @@ Oldenburg Germany Lower Saxony - \ No newline at end of file + diff --git a/spec/fixtures/optional_attributes.xml b/spec/fixtures/optional_attributes.xml index 860d305d..dc895aab 100644 --- a/spec/fixtures/optional_attributes.xml +++ b/spec/fixtures/optional_attributes.xml @@ -3,4 +3,4 @@
- \ No newline at end of file + diff --git a/spec/happymapper_spec.rb b/spec/happymapper_spec.rb index e97c6c02..0ec26e1c 100644 --- a/spec/happymapper_spec.rb +++ b/spec/happymapper_spec.rb @@ -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 = %(
Milchstrasse ÄÖÜ23
) + address = Address.parse(xml, encoding: 'UTF-8') + expect(address.street).to eq('Milchstrasse ÄÖÜ') + end + end end