lib/rspec_html/search.rb in rspec-html-0.2.8 vs lib/rspec_html/search.rb in rspec-html-0.2.9

- old
+ new

@@ -1,9 +1,10 @@ # frozen_string_literal: true module RSpecHTML # Provides element/attribute/text searching for HTML entities + # rubocop:disable Metrics/ClassLength class Search def initialize(element, siblings) @element = element @siblings = siblings end @@ -93,21 +94,27 @@ def zero_index_error raise ArgumentError, 'Index for matched sets starts at 1, not 0.' end def where(tag, query, all: false) - matched = if query[:class] - where_class(tag, query[:class]) & where_xpath(tag, query.merge(class: nil)) - else - where_xpath(tag, query) - end + matched = matched_from_query(tag, query) return nil unless matched || all return matched&.first unless all matched end + def matched_from_query(tag, query) + if query.is_a?(String) + @element&.css("#{tag}#{query}") + elsif query[:class] + where_class(tag, query[:class]) & where_xpath(tag, query.merge(class: nil)) + else + where_xpath(tag, query) + end + end + def where_xpath(tag, query) conditions = "[#{where_conditions(query)}]" unless query.compact.empty? @element&.xpath("//#{tag}#{conditions}") end @@ -130,6 +137,7 @@ return @element&.css(tag.to_s)&.first unless all @element&.css(tag.to_s) end end + # rubocop:enable Metrics/ClassLength end