class Roadie::Stylesheet

Domain object that represents a stylesheet (from disc, perhaps).

It has a name and a list of {StyleBlock}s.

@attr_reader [String] name the name of the stylesheet (“stylesheets/main.css”, “Admin user styles”, etc.). The name of the stylesheet will be visible if any errors occur. @attr_reader [Array<StyleBlock>] blocks

Constants

BOM

Attributes

blocks[R]
name[R]

Public Class Methods

new(name, css) click to toggle source

Parses the CSS string into a {StyleBlock}s and stores it.

@param [String] name @param [String] css

# File lib/roadie/stylesheet.rb, line 19
def initialize(name, css)
  @name = name
  @blocks = parse_blocks(css.sub(BOM, ""))
end

Public Instance Methods

to_s() click to toggle source
# File lib/roadie/stylesheet.rb, line 24
def to_s
  blocks.join("\n")
end

Private Instance Methods

create_style_block(selector_string, rule_set, media_types) click to toggle source
# File lib/roadie/stylesheet.rb, line 46
def create_style_block(selector_string, rule_set, media_types)
  specificity = CssParser.calculate_specificity(selector_string)
  selector = Selector.new(selector_string, specificity)
  properties = []

  rule_set.each_declaration do |prop, val, important|
    properties << StyleProperty.new(prop, val, important, specificity)
  end

  StyleBlock.new(selector, properties, media_types)
end
inlinable_blocks() click to toggle source
# File lib/roadie/stylesheet.rb, line 30
def inlinable_blocks
  blocks.select(&:inlinable?)
end
parse_blocks(css) click to toggle source
# File lib/roadie/stylesheet.rb, line 34
def parse_blocks(css)
  blocks = []
  parser = setup_parser(css)
  parser.each_rule_set do |rule_set, media_types|
    rule_set.selectors.each do |selector_string|
      blocks << create_style_block(selector_string, rule_set, media_types)
    end
  end

  blocks
end
setup_parser(css) click to toggle source
# File lib/roadie/stylesheet.rb, line 58
def setup_parser(css)
  parser = CssParser::Parser.new
  # CssParser::Parser#add_block! mutates input parameter
  parser.add_block! css.dup
  parser
end