Class: Hexp::CssSelector::Attribute Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, operator, value) ⇒ Attribute

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Construct a new Attribute selector

The attributes directly mimic those returned from the SASS parser, even though we don’t use all of them.

Parameters:

  • name (String)

    Name of the attribute, like ‘href’

  • operator (nil, String)

    Operator like ‘~=’, ‘^=’, … Use blank to simply test attribute presence.

  • value (String)

    Value to test for, operator dependent



288
289
290
291
292
# File 'lib/hexp/css_selector.rb', line 288

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

Instance Attribute Details

#flagsString (readonly)

Returns:

  • (String)


269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/hexp/css_selector.rb', line 269

class Attribute
  include Equalizer.new(:name, :operator, :value)

  attr_reader :name, :operator, :value

  # Construct a new Attribute selector
  #
  # The attributes directly mimic those returned from the SASS parser, even
  # though we don't use all of them.
  #
  # @param [String] name
  #   Name of the attribute, like 'href'
  # @param [nil, String] operator
  #   Operator like '~=', '^=', ... Use blank to simply test attribute
  #   presence.
  # @param [String] value
  #   Value to test for, operator dependent
  #
  # @api private
  def initialize(name, operator, value)
    @name      = name.freeze
    @operator  = operator.freeze
    @value     = value.freeze
  end

  # Debugging representation
  #
  # @return [String]
  #
  # @api private
  def inspect
    "<#{self.class.name.split('::').last} name=#{name} operator=#{operator.inspect} value=#{value.inspect}>"
  end

  # Does the node match this attribute selector
  #
  # @param [Hexp::Node] element
  #   node to test against
  #
  # @return [true, false]
  #
  # @api private
  def matches?(element)
    return false unless element[name]
    attribute = element[name]

    value = self.value
    value = $1.gsub('\"', '"') if value =~ /\A"?(.*?)"?\z/

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

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

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

#nameString (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns The attribute name.

Returns:

  • (String)

    The attribute name



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/hexp/css_selector.rb', line 269

class Attribute
  include Equalizer.new(:name, :operator, :value)

  attr_reader :name, :operator, :value

  # Construct a new Attribute selector
  #
  # The attributes directly mimic those returned from the SASS parser, even
  # though we don't use all of them.
  #
  # @param [String] name
  #   Name of the attribute, like 'href'
  # @param [nil, String] operator
  #   Operator like '~=', '^=', ... Use blank to simply test attribute
  #   presence.
  # @param [String] value
  #   Value to test for, operator dependent
  #
  # @api private
  def initialize(name, operator, value)
    @name      = name.freeze
    @operator  = operator.freeze
    @value     = value.freeze
  end

  # Debugging representation
  #
  # @return [String]
  #
  # @api private
  def inspect
    "<#{self.class.name.split('::').last} name=#{name} operator=#{operator.inspect} value=#{value.inspect}>"
  end

  # Does the node match this attribute selector
  #
  # @param [Hexp::Node] element
  #   node to test against
  #
  # @return [true, false]
  #
  # @api private
  def matches?(element)
    return false unless element[name]
    attribute = element[name]

    value = self.value
    value = $1.gsub('\"', '"') if value =~ /\A"?(.*?)"?\z/

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

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

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

#namespaceString (readonly)

Returns:

  • (String)


269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/hexp/css_selector.rb', line 269

