Class: HTMLSelector
- Inherits:
-
Object
- Object
- HTMLSelector
- Defined in:
- lib/rails/dom/testing/assertions/selector_assertions/html_selector.rb
Overview
:nodoc:
Constant Summary collapse
- NO_STRIP =
%w{pre script style textarea}
Instance Attribute Summary collapse
-
#equality_tests ⇒ Object
Returns the value of attribute equality_tests.
-
#message ⇒ Object
Returns the value of attribute message.
-
#root ⇒ Object
Returns the value of attribute root.
-
#selector ⇒ Object
(also: #source)
Returns the value of attribute selector.
Class Method Summary collapse
Instance Method Summary collapse
- #context ⇒ Object
- #equality_tests_from(comparator) ⇒ Object
- #extract_selector(values) ⇒ Object
- #filter(matches) ⇒ Object
-
#initialize(root, args) ⇒ HTMLSelector
constructor
A new instance of HTMLSelector.
- #select ⇒ Object
Constructor Details
#initialize(root, args) ⇒ HTMLSelector
Returns a new instance of HTMLSelector.
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/rails/dom/testing/assertions/selector_assertions/html_selector.rb', line 9 def initialize(root, args) @root = root @selector = extract_selector(args) @equality_tests = equality_tests_from(args.shift) @message = args.shift if args.shift raise ArgumentError, "Not expecting that last argument, you either have too many arguments, or they're the wrong type" end end |
Instance Attribute Details
#equality_tests ⇒ Object
Returns the value of attribute equality_tests.
5 6 7 |
# File 'lib/rails/dom/testing/assertions/selector_assertions/html_selector.rb', line 5 def equality_tests @equality_tests end |
#message ⇒ Object
Returns the value of attribute message.
5 6 7 |
# File 'lib/rails/dom/testing/assertions/selector_assertions/html_selector.rb', line 5 def @message end |
#root ⇒ Object
Returns the value of attribute root.
5 6 7 |
# File 'lib/rails/dom/testing/assertions/selector_assertions/html_selector.rb', line 5 def root @root end |
#selector ⇒ Object Also known as: source
Returns the value of attribute selector.
5 6 7 |
# File 'lib/rails/dom/testing/assertions/selector_assertions/html_selector.rb', line 5 def selector @selector end |
Class Method Details
.can_select_from?(selector) ⇒ Boolean
22 23 24 |
# File 'lib/rails/dom/testing/assertions/selector_assertions/html_selector.rb', line 22 def can_select_from?(selector) selector.respond_to?(:css) end |
Instance Method Details
#context ⇒ Object
94 95 96 |
# File 'lib/rails/dom/testing/assertions/selector_assertions/html_selector.rb', line 94 def context @context ||= SubstitutionContext.new end |
#equality_tests_from(comparator) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/rails/dom/testing/assertions/selector_assertions/html_selector.rb', line 66 def equality_tests_from(comparator) comparisons = {} case comparator when Hash comparisons = comparator when String, Regexp comparisons[:text] = comparator when Integer comparisons[:count] = comparator when Range comparisons[:minimum] = comparator.begin comparisons[:maximum] = comparator.end when FalseClass comparisons[:count] = 0 when NilClass, TrueClass comparisons[:minimum] = 1 else raise ArgumentError, "I don't understand what you're trying to match" end # By default we're looking for at least one match. if comparisons[:count] comparisons[:minimum] = comparisons[:maximum] = comparisons[:count] else comparisons[:minimum] ||= 1 end comparisons end |
#extract_selector(values) ⇒ Object
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/rails/dom/testing/assertions/selector_assertions/html_selector.rb', line 55 def extract_selector(values) selector = values.shift unless selector.is_a? String raise ArgumentError, "Expecting a selector as the first argument" end context.substitute!(selector, values) selector end |
#filter(matches) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/rails/dom/testing/assertions/selector_assertions/html_selector.rb', line 31 def filter(matches) match_with = equality_tests[:text] || equality_tests[:html] return matches if matches.empty? || !match_with content_mismatch = nil text_matches = equality_tests.has_key?(:text) regex_matching = match_with.is_a?(Regexp) remaining = matches.reject do |match| # Preserve markup with to_s for html elements content = text_matches ? match.text : match.children.to_s content.strip! unless NO_STRIP.include?(match.name) content.sub!(/\A\n/, '') if text_matches && match.name == "textarea" next if regex_matching ? (content =~ match_with) : (content == match_with) content_mismatch ||= sprintf("<%s> expected but was\n<%s>.", match_with, content) true end self. ||= content_mismatch if remaining.empty? Nokogiri::XML::NodeSet.new(matches.document, remaining) end |
#select ⇒ Object
27 28 29 |
# File 'lib/rails/dom/testing/assertions/selector_assertions/html_selector.rb', line 27 def select filter root.css(selector, context) end |