Skip to content

Commit 8a12a78

Browse files
committed
For Nokogiri, implement #text? to also include CDATA elements.
Fixes #37.
1 parent f3ddf3f commit 8a12a78

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

dependencyci.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
platform:
2+
Rubygems:
3+
rdf-isomorphic:
4+
tests:
5+
unmaintained: skip

lib/rdf/rdfxml/reader.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,8 @@ def nodeElement(el, ec)
337337
# Determine the content type of this property element
338338
log_fatal "child must be a proxy not a #{child.class}" unless child.is_a?(@implementation::NodeProxy)
339339

340-
text_nodes = child.children.select {|e| e.text? && !e.blank?}
341-
element_nodes = child.children.select {|c| c.element? }
340+
text_nodes = child.children.select(&:text?)
341+
element_nodes = child.children.select(&:element?)
342342
add_debug(child) {"#{text_nodes.to_a.length} text nodes, #{element_nodes.to_a.length} element nodes"}
343343

344344
text_nodes.each do |node|

lib/rdf/rdfxml/reader/nokogiri.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ def display_path
8181
end
8282
end
8383

84+
##
85+
# Return true of this is a text node
86+
#
87+
# @return [Array<:text, :element, :attribute>]
88+
def text?
89+
(@node.text? || @node.cdata?) && !@node.blank?
90+
end
91+
8492
##
8593
# Return true of all child elements are text
8694
#
@@ -104,7 +112,7 @@ def namespaces
104112
def children
105113
@children ||= NodeSetProxy.new(@node.children, self)
106114
end
107-
115+
108116
# Ancestors of this element, in order
109117
def ancestors
110118
@ancestors ||= parent ? parent.ancestors + [parent] : []

spec/reader_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,24 @@
202202
expect(graph).to be_equivalent_graph(expected, logger: logger)
203203
end
204204

205+
it "reads text from CDATA" do
206+
sampledoc = %(<?xml version="1.0" encoding="utf-8"?>
207+
<rdf:RDF
208+
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
209+
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
210+
>
211+
<rdf:Property rdf:about="http://www.w3.org/ns/oa#annotationService">
212+
<rdfs:comment><![CDATA[Text]]></rdfs:comment>
213+
</rdf:Property>
214+
</rdf:RDF>)
215+
expected = %(
216+
<http://www.w3.org/ns/oa#annotationService> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/1999/02/22-rdf-syntax-ns#Property> .
217+
<http://www.w3.org/ns/oa#annotationService> <http://www.w3.org/2000/01/rdf-schema#comment> "Text" .
218+
)
219+
graph = parse(sampledoc, validate: true)
220+
expect(graph).to be_equivalent_graph(expected, logger: logger)
221+
end
222+
205223
context :exceptions do
206224
it "should raise an error if rdf:aboutEach is used, as per the negative parser test rdfms-abouteach-error001 (rdf:aboutEach attribute)" do
207225
sampledoc = %q(<?xml version="1.0" ?>

0 commit comments

Comments
 (0)