Sha256: 6ad8d6513350d5679cc3fcd3d41bb22b9ba4e9d48b9c3db48c8f611e863d26aa

Contents?: true

Size: 823 Bytes

Versions: 1

Compression:

Stored size: 823 Bytes

Contents

require "unused_css/stylesheet"

module Watir

  class Browser
    attr_accessor :stylesheets

    alias_method :original_initialize, :initialize
    def initialize(*args)
      @stylesheets = Stylesheets.new
      original_initialize(*args)
    end

    alias_method :original_goto, :goto
    def goto(*args)
      original_goto(*args)
      @stylesheets.add stylesheets_on_page
      remove_used_styles!
    end

    def stylesheets_on_page
      elements(tag_name: 'link').map { |stylesheet| stylesheet.attribute_value('href') }
    end

    def remove_used_styles!
      @stylesheets.each do |stylesheet|
        stylesheet.styles.delete_if { |style| self.element(css: style).exist? }
        stylesheet.remove_pseudo_styles!
      end
    end
    alias_method :check_for_unused_styles!, :remove_used_styles!
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
unused_css-0.2.0 lib/unused_css/watir.rb