niceogiri 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +31 -0
- data/Rakefile +27 -0
- data/lib/niceogiri/core_ext/nokogiri.rb +44 -0
- data/lib/niceogiri/version.rb +3 -0
- data/lib/niceogiri/xml/node.rb +176 -0
- data/lib/niceogiri.rb +7 -0
- data/niceogiri.gemspec +30 -0
- data/spec/niceogiri/xml/node_spec.rb +204 -0
- data/spec/spec_helper.rb +6 -0
- metadata +182 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Niceogiri
|
2
|
+
|
3
|
+
Copyright (c) 2011 Jeff Smick, Ben Langfeld
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
19
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
20
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
21
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
22
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# Niceogiri [  ](http://travis-ci.org/benlangfeld/niceogiri)
|
2
|
+
|
3
|
+
Some wrappers around and helpers for XML manipulation using Nokogiri
|
4
|
+
|
5
|
+
## Project Pages
|
6
|
+
|
7
|
+
* [Docs](http://rdoc.info/github/benlangfeld/Niceogiri)
|
8
|
+
* [GitHub](https://github.com/benlangfeld/niceorigi)
|
9
|
+
* [Gemcutter](http://gemcutter.org/gems/niceogiri)
|
10
|
+
* [Google Group](http://groups.google.com/group/xmpp-blather)
|
11
|
+
|
12
|
+
# Usage
|
13
|
+
|
14
|
+
## Installation
|
15
|
+
|
16
|
+
gem install niceogiri
|
17
|
+
|
18
|
+
# Contributions
|
19
|
+
|
20
|
+
All contributions are welcome, even encouraged. However, contributions must be
|
21
|
+
well tested. If you send me a branch name to merge that'll get my attention faster
|
22
|
+
than a change set made directly on master.
|
23
|
+
|
24
|
+
# Authors
|
25
|
+
|
26
|
+
* [Jeff Smick](http://github.com/sprsquish)
|
27
|
+
* [Ben Langfeld](http://github.com/benlangfeld)
|
28
|
+
|
29
|
+
# Copyright
|
30
|
+
|
31
|
+
Copyright (c) 2011 Jeff Smick, Ben Langfeld. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
require 'bundler/gem_tasks'
|
5
|
+
|
6
|
+
require 'rake/testtask'
|
7
|
+
Rake::TestTask.new(:test) do |test|
|
8
|
+
test.libs << 'lib' << 'spec'
|
9
|
+
test.pattern = 'spec/**/*_spec.rb'
|
10
|
+
test.verbose = true
|
11
|
+
end
|
12
|
+
|
13
|
+
require 'rcov/rcovtask'
|
14
|
+
Rcov::RcovTask.new do |test|
|
15
|
+
test.libs << 'spec'
|
16
|
+
test.pattern = 'spec/**/*_spec.rb'
|
17
|
+
test.rcov_opts += ['--exclude \/Library\/Ruby,spec\/', '--xrefs']
|
18
|
+
test.verbose = true
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'yard'
|
22
|
+
|
23
|
+
YARD::Rake::YardocTask.new(:doc) do |t|
|
24
|
+
t.options = ['--no-private', '-m', 'markdown', '-o', './doc/public/yard']
|
25
|
+
end
|
26
|
+
|
27
|
+
task :default => :test
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# @private
|
2
|
+
module Nokogiri
|
3
|
+
module XML
|
4
|
+
|
5
|
+
# Nokogiri::Node extensions
|
6
|
+
class Node
|
7
|
+
# Alias #name to #element_name so we can use #name in an XMPP Stanza context
|
8
|
+
alias_method :element_name, :name
|
9
|
+
alias_method :element_name=, :name=
|
10
|
+
|
11
|
+
alias_method :attr_set, :[]=
|
12
|
+
# Override Nokogiri's attribute setter to add the ability to kill an attribute
|
13
|
+
# by setting it to nil and to be able to lookup an attribute by symbol
|
14
|
+
#
|
15
|
+
# @param [#to_s] name the name of the attribute
|
16
|
+
# @param [#to_s, nil] value the new value or nil to remove it
|
17
|
+
def []=(name, value)
|
18
|
+
name = name.to_s
|
19
|
+
value.nil? ? remove_attribute(name) : attr_set(name, value.to_s)
|
20
|
+
end
|
21
|
+
|
22
|
+
alias_method :nokogiri_xpath, :xpath
|
23
|
+
# Override Nokogiri's #xpath method to add the ability to use symbols for lookup
|
24
|
+
# and namespace designation
|
25
|
+
def xpath(*paths)
|
26
|
+
paths[0] = paths[0].to_s
|
27
|
+
|
28
|
+
if paths.size > 1 && (namespaces = paths.pop).is_a?(Hash)
|
29
|
+
paths << namespaces.inject({}) { |h,v| h[v[0].to_s] = v[1]; h }
|
30
|
+
end
|
31
|
+
|
32
|
+
nokogiri_xpath *paths
|
33
|
+
end
|
34
|
+
alias_method :find, :xpath
|
35
|
+
|
36
|
+
# Return the first element at a specified xpath
|
37
|
+
# @see #xpath
|
38
|
+
def find_first(*paths)
|
39
|
+
xpath(*paths).first
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
end # XML
|
44
|
+
end # Nokogiri
|
@@ -0,0 +1,176 @@
|
|
1
|
+
module Niceogiri
|
2
|
+
module XML
|
3
|
+
|
4
|
+
# Base XML Node
|
5
|
+
# All XML classes subclass Node - it allows the addition of helpers
|
6
|
+
class Node < Nokogiri::XML::Node
|
7
|
+
# Create a new Node object
|
8
|
+
#
|
9
|
+
# @param [String, nil] name the element name
|
10
|
+
# @param [XML::Document, nil] doc the document to attach the node to. If
|
11
|
+
# not provided one will be created
|
12
|
+
# @return a new object with the name and namespace
|
13
|
+
def self.new(name = nil, doc = nil, ns = nil)
|
14
|
+
super(name.to_s, (doc || Nokogiri::XML::Document.new)).tap do |node|
|
15
|
+
node.document.root = node unless doc
|
16
|
+
node.namespace = ns if ns
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
# Helper method to read an attribute
|
21
|
+
#
|
22
|
+
# @param [#to_sym] attr_name the name of the attribute
|
23
|
+
# @param [String, Symbol, nil] to_call the name of the method to call on
|
24
|
+
# the returned value
|
25
|
+
# @return nil or the value
|
26
|
+
def read_attr(attr_name, to_call = nil)
|
27
|
+
val = self[attr_name.to_sym]
|
28
|
+
val && to_call ? val.__send__(to_call) : val
|
29
|
+
end
|
30
|
+
|
31
|
+
# Helper method to write a value to an attribute
|
32
|
+
#
|
33
|
+
# @param [#to_sym] attr_name the name of the attribute
|
34
|
+
# @param [#to_s] value the value to set the attribute to
|
35
|
+
def write_attr(attr_name, value)
|
36
|
+
self[attr_name.to_sym] = value
|
37
|
+
end
|
38
|
+
|
39
|
+
# Helper method to read the content of a node
|
40
|
+
#
|
41
|
+
# @param [#to_sym] node the name of the node
|
42
|
+
# @param [String, Symbol, nil] to_call the name of the method to call on
|
43
|
+
# the returned value
|
44
|
+
# @return nil or the value
|
45
|
+
def read_content(node, to_call = nil)
|
46
|
+
val = content_from node.to_sym
|
47
|
+
val && to_call ? val.__send__(to_call) : val
|
48
|
+
end
|
49
|
+
|
50
|
+
# @private
|
51
|
+
alias_method :nokogiri_namespace=, :namespace=
|
52
|
+
# Attach a namespace to the node
|
53
|
+
#
|
54
|
+
# @overload namespace=(ns)
|
55
|
+
# Attach an already created XML::Namespace
|
56
|
+
# @param [XML::Namespace] ns the namespace object
|
57
|
+
# @overload namespace=(ns)
|
58
|
+
# Create a new namespace and attach it
|
59
|
+
# @param [String] ns the namespace uri
|
60
|
+
# @overload namespace=(namespaces)
|
61
|
+
# Createa and add new namespaces from a hash
|
62
|
+
# @param [Hash] namespaces a hash of prefix => uri pairs
|
63
|
+
def namespace=(namespaces)
|
64
|
+
case namespaces
|
65
|
+
when Nokogiri::XML::Namespace
|
66
|
+
self.nokogiri_namespace = namespaces
|
67
|
+
when String
|
68
|
+
self.add_namespace nil, namespaces
|
69
|
+
when Hash
|
70
|
+
self.add_namespace nil, ns if ns = namespaces.delete(nil)
|
71
|
+
namespaces.each do |p, n|
|
72
|
+
ns = self.add_namespace p, n
|
73
|
+
self.nokogiri_namespace = ns
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
# Helper method to get the node's namespace
|
79
|
+
#
|
80
|
+
# @return [XML::Namespace, nil] The node's namespace object if it exists
|
81
|
+
def namespace_href
|
82
|
+
namespace.href if namespace
|
83
|
+
end
|
84
|
+
|
85
|
+
# Remove a child with the name and (optionally) namespace given
|
86
|
+
#
|
87
|
+
# @param [String] name the name or xpath of the node to remove
|
88
|
+
# @param [String, nil] ns the namespace the node is in
|
89
|
+
def remove_child(name, ns = nil)
|
90
|
+
child = xpath(name, ns).first
|
91
|
+
child.remove if child
|
92
|
+
end
|
93
|
+
|
94
|
+
# Remove all children with a given name regardless of namespace
|
95
|
+
#
|
96
|
+
# @param [String] name the name of the nodes to remove
|
97
|
+
def remove_children(name)
|
98
|
+
xpath("./*[local-name()='#{name}']").remove
|
99
|
+
end
|
100
|
+
|
101
|
+
# The content of the named node
|
102
|
+
#
|
103
|
+
# @param [String] name the name or xpath of the node
|
104
|
+
# @param [String, nil] ns the namespace the node is in
|
105
|
+
# @return [String, nil] the content of the node
|
106
|
+
def content_from(name, ns = nil)
|
107
|
+
child = xpath(name, ns).first
|
108
|
+
child.content if child
|
109
|
+
end
|
110
|
+
|
111
|
+
# Sets the content for the specified node.
|
112
|
+
# If the node exists it is updated. If not a new node is created
|
113
|
+
# If the node exists and the content is nil, the node will be removed
|
114
|
+
# entirely
|
115
|
+
#
|
116
|
+
# @param [String] node the name of the node to update/create
|
117
|
+
# @param [String, nil] content the content to set within the node
|
118
|
+
def set_content_for(node, content = nil)
|
119
|
+
if content
|
120
|
+
child = xpath(node).first
|
121
|
+
self << (child = Node.new(node, self.document)) unless child
|
122
|
+
child.content = content
|
123
|
+
else
|
124
|
+
remove_child node
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
alias_method :copy, :dup
|
129
|
+
|
130
|
+
# Inherit the attributes and children of an XML::Node
|
131
|
+
#
|
132
|
+
# @param [XML::Node] node the node to inherit
|
133
|
+
# @return [self]
|
134
|
+
def inherit(node)
|
135
|
+
set_namespace node.namespace if node.namespace
|
136
|
+
inherit_attrs node.attributes
|
137
|
+
node.children.each do |c|
|
138
|
+
self << (n = c.dup)
|
139
|
+
ns = n.namespace_definitions.find { |ns| ns.prefix == c.namespace.prefix }
|
140
|
+
n.namespace = ns if ns
|
141
|
+
end
|
142
|
+
self
|
143
|
+
end
|
144
|
+
|
145
|
+
# Inherit a set of attributes
|
146
|
+
#
|
147
|
+
# @param [Hash] attrs a hash of attributes to set on the node
|
148
|
+
# @return [self]
|
149
|
+
def inherit_attrs(attrs)
|
150
|
+
attrs.each { |name, value| self[name] = value }
|
151
|
+
self
|
152
|
+
end
|
153
|
+
|
154
|
+
# The node as XML
|
155
|
+
#
|
156
|
+
# @return [String] XML representation of the node
|
157
|
+
def inspect
|
158
|
+
self.to_xml
|
159
|
+
end
|
160
|
+
|
161
|
+
# Check that a set of fields are equal between nodes
|
162
|
+
#
|
163
|
+
# @param [Node] other the other node to compare against
|
164
|
+
# @param [*#to_s] fields the set of fields to compare
|
165
|
+
# @return [Fixnum<-1,0,1>]
|
166
|
+
def eql?(o, *fields)
|
167
|
+
o.is_a?(self.class) && fields.all? { |f| self.__send__(f) == o.__send__(f) }
|
168
|
+
end
|
169
|
+
|
170
|
+
# @private
|
171
|
+
def ==(o)
|
172
|
+
eql?(o)
|
173
|
+
end
|
174
|
+
end # Node
|
175
|
+
end # XML
|
176
|
+
end # Niceogiri
|
data/lib/niceogiri.rb
ADDED
data/niceogiri.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "niceogiri/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "niceogiri"
|
7
|
+
s.version = Niceogiri::VERSION
|
8
|
+
s.authors = ["Ben Langfeld", "Jeff Smick"]
|
9
|
+
s.email = ["ben@langfeld.me", "sprsquish@gmail.com"]
|
10
|
+
s.homepage = "https://github.com/benlangfeld/Niceogiri"
|
11
|
+
s.summary = %q{Some additional niceties atop Nokogiri}
|
12
|
+
s.description = %q{Make dealing with XML less painful}
|
13
|
+
|
14
|
+
s.files = `git ls-files`.split("\n")
|
15
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
16
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
|
19
|
+
s.rdoc_options = %w{--charset=UTF-8}
|
20
|
+
s.extra_rdoc_files = %w{LICENSE README.md}
|
21
|
+
|
22
|
+
s.add_dependency("nokogiri", [">= 1.4.0"])
|
23
|
+
|
24
|
+
s.add_development_dependency("minitest", ["~> 1.7.1"])
|
25
|
+
s.add_development_dependency("bundler", ["~> 1.0.0"])
|
26
|
+
s.add_development_dependency("rcov", ["~> 0.9.9"])
|
27
|
+
s.add_development_dependency("yard", ["~> 0.6.1"])
|
28
|
+
s.add_development_dependency("bluecloth", ["~> 2.1.0"])
|
29
|
+
s.add_development_dependency("rake")
|
30
|
+
end
|
@@ -0,0 +1,204 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Niceogiri
|
4
|
+
module XML
|
5
|
+
describe Node do
|
6
|
+
before { @doc = Nokogiri::XML::Document.new }
|
7
|
+
|
8
|
+
it 'generates a new node automatically setting the document' do
|
9
|
+
n = Node.new 'foo'
|
10
|
+
n.element_name.must_equal 'foo'
|
11
|
+
n.document.wont_equal @doc
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'sets the new document root to the node' do
|
15
|
+
n = Node.new 'foo'
|
16
|
+
n.document.root.must_equal n
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'does not set the document root if the document is provided' do
|
20
|
+
n = Node.new 'foo', @doc
|
21
|
+
n.document.root.wont_equal n
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'generates a new node with the given document' do
|
25
|
+
n = Node.new 'foo', @doc
|
26
|
+
n.element_name.must_equal 'foo'
|
27
|
+
n.document.must_equal @doc
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'provides an attribute reader' do
|
31
|
+
foo = Node.new
|
32
|
+
foo.read_attr(:bar).must_be_nil
|
33
|
+
foo[:bar] = 'baz'
|
34
|
+
foo.read_attr(:bar).must_equal 'baz'
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'provides an attribute reader with converstion' do
|
38
|
+
foo = Node.new
|
39
|
+
foo.read_attr(:bar, :to_sym).must_be_nil
|
40
|
+
foo[:bar] = 'baz'
|
41
|
+
foo.read_attr(:bar, :to_sym).must_equal :baz
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'provides an attribute writer' do
|
45
|
+
foo = Node.new
|
46
|
+
foo[:bar].must_be_nil
|
47
|
+
foo.write_attr(:bar, 'baz')
|
48
|
+
foo[:bar].must_equal 'baz'
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'provides a content reader' do
|
52
|
+
foo = Node.new('foo')
|
53
|
+
foo << (bar = Node.new('bar', foo.document))
|
54
|
+
bar.content = 'baz'
|
55
|
+
foo.read_content(:bar).must_equal 'baz'
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'provides a content reader that converts the value' do
|
59
|
+
foo = Node.new('foo')
|
60
|
+
foo << (bar = Node.new('bar', foo.document))
|
61
|
+
bar.content = 'baz'
|
62
|
+
foo.read_content(:bar, :to_sym).must_equal :baz
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'provides a content writer' do
|
66
|
+
foo = Node.new('foo')
|
67
|
+
foo.set_content_for :bar, 'baz'
|
68
|
+
foo.content_from(:bar).must_equal 'baz'
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'provides a content writer that removes a child when set to nil' do
|
72
|
+
foo = Node.new('foo')
|
73
|
+
foo << (bar = Node.new('bar', foo.document))
|
74
|
+
bar.content = 'baz'
|
75
|
+
foo.content_from(:bar).must_equal 'baz'
|
76
|
+
foo.xpath('bar').wont_be_empty
|
77
|
+
|
78
|
+
foo.set_content_for :bar, nil
|
79
|
+
foo.content_from(:bar).must_be_nil
|
80
|
+
foo.xpath('bar').must_be_empty
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'provides "attr_accessor" for namespace' do
|
84
|
+
n = Node.new('foo')
|
85
|
+
n.namespace.must_be_nil
|
86
|
+
|
87
|
+
n.namespace = 'foo:bar'
|
88
|
+
n.namespace_href.must_equal 'foo:bar'
|
89
|
+
end
|
90
|
+
|
91
|
+
it 'will remove a child element' do
|
92
|
+
n = Node.new 'foo'
|
93
|
+
n << Node.new('bar', n.document)
|
94
|
+
n << Node.new('bar', n.document)
|
95
|
+
|
96
|
+
n.find(:bar).size.must_equal 2
|
97
|
+
n.remove_child 'bar'
|
98
|
+
n.find(:bar).size.must_equal 1
|
99
|
+
end
|
100
|
+
|
101
|
+
it 'will remove a child with a specific xmlns' do
|
102
|
+
n = Node.new 'foo'
|
103
|
+
n << Node.new('bar')
|
104
|
+
c = Node.new('bar')
|
105
|
+
c.namespace = 'foo:bar'
|
106
|
+
n << c
|
107
|
+
|
108
|
+
n.find(:bar).size.must_equal 1
|
109
|
+
n.find('//xmlns:bar', :xmlns => 'foo:bar').size.must_equal 1
|
110
|
+
n.remove_child '//xmlns:bar', :xmlns => 'foo:bar'
|
111
|
+
n.find(:bar).size.must_equal 1
|
112
|
+
n.find('//xmlns:bar', :xmlns => 'foo:bar').size.must_equal 0
|
113
|
+
end
|
114
|
+
|
115
|
+
it 'will remove all child elements' do
|
116
|
+
n = Node.new 'foo'
|
117
|
+
n << Node.new('bar')
|
118
|
+
n << Node.new('bar')
|
119
|
+
|
120
|
+
n.find(:bar).size.must_equal 2
|
121
|
+
n.remove_children 'bar'
|
122
|
+
n.find(:bar).size.must_equal 0
|
123
|
+
end
|
124
|
+
|
125
|
+
it 'provides a copy mechanism' do
|
126
|
+
n = Node.new 'foo'
|
127
|
+
n2 = n.copy
|
128
|
+
n2.object_id.wont_equal n.object_id
|
129
|
+
n2.element_name.must_equal n.element_name
|
130
|
+
end
|
131
|
+
|
132
|
+
it 'provides an inherit mechanism' do
|
133
|
+
n = Node.new 'foo'
|
134
|
+
n2 = Node.new 'foo'
|
135
|
+
n2.content = 'bar'
|
136
|
+
n2['foo'] = 'bar'
|
137
|
+
|
138
|
+
n.inherit(n2)
|
139
|
+
n['foo'].must_equal 'bar'
|
140
|
+
n.content.must_equal 'bar'
|
141
|
+
n2.to_s.must_equal n.to_s
|
142
|
+
end
|
143
|
+
|
144
|
+
it 'holds on to namespaces when inheriting content' do
|
145
|
+
n = Nokogiri::XML.parse('<message><bar:foo xmlns:bar="http://bar.com"></message>').root
|
146
|
+
n2 = Node.new('message').inherit n
|
147
|
+
n2.to_s.must_equal n.to_s
|
148
|
+
end
|
149
|
+
|
150
|
+
it 'provides a mechanism to inherit attrs' do
|
151
|
+
n = Node.new 'foo'
|
152
|
+
n2 = Node.new 'foo'
|
153
|
+
n2['foo'] = 'bar'
|
154
|
+
|
155
|
+
n.inherit_attrs(n2.attributes)
|
156
|
+
n['foo'].must_equal 'bar'
|
157
|
+
end
|
158
|
+
|
159
|
+
it 'has a content_from helper that pulls the content from a child node' do
|
160
|
+
f = Node.new('foo')
|
161
|
+
f << (b = Node.new('bar'))
|
162
|
+
b.content = 'content'
|
163
|
+
f.content_from(:bar).must_equal 'content'
|
164
|
+
end
|
165
|
+
|
166
|
+
it 'returns nil when sent #content_from and a missing node' do
|
167
|
+
f = Node.new('foo')
|
168
|
+
f.content_from(:bar).must_be_nil
|
169
|
+
end
|
170
|
+
|
171
|
+
it 'creates a new node and sets content when sent #set_content_for' do
|
172
|
+
f = Node.new('foo')
|
173
|
+
f.must_respond_to :set_content_for
|
174
|
+
f.xpath('bar').must_be_empty
|
175
|
+
f.set_content_for :bar, :baz
|
176
|
+
f.xpath('bar').wont_be_empty
|
177
|
+
f.xpath('bar').first.content.must_equal 'baz'
|
178
|
+
end
|
179
|
+
|
180
|
+
it 'removes a child node when sent #set_content_for with nil' do
|
181
|
+
f = Node.new('foo')
|
182
|
+
f << (b = Node.new('bar'))
|
183
|
+
f.must_respond_to :set_content_for
|
184
|
+
f.xpath('bar').wont_be_empty
|
185
|
+
f.set_content_for :bar, nil
|
186
|
+
f.xpath('bar').must_be_empty
|
187
|
+
end
|
188
|
+
|
189
|
+
it 'will change the content of an existing node when sent #set_content_for' do
|
190
|
+
f = Node.new('foo')
|
191
|
+
f << (b = Node.new('bar'))
|
192
|
+
b.content = 'baz'
|
193
|
+
f.must_respond_to :set_content_for
|
194
|
+
f.xpath('bar').wont_be_empty
|
195
|
+
f.xpath('bar').first.content.must_equal 'baz'
|
196
|
+
control = f.xpath('bar').first.pointer_id
|
197
|
+
|
198
|
+
f.set_content_for :bar, 'fiz'
|
199
|
+
f.xpath('bar').first.content.must_equal 'fiz'
|
200
|
+
f.xpath('bar').first.pointer_id.must_equal control
|
201
|
+
end
|
202
|
+
end
|
203
|
+
end
|
204
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,182 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: niceogiri
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 3
|
9
|
+
version: 0.0.3
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Ben Langfeld
|
13
|
+
- Jeff Smick
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-06-09 00:00:00 +01:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: nokogiri
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
segments:
|
30
|
+
- 1
|
31
|
+
- 4
|
32
|
+
- 0
|
33
|
+
version: 1.4.0
|
34
|
+
type: :runtime
|
35
|
+
version_requirements: *id001
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: minitest
|
38
|
+
prerelease: false
|
39
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ~>
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
segments:
|
45
|
+
- 1
|
46
|
+
- 7
|
47
|
+
- 1
|
48
|
+
version: 1.7.1
|
49
|
+
type: :development
|
50
|
+
version_requirements: *id002
|
51
|
+
- !ruby/object:Gem::Dependency
|
52
|
+
name: bundler
|
53
|
+
prerelease: false
|
54
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ~>
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
segments:
|
60
|
+
- 1
|
61
|
+
- 0
|
62
|
+
- 0
|
63
|
+
version: 1.0.0
|
64
|
+
type: :development
|
65
|
+
version_requirements: *id003
|
66
|
+
- !ruby/object:Gem::Dependency
|
67
|
+
name: rcov
|
68
|
+
prerelease: false
|
69
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
71
|
+
requirements:
|
72
|
+
- - ~>
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
segments:
|
75
|
+
- 0
|
76
|
+
- 9
|
77
|
+
- 9
|
78
|
+
version: 0.9.9
|
79
|
+
type: :development
|
80
|
+
version_requirements: *id004
|
81
|
+
- !ruby/object:Gem::Dependency
|
82
|
+
name: yard
|
83
|
+
prerelease: false
|
84
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ~>
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
segments:
|
90
|
+
- 0
|
91
|
+
- 6
|
92
|
+
- 1
|
93
|
+
version: 0.6.1
|
94
|
+
type: :development
|
95
|
+
version_requirements: *id005
|
96
|
+
- !ruby/object:Gem::Dependency
|
97
|
+
name: bluecloth
|
98
|
+
prerelease: false
|
99
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
100
|
+
none: false
|
101
|
+
requirements:
|
102
|
+
- - ~>
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
segments:
|
105
|
+
- 2
|
106
|
+
- 1
|
107
|
+
- 0
|
108
|
+
version: 2.1.0
|
109
|
+
type: :development
|
110
|
+
version_requirements: *id006
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rake
|
113
|
+
prerelease: false
|
114
|
+
requirement: &id007 !ruby/object:Gem::Requirement
|
115
|
+
none: false
|
116
|
+
requirements:
|
117
|
+
- - ">="
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
segments:
|
120
|
+
- 0
|
121
|
+
version: "0"
|
122
|
+
type: :development
|
123
|
+
version_requirements: *id007
|
124
|
+
description: Make dealing with XML less painful
|
125
|
+
email:
|
126
|
+
- ben@langfeld.me
|
127
|
+
- sprsquish@gmail.com
|
128
|
+
executables: []
|
129
|
+
|
130
|
+
extensions: []
|
131
|
+
|
132
|
+
extra_rdoc_files:
|
133
|
+
- LICENSE
|
134
|
+
- README.md
|
135
|
+
files:
|
136
|
+
- .gitignore
|
137
|
+
- Gemfile
|
138
|
+
- LICENSE
|
139
|
+
- README.md
|
140
|
+
- Rakefile
|
141
|
+
- lib/niceogiri.rb
|
142
|
+
- lib/niceogiri/core_ext/nokogiri.rb
|
143
|
+
- lib/niceogiri/version.rb
|
144
|
+
- lib/niceogiri/xml/node.rb
|
145
|
+
- niceogiri.gemspec
|
146
|
+
- spec/niceogiri/xml/node_spec.rb
|
147
|
+
- spec/spec_helper.rb
|
148
|
+
has_rdoc: true
|
149
|
+
homepage: https://github.com/benlangfeld/Niceogiri
|
150
|
+
licenses: []
|
151
|
+
|
152
|
+
post_install_message:
|
153
|
+
rdoc_options:
|
154
|
+
- --charset=UTF-8
|
155
|
+
require_paths:
|
156
|
+
- lib
|
157
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
158
|
+
none: false
|
159
|
+
requirements:
|
160
|
+
- - ">="
|
161
|
+
- !ruby/object:Gem::Version
|
162
|
+
segments:
|
163
|
+
- 0
|
164
|
+
version: "0"
|
165
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
166
|
+
none: false
|
167
|
+
requirements:
|
168
|
+
- - ">="
|
169
|
+
- !ruby/object:Gem::Version
|
170
|
+
segments:
|
171
|
+
- 0
|
172
|
+
version: "0"
|
173
|
+
requirements: []
|
174
|
+
|
175
|
+
rubyforge_project:
|
176
|
+
rubygems_version: 1.3.7
|
177
|
+
signing_key:
|
178
|
+
specification_version: 3
|
179
|
+
summary: Some additional niceties atop Nokogiri
|
180
|
+
test_files:
|
181
|
+
- spec/niceogiri/xml/node_spec.rb
|
182
|
+
- spec/spec_helper.rb
|