Class: ValidateWebsite::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/validate_website/utils.rb

Overview

Utils class for CSS helpers

Class Method Summary collapse

Class Method Details

.extract_urls_from_css(page) ⇒ Set

Extract urls from CSS page

Parameters:

  • a (Spidr::Page)

    Spidr::Page object

Returns:

  • (Set)

    Lists of urls


12
13
14
15
16
17
18
# File 'lib/validate_website/utils.rb', line 12

def self.extract_urls_from_css(page)
  return Set[] unless page
  return Set[] if page.body.nil?

  nodes = Crass::Parser.parse_stylesheet(page.body)
  extract_urls_from_nodes nodes, page
end

.extract_urls_from_nodes(nodes, page, memo = Set[]) ⇒ Set

Extract urls from Crass nodes

Parameters:

  • Array (Array)

    of nodes from Crass

  • a (Spidr::Page)

    Spidr::Page object

  • memo (Set) (defaults to: Set[])

    for recursivity

Returns:

  • (Set)

    list of urls


41
42
43
44
45
46
47
48
49
# File 'lib/validate_website/utils.rb', line 41

def self.extract_urls_from_nodes(nodes, page, memo = Set[])
  nodes.each_with_object(memo) do |node, result|
    result.merge urls_to_absolute(node, page)
    if node[:children]
      extract_urls_from_nodes node.delete(:children), page, result
    end
    result
  end
end

.urls_to_absolute(node, page) ⇒ Set

Return urls as absolute from Crass nodes

Parameters:

  • node (Hash)

    from Crass

  • a (Spidr::Page)

    Spidr::Page object

Returns:

  • (Set)

    list of obsolute urls


25
26
27
28
29
30
31
32
33
34
# File 'lib/validate_website/utils.rb', line 25

def self.urls_to_absolute(node, page)
  if node[:node] == :function && node[:name] == 'url' || node[:node] == :url
    Array(node[:value]).map do |v|
      url = v.is_a?(String) ? v : v[:value]
      page.to_absolute(url).to_s
    end
  else
    Set.new
  end
end