Sha256: ab53cb8e266ffc65084f8f23300ed219b10dd78b5cb0948693a61941233a6ce0

Contents?: true

Size: 1.34 KB

Versions: 8

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true

module Rails
  module Dom
    module Testing
      module Assertions
        module SelectorAssertions
          class SubstitutionContext # :nodoc:
            def initialize
              @substitute = "?"
            end

            def substitute!(selector, values, format_for_presentation = false)
              selector.gsub @substitute do |match|
                next match[0] if values.empty? || !substitutable?(values.first)
                matcher_for(values.shift, format_for_presentation)
              end
            end

            def match(matches, attribute, matcher)
              matches.find_all { |node| node[attribute] =~ Regexp.new(matcher) }
            end

            private
              def matcher_for(value, format_for_presentation)
                # Nokogiri doesn't like arbitrary values without quotes, hence inspect.
                if format_for_presentation
                  value.inspect # Avoid to_s so Regexps aren't put in quotes.
                elsif value.is_a?(Regexp)
                  "\"#{value}\""
                else
                  value.to_s.inspect
                end
              end

              def substitutable?(value)
                [ Symbol, Numeric, String, Regexp ].any? { |type| value.is_a? type }
              end
          end
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 6 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rails-dom-testing-2.2.0/lib/rails/dom/testing/assertions/selector_assertions/substitution_context.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rails-dom-testing-2.2.0/lib/rails/dom/testing/assertions/selector_assertions/substitution_context.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rails-dom-testing-2.2.0/lib/rails/dom/testing/assertions/selector_assertions/substitution_context.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rails-dom-testing-2.2.0/lib/rails/dom/testing/assertions/selector_assertions/substitution_context.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rails-dom-testing-2.2.0/lib/rails/dom/testing/assertions/selector_assertions/substitution_context.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rails-dom-testing-2.2.0/lib/rails/dom/testing/assertions/selector_assertions/substitution_context.rb
tinymce-rails-7.1.2 vendor/bundle/ruby/3.3.0/gems/rails-dom-testing-2.2.0/lib/rails/dom/testing/assertions/selector_assertions/substitution_context.rb
rails-dom-testing-2.2.0 lib/rails/dom/testing/assertions/selector_assertions/substitution_context.rb