class CssParser::RuleSet
Constants
- BACKGROUND_PROPERTIES
- BORDER_PROPERTIES
- BORDER_STYLE_PROPERTIES
- COLON
-
Tokens for parse_declarations!
- DIMENSIONS
- DIMENSION_DIRECTIONS
- FONT_STYLE_PROPERTIES
- FONT_WEIGHT_PROPERTIES
- IMPORTANT
- LIST_STYLE_PROPERTIES
- LPAREN
- NUMBER_OF_DIMENSIONS
- RE_ELEMENTS_AND_PSEUDO_ELEMENTS
-
Patterns for specificity calculations
- RE_NON_ID_ATTRIBUTES_AND_PSEUDO_CLASSES
- RPAREN
- SEMICOLON
- WHITESPACE_REPLACEMENT
Attributes
the local or remote location
optional field for storing source reference File offset range
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 257 def initialize(*args, selectors: nil, block: nil, offset: nil, filename: nil, specificity: nil) # rubocop:disable Metrics/ParameterLists if args.any? if selectors || block || offset || filename || specificity raise ArgumentError, "don't mix positional and keyword arguments" end warn '[DEPRECATION] positional arguments are deprecated use keyword instead.', uplevel: 1 case args.length when 2 selectors, block = args when 3 selectors, block, specificity = args when 4 filename, offset, selectors, block = args when 5 filename, offset, selectors, block, specificity = args else raise ArgumentError end end @selectors = [] @specificity = specificity unless offset.nil? == filename.nil? raise ArgumentError, 'require both offset and filename or no offset and no filename' end @offset = offset @filename = filename parse_selectors!(selectors) if selectors parse_declarations!(block) end
Public Instance Methods
Source
# File lib/css_parser/rule_set.rb, line 510 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 327 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 320 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 310 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 337 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 372 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 294 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 332 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 642 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 DIMENSION_DIRECTIONS 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 698 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
Source
# File lib/css_parser/rule_set.rb, line 683 def unmatched_open_parenthesis?(declarations) (lparen_index = declarations.index(LPAREN)) && !declarations.index(RPAREN, lparen_index) end