diff --git a/lib/nori/parser/nokogiri.rb b/lib/nori/parser/nokogiri.rb index 46b9cb6..af45ecb 100644 --- a/lib/nori/parser/nokogiri.rb +++ b/lib/nori/parser/nokogiri.rb @@ -19,15 +19,27 @@ def start_element(name, attrs = []) stack.push Nori::XMLUtilityNode.new(options, name, Hash[*attrs.flatten]) end + # To keep backward behaviour compatibility + # delete last child if it is a space-only text node def end_element(name) if stack.size > 1 last = stack.pop + maybe_string = last.children.last + if maybe_string.is_a?(String) and maybe_string.strip.empty? + last.children.pop + end stack.last.add_node last end end + # If this node is a successive character then add it as is. + # First child being a space-only text node will not be added + # because there is no previous characters. def characters(string) - stack.last.add_node(string) unless string.strip.length == 0 || stack.empty? + last = stack.last + if last and last.children.last.is_a?(String) or string.strip.size > 0 + last.add_node(string) + end end alias cdata_block characters