premailer 1.33.0 → 1.34.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b30285e2878d409bc44b84777bc055096cd023b666dd7453164586fb8e97a7dd
4
- data.tar.gz: 47bfb72f292747619e4067332275f40f85bd225725977507a57e97bebffbdd71
3
+ metadata.gz: b2f1c3fb322129a461edb59dedb12c4e1c69c16f72ca15e71bffc58d61c000de
4
+ data.tar.gz: 0be4bbe859a835cf111c43f7bf35e7f32db473f1beac237998ab66713aa3edaa
5
5
  SHA512:
6
- metadata.gz: 77556f76f97fa85e0047b1e45ebdd530853d3cc33794366b3979b4475920a6b87163853d8349b2f507e6fbafa24a2a747529f448f3335b3b03446713266b3ab2
7
- data.tar.gz: 50ac02894d5fef9ccc6e5b8df0e75feac8dcedff61eacdca4fac43230f9802319907648377666f132e19da46585965020adf128d70d0a7e7d12348d70103f7d9
6
+ metadata.gz: 331b19085821f14b95078855532b79c97395bf06551ddb63f542838a65eda5e1597a946f68adc809624a4edc99b7cfdc6f71bf97015f5c20e1cfd870a30dde1a
7
+ data.tar.gz: '087bb9a3e49a924324628a1e7b21b1fa710a03835b49648861481845311ed64b8940903da7a4044ad9c5d40147fe969515b6f8babb46fc71fc86962c4e63b554'
@@ -6,11 +6,10 @@ class Premailer
6
6
  # Nokogiri adapter
7
7
  module Nokogiri
8
8
  WIDTH_AND_HIGHT = ['width', 'height'].freeze
9
- # Selectors made only of element names, classes, ids, combinators and
10
- # pseudo-classes without arguments: no attribute, string, function or escape
11
- # can hide a "." that is not a class.
12
- PLAIN_SELECTOR = /\A[\w\s.#>+~*:-]+\z/
13
9
  CLASS_IN_SELECTOR = /\.([\w-]+)/
10
+ ID_IN_SELECTOR = /#([\w-]+)/
11
+ ATTR_IN_SELECTOR = /\[\s*([\w-]+)/
12
+ TYPE_IN_SELECTOR = /(?:\A|[\s>+~])([a-zA-Z][\w-]*)/
14
13
 
15
14
  include AdapterHelper::RgbToHex
16
15
  # Merge CSS into the HTML document.
@@ -36,9 +35,14 @@ class Premailer
36
35
  rs = rule_set_cache.fetch(style) { rule_set_cache[style] = build_rule_set(style, 1000) }
37
36
  rules_by_node[el] = rs ? [rs] : []
38
37
  end
39
- # A selector requiring a class that no element carries cannot match:
40
- # skip its (costly) document-wide search.
41
- classes_in_doc = doc.xpath('.//@class').flat_map { |attribute| attribute.value.split }.to_set
38
+ # A selector requiring a class, id, attribute or element type that no node in the document
39
+ # carries cannot match: skip its (costly) document-wide search.
40
+ tokens_in_doc = {
41
+ classes: doc.xpath('.//@class').flat_map { |attribute| attribute.value.split }.to_set,
42
+ ids: doc.xpath('.//@id').to_set(&:value),
43
+ attrs: doc.xpath('.//@*').to_set(&:name),
44
+ types: doc.xpath('.//*').to_set { |node| node.name.downcase }
45
+ }
42
46
 
43
47
  # Iterate through the rules and merge them into the HTML
44
48
  @css_parser.each_selector(:all) do |selector, declaration, specificity, media_types|
@@ -58,7 +62,7 @@ class Premailer
58
62
  @unmergable_rules.add_rule_set!(CssParser::RuleSet.new(selectors: selector, block: declaration))
59
63
  end
60
64
 
61
- next if selector_requires_absent_class?(selector, classes_in_doc)
65
+ next unless selector_can_match?(selector, tokens_in_doc)
62
66
 
63
67
  # Change single ID CSS selectors into xpath so that we can match more
64
68
  # than one element. Added to work around dodgy generated code.
@@ -223,9 +227,22 @@ class Premailer
223
227
  [merged.declarations_to_s, attributes]
224
228
  end
225
229
 
226
- def selector_requires_absent_class?(selector, classes_in_doc) # :nodoc:
227
- PLAIN_SELECTOR.match?(selector) &&
228
- selector.scan(CLASS_IN_SELECTOR).any? { |(name)| !classes_in_doc.include?(name) }
230
+ # Whether `selector` can match something, false means its document-wide search can be skipped.
231
+ def selector_can_match?(selector, tokens_in_doc) # :nodoc:
232
+ # Selectors with a functional pseudo-class (e.g. :not(...)) can match by the absence of a token
233
+ # and escapes can hide token boundaries, so those are always searched.
234
+ return true if selector.include?("(") || selector.include?("\\")
235
+
236
+ # An attribute selector needs its named attribute to exist
237
+ return false unless selector.scan(ATTR_IN_SELECTOR).all? { |(n)| tokens_in_doc[:attrs].include?(n) }
238
+
239
+ # Drop attribute selectors so a quoted value (e.g. [href$=".pdf"]) is not mistaken for a class or id
240
+ structural = selector.gsub(/\[[^\]]*\]/, "")
241
+
242
+ # Every class, id and element type the selector names must exist somewhere in the document
243
+ structural.scan(CLASS_IN_SELECTOR).all? { |(n)| tokens_in_doc[:classes].include?(n) } &&
244
+ structural.scan(ID_IN_SELECTOR).all? { |(n)| tokens_in_doc[:ids].include?(n) } &&
245
+ structural.scan(TYPE_IN_SELECTOR).all? { |(n)| tokens_in_doc[:types].include?(n.downcase) }
229
246
  end
230
247
 
231
248
  # Build an expanded rule set for folding. Returns nil (and optionally
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
  class Premailer
3
3
  # Premailer version.
4
- VERSION = '1.33.0'
4
+ VERSION = '1.34.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: premailer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.33.0
4
+ version: 1.34.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Dunae