Class: Hexp::CssSelector::Attribute Private
- Inherits:
-
Object
- Object
- Hexp::CssSelector::Attribute
- 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
- #flags ⇒ String readonly
-
#name ⇒ String
readonly
private
The attribute name.
- #namespace ⇒ String readonly
-
#operator ⇒ String
readonly
private
The operator that works on an attribute value.
-
#value ⇒ String
readonly
private
The value to match against.
Instance Method Summary collapse
-
#initialize(name, operator, value) ⇒ Attribute
constructor
private
Construct a new Attribute selector.
-
#inspect ⇒ String
private
Debugging representation.
-
#matches?(element) ⇒ true, false
private
Does the node match this attribute selector.
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.
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
#flags ⇒ String (readonly)
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 |
#name ⇒ String (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.
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 |
#namespace ⇒ String (readonly)
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 |
#operator ⇒ String (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.
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 |
#value ⇒ String (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.
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
#inspect ⇒ String
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
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
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 |