Sha256: 5b12dd4151a7208c4d390e5bc6aed93f0c2ac29c07bf6d52b05c9964f21a0fb6

Contents?: true

Size: 1.75 KB

Versions: 8

Compression:

Stored size: 1.75 KB

Contents

module TestXml
  module NokogiriExt
    module Node
      def match?(element, compare_value = false)
        if compare_value && element.leaf?
          comparable_attributes == element.comparable_attributes and equal_text?(element)
        else

          #TODO clean this part of the code
          if compare_value
            (comparable_attributes == element.comparable_attributes) &&
            contains_elements_of?(element) &&
            element.elements.all? {|el| matches_at_least_one?(el, compare_value) }
          else
            contains_elements_of?(element) &&
            element.elements.all? {|el| matches_at_least_one?(el, compare_value) }
          end
        end
      end
      
      def elements
        children.collect {|node| node if node.element? }.delete_if {|node| node.nil?}
      end
      
      # Attributes of the current node.
      def comparable_attributes
        attributes.collect {|k,a| [k.downcase, a.value]}.sort
      end
      
      # Check if node is either childless, self-closing, or has content text. 
      def leaf?
        children.size == 0 or (children.size == 1 && children.first.text?)
      end

      def placeholder?
        TestXml.placeholders_enabled? and (content =~ /^`.*`$/)
      end

    private
      def equal_text?(element)
        element.content = content if element.placeholder?

        content == element.content
      end


      def contains_elements_of?(element)
        element.elements.find {|el| not contains?(el)}.nil?
      end

      def contains?(element)
        children.find {|node| node.element? && node.name == element.name }
      end

      def matches_at_least_one?(element, compare_value)
        search(element.name).find { |el| el.match?(element, compare_value) }
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/test_xml-0.1.8/lib/test_xml/nokogiri/node.rb
test_xml-0.1.8 lib/test_xml/nokogiri/node.rb
test_xml-0.1.7 lib/test_xml/nokogiri/node.rb
test_xml-0.1.6 lib/test_xml/nokogiri/node.rb
test_xml-0.1.5 lib/test_xml/nokogiri/node.rb
test_xml-0.1.4 lib/test_xml/nokogiri/node.rb
test_xml-0.1.3 lib/test_xml/nokogiri/node.rb
test_xml-0.1.2 lib/test_xml/nokogiri/node.rb