class Attribute
  include Equalizer.new(:name, :operator, :value)

  attr_reader :name, :operator, :value

  # Construct a new Attribute selector
  #
  # The attributes directly mimic those returned from the SASS parser, even
  # though we don't use all of them.
  #
  # @param [String] name
  #   Name of the attribute, like 'href'
  # @param [nil, String] operator
  #   Operator like '~=', '^=', ... Use blank to simply test attribute
  #   presence.
  # @param [String] value
  #   Value to test for, operator dependent
  #
  # @api private
  def initialize(name, operator, value)
    @name      = name.freeze
    @operator  = operator.freeze
    @value     = value.freeze
  end

  # Debugging representation
  #
  # @return [String]
  #
  # @api private
  def inspect
    "<#{self.class.name.split('::').last} name=#{name} operator=#{operator.inspect} value=#{value.inspect}>"
  end

  # Does the node match this attribute selector
  #
  # @param [Hexp::Node] element
  #   node to test against
  #
  # @return [true, false]
  #
  # @api private
  def matches?(element)
    return false unless element[name]
    attribute = element[name]

    value = self.value
    value = $1.gsub('\"', '"') if value =~ /\A"?(.*?)"?\z/

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

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

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

#operatorString (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns The operator that works on an attribute value.

Returns:

  • (String)

    The operator that works on an attribute value



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/hexp/css_selector.rb', line 269

class Attribute
  include Equalizer.new(:name, :operator, :value)

  attr_reader :name, :operator, :value

  # Construct a new Attribute selector
  #
  # The attributes directly mimic those returned from the SASS parser, even
  # though we don't use all of them.
  #
  # @param [String] name
  #   Name of the attribute, like 'href'
  # @param [nil, String] operator
  #   Operator like '~=', '^=', ... Use blank to simply test attribute
  #   presence.
  # @param [String] value
  #   Value to test for, operator dependent
  #
  # @api private
  def initialize(name, operator, value)
    @name      = name.freeze
    @operator  = operator.freeze
    @value     = value.freeze
  end

  # Debugging representation
  #
  # @return [String]
  #
  # @api private
  def inspect
    "<#{self.class.name.split('::').last} name=#{name} operator=#{operator.inspect} value=#{value.inspect}>"
  end

  # Does the node match this attribute selector
  #
  # @param [Hexp::Node] element
  #   node to test against
  #
  # @return [true, false]
  #
  # @api private
  def matches?(element)
    return false unless element[name]
    attribute = element[name]

    value = self.value
    value = $1.gsub('\"', '"') if value =~ /\A"?(.*?)"?\z/

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

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

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

#valueString (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns The value to match against.

Returns:

  • (String)

    The value to match against



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/hexp/css_selector.rb', line 269

class Attribute
  include Equalizer.new(:name, :operator, :value)

  attr_reader :name, :operator, :value

  # Construct a new Attribute selector
  #
  # The attributes directly mimic those returned from the SASS parser, even
  # though we don't use all of them.
  #
  # @param [String] name
  #   Name of the attribute, like 'href'
  # @param [nil, String] operator
  #   Operator like '~=', '^=', ... Use blank to simply test attribute
  #   presence.
  # @param [String] value
  #   Value to test for, operator dependent
  #
  # @api private
  def initialize(name, operator, value)
    @name      = name.freeze
    @operator  = operator.freeze
    @value     = value.freeze
  end

  # Debugging representation
  #
  # @return [String]
  #
  # @api private
  def inspect
    "<#{self.class.name.split('::').last} name=#{name} operator=#{operator.inspect} value=#{value.inspect}>"
  end

  # Does the node match this attribute selector
  #
  # @param [Hexp::Node] element
  #   node to test against
  #
  # @return [true, false]
  #
  # @api private
  def matches?(element)
    return false unless element[name]
    attribute = element[name]

    value = self.value
    value = $1.gsub('\"', '"') if value =~ /\A"?(.*?)"?\z/

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

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

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

Instance Method Details

#inspectString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Debugging representation

Returns:

  • (String)


299
300
301
# File 'lib/hexp/css_selector.rb', line 299

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

#matches?(element) ⇒ true, false

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Does the node match this attribute selector

Parameters:

Returns:

  • (true, false)


311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/hexp/css_selector.rb', line 311

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

  value = self.value
  value = $1.gsub('\"', '"') if value =~ /\A"?(.*?)"?\z/

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

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

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