Class: Hexp::CssSelector::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/hexp/css_selector.rb

Overview

An attribute selector, like [href^="http://"]

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Attribute) initialize(name, namespace, operator, value, flags)

A new instance of Attribute



164
165
166
167
168
169
170
# File 'lib/hexp/css_selector.rb', line 164

def initialize(name, namespace, operator, value, flags)
  @name = name.freeze
  @namespace = namespace.freeze
  @operator = operator.freeze
  @value = value.freeze
  @flag = flags.freeze
end

Instance Attribute Details

- (Object) flags (readonly)

Returns the value of attribute flags



162
163
164
# File 'lib/hexp/css_selector.rb', line 162

def flags
  @flags
end

- (Object) name (readonly)

Returns the value of attribute name



162
163
164
# File 'lib/hexp/css_selector.rb', line 162

def name
  @name
end

- (Object) namespace (readonly)

Returns the value of attribute namespace



162
163
164
# File 'lib/hexp/css_selector.rb', line 162

def namespace
  @namespace
end

- (Object) operator (readonly)

Returns the value of attribute operator



162
163
164
# File 'lib/hexp/css_selector.rb', line 162

def operator
  @operator
end

- (Object) value (readonly)

Returns the value of attribute value



162
163
164
# File 'lib/hexp/css_selector.rb', line 162

def value
  @value
end

Instance Method Details

- (Object) inspect



172
173
174
# File 'lib/hexp/css_selector.rb', line 172

def inspect
  "<#{self.class.name.split('::').last} name=#{name} namespace=#{namespace.inspect} operator=#{operator.inspect} value=#{value.inspect} flags=#{flags.inspect}>"
end

- (Boolean) matches?(element)

Returns:

  • (Boolean)


176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/hexp/css_selector.rb', line 176

def matches?(element)
  return false unless element[name]
  attribute = element[name]

  case operator
    # CSS 2
  when nil
    true
  when '='  # exact match
    attribute == value
  when '~=' # space separated list contains
    attribute.split(' ').include?(value)
  when '|=' # equal to, or starts with followed by a dash
    attribute =~ /\A#{Regexp.escape(value)}(-|\z)/

    # CSS 3
  when '^=' # starts with
    attribute.index(value) == 0
  when '$=' # ends with
    attribute =~ /#{Regexp.escape(value)}\z/
  when '*=' # contains
    !!(attribute =~ /#{Regexp.escape(value)}/)

  else
    raise "Unknown operator : #{operator}"
  end
end