Class: Checker::CSS

Inherits:
Object
  • Object
show all
Defined in:
lib/checker/css.rb

Constant Summary collapse

CSS_VALID_VALUE =
/\s*[\"\']?[\w\-\s]+[\"\']?\s*/
COMMA_SEPARATED_INSPECTION_REGEX =
/^(#{CSS_VALID_VALUE})(,#{CSS_VALID_VALUE})+$/

Class Method Summary collapse

Class Method Details

.comma_separated_values_match?(inspection_value, actual_value) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/checker/css.rb', line 68

def self.comma_separated_values_match?(inspection_value, actual_value)
  inspection_value.comma_separated_words == actual_value.comma_separated_words
end

.inspect(parser, inspection, binding) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/checker/css.rb', line 32

def self.inspect(parser, inspection, binding)
  case inspection.target.to_s.split(':').size
    when 0 then inspect_selector(parser, inspection, binding)
    when 1 then inspect_property(parser, inspection, binding)
    when 2 then inspect_property_and_value(parser, inspection, binding)
    else raise "Malformed target value."
  end
end

.inspect_property(parser, inspection, binding) ⇒ Object



45
46
47
48
# File 'lib/checker/css.rb', line 45

def self.inspect_property(parser, inspection, binding)
  property, _ = parse_target(inspection.target)
  parser.dig(binding, property).present?
end

.inspect_property_and_value(parser, inspection, binding) ⇒ Object



50
51
52
53
54
# File 'lib/checker/css.rb', line 50

def self.inspect_property_and_value(parser, inspection, binding)
  property, value = parse_target(inspection.target)
  actual_value = parser.dig(binding, property) || ''
  values_match? value, actual_value
end

.inspect_selector(parser, inspection, binding) ⇒ Object



41
42
43
# File 'lib/checker/css.rb', line 41

def self.inspect_selector(parser, inspection, binding)
  parser.dig(binding).present?
end

.parse_target(target) ⇒ Object



64
65
66
# File 'lib/checker/css.rb', line 64

def self.parse_target(target)
  target.to_s.split(':')
end

.run(document, expectation, binding) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/checker/css.rb', line 23

def self.run(document, expectation, binding)
  content = document.xpath('//style').text.presence || document.text
  inspection = expectation.inspection
  parser = CssParser::Parser.new
  parser.load_string! content
  raise "Unsupported inspection #{inspection.type}" unless ['DeclaresStyle', 'DeclaresStyle:'].include? inspection.type
  inspect parser, inspection, binding
end

.values_match?(inspection_value, actual_value) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
59
60
61
62
# File 'lib/checker/css.rb', line 56

def self.values_match?(inspection_value, actual_value)
  if inspection_value =~ COMMA_SEPARATED_INSPECTION_REGEX
    comma_separated_values_match? inspection_value, actual_value
  else
    actual_value.css_values.include? inspection_value
  end
end