Skip to content

Commit

Permalink
Update css_parser
Browse files Browse the repository at this point in the history
  • Loading branch information
aanand committed Jan 18, 2010
1 parent 97c630c commit f885735
Show file tree
Hide file tree
Showing 22 changed files with 31 additions and 118 deletions.
13 changes: 0 additions & 13 deletions vendor/gems/css_parser-0.9.1/CHANGELOG

This file was deleted.

21 changes: 0 additions & 21 deletions vendor/gems/css_parser-0.9.1/LICENSE

This file was deleted.

58 changes: 0 additions & 58 deletions vendor/gems/css_parser-0.9.1/README

This file was deleted.

@@ -1,6 +1,6 @@
$:.unshift File.dirname(__FILE__) $:.unshift File.dirname(__FILE__)
require 'uri' require 'uri'
require 'md5' require 'digest/md5'
require 'zlib' require 'zlib'
require 'iconv' require 'iconv'
require 'css_parser/rule_set' require 'css_parser/rule_set'
Expand Down
Expand Up @@ -15,7 +15,7 @@ class CircularReferenceError < StandardError; end
# [<tt>import</tt>] Follow <tt>@import</tt> rules. Boolean, default is <tt>true</tt>. # [<tt>import</tt>] Follow <tt>@import</tt> rules. Boolean, default is <tt>true</tt>.
# [<tt>io_exceptions</tt>] Throw an exception if a link can not be found. Boolean, default is <tt>true</tt>. # [<tt>io_exceptions</tt>] Throw an exception if a link can not be found. Boolean, default is <tt>true</tt>.
class Parser class Parser
USER_AGENT = "Ruby CSS Parser/#{VERSION} (http://code.dunae.ca/css_parser/)" USER_AGENT = "Ruby CSS Parser/#{RUBY_VERSION} (http://code.dunae.ca/css_parser/)"


STRIP_CSS_COMMENTS_RX = /\/\*.*?\*\//m STRIP_CSS_COMMENTS_RX = /\/\*.*?\*\//m
STRIP_HTML_COMMENTS_RX = /\<\!\-\-|\-\-\>/m STRIP_HTML_COMMENTS_RX = /\<\!\-\-|\-\-\>/m
Expand Down
Expand Up @@ -2,17 +2,17 @@ module CssParser
# :stopdoc: # :stopdoc:
# Base types # Base types
RE_NL = Regexp.new('(\n|\r\n|\r|\f)') RE_NL = Regexp.new('(\n|\r\n|\r|\f)')
RE_NON_ASCII = Regexp.new('([\x00-\xFF])', Regexp::IGNORECASE) #[^\0-\177] RE_NON_ASCII = Regexp.new('([\x00-\xFF])', Regexp::IGNORECASE, 'n') #[^\0-\177]
RE_UNICODE = Regexp.new('(\\\\[0-9a-f]{1,6}(\r\n|[ \n\r\t\f])*)', Regexp::IGNORECASE | Regexp::EXTENDED | Regexp::MULTILINE) RE_UNICODE = Regexp.new('(\\\\[0-9a-f]{1,6}(\r\n|[ \n\r\t\f])*)', Regexp::IGNORECASE | Regexp::EXTENDED | Regexp::MULTILINE, 'n')
RE_ESCAPE = Regexp.union(RE_UNICODE, '|(\\\\[^\n\r\f0-9a-f])') RE_ESCAPE = Regexp.union(RE_UNICODE, '|(\\\\[^\n\r\f0-9a-f])')
RE_IDENT = Regexp.new("[\-]?([_a-z]|#{RE_NON_ASCII}|#{RE_ESCAPE})([_a-z0-9\-]|#{RE_NON_ASCII}|#{RE_ESCAPE})*", Regexp::IGNORECASE) RE_IDENT = Regexp.new("[\-]?([_a-z]|#{RE_NON_ASCII}|#{RE_ESCAPE})([_a-z0-9\-]|#{RE_NON_ASCII}|#{RE_ESCAPE})*", Regexp::IGNORECASE, 'n')


# General strings # General strings
RE_STRING1 = Regexp.new('(\"(.[^\n\r\f\\"]*|\\\\' + RE_NL.to_s + '|' + RE_ESCAPE.to_s + ')*\")') RE_STRING1 = Regexp.new('(\"(.[^\n\r\f\\"]*|\\\\' + RE_NL.to_s + '|' + RE_ESCAPE.to_s + ')*\")')
RE_STRING2 = Regexp.new('(\'(.[^\n\r\f\\\']*|\\\\' + RE_NL.to_s + '|' + RE_ESCAPE.to_s + ')*\')') RE_STRING2 = Regexp.new('(\'(.[^\n\r\f\\\']*|\\\\' + RE_NL.to_s + '|' + RE_ESCAPE.to_s + ')*\')')
RE_STRING = Regexp.union(RE_STRING1, RE_STRING2) RE_STRING = Regexp.union(RE_STRING1, RE_STRING2)


RE_URI = Regexp.new('(url\([\s]*([\s]*' + RE_STRING.to_s + '[\s]*)[\s]*\))|(url\([\s]*([!#$%&*\-~]|' + RE_NON_ASCII.to_s + '|' + RE_ESCAPE.to_s + ')*[\s]*)\)', Regexp::IGNORECASE | Regexp::EXTENDED | Regexp::MULTILINE) RE_URI = Regexp.new('(url\([\s]*([\s]*' + RE_STRING.to_s + '[\s]*)[\s]*\))|(url\([\s]*([!#$%&*\-~]|' + RE_NON_ASCII.to_s + '|' + RE_ESCAPE.to_s + ')*[\s]*)\)', Regexp::IGNORECASE | Regexp::EXTENDED | Regexp::MULTILINE, 'n')
URI_RX = /url\(("([^"]*)"|'([^']*)'|([^)]*))\)/im URI_RX = /url\(("([^"]*)"|'([^']*)'|([^)]*))\)/im


