class Hatemile::Util::Css::Rcp::RCPParser

The RCPParser class is official implementation of Hatemile::Util::Css::StyleSheetParser for Ruby CSS Parser.

Public Class Methods

new(css_or_hp, current_url = nil) click to toggle source

Initializes a new object that encapsulate the Ruby CSS Parser.

@param css_or_hp [String, Hatemile::Util::Html::HTMLDOMParser] The

HTML parser or CSS code of page.

@param current_url [String] The current URL of page.

# File lib/hatemile/util/css/rcp/rcp_parser.rb, line 73
def initialize(css_or_hp, current_url = nil)
  Hatemile::Helper.require_not_nil(css_or_hp)
  Hatemile::Helper.require_valid_type(
    css_or_hp,
    Hatemile::Util::Html::HTMLDOMParser,
    String
  )
  Hatemile::Helper.require_valid_type(current_url, String)

  @css_parser = CssParser::Parser.new
  if css_or_hp.is_a?(String)
    @css_parser.load_string!(css_or_hp)
  else
    load_stylesheets(css_or_hp, current_url)
  end
end

Public Instance Methods

get_rules(properties = nil) click to toggle source

@see Hatemile::Util::Css::StyleSheetParser#get_rules

# File lib/hatemile/util/css/rcp/rcp_parser.rb, line 92
def get_rules(properties = nil)
  rules = []
  @css_parser.each_rule_set do |rule|
    auxiliar_rule = RCPRule.new(rule)

    if properties.nil?
      rules.push(auxiliar_rule)
      next
    end

    properties.each do |property_name|
      if auxiliar_rule.has_property?(property_name)
        rules.push(auxiliar_rule)
        break
      end
    end
  end
  rules
end

Protected Instance Methods

load_stylesheets(html_parser, current_url) click to toggle source

Load the stylesheets of page.

@param html_parser [Hatemile::Util::Html::HTMLDOMParser] The HTML

parser.

@param current_url [String] The current URL of page.

# File lib/hatemile/util/css/rcp/rcp_parser.rb, line 50
def load_stylesheets(html_parser, current_url)
  elements = html_parser.find(
    'style,link[rel="stylesheet"]'
  ).list_results
  elements.each do |element|
    if element.get_tag_name == 'STYLE'
      @css_parser.load_string!(element.get_text_content)
    else
      @css_parser.load_uri!(
        URI.join(current_url, element.get_attribute('href'))
      )
    end
  end
end