Sha256: 20dd28cf6f94acdd46a8fb3b2260afc98ee6d4aaca8960c9c114ef332c809334

Contents?: true

Size: 1.56 KB

Versions: 6

Compression:

Stored size: 1.56 KB

Contents

module Watir
  module Locators
    class Element
      class SelectorBuilder
        class CSS
          def build(selectors)
            return unless use_css?(selectors)

            if selectors.empty?
              css = '*'
            else
              css = ''
              css << (selectors.delete(:tag_name) || '')

              klass = selectors.delete(:class)
              if klass
                if klass.include? ' '
                  css << %([class="#{css_escape klass}"])
                else
                  css << ".#{klass}"
                end
              end

              href = selectors.delete(:href)
              if href
                css << %([href~="#{css_escape href}"])
              end

              selectors.each do |key, value|
                key = key.to_s.tr("_", "-")
                css << %([#{key}="#{css_escape value}"]) # TODO: proper escaping
              end
            end

            [:css, css]
          end

          private

          def use_css?(selectors)
            return false unless Watir.prefer_css?

            if selectors.key?(:text) || selectors.key?(:label) || selectors.key?(:index)
              return false
            end

            if selectors[:tag_name] == 'input' && selectors.key?(:type)
              return false
            end

            if selectors.key?(:class) && selectors[:class] !~ /^[\w-]+$/ui
              return false
            end

            true
          end

          def css_escape(str)
            str.gsub('"', '\\"')
          end
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
watir-webdriver-0.9.9 lib/watir-webdriver/locators/element/selector_builder/css.rb
watir-6.0.0.beta3 lib/watir/locators/element/selector_builder/css.rb
watir-6.0.0.beta2 lib/watir/locators/element/selector_builder/css.rb
watir-6.0.0.beta1 lib/watir/locators/element/selector_builder/css.rb
watir-webdriver-0.9.3 lib/watir-webdriver/locators/element/selector_builder/css.rb
watir-webdriver-0.9.2 lib/watir-webdriver/locators/element/selector_builder/css.rb