lib/css_parser/parser.rb in css_parser-1.1.9 vs lib/css_parser/parser.rb in css_parser-1.2.1

- old
+ new

@@ -119,11 +119,11 @@ 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) + import_uri = Addressable::URI.parse(options[:base_uri].to_s) + Addressable::URI.parse(import_path) load_uri!(import_uri, options[:base_uri], media_types) elsif options[:base_dir] load_file!(import_path, options[:base_dir], media_types) end end @@ -291,11 +291,11 @@ # # See add_block! for options. # # Deprecated: originally accepted three params: `uri`, `base_uri` and `media_types` def load_uri!(uri, options = {}, deprecated = nil) - uri = URI.parse(uri) unless uri.respond_to? :scheme + uri = Addressable::URI.parse(uri) unless uri.respond_to? :scheme #base_uri = nil, media_types = :all, options = {} opts = {:base_uri => nil, :media_types => :all} if options.is_a? Hash @@ -322,20 +322,36 @@ # Load a local CSS file. def load_file!(file_name, base_dir = nil, media_types = :all) file_name = File.expand_path(file_name, base_dir) return unless File.readable?(file_name) + return unless circular_reference_check(file_name) src = IO.read(file_name) base_dir = File.dirname(file_name) add_block!(src, {:media_types => media_types, :base_dir => base_dir}) end protected + # Check that a path hasn't been loaded already + # + # Raises a CircularReferenceError exception if io_exceptions are on, + # otherwise returns true/false. + def circular_reference_check(path) + path = path.to_s + if @loaded_uris.include?(path) + raise CircularReferenceError, "can't load #{path} more than once" if @options[:io_exceptions] + return false + else + @loaded_uris << path + return true + end + end + # Strip comments and clean up blank lines from a block of CSS. # # Returns a string. def cleanup_block(block) # :nodoc: # Strip CSS comments @@ -356,32 +372,30 @@ # Returns the file's data and character set in an array. #-- # TODO: add option to fail silently or throw and exception on a 404 #++ def read_remote_file(uri) # :nodoc: - if @loaded_uris.include?(uri.to_s) - raise CircularReferenceError, "can't load #{uri.to_s} more than once" if @options[:io_exceptions] - return '', nil - end + return nil, nil unless circular_reference_check(uri.to_s) - @loaded_uris << uri.to_s - src = '', charset = nil begin - uri = URI.parse(uri.to_s) - http = Net::HTTP.new(uri.host, uri.port) + uri = Addressable::URI.parse(uri.to_s) if uri.scheme == 'file' # local file fh = open(uri.path, 'rb') src = fh.read fh.close else # remote file if uri.scheme == 'https' + uri.port = 443 unless uri.port + http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE + else + http = Net::HTTP.new(uri.host, uri.port) end res, src = http.get(uri.path, {'User-Agent' => USER_AGENT, 'Accept-Encoding' => 'gzip'}) charset = fh.respond_to?(:charset) ? fh.charset : 'utf-8'