# Initial parsing # Initial parsing
Expand Down
Expand Up @@ -48,6 +48,11 @@ def get_value(property)
# #
# If the property already exists its value will be over-written. # If the property already exists its value will be over-written.
def add_declaration!(property, value) def add_declaration!(property, value)
if value.nil? or value.empty?
@declarations.delete(property)
return
end

value.gsub!(/;\Z/, '') value.gsub!(/;\Z/, '')
is_important = !value.gsub!(CssParser::IMPORTANT_IN_PROPERTY_RX, '').nil? is_important = !value.gsub!(CssParser::IMPORTANT_IN_PROPERTY_RX, '').nil?
property = property.downcase.strip property = property.downcase.strip
Expand Down Expand Up @@ -84,13 +89,14 @@ def each_declaration # :yields: property, value, is_important
end end


# Return all declarations as a string. # Return all declarations as a string.
#--
# TODO: Clean-up regexp doesn't seem to work
#++
def declarations_to_s(options = {}) def declarations_to_s(options = {})
options = {:force_important => false}.merge(options) options = {:force_important => false}.merge(options)
str = '' str = ''
each_declaration do |prop, val, is_important| importance = options[:force_important] ? ' !important' : ''
importance = (options[:force_important] || is_important) ? ' !important' : '' each_declaration { |prop, val| str += "#{prop}: #{val}#{importance}; " }
str += "#{prop}: #{val}#{importance}; "
end
str.gsub(/^[\s]+|[\n\r\f\t]*|[\s]+$/mx, '').strip str.gsub(/^[\s]+|[\n\r\f\t]*|[\s]+$/mx, '').strip
end end


Expand Down Expand Up @@ -138,6 +144,7 @@ def parse_selectors!(selectors) # :nodoc:
@selectors = selectors.split(',') @selectors = selectors.split(',')
end end


public
# Split shorthand dimensional declarations (e.g. <tt>margin: 0px auto;</tt>) # Split shorthand dimensional declarations (e.g. <tt>margin: 0px auto;</tt>)
# into their constituent parts. # into their constituent parts.
def expand_dimensions_shorthand! # :nodoc: def expand_dimensions_shorthand! # :nodoc:
Expand Down Expand Up @@ -372,9 +379,5 @@ def create_font_shorthand! # :nodoc:
end end


end end




end end
end end
@@ -1,3 +1,4 @@
# coding: iso-8859-1
require File.dirname(__FILE__) + '/test_helper' require File.dirname(__FILE__) + '/test_helper'


# Test cases for CSS regular expressions # Test cases for CSS regular expressions
Expand Down
@@ -1,8 +1,8 @@
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__), '../')) $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__), '../'))
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__), '../lib/')) $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__), '../lib/'))
require 'rubygems' require 'rubygems'
require 'test/unit' require 'test/unit'
require 'css_parser' require 'css_parser'
require 'net/http' require 'net/http'
require 'open-uri' require 'open-uri'
require 'WEBrick' require 'WEBrick'
@@ -1,4 +1,5 @@
require File.dirname(__FILE__) + '/test_helper' require File.dirname(__FILE__) + '/test_helper'
require "set"


# Test cases for parsing CSS blocks # Test cases for parsing CSS blocks
class RuleSetTests < Test::Unit::TestCase class RuleSetTests < Test::Unit::TestCase
Expand Down Expand Up @@ -32,7 +33,7 @@ def test_each_selector
expected = [ expected = [
{:selector => "#content p", :declarations => "color: #fff;", :specificity => 101}, {:selector => "#content p", :declarations => "color: #fff;", :specificity => 101},
{:selector => "a", :declarations => "color: #fff;", :specificity => 1} {:selector => "a", :declarations => "color: #fff;", :specificity => 1}
] ]


actual = [] actual = []
rs = RuleSet.new('#content p, a', 'color: #fff;') rs = RuleSet.new('#content p, a', 'color: #fff;')
Expand All @@ -44,13 +45,13 @@ def test_each_selector
end end


def test_each_declaration def test_each_declaration
expected = [ expected = Set.new([
{:property => 'margin', :value => '1px -0.25em', :is_important => false}, {:property => 'margin', :value => '1px -0.25em', :is_important => false},
{:property => 'background', :value => 'white none no-repeat', :is_important => true}, {:property => 'background', :value => 'white none no-repeat', :is_important => true},
{:property => 'color', :value => '#fff', :is_important => false} {:property => 'color', :value => '#fff', :is_important => false}
] ])


actual = [] actual = Set.new
rs = RuleSet.new(nil, 'color: #fff; Background: white none no-repeat !important; margin: 1px -0.25em;') rs = RuleSet.new(nil, 'color: #fff; Background: white none no-repeat !important; margin: 1px -0.25em;')
rs.each_declaration do |prop, val, imp| rs.each_declaration do |prop, val, imp|
actual << {:property => prop, :value => val, :is_important => imp} actual << {:property => prop, :value => val, :is_important => imp}
Expand Down

0 comments on commit f885735

Please sign in to comment.