Sha256: 22a14fd16e1170bb44b04bd2ea9c29ecf1c4c7d7d9cc3b2c0da0bf19f055b296

Contents?: true

Size: 950 Bytes

Versions: 2

Compression:

Stored size: 950 Bytes

Contents

require "beautiful-css/rule"

module BeautifulCss
  class RuleParser

    def initialize(str)
      @body = str
      @body = @body.gsub( /url\( *data:/ , '__url_data__')
      @body = @body.gsub( /;base64,/ , '__base64__')
    end

    def selectors
      @body.match(/[^{]*/).to_s.split(/,/).map{|s| s.strip}.select{|s| !s.empty? }
    end

    def props
      begin
      p = @body.match(/\{([^}]*)/)[1].split(';').select{|s| !s.strip.empty? }
      p.map{|s| s.strip.split(':') }
      rescue
        []
      end
    end

    def restore_special_strings str
      str = str.gsub( /__url_data__/, 'url(data:')
      str = str.gsub( /__base64__/, ';base64,')
      str
    end

    def to_rules
      selectors.map do |selector|
        props.map do |prop|
          val = restore_special_strings prop[1]
          BeautifulCss::Rule.new(selector, prop[0], val)
        end
      end.flatten
    end

    def body
      @body
    end


  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
beautiful-css-0.0.10 lib/beautiful-css/rule_parser.rb
beautiful-css-0.0.9 lib/beautiful-css/rule_parser.rb