class CssParser::RuleSet
Constants
- BACKGROUND_PROPERTIES
- BORDER_PROPERTIES
- BORDER_STYLE_PROPERTIES
- COLON
Tokens for parse_declarations!
- DIMENSIONS
- FONT_STYLE_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
declarations[RW]
filename[RW]
the local or remote location
offset[R]
optional field for storing source reference File offset range
selectors[R]
Array of selector strings.
specificity[RW]
Integer with the specificity to use for this RuleSet
.
Public Class Methods
new(*args, selectors: nil, block: nil, offset: nil, filename: nil, specificity: nil)
click to toggle source
# File lib/css_parser/rule_set.rb, line 252 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
create_shorthand!()
click to toggle source
Create shorthand declarations (e.g. margin
or font
) whenever possible.
# File lib/css_parser/rule_set.rb, line 505 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 322 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 315 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 305 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 332 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 367 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 289 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 327 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 637 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 693 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
unmatched_open_parenthesis?(declarations)
click to toggle source
# File lib/css_parser/rule_set.rb, line 678 def unmatched_open_parenthesis?(declarations) (lparen_index = declarations.index(LPAREN)) && !declarations.index(RPAREN, lparen_index) end