lib/csl/parser.rb in csl-1.2.0 vs lib/csl/parser.rb in csl-1.2.1
- old
+ new
@@ -9,11 +9,13 @@
attr_accessor :parser
@engines = {
:nokogiri => lambda { |source|
Nokogiri::XML::Document.parse(source, nil, nil,
- Nokogiri::XML::ParseOptions::DEFAULT_XML | Nokogiri::XML::ParseOptions::NOBLANKS | Nokogiri::XML::ParseOptions::NOENT)
+ Nokogiri::XML::ParseOptions::DEFAULT_XML |
+ Nokogiri::XML::ParseOptions::NOBLANKS |
+ Nokogiri::XML::ParseOptions::NOENT)
},
:default => lambda { |source|
REXML::Document.new(source, :compress_whitespace => :all, :ignore_whitespace_nodes => :all)
}
}
@@ -66,30 +68,46 @@
root = parse_node node, scope
scope = specialize_scope(root, scope)
node.children.each do |child|
- root << parse_tree(child, scope) unless comment?(child)
+ root << parse_tree(child, scope) unless skip?(child)
end unless root.textnode?
root
end
def parse_text(node)
if node.respond_to?(:has_text?)
node.has_text? && node.text
else
child = node.children[0]
- child && child.respond_to?(:text?) && child.text? && child.text
+ return unless child && child.respond_to?(:text?) && child.text?
+
+ text = child.text
+ return if text.to_s.strip.empty?
+
+ text
end
end
def comment?(node)
node.respond_to?(:comment?) && node.comment? ||
node.respond_to?(:node_type) &&
[:comment, :xmldecl, :processing_instruction, 7].include?(node.node_type)
end
- alias skip? comment?
+
+ def text?(node)
+ if defined?(Nokogiri)
+ node.is_a?(Nokogiri::XML::Text)
+ else
+ false
+ end
+ end
+
+ def skip?(node)
+ comment?(node) || text?(node)
+ end
def specialize_scope(root, scope = Node)
case root
when Style
Style