From 64da5673410ce63e58c99e70936ec166ccce01c1 Mon Sep 17 00:00:00 2001 From: Yoko Harada Date: Fri, 8 Jul 2011 14:11:00 -0400 Subject: [PATCH] Fix for issue#489. Also adds a test of blank? to test/xml/test_node.rb --- ext/java/nokogiri/XmlNode.java | 9 +++++++-- test/xml/test_node.rb | 5 +++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/ext/java/nokogiri/XmlNode.java b/ext/java/nokogiri/XmlNode.java index ddc956016b8..aa56ba57aef 100644 --- a/ext/java/nokogiri/XmlNode.java +++ b/ext/java/nokogiri/XmlNode.java @@ -553,8 +553,13 @@ public IRubyObject attribute_with_ns(ThreadContext context, IRubyObject name, IR @JRubyMethod(name = "blank?") public IRubyObject blank_p(ThreadContext context) { - String data = node.getTextContent(); - if ("".equals(data.trim())) return context.getRuntime().getTrue(); + // according to libxml doc, + // a node is blank if if it is a Text or CDATA node consisting of whitespace only + if (node.getNodeType() == Node.TEXT_NODE || node.getNodeType() == Node.CDATA_SECTION_NODE) { + String data = node.getTextContent(); + if (data == null) return context.getRuntime().getTrue(); + if ("".equals(data.trim())) return context.getRuntime().getTrue(); + } return context.getRuntime().getFalse(); } diff --git a/test/xml/test_node.rb b/test/xml/test_node.rb index ab01ddc87f8..872cc58c174 100644 --- a/test/xml/test_node.rb +++ b/test/xml/test_node.rb @@ -912,6 +912,11 @@ def awesome! ; end assert_equal @xml, node_set.document assert node_set.respond_to?(:awesome!) end + + def test_blank + doc = Nokogiri::XML('') + assert_equal false, doc.blank? + end end end end