xmlmapper 0.7.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 92105182b50fec5c2c9b4d00664c6807b2c51d01
4
- data.tar.gz: 8721d8e5db3c52d269db18d592ad2742e72199e2
2
+ SHA256:
3
+ metadata.gz: 6f441ea6ddce1f32eefcb965b6c4e3188ba70217a363433044b425ee4ba5aaf7
4
+ data.tar.gz: 0ebf4f7fd0919509aa7fa12488ac17c66322fd6c53c4b8c541087bb50c190501
5
5
  SHA512:
6
- metadata.gz: 8d06c8b61063ca2234f4a720e7b19c14f4076a0befc7448f26b120647a3281aec2ed395f9f3a8e91fa221a13409ef3af1b15edc83fad407832d9929c0836ca84
7
- data.tar.gz: b6a4763251048f38892b364579678e9367dc6dda150539b3f88f2c4fc064b4e0778bfad40c73d0aabca7a34408c7e1c1361b3e0be04898b12e21f233285dd4fe
6
+ metadata.gz: 3ee1dad25a3fb5654c6a49d0c4d34fc466f65968c1b123b5eeb77007097d0b7ceaa47fa78c896f7f65f2d91bab3c97c1448bf9aa93edfc162388940b5ff5633a
7
+ data.tar.gz: af8e018ec9910204b0465113f886e8f2c900e822058227e8841ab2b970984d46b8d0943eed6bad643dda635ef1fb7d0c8c43ac04030ab8a9977a1c6a9ff9fe33
data/lib/xmlmapper.rb CHANGED
@@ -437,8 +437,8 @@ module XmlMapper
437
437
  if obj.respond_to?('xml_value=')
438
438
  # n.namespaces.each {|name,path| n[name] = path }
439
439
 
440
- # obj.xml_value = n.canonicalize(Nokogiri::XML::XML_C14N_EXCLUSIVE_1_0, n.document.collect_namespaces.keys.map { |name| name.split(":").last })
441
- obj.xml_value = n.to_xml(save_with: Nokogiri::XML::Node::SaveOptions::AS_XML | Nokogiri::XML::Node::SaveOptions::NO_DECLARATION)
440
+ obj.xml_value = n.canonicalize(Nokogiri::XML::XML_C14N_EXCLUSIVE_1_0, n.document.collect_namespaces.keys.map { |name| name.split(":").last })
441
+ # obj.xml_value = n.to_xml(save_with: Nokogiri::XML::Node::SaveOptions::AS_XML | Nokogiri::XML::Node::SaveOptions::NO_DECLARATION)
442
442
  end
443
443
 
444
444
  if obj.respond_to?('xml_node=')
@@ -687,7 +687,34 @@ module XmlMapper
687
687
  # an empty element will be written to the xml
688
688
  #
689
689
  if value.nil? && element.options[:single] && element.options[:state_when_nil]
690
- xml.send("#{tag}_","")
690
+ #
691
+ # NOTE
692
+ # In JRuby 9.0.4.0+ and Nokogiri version 1.6.8 or with Nokogiri version >= 1.12.0 (libxml >= 2.9.12),
693
+ # the Nokogiri::XML::Builder::NodeBuilder does not retain the XML namespace prefix for an element
694
+ # when adding an element to a parent node.
695
+ #
696
+ # The namespace prefix must be specified when adding the node to its parent.
697
+ # This issue manifests when setting an element's :state_when_nil' option to true.
698
+ #
699
+ # This workaround is intended for XML element prefixes that originate from a
700
+ # single namespace defined in 'registered_namespaces'. If there are
701
+ # multiple namespaces defined in the 'registered_namespaces' array,
702
+ # then the first namespace is selected.
703
+ #
704
+ # Possible related open issues in Nokogiri:
705
+ # 1. Nokogiri under jruby fails to create namespaces named the same as a sibling
706
+ # https://github.com/sparklemotion/nokogiri/issues/1247
707
+ # 2. Attribute loses namespace when node moved
708
+ # https://github.com/sparklemotion/nokogiri/issues/1278
709
+ # 3. Adding namespace-less node to namespaced parent attaches the parent namespace to the child
710
+ # https://github.com/sparklemotion/nokogiri/issues/425
711
+ #
712
+ if (RUBY_ENGINE == 'jruby' || Nokogiri.uses_libxml?('>= 2.9.12')) && !registered_namespaces.empty?
713
+ ns = registered_namespaces.keys.first.to_sym
714
+ xml[ns].send("#{tag}_","")
715
+ else
716
+ xml.send("#{tag}_","")
717
+ end
691
718
  end
692
719
 
693
720
  #
@@ -2,7 +2,11 @@ module XmlMapper
2
2
  class TextNode < Item
3
3
 
4
4
  def find(node, namespace, xpath_options)
5
- yield(node.children.detect{|c| c.text?})
5
+ if node.children.any? {|c| c.text?}
6
+ yield(node)
7
+ else
8
+ yield(nil)
9
+ end
6
10
  end
7
11
  end
8
12
  end
@@ -1,3 +1,3 @@
1
1
  module XmlMapper
2
- VERSION = "0.7.0"
2
+ VERSION = '0.8.0'.freeze
3
3
  end
@@ -0,0 +1,2 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <textnode>With <!----> Comment</textnode>
@@ -138,8 +138,15 @@ describe "Saving #to_xml", "with xml namespaces" do
138
138
  end
139
139
 
140
140
  context "when an element has a 'state_when_nil' parameter" do
