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
Array of selector strings.
Integer with the specificity to use for this RuleSet
.
Public Class Methods
Source
# File lib/css_parser/rule_set.rb, line 240 def initialize(selectors, block, specificity = nil) @selectors = [] @specificity = specificity parse_selectors!(selectors) if selectors parse_declarations!(block) end
Public Instance Methods
Source
# File lib/css_parser/rule_set.rb, line 464 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
Create shorthand declarations (e.g. margin
or font
) whenever possible.
Source
# File lib/css_parser/rule_set.rb, line 281 def declarations_to_s(options = {}) declarations.to_s(options) end
Return all declarations as a string.
Source
# File lib/css_parser/rule_set.rb, line 274 def each_declaration # :yields: property, value, is_important declarations.each do |property_name, value| yield property_name, value.value, value.important end end
Iterate through declarations.
Source
# File lib/css_parser/rule_set.rb, line 264 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
Iterate through selectors.
Options
-
force_important
– boolean
Example¶ ↑
ruleset.each_selector do |sel, dec, spec| ... end
Source
# File lib/css_parser/rule_set.rb, line 291 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
Split shorthand declarations (e.g. margin
or font
) into their constituent parts.
Source
# File lib/css_parser/rule_set.rb, line 326 def extract_background_size_from(value) size = value.slice!(CssParser::RE_BACKGROUND_SIZE) size.sub(%r{^\s*/\s*}, '') if size end
Source
# File lib/css_parser/rule_set.rb, line 248 def get_value(property) return '' unless (value = declarations[property]) "#{value};" end
Get the value of a property
Also aliased as: []
Source
# File lib/css_parser/rule_set.rb, line 286 def to_s "#{@selectors.join(',')} { #{declarations} }" end
Return the CSS rule set as a string.
Private Instance Methods
Source
# File lib/css_parser/rule_set.rb, line 596 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
Source
# File lib/css_parser/rule_set.rb, line 640 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