lib/css_parser/parser.rb in css_parser-1.1.2 vs lib/css_parser/parser.rb in css_parser-1.1.3
- old
+ new
@@ -79,10 +79,14 @@
# Add a raw block of CSS.
#
# In order to follow +@import+ rules you must supply either a
# +:base_dir+ or +:base_uri+ option.
#
+ # Use the +:media_types+ option to set the media type(s) for this block. Takes an array of symbols.
+ #
+ # Use the +:only_media_types+ option to selectively follow +@import+ rules. Takes an array of symbols.
+ #
# ==== Example
# css = <<-EOT
# body { font-size: 10pt }
# p { margin: 0px; }
# @media screen, print {
@@ -90,32 +94,33 @@
# }
# EOT
#
# parser = CssParser::Parser.new
# parser.add_block!(css)
- #--
- # TODO: add media_type
- #++
def add_block!(block, options = {})
- options = {:base_uri => nil, :base_dir => nil, :charset => nil, :media_types => :all}.merge(options)
-
+ options = {:base_uri => nil, :base_dir => nil, :charset => nil, :media_types => :all, :only_media_types => :all}.merge(options)
+ options[:media_types] = [options[:media_types]].flatten
+ options[:only_media_types] = [options[:only_media_types]].flatten
+
block = cleanup_block(block)
if options[:base_uri] and @options[:absolute_paths]
block = CssParser.convert_uris(block, options[:base_uri])
end
# Load @imported CSS
- block.scan(RE_AT_IMPORT_RULE).each do |import_rule|
+ block.scan(RE_AT_IMPORT_RULE).each do |import_rule|
media_types = []
if media_string = import_rule[-1]
media_string.split(/\s|\,/).each do |t|
media_types << t.to_sym unless t.empty?
end
end
+
+ next unless options[:only_media_types].include?(:all) or media_types.length < 1 or (media_types & options[:only_media_types]).length > 0
import_path = import_rule[0].to_s.gsub(/['"]*/, '').strip
-
+
if options[:base_uri]
import_uri = URI.parse(options[:base_uri].to_s).merge(import_path)
load_uri!(import_uri, options[:base_uri], media_types)
elsif options[:base_dir]
load_file!(import_path, options[:base_dir], media_types)