141
+ let(:element) { subject.xpath('address:description').first }
142
+
141
143
  it "saves an empty element" do
142
- expect(subject.xpath('address:description').text).to eq ""
144
+ expect(element.text).to eq ""
145
+ end
146
+
147
+ it "the empty element has the correct namespace prefix from the array of registered namespaces" do
148
+ ns_scopes = element.namespace_scopes
149
+ expect(ns_scopes.select { |ns| ns == element.namespace }.first.prefix).to eq "address"
143
150
  end
144
151
  end
145
152
 
@@ -194,10 +201,27 @@ describe "Saving #to_xml", "with xml namespaces" do
194
201
  end
195
202
  end
196
203
 
204
+ # NOTE While the original implementation of the test below is correct, the order of XML namespaces
205
+ # in the XML generated by 'to_xml' differs under JRuby and MRI. In terms of information content,
206
+ # the XML produced under both platforms are identical. However, the difference in namespace ordering
207
+ # will produce different XML documents and consequently XML digests. The test below and various tests
208
+ # in the 'libsaml' gem will compare XML documents and digests of XML documents generated by 'to_xml' which,
209
+ # while informationally correct, have different values. Unfortunately, the cause of this behavior has
210
+ # not been identified. In the meantime, it is important to explictly call out this behavior.
211
+
197
212
  context 'namespace supplied by element declaration trumps namespace ' \
198
213
  'specified by element class' do
199
214
 
200
- let(:expected_xml) do
215
+ let(:expected_xml_jruby) do
216
+ <<-XML.gsub(/^\s*\|/, '')
217
+ |<?xml version="1.0"?>
218
+ |<coffeemachine xmlns:beverage="http://beverages.org/Beverage/0.1" xmlns:coffee="http://coffee.org/Coffee/0.1">
219
+ | <beverage:beverage name="coffee"/>
220
+ |</coffeemachine>
221
+ XML
222
+ end
223
+
224
+ let(:expected_xml_mri) do
201
225
  <<-XML.gsub(/^\s*\|/, '')
202
226
  |<?xml version="1.0"?>
203
227
  |<coffeemachine xmlns:coffee="http://coffee.org/Coffee/0.1" xmlns:beverage="http://beverages.org/Beverage/0.1">
@@ -224,7 +248,11 @@ describe "Saving #to_xml", "with xml namespaces" do
224
248
  it 'uses the element declaration namespace on the element' do
225
249
  machine = CoffeeMachine.new
226
250
  machine.beverage = Beverage.new.tap {|obj| obj.name = 'coffee'}
227
- machine.to_xml.should be == expected_xml
251
+ if RUBY_ENGINE == 'jruby'
252
+ machine.to_xml.should be == expected_xml_jruby
253
+ else
254
+ machine.to_xml.should be == expected_xml_mri
255
+ end
228
256
  end
229
257
  end
230
258
 
@@ -554,7 +554,6 @@ end
554
554
  class DefaultNamespaceCombi
555
555
  include XmlMapper
556
556
 
557
-
558
557
  register_namespace 'bk', "urn:loc.gov:books"
559
558
  register_namespace 'isbn', "urn:ISBN:0-395-36341-6"
560
559
  register_namespace 'p', "urn:loc.gov:people"
@@ -567,6 +566,14 @@ class DefaultNamespaceCombi
567
566
  element :author, String, :namespace => 'p', :tag => "author"
568
567
  end
569
568
 
569
+ class TextNodeWithComment
570
+ include XmlMapper
571
+
572
+ tag 'textnode'
573
+
574
+ content :value, String
575
+ end
576
+
570
577
  describe XmlMapper do
571
578
 
572
579
  describe "being included into another class" do
@@ -1134,4 +1141,13 @@ describe XmlMapper do
1134
1141
  end
1135
1142
  end
1136
1143
 
1144
+ context 'text_node with comments' do
1145
+ it 'returns the full text in the node' do
1146
+ xml = fixture_file('text_node_with_comment.xml')
1147
+ text_node = TextNodeWithComment.parse(xml, single: true)
1148
+
1149
+ expect(text_node.value).to eq "With Comment"
1150
+ end
1151
+ end
1152
+
1137
1153
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xmlmapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Damien Le Berrigaud
@@ -90,6 +90,7 @@ files:
90
90
  - spec/fixtures/set_config_options.xml
91
91
  - spec/fixtures/statuses.xml
92
92
  - spec/fixtures/subclass_namespace.xml
93
+ - spec/fixtures/text_node_with_comment.xml
93
94
  - spec/fixtures/unformatted_address.xml
94
95
  - spec/fixtures/wrapper.xml
95
96
  - spec/has_many_empty_array_spec.rb
@@ -128,8 +129,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
128
129
  - !ruby/object:Gem::Version
129
130
  version: '0'
130
131
  requirements: []
131
- rubyforge_project:
132
- rubygems_version: 2.5.1
132
+ rubygems_version: 3.1.4
133
133
  signing_key:
134
134
  specification_version: 3
135
135
  summary: Provides a simple way to map XML to Ruby Objects and back again.
@@ -162,6 +162,7 @@ test_files:
162
162
  - spec/fixtures/set_config_options.xml
163
163
  - spec/fixtures/statuses.xml
164
164
  - spec/fixtures/subclass_namespace.xml
165
+ - spec/fixtures/text_node_with_comment.xml
165
166
  - spec/fixtures/unformatted_address.xml
166
167
  - spec/fixtures/wrapper.xml
167
168
  - spec/has_many_empty_array_spec.rb