Class: CSSes

Inherits:
Object
  • Object
show all
Defined in:
lib/csses.rb

Overview

The CSSes class takes some files, figures out what HTML is referenced or generated in them, and puts those all into an array called elements.

Constant Summary collapse

VERSION =
'1.0.0'
UNFASHIONABLE_ELEMENTS =
%w(html head meta title)
RAILS_HELPERS =
{ /\blink_to\w*\b/ => 'a',
/\bimage_tag\b/ => 'img',
/\blabel\b/ => 'label',
/\bform(?:_(?:for|tag))\b/ => 'form',
/\btext_area(?:_tag)?\b/ => 'textarea',
/\btext_field(?:_tag)?\b/ => 'input',
/\bcheck_box_tag\b/ => 'input',
/\bfield_set_tag\b/ => 'fieldset',
/\bfile_field_tag\b/ => 'input',
/\bimage_submit_tag\b/ => 'input',
/\bpassword_field_tag\b/ => 'input',
/\bradio_button_tag\b/ => 'input',
/\bselect_tag\b/ => 'select',
/\bsubmit_tag\b/ => 'input' }
RAILS_HTML_ID_OR_CLASS =
/(?::(id|class))\s*=>\s*['"](\w+)['"]/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(files) ⇒ CSSes

Give it a list of files as an array (not splatted), get back, well, a CSS object, but one with an array of CSS elements as an attribute.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/csses.rb', line 31

def initialize(files)
  @elements = []
  files.each do |file|
    source = File.read(file)
    @elements << source.scan(/<(\w+)/)
    source.scan(/<(\w+)\s+[^>]*(class|id)="(\w+)"/).each do |elem|
      @elements << "#{elem[0]}#{css_sym_for_attribute(elem[1])}#{elem[2]}"
    end
    RAILS_HELPERS.each do |regex, tag|
      source.scan(/#{regex}.*/).each do |line|
        if line.match(RAILS_HTML_ID_OR_CLASS)
          @elements << "#{tag}#{css_sym_for_attribute($1)}#{$2}"
        end
        @elements << tag
      end
    end
  end
  
  @elements.flatten!
  @elements.uniq!
  @elements.sort!
  @elements -= UNFASHIONABLE_ELEMENTS
  
  apply_pseudo_classes!
end

Instance Attribute Details

#elementsObject (readonly)

Returns the value of attribute elements.



27
28
29
# File 'lib/csses.rb', line 27

def elements
  @elements
end