Permalink
Browse files

Added Node#processing_instruction? and refactored some code to use it.

  • Loading branch information...
1 parent 22e0ef5 commit db1f4c1a9db99e14841cbb70dddc51a63d257f3a @flavorjones flavorjones committed Jun 21, 2014
Showing with 26 additions and 3 deletions.
  1. +5 −0 CHANGELOG.ja.rdoc
  2. +5 −0 CHANGELOG.rdoc
  3. +8 −3 lib/nokogiri/xml/node.rb
  4. +8 −0 test/xml/test_node.rb
View
@@ -1,5 +1,10 @@
=== 1.6.3 / unreleased
+==== Features
+
+* Added Node#document? and Node#processing_instruction?
+
+
==== Bug fixes
* [JRuby] Fix Ruby memory exhaustion vulnerability. #1087 (Thanks, @ocher)
View
@@ -1,5 +1,10 @@
=== 1.6.3 / unreleased
+==== Features
+
+* Added Node#document? and Node#processing_instruction?
+
+
==== Bug fixes
* [JRuby] Fix Ruby memory exhaustion vulnerability. #1087 (Thanks, @ocher)
View
@@ -287,7 +287,7 @@ def add_child node_or_tags
def prepend_child node_or_tags
if first = children.first
# Mimic the error add_child would raise.
- raise RuntimeError, "Document already has a root node" if document? && !node_or_tags.is_a?(XML::ProcessingInstruction)
+ raise RuntimeError, "Document already has a root node" if document? && !node_or_tags.processing_instruction?
first.__send__(:add_sibling, :previous, node_or_tags)
else
add_child(node_or_tags)
@@ -313,7 +313,7 @@ def << node_or_tags
#
# Also see related method +before+.
def add_previous_sibling node_or_tags
- raise ArgumentError.new("A document may not have multiple root nodes.") if (parent && parent.document?) && !node_or_tags.is_a?(XML::ProcessingInstruction)
+ raise ArgumentError.new("A document may not have multiple root nodes.") if (parent && parent.document?) && !node_or_tags.processing_instruction?
add_sibling :previous, node_or_tags
end
@@ -326,7 +326,7 @@ def add_previous_sibling node_or_tags
#
# Also see related method +after+.
def add_next_sibling node_or_tags
- raise ArgumentError.new("A document may not have multiple root nodes.") if (parent && parent.document?) && !node_or_tags.is_a?(XML::ProcessingInstruction)
+ raise ArgumentError.new("A document may not have multiple root nodes.") if (parent && parent.document?) && !node_or_tags.processing_instruction?
add_sibling :next, node_or_tags
end
@@ -606,6 +606,11 @@ def document?
is_a? XML::Document
end
+ # Returns true if this is a ProcessingInstruction node
+ def processing_instruction?
+ type == PI_NODE
+ end
+
# Returns true if this is a Text node
def text?
type == TEXT_NODE
View
@@ -1184,6 +1184,14 @@ def test_document_eh
assert ! html_node.document?
assert ! xml_node.document?
end
+
+ def test_processing_instruction_eh
+ xml_doc = Nokogiri::XML %Q{<?xml version="1.0"?>\n<?xml-stylesheet type="text/xsl" href="foo.xsl"?>\n<?xml-stylesheet type="text/xsl" href="foo2.xsl"?>\n<root><div>foo</div></root>}
+ pi_node = xml_doc.children.first
+ div_node = xml_doc.at_css "div"
+ assert pi_node.processing_instruction?
+ assert ! div_node.processing_instruction?
+ end
end
end
end

0 comments on commit db1f4c1

Please sign in to comment.