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 +4 -4
- data/lib/premailer/adapter/nokogiri.rb +28 -11
- data/lib/premailer/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: b2f1c3fb322129a461edb59dedb12c4e1c69c16f72ca15e71bffc58d61c000de
|
|
4
|
+
data.tar.gz: 0be4bbe859a835cf111c43f7bf35e7f32db473f1beac237998ab66713aa3edaa
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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
|
|
40
|
-
# skip its (costly) document-wide search.
|
|
41
|
-
|
|
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
|
|
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
|
-
|
|
227
|
-
|
|
228
|
-
|
|
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
|
data/lib/premailer/version.rb
CHANGED