Sha256: fc9b474f33da4ae8b645efd5b98c1b6a6bd2e402115629d67d9ec4db46cd2d06

Contents?: true

Size: 1.02 KB

Versions: 1

Compression:

Stored size: 1.02 KB

Contents

require "css_parser"

class Stylesheet
  attr_accessor :uri, :unused_styles, :parser

  def initialize (uri)
    @unused_styles = Set.new
    @uri = uri
    parse_styles!
  end

  def parse_styles!
    @parser = CssParser::Parser.new
    @parser.load_uri! @uri
    @parser.each_selector { |style| @unused_styles << style }
  end

  def remove_pseudo_styles!
    @unused_styles.delete_if { |style| style.match /::?[\w\-]+/ }
  end
end


class Stylesheets
  include Enumerable
  attr_accessor :stylesheets

  def initialize
    @stylesheets = []
  end

  def add uris
    uris = Array(uris)
    uris.each { |uri| @stylesheets << Stylesheet.new(uri) unless already_included? uri }
  end

  def already_included? uri
    @stylesheets.any? { |stylesheet| stylesheet.uri == uri }
  end

  def [](uri)
    @stylesheets.find { |stylesheet| stylesheet.uri == uri }
  end

  def each &block
    @stylesheets.each &block
  end

  def unused_styles
    @stylesheets.inject(Set.new) {|styles, stylesheet| styles.merge stylesheet.unused_styles }
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
unused_css-1.0.0 lib/unused_css/stylesheet.rb