Class: StyleStats::Css

Inherits:
Object
  • Object
show all
Defined in:
lib/style_stats/css.rb,
lib/style_stats/css/fetch.rb,
lib/style_stats/css/analyze.rb,
lib/style_stats/css/selector.rb,
lib/style_stats/css/declaration.rb,
lib/style_stats/css/aggregate_declaration.rb

Defined Under Namespace

Classes: AggregateDeclaration, Declaration, Fetch, Selector

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil) ⇒ Css

Returns a new instance of Css.



11
12
13
14
15
16
17
18
19
20
21
# 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

Instance Attribute Details

#elementsObject

Returns the value of attribute elements.



9
10
11
# File 'lib/style_stats/css.rb', line 9

def elements
  @elements
end

#media_typesObject

Returns the value of attribute media_types.



9
10
11
# File 'lib/style_stats/css.rb', line 9

def media_types
  @media_types
end

#pathObject

Returns the value of attribute path.



9
10
11
# File 'lib/style_stats/css.rb', line 9

def path
  @path
end

#pathsObject

Returns the value of attribute paths.



9
10
11
# File 'lib/style_stats/css.rb', line 9

def paths
  @paths
end

#rulesObject

Returns the value of attribute rules.



9
10
11
# File 'lib/style_stats/css.rb', line 9

def rules
  @rules
end

#selectorsObject

Returns the value of attribute selectors.



9
10
11
# File 'lib/style_stats/css.rb', line 9

def selectors
  @selectors
end

#stylesheetsObject

Returns the value of attribute stylesheets.



9
10
11
# File 'lib/style_stats/css.rb', line 9

def stylesheets
  @stylesheets
end

Instance Method Details

#[](property) ⇒ Object



90
91
92
# File 'lib/style_stats/css.rb', line 90

def [](property)
  aggregate_declaration[property]
end

#aggregate_declarationObject



98
99
100
# File 'lib/style_stats/css.rb', line 98

def aggregate_declaration
  @aggregate_declaration ||= aggregate
end

#analyzeObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 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_aggregateObject



94
95
96
# File 'lib/style_stats/css.rb', line 94

def clear_aggregate
  @aggregate_declaration = nil
end

#data_uri_sizeObject



38
39
40
# 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

#declarationsObject



102
103
104
# File 'lib/style_stats/css.rb', line 102

def declarations
  self.selectors.map(&:declarations).flatten
end

#declarations_count(type = nil) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# 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_sizeObject



46
47
48
# File 'lib/style_stats/css.rb', line 46

def gzipped_size
  Zlib::Deflate.deflate(self.stylesheets.join + self.elements.join).size
end

#merge(css) ⇒ Object



23
24
25
# File 'lib/style_stats/css.rb', line 23

def merge(css)
  dup.merge!(css)
end

#merge!(css) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# 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) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# 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

#sizeObject



42
43
44
# File 'lib/style_stats/css.rb', line 42

def size
  (self.stylesheets.join + self.elements.join).size
end

#sort_selector_by_declarations_countObject



50
51
52
# 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