Sha256: 5723c8e99974078fbad18a0a353ce5d0c9789930b1fe332a55d9360f9deadb99

Contents?: true

Size: 1.51 KB

Versions: 2

Compression:

Stored size: 1.51 KB

Contents

module WatirCss
  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)
            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

2 entries across 2 versions & 1 rubygems

Version Path
watir_css-0.2.0 lib/watir_css/locators/element/selector_builder/css.rb
watir_css-0.1.0 lib/watir_css/locators/element/selector_builder/css.rb