css_parser 1.19.1 → 1.21.0
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 +14 -17
- data/lib/css_parser/rule_set.rb +16 -14
- data/lib/css_parser/version.rb +1 -1
- data/lib/css_parser.rb +3 -4
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c8880ebd6978e8a8a3108ea93b4d567252aee152b28f237dab31809f1488f42e
|
4
|
+
data.tar.gz: 4e8379acf246b7fe2573a2ada4fc49c01e9c3b2de43fe80d07f42390f8f16450
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 480947bb06a6c40ab955f76ff9eb7ba9ac5caf160d7fe343d16901e9f37210c9ccab22a07a4d3165ec26bec4cd28547f530718978d2d969ed398c8195351a0c0
|
7
|
+
data.tar.gz: e7a757c9dc096e71d5f617343f0900bacddea42bef4b26ca68f2830728a480ace908807dc4db82b6b0b77a11c1b2c9295ad814bb2aaa80fd069c959cd58afe65
|
data/lib/css_parser/parser.rb
CHANGED
@@ -359,9 +359,9 @@ module CssParser
|
|
359
359
|
in_at_media_rule = false
|
360
360
|
in_media_block = false
|
361
361
|
|
362
|
-
current_selectors =
|
363
|
-
current_media_query =
|
364
|
-
current_declarations =
|
362
|
+
current_selectors = +''
|
363
|
+
current_media_query = +''
|
364
|
+
current_declarations = +''
|
365
365
|
|
366
366
|
# once we are in a rule, we will use this to store where we started if we are capturing offsets
|
367
367
|
rule_start = nil
|
@@ -405,18 +405,19 @@ module CssParser
|
|
405
405
|
media_types: current_media_queries
|
406
406
|
}
|
407
407
|
if options[:capture_offsets]
|
408
|
-
add_rule_options
|
408
|
+
add_rule_options[:filename] = options[:filename]
|
409
|
+
add_rule_options[:offset] = rule_start..end_offset
|
409
410
|
end
|
410
411
|
add_rule!(**add_rule_options)
|
411
412
|
end
|
412
413
|
|
413
|
-
current_selectors =
|
414
|
-
current_declarations =
|
414
|
+
current_selectors = +''
|
415
|
+
current_declarations = +''
|
415
416
|
|
416
417
|
# restart our search for selectors and declarations
|
417
418
|
rule_start = nil if options[:capture_offsets]
|
418
419
|
end
|
419
|
-
elsif
|
420
|
+
elsif /@media/i.match?(token)
|
420
421
|
# found '@media', reset current media_types
|
421
422
|
in_at_media_rule = true
|
422
423
|
current_media_queries = []
|
@@ -426,14 +427,14 @@ module CssParser
|
|
426
427
|
in_at_media_rule = false
|
427
428
|
in_media_block = true
|
428
429
|
current_media_queries << CssParser.sanitize_media_query(current_media_query)
|
429
|
-
current_media_query =
|
430
|
+
current_media_query = +''
|
430
431
|
elsif token.include?(',')
|
431
432
|
# new media query begins
|
432
433
|
token.tr!(',', ' ')
|
433
434
|
token.strip!
|
434
435
|
current_media_query << token << ' '
|
435
436
|
current_media_queries << CssParser.sanitize_media_query(current_media_query)
|
436
|
-
current_media_query =
|
437
|
+
current_media_query = +''
|
437
438
|
else
|
438
439
|
token.strip!
|
439
440
|
# special-case the ( and ) tokens to remove inner-whitespace
|
@@ -478,7 +479,8 @@ module CssParser
|
|
478
479
|
media_types: current_media_queries
|
479
480
|
}
|
480
481
|
if options[:capture_offsets]
|
481
|
-
add_rule_options
|
482
|
+
add_rule_options[:filename] = options[:filename]
|
483
|
+
add_rule_options[:offset] = rule_start..end_offset
|
482
484
|
end
|
483
485
|
add_rule!(**add_rule_options)
|
484
486
|
end
|
@@ -677,12 +679,7 @@ module CssParser
|
|
677
679
|
end
|
678
680
|
|
679
681
|
if charset
|
680
|
-
|
681
|
-
src.encode!('UTF-8', charset)
|
682
|
-
else
|
683
|
-
ic = Iconv.new('UTF-8//IGNORE', charset)
|
684
|
-
src = ic.iconv(src)
|
685
|
-
end
|
682
|
+
src.encode!('UTF-8', charset)
|
686
683
|
end
|
687
684
|
rescue
|
688
685
|
@redirect_count = nil
|
@@ -723,7 +720,7 @@ module CssParser
|
|
723
720
|
nodes = {}
|
724
721
|
lines.each do |line|
|
725
722
|
parts = line.split(':', 2)
|
726
|
-
if parts[1]
|
723
|
+
if parts[1].include?(':')
|
727
724
|
nodes[parts[0]] = css_node_to_h(hash, parts[0], parts[1])
|
728
725
|
else
|
729
726
|
nodes[parts[0].to_s.strip] = parts[1].to_s.strip
|
data/lib/css_parser/rule_set.rb
CHANGED
@@ -11,8 +11,10 @@ module CssParser
|
|
11
11
|
BACKGROUND_PROPERTIES = ['background-color', 'background-image', 'background-repeat', 'background-position', 'background-size', 'background-attachment'].freeze
|
12
12
|
LIST_STYLE_PROPERTIES = ['list-style-type', 'list-style-position', 'list-style-image'].freeze
|
13
13
|
FONT_STYLE_PROPERTIES = ['font-style', 'font-variant', 'font-weight', 'font-size', 'line-height', 'font-family'].freeze
|
14
|
+
FONT_WEIGHT_PROPERTIES = ['font-style', 'font-weight', 'font-variant'].freeze
|
14
15
|
BORDER_STYLE_PROPERTIES = ['border-width', 'border-style', 'border-color'].freeze
|
15
16
|
BORDER_PROPERTIES = ['border', 'border-left', 'border-right', 'border-top', 'border-bottom'].freeze
|
17
|
+
DIMENSION_DIRECTIONS = [:top, :right, :bottom, :left].freeze
|
16
18
|
|
17
19
|
NUMBER_OF_DIMENSIONS = 4
|
18
20
|
|
@@ -196,7 +198,7 @@ module CssParser
|
|
196
198
|
end
|
197
199
|
|
198
200
|
def to_s(options = {})
|
199
|
-
str = declarations.reduce(
|
201
|
+
str = declarations.reduce(+'') do |memo, (prop, value)|
|
200
202
|
importance = options[:force_important] || value.important ? ' !important' : ''
|
201
203
|
memo << "#{prop}: #{value.value}#{importance}; "
|
202
204
|
end
|
@@ -455,17 +457,17 @@ module CssParser
|
|
455
457
|
else
|
456
458
|
font_props['font-family'] = m
|
457
459
|
end
|
458
|
-
elsif
|
459
|
-
|
460
|
+
elsif /normal|inherit/i.match?(m)
|
461
|
+
FONT_WEIGHT_PROPERTIES.each do |font_prop|
|
460
462
|
font_props[font_prop] ||= m
|
461
463
|
end
|
462
|
-
elsif
|
464
|
+
elsif /italic|oblique/i.match?(m)
|
463
465
|
font_props['font-style'] = m
|
464
|
-
elsif
|
466
|
+
elsif /small-caps/i.match?(m)
|
465
467
|
font_props['font-variant'] = m
|
466
|
-
elsif
|
468
|
+
elsif /[1-9]00$|bold|bolder|lighter/i.match?(m)
|
467
469
|
font_props['font-weight'] = m
|
468
|
-
elsif
|
470
|
+
elsif CssParser::FONT_UNITS_RX.match?(m)
|
469
471
|
if m.include?('/')
|
470
472
|
font_props['font-size'], font_props['line-height'] = m.split('/', 2)
|
471
473
|
else
|
@@ -488,7 +490,7 @@ module CssParser
|
|
488
490
|
value = declaration.value.dup
|
489
491
|
|
490
492
|
replacement =
|
491
|
-
if
|
493
|
+
if CssParser::RE_INHERIT.match?(value)
|
492
494
|
LIST_STYLE_PROPERTIES.to_h { |key| [key, 'inherit'] }
|
493
495
|
else
|
494
496
|
{
|
@@ -554,15 +556,15 @@ module CssParser
|
|
554
556
|
#
|
555
557
|
# TODO: this is extremely similar to create_background_shorthand! and should be combined
|
556
558
|
def create_border_shorthand! # :nodoc:
|
557
|
-
values = BORDER_STYLE_PROPERTIES.
|
559
|
+
values = BORDER_STYLE_PROPERTIES.filter_map do |property|
|
558
560
|
next unless (declaration = declarations[property])
|
559
561
|
next if declaration.important
|
560
562
|
# can't merge if any value contains a space (i.e. has multiple values)
|
561
563
|
# we temporarily remove any spaces after commas for the check (inside rgba, etc...)
|
562
|
-
next if declaration.value.gsub(/,\s/, ',').strip
|
564
|
+
next if /\s/.match?(declaration.value.gsub(/,\s/, ',').strip)
|
563
565
|
|
564
566
|
declaration.value
|
565
|
-
end
|
567
|
+
end
|
566
568
|
|
567
569
|
return if values.size != BORDER_STYLE_PROPERTIES.size
|
568
570
|
|
@@ -579,7 +581,7 @@ module CssParser
|
|
579
581
|
return if declarations.size < NUMBER_OF_DIMENSIONS
|
580
582
|
|
581
583
|
DIMENSIONS.each do |property, dimensions|
|
582
|
-
values =
|
584
|
+
values = DIMENSION_DIRECTIONS.each_with_index.with_object({}) do |(side, index), result|
|
583
585
|
next unless (declaration = declarations[dimensions[index]])
|
584
586
|
|
585
587
|
result[side] = declaration.value
|
@@ -602,7 +604,7 @@ module CssParser
|
|
602
604
|
def create_font_shorthand! # :nodoc:
|
603
605
|
return unless FONT_STYLE_PROPERTIES.all? { |prop| declarations.key?(prop) }
|
604
606
|
|
605
|
-
new_value =
|
607
|
+
new_value = +''
|
606
608
|
['font-style', 'font-variant', 'font-weight'].each do |property|
|
607
609
|
unless declarations[property].value == 'normal'
|
608
610
|
new_value << declarations[property].value << ' '
|
@@ -639,7 +641,7 @@ module CssParser
|
|
639
641
|
return [:top] if values.values.uniq.count == 1
|
640
642
|
|
641
643
|
# `/* top | right | bottom | left */`
|
642
|
-
return
|
644
|
+
return DIMENSION_DIRECTIONS if values[:left] != values[:right]
|
643
645
|
|
644
646
|
# Vertical are the same & horizontal are the same, `/* vertical | horizontal */`
|
645
647
|
return [:top, :left] if values[:top] == values[:bottom]
|
data/lib/css_parser/version.rb
CHANGED
data/lib/css_parser.rb
CHANGED
@@ -6,7 +6,6 @@ require 'net/https'
|
|
6
6
|
require 'digest/md5'
|
7
7
|
require 'zlib'
|
8
8
|
require 'stringio'
|
9
|
-
require 'iconv' unless String.method_defined?(:encode)
|
10
9
|
|
11
10
|
require 'css_parser/version'
|
12
11
|
require 'css_parser/rule_set'
|
@@ -58,7 +57,7 @@ module CssParser
|
|
58
57
|
# in case called like CssParser.merge([rule_set, rule_set])
|
59
58
|
rule_sets.flatten! if rule_sets[0].is_a?(Array)
|
60
59
|
|
61
|
-
unless rule_sets.all?
|
60
|
+
unless rule_sets.all?(CssParser::RuleSet)
|
62
61
|
raise ArgumentError, 'all parameters must be CssParser::RuleSets.'
|
63
62
|
end
|
64
63
|
|
@@ -71,7 +70,7 @@ module CssParser
|
|
71
70
|
rule_set.expand_shorthand!
|
72
71
|
|
73
72
|
specificity = rule_set.specificity
|
74
|
-
specificity ||= rule_set.selectors.
|
73
|
+
specificity ||= rule_set.selectors.filter_map { |s| calculate_specificity(s) }.max || 0
|
75
74
|
|
76
75
|
rule_set.each_declaration do |property, value, is_important|
|
77
76
|
# Add the property to the list to be folded per http://www.w3.org/TR/CSS21/cascade.html#cascading-order
|
@@ -139,7 +138,7 @@ module CssParser
|
|
139
138
|
css.gsub(URI_RX) do
|
140
139
|
uri = Regexp.last_match(1).to_s.gsub(/["']+/, '')
|
141
140
|
# Don't process URLs that are already absolute
|
142
|
-
unless uri.match(%r{^[a-z]+://}i)
|
141
|
+
unless uri.match?(%r{^[a-z]+://}i)
|
143
142
|
begin
|
144
143
|
uri = base_uri.join(uri)
|
145
144
|
rescue
|
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.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: 2024-
|
11
|
+
date: 2024-12-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: addressable
|