css_parser 1.7.0 → 1.21.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/css_parser/parser.rb +189 -130
- data/lib/css_parser/regexps.rb +59 -36
- data/lib/css_parser/rule_set.rb +444 -265
- data/lib/css_parser/version.rb +3 -1
- data/lib/css_parser.rb +26 -37
- metadata +12 -9
data/lib/css_parser/version.rb
CHANGED
data/lib/css_parser.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
|
2
3
|
require 'addressable/uri'
|
3
4
|
require 'uri'
|
4
5
|
require 'net/https'
|
5
6
|
require 'digest/md5'
|
6
7
|
require 'zlib'
|
7
8
|
require 'stringio'
|
8
|
-
require 'iconv' unless String.method_defined?(:encode)
|
9
9
|
|
10
10
|
require 'css_parser/version'
|
11
11
|
require 'css_parser/rule_set'
|
@@ -13,7 +13,6 @@ require 'css_parser/regexps'
|
|
13
13
|
require 'css_parser/parser'
|
14
14
|
|
15
15
|
module CssParser
|
16
|
-
|
17
16
|
# Merge multiple CSS RuleSets by cascading according to the CSS 2.1 cascading rules
|
18
17
|
# (http://www.w3.org/TR/REC-CSS2/cascade.html#cascading-order).
|
19
18
|
#
|
@@ -56,10 +55,10 @@ module CssParser
|
|
56
55
|
@folded_declaration_cache = {}
|
57
56
|
|
58
57
|
# in case called like CssParser.merge([rule_set, rule_set])
|
59
|
-
rule_sets.flatten! if rule_sets[0].
|
58
|
+
rule_sets.flatten! if rule_sets[0].is_a?(Array)
|
60
59
|
|
61
|
-
unless rule_sets.all?
|
62
|
-
raise ArgumentError,
|
60
|
+
unless rule_sets.all?(CssParser::RuleSet)
|
61
|
+
raise ArgumentError, 'all parameters must be CssParser::RuleSets.'
|
63
62
|
end
|
64
63
|
|
65
64
|
return rule_sets[0] if rule_sets.length == 1
|
@@ -71,38 +70,27 @@ module CssParser
|
|
71
70
|
rule_set.expand_shorthand!
|
72
71
|
|
73
72
|
specificity = rule_set.specificity
|
74
|
-
|
75
|
-
if rule_set.selectors.length == 0
|
76
|
-
specificity = 0
|
77
|
-
else
|
78
|
-
specificity = rule_set.selectors.map { |s| calculate_specificity(s) }.compact.max || 0
|
79
|
-
end
|
80
|
-
end
|
73
|
+
specificity ||= rule_set.selectors.filter_map { |s| calculate_specificity(s) }.max || 0
|
81
74
|
|
82
75
|
rule_set.each_declaration do |property, value, is_important|
|
83
76
|
# Add the property to the list to be folded per http://www.w3.org/TR/CSS21/cascade.html#cascading-order
|
84
|
-
if
|
85
|
-
properties[property] = {:
|
77
|
+
if !properties.key?(property)
|
78
|
+
properties[property] = {value: value, specificity: specificity, is_important: is_important}
|
86
79
|
elsif is_important
|
87
|
-
if
|
88
|
-
properties[property] = {:
|
80
|
+
if !properties[property][:is_important] || properties[property][:specificity] <= specificity
|
81
|
+
properties[property] = {value: value, specificity: specificity, is_important: is_important}
|
89
82
|
end
|
90
|
-
elsif properties[property][:specificity] < specificity
|
83
|
+
elsif properties[property][:specificity] < specificity || properties[property][:specificity] == specificity
|
91
84
|
unless properties[property][:is_important]
|
92
|
-
properties[property] = {:
|
85
|
+
properties[property] = {value: value, specificity: specificity, is_important: is_important}
|
93
86
|
end
|
94
87
|
end
|
95
|
-
|
88
|
+
end
|
96
89
|
end
|
97
90
|
|
98
|
-
merged = RuleSet.new(nil, nil)
|
99
|
-
|
100
|
-
|
101
|
-
if details[:is_important]
|
102
|
-
merged[property.strip] = details[:value].strip.gsub(/\;\Z/, '') + '!important'
|
103
|
-
else
|
104
|
-
merged[property.strip] = details[:value].strip
|
105
|
-
end
|
91
|
+
merged = properties.each_with_object(RuleSet.new(nil, nil)) do |(property, details), rule_set|
|
92
|
+
value = details[:value].strip
|
93
|
+
rule_set[property.strip] = details[:is_important] ? "#{value.gsub(/;\Z/, '')}!important" : value
|
106
94
|
end
|
107
95
|
|
108
96
|
merged.create_shorthand!
|
@@ -122,13 +110,13 @@ module CssParser
|
|
122
110
|
#++
|
123
111
|
def self.calculate_specificity(selector)
|
124
112
|
a = 0
|
125
|
-
b = selector.scan(
|
113
|
+
b = selector.scan('#').length
|
126
114
|
c = selector.scan(NON_ID_ATTRIBUTES_AND_PSEUDO_CLASSES_RX_NC).length
|
127
115
|
d = selector.scan(ELEMENTS_AND_PSEUDO_ELEMENTS_RX_NC).length
|
128
116
|
|
129
117
|
"#{a}#{b}#{c}#{d}".to_i
|
130
118
|
rescue
|
131
|
-
|
119
|
+
0
|
132
120
|
end
|
133
121
|
|
134
122
|
# Make <tt>url()</tt> links absolute.
|
@@ -145,23 +133,24 @@ module CssParser
|
|
145
133
|
# "http://example.org/style/basic.css").inspect
|
146
134
|
# => "body { background: url('http://example.org/style/yellow.png?abc=123') };"
|
147
135
|
def self.convert_uris(css, base_uri)
|
148
|
-
base_uri = Addressable::URI.parse(base_uri) unless base_uri.
|
136
|
+
base_uri = Addressable::URI.parse(base_uri) unless base_uri.is_a?(Addressable::URI)
|
149
137
|
|
150
138
|
css.gsub(URI_RX) do
|
151
|
-
uri =
|
152
|
-
uri.gsub!(/["']+/, '')
|
139
|
+
uri = Regexp.last_match(1).to_s.gsub(/["']+/, '')
|
153
140
|
# Don't process URLs that are already absolute
|
154
|
-
unless uri
|
141
|
+
unless uri.match?(%r{^[a-z]+://}i)
|
155
142
|
begin
|
156
|
-
uri = base_uri
|
157
|
-
rescue
|
143
|
+
uri = base_uri.join(uri)
|
144
|
+
rescue
|
145
|
+
nil
|
146
|
+
end
|
158
147
|
end
|
159
|
-
"url('#{uri
|
148
|
+
"url('#{uri}')"
|
160
149
|
end
|
161
150
|
end
|
162
151
|
|
163
152
|
def self.sanitize_media_query(raw)
|
164
|
-
mq = raw.to_s.gsub(
|
153
|
+
mq = raw.to_s.gsub(/\s+/, ' ')
|
165
154
|
mq.strip!
|
166
155
|
mq = 'all' if mq.empty?
|
167
156
|
mq.to_sym
|
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.21.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alex Dunae
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2025-03-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: addressable
|
@@ -39,8 +39,12 @@ files:
|
|
39
39
|
homepage: https://github.com/premailer/css_parser
|
40
40
|
licenses:
|
41
41
|
- MIT
|
42
|
-
metadata:
|
43
|
-
|
42
|
+
metadata:
|
43
|
+
changelog_uri: https://github.com/premailer/css_parser/blob/master/CHANGELOG.md
|
44
|
+
source_code_uri: https://github.com/premailer/css_parser
|
45
|
+
bug_tracker_uri: https://github.com/premailer/css_parser/issues
|
46
|
+
rubygems_mfa_required: 'true'
|
47
|
+
post_install_message:
|
44
48
|
rdoc_options: []
|
45
49
|
require_paths:
|
46
50
|
- lib
|
@@ -48,16 +52,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
48
52
|
requirements:
|
49
53
|
- - ">="
|
50
54
|
- !ruby/object:Gem::Version
|
51
|
-
version: '0'
|
55
|
+
version: '3.0'
|
52
56
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
57
|
requirements:
|
54
58
|
- - ">="
|
55
59
|
- !ruby/object:Gem::Version
|
56
60
|
version: '0'
|
57
61
|
requirements: []
|
58
|
-
|
59
|
-
|
60
|
-
signing_key:
|
62
|
+
rubygems_version: 3.4.19
|
63
|
+
signing_key:
|
61
64
|
specification_version: 4
|
62
65
|
summary: Ruby CSS parser.
|
63
66
|
test_files: []
|