class StyleStats::Css

Attributes

elements[RW]
media_types[RW]
path[RW]
paths[RW]
rules[RW]
selectors[RW]
stylesheets[RW]

Public Class Methods

new(path = nil) click to toggle source
# File lib/style_stats/css.rb, line 11
def initialize(path = nil)
  self.path = path
  self.paths = path ? [path] : []
  self.rules = []
  self.media_types = []
  self.selectors = []
  self.stylesheets = []
  self.elements = []

  parse if path
end

Public Instance Methods

[](property) click to toggle source
# File lib/style_stats/css.rb, line 90
def [](property)
  aggregate_declaration[property]
end
aggregate_declaration() click to toggle source
# File lib/style_stats/css.rb, line 98
def aggregate_declaration
  @aggregate_declaration ||= aggregate
end
analyze() click to toggle source
# File lib/style_stats/css/analyze.rb, line 3
def analyze
  @result = {}
  @selector = sort_selector_by_declarations_count.first
  @most_indentifier_selector = selectors.first || StyleStats::Css::Selector.new("")
  
  analyze_published
  analyze_paths
  analyze_stylesheets
  analyze_style_elements
  analyze_size
  analyze_data_uri_size
  analyze_ratio_of_data_uri_size
  analyze_gzipped_size
  analyze_rules
  analyze_selectors
  analyze_declarations
  analyze_simplicity
  analyze_average_of_identifier
  analyze_most_identifier
  analyze_most_identifier_selector
  analyze_average_of_cohesion
  analyze_lowest_cohesion
  analyze_lowest_cohesion_selector
  analyze_total_unique_font_sizes
  analyze_unique_font_sizes
  analyze_total_unique_font_families
  analyze_unique_font_families
  analyze_total_unique_colors
  analyze_unique_colors
  analyze_id_selectors
  analyze_universal_selectors
  analyze_unqualified_attribute_selectors
  analyze_javascript_specific_selectors
  analyze_user_specific_selectors
  analyze_important_keywords
  analyze_fload_properties
  analyze_properties_count
  analyze_media_queries
  @result
end
clear_aggregate() click to toggle source
# File lib/style_stats/css.rb, line 94
def clear_aggregate
  @aggregate_declaration = nil
end
data_uri_size() click to toggle source
# File lib/style_stats/css.rb, line 38
def data_uri_size
  declarations.inject(0) { |sum, declaration| sum += declaration.value.match(/data\:image\/[A-Za-z0-9;,\+\=\/]+/).to_s.size }
end
declarations() click to toggle source
# File lib/style_stats/css.rb, line 102
def declarations
  self.selectors.map(&:declarations).flatten
end
declarations_count(type = nil) click to toggle source
# File lib/style_stats/css.rb, line 79
def declarations_count(type = nil)
  case type
  when :important
    declarations.map(&:important).count { |important| important }
  when :float
    declarations.count { |declaration| declaration.property.match(/float/) }
  else
    declarations.count
  end
end
gzipped_size() click to toggle source
# File lib/style_stats/css.rb, line 46
def gzipped_size
  Zlib::Deflate.deflate(self.stylesheets.join + self.elements.join).size
end
merge(css) click to toggle source
# File lib/style_stats/css.rb, line 23
def merge(css)
  dup.merge!(css)
end
merge!(css) click to toggle source
# File lib/style_stats/css.rb, line 27
def merge!(css)
  clear_aggregate
  self.paths.push(css.path) if css.path
  self.rules += css.rules
  self.media_types += css.media_types
  self.selectors += css.selectors
  self.stylesheets += css.stylesheets
  self.elements += css.elements
  self
