lib/compare-xml.rb in compare-xml-0.5.1 vs lib/compare-xml.rb in compare-xml-0.5.2

- old
+ new

@@ -23,13 +23,13 @@ # when true, ignores all text nodes (although blank text nodes are always ignored) # when false, all text nodes are compared to their counterparts (except the empty ones) ignore_text_nodes: false, - # when true, trims and squeezes whitespace in text nodes and comments to a single space + # when true, trims and collapses whitespace in text nodes and comments to a single space # when false, all whitespace is preserved as it is without any changes - squeeze_whitespace: true, + collapse_whitespace: true, # when true, provides a list of all error messages encountered in comparisons # when false, execution stops when the first error is encountered with no error messages verbose: false } @@ -121,11 +121,11 @@ # @return type of equivalence (from equivalence constants) # def compareCommentNodes(n1, n2, opts, errors, status = EQUIVALENT) return true if opts[:ignore_comments] t1, t2 = n1.content, n2.content - t1, t2 = squeeze(t1), squeeze(t2) if opts[:squeeze_whitespace] + t1, t2 = collapse(t1), collapse(t2) if opts[:collapse_whitespace] unless t1 == t2 status = UNEQUAL_COMMENTS errors << [nodePath(n1.parent), t1, status, t2, nodePath(n2.parent)] if opts[:verbose] end status @@ -226,11 +226,11 @@ # @return type of equivalence (from equivalence constants) # def compareTextNodes(n1, n2, opts, errors, status = EQUIVALENT) return true if opts[:ignore_text_nodes] t1, t2 = n1.content, n2.content - t1, t2 = squeeze(t1), squeeze(t2) if opts[:squeeze_whitespace] + t1, t2 = collapse(t1), collapse(t2) if opts[:collapse_whitespace] unless t1 == t2 status = UNEQUAL_TEXT_CONTENTS errors << [nodePath(n1.parent), t1, status, t2, nodePath(n2.parent)] if opts[:verbose] end status @@ -360,11 +360,11 @@ # # @return true if excluded, false otherwise # def nodeExcluded?(n, opts) return true if n.is_a?(Nokogiri::XML::Comment) && opts[:ignore_comments] - return true if n.is_a?(Nokogiri::XML::Text) && (opts[:ignore_text_nodes] || squeeze(n.content).empty?) + return true if n.is_a?(Nokogiri::XML::Text) && (opts[:ignore_text_nodes] || collapse(n.content).empty?) opts[:ignore_nodes].each do |css| return true if n.xpath('../*').css(css).include?(n) end false end @@ -433,17 +433,17 @@ end end ## - # Strips the whitespace (from beginning and end) and squeezes it, - # i.e. multiple spaces, new lines and tabs are all squeezed to a single space. + # Strips the whitespace (from beginning and end) and collapses it, + # i.e. multiple spaces, new lines and tabs are all collapsed to a single space. # - # @param [String] text string to squeeze + # @param [String] text string to collapse # - # @return squeezed string + # @return collapsed string # - def squeeze(text) + def collapse(text) text = text.to_s unless text.is_a? String text.strip.gsub(/\s+/, ' ') end end \ No newline at end of file