class CssParser::RuleSet

Constants

BACKGROUND_PROPERTIES
BORDER_PROPERTIES
BORDER_STYLE_PROPERTIES
DIMENSIONS
FONT_STYLE_PROPERTIES
LIST_STYLE_PROPERTIES
NUMBER_OF_DIMENSIONS
RE_ELEMENTS_AND_PSEUDO_ELEMENTS

Patterns for specificity calculations

RE_NON_ID_ATTRIBUTES_AND_PSEUDO_CLASSES
WHITESPACE_REPLACEMENT

Attributes

declarations[RW]
selectors[R]

Array of selector strings.

specificity[RW]

Integer with the specificity to use for this RuleSet.

Public Class Methods

new(selectors, block, specificity = nil) click to toggle source
# File lib/css_parser/rule_set.rb, line 238
def initialize(selectors, block, specificity = nil)
  @selectors = []
  @specificity = specificity
  parse_selectors!(selectors) if selectors
  parse_declarations!(block)
end

Public Instance Methods

[](property)
Alias for: get_value
create_shorthand!() click to toggle source

Create shorthand declarations (e.g. margin or font) whenever possible.

# File lib/css_parser/rule_set.rb, line 462
def create_shorthand!
  create_background_shorthand!
  create_dimensions_shorthand!
  # border must be shortened after dimensions
  create_border_shorthand!
  create_font_shorthand!
  create_list_style_shorthand!
end
declarations_to_s(options = {}) click to toggle source

Return all declarations as a string.

# File lib/css_parser/rule_set.rb, line 279
def declarations_to_s(options = {})
  declarations.to_s(options)
end
each_declaration() { |property, value, is_important| ... } click to toggle source

Iterate through declarations.

# File lib/css_parser/rule_set.rb, line 272
def each_declaration # :yields: property, value, is_important
  declarations.each do |property_name, value|
    yield property_name, value.value, value.important
  end
end
each_selector(options = {}) { |selector, declarations, specificity| ... } click to toggle source

Iterate through selectors.

Options

  • force_important – boolean

Example

ruleset.each_selector do |sel, dec, spec|
  ...
end
# File lib/css_parser/rule_set.rb, line 262
def each_selector(options = {}) # :yields: selector, declarations, specificity
  decs = declarations.to_s(options)
  if @specificity
    @selectors.each { |sel| yield sel.strip, decs, @specificity }
  else
    @selectors.each { |sel| yield sel.strip, decs, CssParser.calculate_specificity(sel) }
  end
end
expand_shorthand!() click to toggle source

Split shorthand declarations (e.g. margin or font) into their constituent parts.

# File lib/css_parser/rule_set.rb, line 289
def expand_shorthand!
  # border must be expanded before dimensions
  expand_border_shorthand!
  expand_dimensions_shorthand!
  expand_font_shorthand!
  expand_background_shorthand!
  expand_list_style_shorthand!
end
extract_background_size_from(value) click to toggle source
# File lib/css_parser/rule_set.rb, line 324
def extract_background_size_from(value)
  size = value.slice!(CssParser::RE_BACKGROUND_SIZE)

  size.sub(%r{^\s*/\s*}, '') if size
end
get_value(property) click to toggle source

Get the value of a property

# File lib/css_parser/rule_set.rb, line 246
def get_value(property)
  return '' unless (value = declarations[property])

  "#{value};"
end
Also aliased as: []
to_s() click to toggle source

Return the CSS rule set as a string.

# File lib/css_parser/rule_set.rb, line 284
def to_s
  "#{@selectors.join(',')} { #{declarations} }"
end

Private Instance Methods

compute_dimensions_shorthand(values) click to toggle source
# File lib/css_parser/rule_set.rb, line 594
def compute_dimensions_shorthand(values)
  # All four sides are equal, returning single value
  return [:top] if values.values.uniq.count == 1

  # `/* top | right | bottom | left */`
  return [:top, :right, :bottom, :left] if values[:left] != values[:right]

  # Vertical are the same & horizontal are the same, `/* vertical | horizontal */`
  return [:top, :left] if values[:top] == values[:bottom]

  [:top, :left, :bottom]
end
split_value_preserving_function_whitespace(value) click to toggle source
# File lib/css_parser/rule_set.rb, line 638
def split_value_preserving_function_whitespace(value)
  split_value = value.gsub(RE_FUNCTIONS) do |c|
    c.gsub!(/\s+/, WHITESPACE_REPLACEMENT)
    c
  end

  matches = split_value.strip.split(/\s+/)

  matches.each do |c|
    c.gsub!(WHITESPACE_REPLACEMENT, ' ')
  end
end