css_parser 1.1.2 → 1.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/css_parser/parser.rb +12 -7
- data/test/test_css_parser_media_types.rb +38 -3
- metadata +4 -4
data/lib/css_parser/parser.rb
CHANGED
@@ -81,6 +81,10 @@ module CssParser
|
|
81
81
|
# In order to follow +@import+ rules you must supply either a
|
82
82
|
# +:base_dir+ or +:base_uri+ option.
|
83
83
|
#
|
84
|
+
# Use the +:media_types+ option to set the media type(s) for this block. Takes an array of symbols.
|
85
|
+
#
|
86
|
+
# Use the +:only_media_types+ option to selectively follow +@import+ rules. Takes an array of symbols.
|
87
|
+
#
|
84
88
|
# ==== Example
|
85
89
|
# css = <<-EOT
|
86
90
|
# body { font-size: 10pt }
|
@@ -92,12 +96,11 @@ module CssParser
|
|
92
96
|
#
|
93
97
|
# parser = CssParser::Parser.new
|
94
98
|
# parser.add_block!(css)
|
95
|
-
#--
|
96
|
-
# TODO: add media_type
|
97
|
-
#++
|
98
99
|
def add_block!(block, options = {})
|
99
|
-
options = {:base_uri => nil, :base_dir => nil, :charset => nil, :media_types => :all}.merge(options)
|
100
|
-
|
100
|
+
options = {:base_uri => nil, :base_dir => nil, :charset => nil, :media_types => :all, :only_media_types => :all}.merge(options)
|
101
|
+
options[:media_types] = [options[:media_types]].flatten
|
102
|
+
options[:only_media_types] = [options[:only_media_types]].flatten
|
103
|
+
|
101
104
|
block = cleanup_block(block)
|
102
105
|
|
103
106
|
if options[:base_uri] and @options[:absolute_paths]
|
@@ -105,16 +108,18 @@ module CssParser
|
|
105
108
|
end
|
106
109
|
|
107
110
|
# Load @imported CSS
|
108
|
-
block.scan(RE_AT_IMPORT_RULE).each do |import_rule|
|
111
|
+
block.scan(RE_AT_IMPORT_RULE).each do |import_rule|
|
109
112
|
media_types = []
|
110
113
|
if media_string = import_rule[-1]
|
111
114
|
media_string.split(/\s|\,/).each do |t|
|
112
115
|
media_types << t.to_sym unless t.empty?
|
113
116
|
end
|
114
117
|
end
|
118
|
+
|
119
|
+
next unless options[:only_media_types].include?(:all) or media_types.length < 1 or (media_types & options[:only_media_types]).length > 0
|
115
120
|
|
116
121
|
import_path = import_rule[0].to_s.gsub(/['"]*/, '').strip
|
117
|
-
|
122
|
+
|
118
123
|
if options[:base_uri]
|
119
124
|
import_uri = URI.parse(options[:base_uri].to_s).merge(import_path)
|
120
125
|
load_uri!(import_uri, options[:base_uri], media_types)
|
@@ -28,7 +28,7 @@ class CssParserMediaTypesTests < Test::Unit::TestCase
|
|
28
28
|
assert_equal 'font-size: 13px; line-height: 1.2;', @cp.find_by_selector('body', :screen).join(' ')
|
29
29
|
end
|
30
30
|
|
31
|
-
def
|
31
|
+
def test_finding_by_multiple_media_types
|
32
32
|
css = <<-EOT
|
33
33
|
@media print {
|
34
34
|
body { font-size: 10pt }
|
@@ -55,14 +55,49 @@ class CssParserMediaTypesTests < Test::Unit::TestCase
|
|
55
55
|
assert_equal 'font-size: 10pt;', @cp.find_by_selector('body', :screen).join(' ')
|
56
56
|
assert @cp.find_by_selector('body', :handheld).empty?
|
57
57
|
end
|
58
|
+
|
59
|
+
def test_adding_block_and_limiting_media_types1
|
60
|
+
css = <<-EOT
|
61
|
+
@import "import1.css", print
|
62
|
+
EOT
|
63
|
+
|
64
|
+
base_dir = File.dirname(__FILE__) + '/fixtures/'
|
65
|
+
|
66
|
+
@cp.add_block!(css, :only_media_types => :screen, :base_dir => base_dir)
|
67
|
+
assert @cp.find_by_selector('div').empty?
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_adding_block_and_limiting_media_types2
|
72
|
+
css = <<-EOT
|
73
|
+
@import "import1.css", print
|
74
|
+
EOT
|
75
|
+
|
76
|
+
base_dir = File.dirname(__FILE__) + '/fixtures/'
|
77
|
+
|
78
|
+
@cp.add_block!(css, :only_media_types => :print, :base_dir => base_dir)
|
79
|
+
assert_match 'color: lime', @cp.find_by_selector('div').join(' ')
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_adding_block_and_limiting_media_types
|
83
|
+
css = <<-EOT
|
84
|
+
@import "import1.css"
|
85
|
+
EOT
|
86
|
+
|
87
|
+
base_dir = File.dirname(__FILE__) + '/fixtures/'
|
88
|
+
|
89
|
+
@cp.add_block!(css, :only_media_types => :print, :base_dir => base_dir)
|
90
|
+
assert_match 'color: lime', @cp.find_by_selector('div').join(' ')
|
91
|
+
end
|
92
|
+
|
58
93
|
|
59
|
-
def
|
94
|
+
def test_adding_rule_set_with_media_type
|
60
95
|
@cp.add_rule!('body', 'color: black;', [:handheld,:tty])
|
61
96
|
@cp.add_rule!('body', 'color: blue;', :screen)
|
62
97
|
assert_equal 'color: black;', @cp.find_by_selector('body', :handheld).join(' ')
|
63
98
|
end
|
64
99
|
|
65
|
-
def
|
100
|
+
def test_selecting_with_all_meda_type
|
66
101
|
@cp.add_rule!('body', 'color: black;', [:handheld,:tty])
|
67
102
|
assert_equal 'color: black;', @cp.find_by_selector('body', :all).join(' ')
|
68
103
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: css_parser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 21
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 1.1.
|
9
|
+
- 3
|
10
|
+
version: 1.1.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Alex Dunae
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-
|
18
|
+
date: 2010-11-03 00:00:00 -07:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|