niceogiri 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/niceogiri/version.rb +1 -1
- data/lib/niceogiri/xml/node.rb +3 -2
- data/spec/niceogiri/core_ext/nokogiri_spec.rb +88 -50
- data/spec/niceogiri/xml/node_spec.rb +6 -0
- metadata +47 -17
data/lib/niceogiri/version.rb
CHANGED
data/lib/niceogiri/xml/node.rb
CHANGED
@@ -65,7 +65,8 @@ module Niceogiri
|
|
65
65
|
when Nokogiri::XML::Namespace
|
66
66
|
self.nokogiri_namespace = namespaces
|
67
67
|
when String
|
68
|
-
self.add_namespace nil, namespaces
|
68
|
+
ns = self.add_namespace nil, namespaces
|
69
|
+
self.nokogiri_namespace = ns
|
69
70
|
when Hash
|
70
71
|
self.add_namespace nil, ns if ns = namespaces.delete(nil)
|
71
72
|
namespaces.each do |p, n|
|
@@ -132,7 +133,7 @@ module Niceogiri
|
|
132
133
|
# @param [XML::Node] node the node to inherit
|
133
134
|
# @return [self]
|
134
135
|
def inherit(node)
|
135
|
-
|
136
|
+
nokogiri_namespace = node.namespace
|
136
137
|
inherit_attrs node.attributes
|
137
138
|
node.children.each { |c| self << c.dup }
|
138
139
|
self
|
@@ -3,90 +3,128 @@ require 'spec_helper'
|
|
3
3
|
describe Nokogiri::XML::Node do
|
4
4
|
let(:doc) { Nokogiri::XML::Document.new }
|
5
5
|
|
6
|
+
subject { Nokogiri::XML::Node.new 'foo', doc }
|
7
|
+
|
8
|
+
before { doc.root = subject }
|
9
|
+
|
6
10
|
it 'aliases #name to #element_name' do
|
7
|
-
|
8
|
-
|
9
|
-
node.element_name.should == node.name
|
11
|
+
subject.should respond_to :element_name
|
12
|
+
subject.element_name.should == subject.name
|
10
13
|
end
|
11
14
|
|
12
15
|
it 'aliases #name= to #element_name=' do
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
node.element_name.should == 'bar'
|
16
|
+
subject.should respond_to :element_name=
|
17
|
+
subject.element_name.should == subject.name
|
18
|
+
subject.element_name = 'bar'
|
19
|
+
subject.element_name.should == 'bar'
|
18
20
|
end
|
19
21
|
|
20
22
|
it 'allows symbols as hash keys for attributes' do
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
attrs['foo'].should == 'bar'
|
25
|
-
attrs[:foo].should == 'bar'
|
23
|
+
subject['foo'] = 'bar'
|
24
|
+
subject['foo'].should == 'bar'
|
25
|
+
subject[:foo].should == 'bar'
|
26
26
|
end
|
27
27
|
|
28
28
|
it 'ensures a string is passed to the attribute setter' do
|
29
|
-
|
30
|
-
|
31
|
-
attrs[:foo].should == '1'
|
29
|
+
subject[:foo] = 1
|
30
|
+
subject[:foo].should == '1'
|
32
31
|
|
33
|
-
|
34
|
-
|
32
|
+
subject[:some_attr] = :bah
|
33
|
+
subject[:some_attr].should == 'bah'
|
35
34
|
end
|
36
35
|
|
37
36
|
it 'joins an array into a string when passed to the attribute setter' do
|
38
|
-
|
39
|
-
|
40
|
-
attrs[:foo].should == '1'
|
37
|
+
subject[:foo] = 1
|
38
|
+
subject[:foo].should == '1'
|
41
39
|
|
42
|
-
|
43
|
-
|
40
|
+
subject[:some_attr] = [:bah, :boo]
|
41
|
+
subject[:some_attr].should == 'bahboo'
|
44
42
|
end
|
45
43
|
|
46
44
|
it 'removes an attribute when set to nil' do
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
attrs['foo'] = nil
|
52
|
-
attrs['foo'].should be_nil
|
45
|
+
subject['foo'] = 'bar'
|
46
|
+
subject['foo'].should == 'bar'
|
47
|
+
subject['foo'] = nil
|
48
|
+
subject['foo'].should be_nil
|
53
49
|
end
|
54
50
|
|
55
51
|
it 'allows attribute values to change' do
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
attrs['foo'] = 'baz'
|
61
|
-
attrs['foo'].should == 'baz'
|
52
|
+
subject['foo'] = 'bar'
|
53
|
+
subject['foo'].should == 'bar'
|
54
|
+
subject['foo'] = 'baz'
|
55
|
+
subject['foo'].should == 'baz'
|
62
56
|
end
|
63
57
|
|
64
58
|
it 'allows symbols as the path in #xpath' do
|
65
|
-
|
66
|
-
|
67
|
-
doc.root = node
|
59
|
+
subject.should respond_to :find
|
60
|
+
doc.root = subject
|
68
61
|
doc.xpath(:foo).first.should_not be_nil
|
69
62
|
doc.xpath(:foo).first.should == doc.xpath('/foo').first
|
70
63
|
end
|
71
64
|
|
72
65
|
it 'allows symbols as namespace names in #xpath' do
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
node.xpath('/bar:foo', :bar => 'baz').first.should_not be_nil
|
66
|
+
subject.namespace = subject.add_namespace('bar', 'baz')
|
67
|
+
doc.root = subject
|
68
|
+
subject.xpath('/bar:foo', :bar => 'baz').first.should_not be_nil
|
77
69
|
end
|
78
70
|
|
79
71
|
it 'aliases #xpath to #find' do
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
node.find('/foo').first.should_not be_nil
|
72
|
+
subject.should respond_to :find
|
73
|
+
doc.root = subject
|
74
|
+
subject.find('/foo').first.should_not be_nil
|
84
75
|
end
|
85
76
|
|
86
77
|
it 'has a helper function #find_first' do
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
78
|
+
subject.should respond_to :find
|
79
|
+
doc.root = subject
|
80
|
+
subject.find_first('/foo').should == subject.find('/foo').first
|
81
|
+
end
|
82
|
+
|
83
|
+
describe "JRuby bugs" do
|
84
|
+
describe '#to_xml' do
|
85
|
+
context 'with a namespace on a child node' do
|
86
|
+
let(:ns_href) { 'foo' }
|
87
|
+
let(:child_node) { Nokogiri::XML::Node.new 'bar', doc }
|
88
|
+
|
89
|
+
before do
|
90
|
+
child_node.add_namespace nil, ns_href
|
91
|
+
subject << child_node
|
92
|
+
end
|
93
|
+
|
94
|
+
it 'should have the correct namespace in the rendered XML' do
|
95
|
+
p subject.to_xml
|
96
|
+
subject.to_xml.should match(/xmlns="foo"/)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
describe '#xpath' do
|
102
|
+
context 'looking for a namespaced element inside a prefixed element' do
|
103
|
+
let(:child_node) { Nokogiri::XML::Node.new 'bar', doc }
|
104
|
+
|
105
|
+
let(:outer_ns_prefix) { 'pref' }
|
106
|
+
let(:outer_ns_href) { 'outer_ns' }
|
107
|
+
let(:inner_ns_href) { 'inner_ns' }
|
108
|
+
|
109
|
+
before do
|
110
|
+
ns = subject.add_namespace outer_ns_prefix, outer_ns_href
|
111
|
+
subject.namespace = ns
|
112
|
+
child_node.add_namespace nil, inner_ns_href
|
113
|
+
subject << child_node
|
114
|
+
end
|
115
|
+
|
116
|
+
it 'should have the correct namespace' do
|
117
|
+
ns = subject.children.first.namespace
|
118
|
+
ns.should be_a Nokogiri::XML::Namespace
|
119
|
+
ns.prefix.should be == nil
|
120
|
+
ns.href.should be == inner_ns_href
|
121
|
+
end
|
122
|
+
|
123
|
+
it 'should find the element' do
|
124
|
+
x = subject.xpath("//inner_ns:bar", 'inner_ns' => inner_ns_href)
|
125
|
+
x.first.should be child_node
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
91
129
|
end
|
92
130
|
end
|
@@ -145,6 +145,12 @@ module Niceogiri
|
|
145
145
|
n2.to_s.should == n.to_s
|
146
146
|
end
|
147
147
|
|
148
|
+
it 'holds on to namespaces without a prefix when inheriting content' do
|
149
|
+
n = Nokogiri::XML.parse('<message><bar:foo xmlns="http://bar.com"></message>').root
|
150
|
+
n2 = Node.new('message').inherit n
|
151
|
+
n2.to_s.should == n.to_s
|
152
|
+
end
|
153
|
+
|
148
154
|
it 'holds on to namespaces when inheriting attributes' do
|
149
155
|
n = Nokogiri::XML.parse('<foo xml:bar="http://bar.com"/>').root
|
150
156
|
n2 = Node.new('foo').inherit n
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: niceogiri
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,11 +10,11 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2012-
|
13
|
+
date: 2012-06-07 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: nokogiri
|
17
|
-
requirement:
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
20
20
|
- - ~>
|
@@ -22,10 +22,15 @@ dependencies:
|
|
22
22
|
version: '1.4'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
|
-
version_requirements:
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ~>
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: '1.4'
|
26
31
|
- !ruby/object:Gem::Dependency
|
27
32
|
name: rspec
|
28
|
-
requirement:
|
33
|
+
requirement: !ruby/object:Gem::Requirement
|
29
34
|
none: false
|
30
35
|
requirements:
|
31
36
|
- - ! '>='
|
@@ -33,10 +38,15 @@ dependencies:
|
|
33
38
|
version: 2.7.0
|
34
39
|
type: :development
|
35
40
|
prerelease: false
|
36
|
-
version_requirements:
|
41
|
+
version_requirements: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ! '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 2.7.0
|
37
47
|
- !ruby/object:Gem::Dependency
|
38
48
|
name: bundler
|
39
|
-
requirement:
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
40
50
|
none: false
|
41
51
|
requirements:
|
42
52
|
- - ! '>='
|
@@ -44,10 +54,15 @@ dependencies:
|
|
44
54
|
version: 1.0.0
|
45
55
|
type: :development
|
46
56
|
prerelease: false
|
47
|
-
version_requirements:
|
57
|
+
version_requirements: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ! '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: 1.0.0
|
48
63
|
- !ruby/object:Gem::Dependency
|
49
64
|
name: yard
|
50
|
-
requirement:
|
65
|
+
requirement: !ruby/object:Gem::Requirement
|
51
66
|
none: false
|
52
67
|
requirements:
|
53
68
|
- - ! '>='
|
@@ -55,10 +70,15 @@ dependencies:
|
|
55
70
|
version: 0.6.1
|
56
71
|
type: :development
|
57
72
|
prerelease: false
|
58
|
-
version_requirements:
|
73
|
+
version_requirements: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ! '>='
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: 0.6.1
|
59
79
|
- !ruby/object:Gem::Dependency
|
60
80
|
name: rake
|
61
|
-
requirement:
|
81
|
+
requirement: !ruby/object:Gem::Requirement
|
62
82
|
none: false
|
63
83
|
requirements:
|
64
84
|
- - ! '>='
|
@@ -66,10 +86,15 @@ dependencies:
|
|
66
86
|
version: '0'
|
67
87
|
type: :development
|
68
88
|
prerelease: false
|
69
|
-
version_requirements:
|
89
|
+
version_requirements: !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ! '>='
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
70
95
|
- !ruby/object:Gem::Dependency
|
71
96
|
name: guard-rspec
|
72
|
-
requirement:
|
97
|
+
requirement: !ruby/object:Gem::Requirement
|
73
98
|
none: false
|
74
99
|
requirements:
|
75
100
|
- - ! '>='
|
@@ -77,7 +102,12 @@ dependencies:
|
|
77
102
|
version: '0'
|
78
103
|
type: :development
|
79
104
|
prerelease: false
|
80
|
-
version_requirements:
|
105
|
+
version_requirements: !ruby/object:Gem::Requirement
|
106
|
+
none: false
|
107
|
+
requirements:
|
108
|
+
- - ! '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
81
111
|
description: Make dealing with XML less painful
|
82
112
|
email:
|
83
113
|
- ben@langfeld.me
|
@@ -118,7 +148,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
118
148
|
version: '0'
|
119
149
|
segments:
|
120
150
|
- 0
|
121
|
-
hash:
|
151
|
+
hash: 2448257972408275473
|
122
152
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
123
153
|
none: false
|
124
154
|
requirements:
|
@@ -127,10 +157,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
127
157
|
version: '0'
|
128
158
|
segments:
|
129
159
|
- 0
|
130
|
-
hash:
|
160
|
+
hash: 2448257972408275473
|
131
161
|
requirements: []
|
132
162
|
rubyforge_project:
|
133
|
-
rubygems_version: 1.8.
|
163
|
+
rubygems_version: 1.8.21
|
134
164
|
signing_key:
|
135
165
|
specification_version: 3
|
136
166
|
summary: Some additional niceties atop Nokogiri
|