end
selectors_count(type = nil) click to toggle source
# File lib/style_stats/css.rb, line 54
def selectors_count(type = nil)
  case type
  when :id
    selectors.count { |selector| selector.name.match(/#/) }
  when :universal
    selectors.count { |selector| selector.name.match(/\*/) }
  when :unqualified
    selectors.count { |selector| selector.name.match(/\[.+\]$/) }
  when :js
    if StyleStats.configuration.options[:javascriptSpecificSelectors]
      selectors.count { |selector| selector.name.match(/#{StyleStats.configuration.options[:javascriptSpecificSelectors]}/) }
    else
      0
    end
  when :user
    if StyleStats.configuration.options[:userSpecifiedSelectors]
      selectors.count { |selector| selector.name.match(/#{StyleStats.configuration.options[:userSpecifiedSelectors]}/) }
    else
      0
    end
  else
    selectors.count
  end
end
size() click to toggle source
# File lib/style_stats/css.rb, line 42
def size
  (self.stylesheets.join + self.elements.join).size
end
sort_selector_by_declarations_count() click to toggle source
# File lib/style_stats/css.rb, line 50
def sort_selector_by_declarations_count
  self.selectors.sort { |a, b| b.declarations.count <=> a.declarations.count }
end

Private Instance Methods

aggregate() click to toggle source
# File lib/style_stats/css.rb, line 142
def aggregate
  aggregate_declaration = AggregateDeclaration.new
  declarations.each { |declaration| aggregate_declaration.add(declaration.property, declaration.value) }
  aggregate_declaration
end
analyze_average_of_cohesion() click to toggle source
# File lib/style_stats/css/analyze.rb, line 105
def analyze_average_of_cohesion
  @result["Average of Cohesion"] = declarations.count.fdiv(rules.count).round(3) if StyleStats.configuration.options[:averageOfCohesion]
end
analyze_average_of_identifier() click to toggle source
# File lib/style_stats/css/analyze.rb, line 93
def analyze_average_of_identifier
  @result["Average of Identifier"] = selectors.map(&:identifier_count).inject(0, :+).fdiv(selectors.count).round(3) if StyleStats.configuration.options[:averageOfIdentifier]
end
analyze_data_uri_size() click to toggle source
# File lib/style_stats/css/analyze.rb, line 65
def analyze_data_uri_size
  @result["Data URI Size"] = data_uri_size if StyleStats.configuration.options[:dataUriSize]
end
analyze_declarations() click to toggle source
# File lib/style_stats/css/analyze.rb, line 85
def analyze_declarations
  @result["Declarations"] = declarations.count if StyleStats.configuration.options[:declarations]
end
analyze_fload_properties() click to toggle source
# File lib/style_stats/css/analyze.rb, line 165
def analyze_fload_properties
  @result["Float Properties"] = declarations_count(:float) if StyleStats.configuration.options[:floatProperties]
end
analyze_gzipped_size() click to toggle source
# File lib/style_stats/css/analyze.rb, line 73
def analyze_gzipped_size
  @result["Gzipped Size"] = gzipped_size if StyleStats.configuration.options[:gzippedSize]
end
analyze_id_selectors() click to toggle source
# File lib/style_stats/css/analyze.rb, line 141
def analyze_id_selectors
  @result["ID Selectors"] = selectors_count(:id) if StyleStats.configuration.options[:idSelectors]
end
analyze_important_keywords() click to toggle source
# File lib/style_stats/css/analyze.rb, line 161
def analyze_important_keywords
  @result["Important Keywords"] = declarations_count(:important) if StyleStats.configuration.options[:importantKeywords]
end
analyze_javascript_specific_selectors() click to toggle source
# File lib/style_stats/css/analyze.rb, line 153
def analyze_javascript_specific_selectors
  @result["JavaScript Specific Selectors"] = selectors_count(:js) if StyleStats.configuration.options[:javascriptSpecificSelectors]
end
analyze_lowest_cohesion() click to toggle source
# File lib/style_stats/css/analyze.rb, line 109
def analyze_lowest_cohesion
  @result["Lowest Cohesion"] = @selector.declarations.count if StyleStats.configuration.options[:lowestCohesion]
end
analyze_lowest_cohesion_selector() click to toggle source
# File lib/style_stats/css/analyze.rb, line 113
def analyze_lowest_cohesion_selector
  @result["Lowest Cohesion Selector"] = @selector.name if StyleStats.configuration.options[:lowestCohesionSelector]
end
analyze_media_queries() click to toggle source
# File lib/style_stats/css/analyze.rb, line 173
def analyze_media_queries
  @result["Media Queries"] = media_types.count if StyleStats.configuration.options[:mediaQueries]
end
analyze_most_identifier() click to toggle source
# File lib/style_stats/css/analyze.rb, line 97
def analyze_most_identifier
  @result["Most Identifier"] = @most_indentifier_selector.identifier_count if StyleStats.configuration.options[:mostIdentifier]
end
analyze_most_identifier_selector() click to toggle source
# File lib/style_stats/css/analyze.rb, line 101
def analyze_most_identifier_selector
  @result["Most Identifier Selector"] = @most_indentifier_selector.name if StyleStats.configuration.options[:mostIdentifierSelector]
end
analyze_paths() click to toggle source
# File lib/style_stats/css/analyze.rb, line 49
def analyze_paths
  @result["Paths"] = paths if StyleStats.configuration.options[:paths]
end
analyze_properties_count() click to toggle source
# File lib/style_stats/css/analyze.rb, line 169
def analyze_properties_count
  @result["Properties Count"] = aggregate_declaration.declarations.sort { |(_, v1), (_, v2)| v2[:count] <=> v1[:count] }.take(10).map{ |property, declaration| "#{property}: #{declaration[:count]}" } if StyleStats.configuration.options[:propertiesCount]
end
analyze_published() click to toggle source
# File lib/style_stats/css/analyze.rb, line 45
def analyze_published
  @result["Published"] = Time.now if StyleStats.configuration.options[:published]
end
analyze_ratio_of_data_uri_size() click to toggle source
# File lib/style_stats/css/analyze.rb, line 69
def analyze_ratio_of_data_uri_size
  @result["Ratio of Data URI Size"] = "#{data_uri_size.fdiv(size).round(1) * 100}%" if StyleStats.configuration.options[:ratioOfDataUriSize]
end
analyze_rules() click to toggle source
# File lib/style_stats/css/analyze.rb, line 77
def analyze_rules
  @result["Rules"] = rules.count if StyleStats.configuration.options[:rules]
end
analyze_selectors() click to toggle source
# File lib/style_stats/css/analyze.rb, line 81
def analyze_selectors
  @result["Selectors"] = selectors.count if StyleStats.configuration.options[:selectors]
end
analyze_simplicity() click to toggle source
# File lib/style_stats/css/analyze.rb, line 89
def analyze_simplicity
  @result["Simplicity"] = "#{rules.count.fdiv(selectors.count).round(1) * 100}%" if StyleStats.configuration.options[:simplicity]
end
analyze_size() click to toggle source
# File lib/style_stats/css/analyze.rb, line 61
def analyze_size
  @result["Size"] = size if StyleStats.configuration.options[:size]
end
analyze_style_elements() click to toggle source
# File lib/style_stats/css/analyze.rb, line 57
def analyze_style_elements
  @result["Style Elements"] = elements.count if StyleStats.configuration.options[:styleElements]
end
analyze_stylesheets() click to toggle source
# File lib/style_stats/css/analyze.rb, line 53
def analyze_stylesheets
  @result["Style Sheets"] = stylesheets.count if StyleStats.configuration.options[:stylesheets]
end
analyze_total_unique_colors() click to toggle source
# File lib/style_stats/css/analyze.rb, line 133
def analyze_total_unique_colors
  @result["Total Unique Colors"] = (self["color"][:values] || []).count if StyleStats.configuration.options[:totalUniqueColors]
end
analyze_total_unique_font_families() click to toggle source
# File lib/style_stats/css/analyze.rb, line 125
def analyze_total_unique_font_families
  @result["Total Unique Font Families"] = (self["font-family"][:values] || []).count if StyleStats.configuration.options[:totalUniqueFontFamilies]
end
analyze_total_unique_font_sizes() click to toggle source
# File lib/style_stats/css/analyze.rb, line 117
def analyze_total_unique_font_sizes
  @result["Total Unique Font Sizes"] = (self["font-size"][:values] || []).count if StyleStats.configuration.options[:totalUniqueFontSizes]
end
analyze_unique_colors() click to toggle source
# File lib/style_stats/css/analyze.rb, line 137
def analyze_unique_colors
  @result["Unique Colors"] = (self["color"][:values] || []) if StyleStats.configuration.options[:uniqueColors]
end
analyze_unique_font_families() click to toggle source
# File lib/style_stats/css/analyze.rb, line 129
def analyze_unique_font_families
  @result["Unique Font Families"] = (self["font-family"][:values] || []) if StyleStats.configuration.options[:uniqueFontFamilies]
end
analyze_unique_font_sizes() click to toggle source
# File lib/style_stats/css/analyze.rb, line 121
def analyze_unique_font_sizes
  @result["Unique Font Sizes"] = (self["font-size"][:values] || []) if StyleStats.configuration.options[:uniqueFontSizes]
end
analyze_universal_selectors() click to toggle source
# File lib/style_stats/css/analyze.rb, line 145
def analyze_universal_selectors
  @result["Universal Selectors"] = selectors_count(:universal) if StyleStats.configuration.options[:universalSelectors]
end
analyze_unqualified_attribute_selectors() click to toggle source
# File lib/style_stats/css/analyze.rb, line 149
def analyze_unqualified_attribute_selectors
  @result["Unqualified Attribute Selectors"] = selectors_count(:unqualified) if StyleStats.configuration.options[:unqualifiedAttributeSelectors]
end
analyze_user_specific_selectors() click to toggle source
# File lib/style_stats/css/analyze.rb, line 157
def analyze_user_specific_selectors
  @result["User Specific Selectors"] = selectors_count(:user) if StyleStats.configuration.options[:userSpecificSelectors]
end
create_css_parser(style) click to toggle source
# File lib/style_stats/css.rb, line 117
def create_css_parser(style)
  parser = CssParser::Parser.new
  parser.add_block!(style.dup)
  parser
end
merge_css_parser(parser) click to toggle source
# File lib/style_stats/css.rb, line 123
def merge_css_parser(parser)
  parser.each_rule_set do |rule, media_types|
    self.rules.push(rule)
    self.media_types.concat(media_types)
    rule.each_selector do |selector, declarations, specificity|
      declarations = declarations.split(/;(?!base64)/).map do |declaration|
        property, value = declaration.split(':', 2).map(&:strip)
        Declaration.new(property, value)
      end
      self.selectors.push(Selector.new(selector, declarations))
    end
  end

  self.media_types.uniq!
  self.media_types.delete(:all)
  self.selectors.sort! { |a, b| b.identifier_count <=> a.identifier_count }
  clear_aggregate
end
parse() click to toggle source
# File lib/style_stats/css.rb, line 107
def parse
  fetch = Fetch.new(self.path)

  self.stylesheets = fetch.stylesheets
  self.elements = fetch.elements

  parsers = (self.stylesheets + self.elements).inject([]) { |parsers, style| parsers.push(create_css_parser(style)) }
  parsers.each { |parser| merge_css_parser(parser) }
end