css_parser 1.5.0 → 1.12.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/lib/css_parser/parser.rb +122 -118
- data/lib/css_parser/regexps.rb +55 -34
- data/lib/css_parser/rule_set.rb +404 -268
- data/lib/css_parser/version.rb +3 -1
- data/lib/css_parser.rb +28 -36
- metadata +120 -6
data/lib/css_parser/version.rb
CHANGED
data/lib/css_parser.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'addressable/uri'
|
2
4
|
require 'uri'
|
3
5
|
require 'net/https'
|
@@ -12,7 +14,6 @@ require 'css_parser/regexps'
|
|
12
14
|
require 'css_parser/parser'
|
13
15
|
|
14
16
|
module CssParser
|
15
|
-
|
16
17
|
# Merge multiple CSS RuleSets by cascading according to the CSS 2.1 cascading rules
|
17
18
|
# (http://www.w3.org/TR/REC-CSS2/cascade.html#cascading-order).
|
18
19
|
#
|
@@ -55,10 +56,10 @@ module CssParser
|
|
55
56
|
@folded_declaration_cache = {}
|
56
57
|
|
57
58
|
# in case called like CssParser.merge([rule_set, rule_set])
|
58
|
-
rule_sets.flatten! if rule_sets[0].
|
59
|
+
rule_sets.flatten! if rule_sets[0].is_a?(Array)
|
59
60
|
|
60
|
-
unless rule_sets.all? {|rs| rs.
|
61
|
-
raise ArgumentError,
|
61
|
+
unless rule_sets.all? { |rs| rs.is_a?(CssParser::RuleSet) }
|
62
|
+
raise ArgumentError, 'all parameters must be CssParser::RuleSets.'
|
62
63
|
end
|
63
64
|
|
64
65
|
return rule_sets[0] if rule_sets.length == 1
|
@@ -70,38 +71,27 @@ module CssParser
|
|
70
71
|
rule_set.expand_shorthand!
|
71
72
|
|
72
73
|
specificity = rule_set.specificity
|
73
|
-
|
74
|
-
if rule_set.selectors.length == 0
|
75
|
-
specificity = 0
|
76
|
-
else
|
77
|
-
specificity = rule_set.selectors.map { |s| calculate_specificity(s) }.compact.max || 0
|
78
|
-
end
|
79
|
-
end
|
74
|
+
specificity ||= rule_set.selectors.map { |s| calculate_specificity(s) }.compact.max || 0
|
80
75
|
|
81
76
|
rule_set.each_declaration do |property, value, is_important|
|
82
77
|
# Add the property to the list to be folded per http://www.w3.org/TR/CSS21/cascade.html#cascading-order
|
83
|
-
if not properties.
|
84
|
-
properties[property] = {:
|
78
|
+
if not properties.key?(property)
|
79
|
+
properties[property] = {value: value, specificity: specificity, is_important: is_important}
|
85
80
|
elsif is_important
|
86
81
|
if not properties[property][:is_important] or properties[property][:specificity] <= specificity
|
87
|
-
properties[property] = {:
|
82
|
+
properties[property] = {value: value, specificity: specificity, is_important: is_important}
|
88
83
|
end
|
89
84
|
elsif properties[property][:specificity] < specificity or properties[property][:specificity] == specificity
|
90
85
|
unless properties[property][:is_important]
|
91
|
-
properties[property] = {:
|
86
|
+
properties[property] = {value: value, specificity: specificity, is_important: is_important}
|
92
87
|
end
|
93
88
|
end
|
94
|
-
|
89
|
+
end
|
95
90
|
end
|
96
91
|
|
97
|
-
merged = RuleSet.new(nil, nil)
|
98
|
-
|
99
|
-
|
100
|
-
if details[:is_important]
|
101
|
-
merged[property.strip] = details[:value].strip.gsub(/\;\Z/, '') + '!important'
|
102
|
-
else
|
103
|
-
merged[property.strip] = details[:value].strip
|
104
|
-
end
|
92
|
+
merged = properties.each_with_object(RuleSet.new(nil, nil)) do |(property, details), rule_set|
|
93
|
+
value = details[:value].strip
|
94
|
+
rule_set[property.strip] = details[:is_important] ? "#{value.gsub(/;\Z/, '')}!important" : value
|
105
95
|
end
|
106
96
|
|
107
97
|
merged.create_shorthand!
|
@@ -122,12 +112,12 @@ module CssParser
|
|
122
112
|
def self.calculate_specificity(selector)
|
123
113
|
a = 0
|
124
114
|
b = selector.scan(/\#/).length
|
125
|
-
c = selector.scan(
|
126
|
-
d = selector.scan(
|
115
|
+
c = selector.scan(NON_ID_ATTRIBUTES_AND_PSEUDO_CLASSES_RX_NC).length
|
116
|
+
d = selector.scan(ELEMENTS_AND_PSEUDO_ELEMENTS_RX_NC).length
|
127
117
|
|
128
|
-
|
118
|
+
"#{a}#{b}#{c}#{d}".to_i
|
129
119
|
rescue
|
130
|
-
|
120
|
+
0
|
131
121
|
end
|
132
122
|
|
133
123
|
# Make <tt>url()</tt> links absolute.
|
@@ -144,23 +134,25 @@ module CssParser
|
|
144
134
|
# "http://example.org/style/basic.css").inspect
|
145
135
|
# => "body { background: url('http://example.org/style/yellow.png?abc=123') };"
|
146
136
|
def self.convert_uris(css, base_uri)
|
147
|
-
base_uri = Addressable::URI.parse(base_uri) unless base_uri.
|
137
|
+
base_uri = Addressable::URI.parse(base_uri) unless base_uri.is_a?(Addressable::URI)
|
148
138
|
|
149
139
|
css.gsub(URI_RX) do
|
150
|
-
uri =
|
151
|
-
uri.gsub!(/["']+/, '')
|
140
|
+
uri = Regexp.last_match(1).to_s.gsub(/["']+/, '')
|
152
141
|
# Don't process URLs that are already absolute
|
153
|
-
unless uri
|
142
|
+
unless uri.match(%r{^[a-z]+://}i)
|
154
143
|
begin
|
155
|
-
uri = base_uri
|
156
|
-
rescue
|
144
|
+
uri = base_uri.join(uri)
|
145
|
+
rescue
|
146
|
+
nil
|
147
|
+
end
|
157
148
|
end
|
158
|
-
"url('#{uri
|
149
|
+
"url('#{uri}')"
|
159
150
|
end
|
160
151
|
end
|
161
152
|
|
162
153
|
def self.sanitize_media_query(raw)
|
163
|
-
mq = raw.to_s.gsub(
|
154
|
+
mq = raw.to_s.gsub(/\s+/, ' ')
|
155
|
+
mq.strip!
|
164
156
|
mq = 'all' if mq.empty?
|
165
157
|
mq.to_sym
|
166
158
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: css_parser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.12.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alex Dunae
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-09-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: addressable
|
@@ -24,6 +24,118 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: benchmark-ips
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bump
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: maxitest
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: memory_profiler
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rake
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rubocop
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '1.8'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '1.8'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rubocop-rake
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: webrick
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
27
139
|
description: A set of classes for parsing CSS in Ruby.
|
28
140
|
email: code@dunae.ca
|
29
141
|
executables: []
|
@@ -39,7 +151,10 @@ files:
|
|
39
151
|
homepage: https://github.com/premailer/css_parser
|
40
152
|
licenses:
|
41
153
|
- MIT
|
42
|
-
metadata:
|
154
|
+
metadata:
|
155
|
+
changelog_uri: https://github.com/premailer/css_parser/blob/master/CHANGELOG.md
|
156
|
+
source_code_uri: https://github.com/premailer/css_parser
|
157
|
+
bug_tracker_uri: https://github.com/premailer/css_parser/issues
|
43
158
|
post_install_message:
|
44
159
|
rdoc_options: []
|
45
160
|
require_paths:
|
@@ -48,15 +163,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
48
163
|
requirements:
|
49
164
|
- - ">="
|
50
165
|
- !ruby/object:Gem::Version
|
51
|
-
version: '
|
166
|
+
version: '2.4'
|
52
167
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
168
|
requirements:
|
54
169
|
- - ">="
|
55
170
|
- !ruby/object:Gem::Version
|
56
171
|
version: '0'
|
57
172
|
requirements: []
|
58
|
-
|
59
|
-
rubygems_version: 2.6.11
|
173
|
+
rubygems_version: 3.1.6
|
60
174
|
signing_key:
|
61
175
|
specification_version: 4
|
62
176
|
summary: Ruby CSS parser.
|