p_css 0.1.4 → 0.1.5

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: 20afa2206ed855fdd796f19179d06a8dc8b76231c223560d59e664cb43ddf897
4
- data.tar.gz: e951f89d04ff6db6f68151ad05a414e4f0f7d05f2dfc48580b22ef31b6f949de
3
+ metadata.gz: 965047de25f5fe1719b2d2b4c59d2fa2d74550249e2ffa23d4175ae88dc09ba9
4
+ data.tar.gz: ab8078d7209427b3f23cd774492fae69543bd8910b812a1a87fc7da38b8df89c
5
5
  SHA512:
6
- metadata.gz: dc533dd2a146654d7a622b3206568168bea1e404b163ef6b24ae1c841ef4cd5ff3a4621bc6794f64178ffd5c87a0c4e6a46f70fd6c7999a52539091849ae2941
7
- data.tar.gz: 67d08837559466bc5713aa6a40d8e67030ff9389228527b4ed8b4bb5e1121965fa90a31e6cf5aae40167151e535ae83b9f6073068e72d495a5098559b331698d
6
+ metadata.gz: '069d1c89de282155ee708b19aee7cc61226a68bd7ad662a869536b823bb99d58c6b8b1dae2567fa9f27d337fda392ea52f62592a718580aead69e1ded17cbf47'
7
+ data.tar.gz: 2d0ec3fd003f145859790f3140ec808e5ba1d1273dc2e78e379d7ef5eec3dd44c9c031cd8e5db22f5b1b63b27d52f9973cc18f19b0f184ebd6a882d73b24ac36
data/lib/css/cascade.rb CHANGED
@@ -31,7 +31,11 @@ module CSS
31
31
  end
32
32
 
33
33
  # Returns Hash<String, Declaration> of winning declarations.
34
- def resolve(element, inline_style: nil)
34
+ #
35
+ # `state:` opts into stateful-pseudo matching — see
36
+ # `Selectors::Matcher#matches?` for the shape. Defaults to the
37
+ # stateless behavior (`:hover`, `:focus`, etc. never match).
38
+ def resolve(element, inline_style: nil, state: nil)
35
39
  cache = {}
36
40
  candidates = collect_candidate_indexes(element, cache)
37
41
  order = 0
@@ -39,7 +43,7 @@ module CSS
39
43
 
40
44
  candidates.each do |idx|
41
45
  entry = @entries[idx]
42
- spec = best_matching_specificity(element, entry.selector_pairs, cache)
46
+ spec = best_matching_specificity(element, entry.selector_pairs, cache, state)
43
47
 
44
48
  next if spec.nil?
45
49
 
@@ -204,11 +208,11 @@ module CSS
204
208
  out
205
209
  end
206
210
 
207
- def best_matching_specificity(element, selector_pairs, cache)
211
+ def best_matching_specificity(element, selector_pairs, cache, state)
208
212
  best = nil
209
213
 
210
214
  selector_pairs.each do |sel, spec|
211
- next unless Selectors::Matcher.matches?(element, sel, cache: cache)
215
+ next unless Selectors::Matcher.matches?(element, sel, cache: cache, state: state)
212
216
 
213
217
  best = spec if best.nil? || spec > best
214
218
  end
@@ -16,8 +16,9 @@ module CSS
16
16
  # protocol out of the box.
17
17
  #
18
18
  # Pseudo-classes that depend on user-agent state (`:hover`, `:focus`,
19
- # `:visited`, validity-API states, `:fullscreen`, etc.) always return
20
- # false; this matcher is intended for stateless analysis.
19
+ # `:visited`, etc.) return false by default; pass an explicit `state:`
20
+ # mapping to opt into stateful matching. Validity-API and viewport-
21
+ # only states (`:fullscreen`, `:valid`, …) are not exposed.
21
22
  module Matcher
22
23
  extend self
23
24
 
@@ -26,6 +27,16 @@ module CSS
26
27
  LINK_TAGS = %w[a area link].freeze
27
28
  RO_INPUT_TYPES = %w[hidden range color checkbox radio file submit image reset button].freeze
28
29
 
30
+ # User-agent state pseudos. The matcher returns `false` for these
31
+ # unless the caller passes a `state:` Hash describing which
32
+ # elements (or "all") should match.
33
+ STATEFUL_PSEUDOS = %w[hover focus focus-within focus-visible active visited target].to_set.freeze
34
+
35
+ # Per spec these states propagate up the ancestor chain — if a
36
+ # descendant is hovered/active/contains-focus, the ancestors
37
+ # share the state for selector-matching purposes.
38
+ PROPAGATING_STATEFUL_PSEUDOS = %w[hover active focus-within].to_set.freeze
39
+
29
40
  # Per-element cache used to avoid recomputing tag / id / class set
30
41
  # for every selector in a hot loop (e.g. `Cascade#resolve` against
31
42
  # hundreds of rules). Keyed by `Object#object_id`; only valid for
@@ -34,16 +45,16 @@ module CSS
34
45
 
35
46
  EMPTY_CLASS_SET = Set.new.freeze
36
47
 
37
- def matches?(element, selector, cache: nil)
48
+ def matches?(element, selector, cache: nil, state: nil)
38
49
  sel = selector.is_a?(String) ? Parser.parse_selector_list(selector) : selector
39
50
 
40
51
  case sel
41
52
  when SelectorList
42
- sel.selectors.any? { match_complex(element, it, cache) }
53
+ sel.selectors.any? { match_complex(element, it, cache, state) }
43
54
  when ComplexSelector
44
- match_complex(element, sel, cache)
55
+ match_complex(element, sel, cache, state)
45
56
  when CompoundSelector
46
- match_compound(element, sel, cache)
57
+ match_compound(element, sel, cache, state)
47
58
  else
48
59
  raise ArgumentError, "expected a selector node or string, got #{sel.class}"
49
60
  end
@@ -54,32 +65,32 @@ module CSS
54
65
  # Walks the complex selector right-to-left starting at the rightmost
55
66
  # compound. Each combinator either succeeds against ancestors /
56
67
  # siblings of the current candidate or fails the whole match.
57
- def match_complex(element, complex, cache)
58
- match_at(element, complex, complex.compounds.size - 1, cache)
68
+ def match_complex(element, complex, cache, state)
69
+ match_at(element, complex, complex.compounds.size - 1, cache, state)
59
70
  end
60
71
 
61
- def match_at(element, complex, index, cache)
72
+ def match_at(element, complex, index, cache, state)
62
73
  return false if element.nil?
63
- return false unless match_compound(element, complex.compounds[index], cache)
74
+ return false unless match_compound(element, complex.compounds[index], cache, state)
64
75
  return true if index.zero?
65
76
 
66
77
  prev = index - 1
67
78
 
68
79
  case complex.combinators[prev]
69
- when :descendant then walk_until_match(element, complex, prev, :parent_element, cache)
70
- when :child then match_at(parent_element(element), complex, prev, cache)
71
- when :next_sibling then match_at(previous_element(element), complex, prev, cache)
72
- when :subsequent_sibling then walk_until_match(element, complex, prev, :previous_element, cache)
80
+ when :descendant then walk_until_match(element, complex, prev, :parent_element, cache, state)
81
+ when :child then match_at(parent_element(element), complex, prev, cache, state)
82
+ when :next_sibling then match_at(previous_element(element), complex, prev, cache, state)
83
+ when :subsequent_sibling then walk_until_match(element, complex, prev, :previous_element, cache, state)
73
84
  end
74
85
  end
75
86
 
76
87
  # Steps along the DOM via `direction` until a candidate matches the
77
88
  # remaining complex selector or the chain runs out.
78
- def walk_until_match(element, complex, index, direction, cache)
89
+ def walk_until_match(element, complex, index, direction, cache, state)
79
90
  candidate = send(direction, element)
80
91
 
81
92
  while candidate
82
- return true if match_at(candidate, complex, index, cache)
93
+ return true if match_at(candidate, complex, index, cache, state)
83
94
 
84
95
  candidate = send(direction, candidate)
85
96
  end
@@ -87,18 +98,18 @@ module CSS
87
98
  false
88
99
  end
89
100
 
90
- def match_compound(element, compound, cache)
91
- compound.components.all? { match_simple(element, it, cache) }
101
+ def match_compound(element, compound, cache, state)
102
+ compound.components.all? { match_simple(element, it, cache, state) }
92
103
  end
93
104
 
94
- def match_simple(element, simple, cache)
105
+ def match_simple(element, simple, cache, state)
95
106
  case simple
96
107
  when TypeSelector then tag_of(element, cache).casecmp?(simple.name)
97
108
  when UniversalSelector then true
98
109
  when IdSelector then id_of(element, cache) == simple.name
99
110
  when ClassSelector then classes_of(element, cache).include?(simple.name)
100
111
  when AttributeSelector then match_attribute(element, simple)
101
- when PseudoClass then match_pseudo_class(element, simple, cache)
112
+ when PseudoClass then match_pseudo_class(element, simple, cache, state)
102
113
  when PseudoElement then false
103
114
  when NestingSelector then false
104
115
  else false
@@ -172,10 +183,14 @@ module CSS
172
183
 
173
184
  # Pseudo-class matching -------------------------------------------
174
185
 
175
- def match_pseudo_class(element, pc, cache)
176
- case pc.name.downcase
177
- when 'is', 'where', 'matches' then match_selector_list_arg(element, pc.argument, cache)
178
- when 'not' then negate_selector_list_arg(element, pc.argument, cache)
186
+ def match_pseudo_class(element, pc, cache, state)
187
+ name = pc.name.downcase
188
+
189
+ return match_stateful_pseudo?(name, element, state) if STATEFUL_PSEUDOS.include?(name)
190
+
191
+ case name
192
+ when 'is', 'where', 'matches' then match_selector_list_arg(element, pc.argument, cache, state)
193
+ when 'not' then negate_selector_list_arg(element, pc.argument, cache, state)
179
194
  when 'has' then false
180
195
  when 'root' then parent_element(element).nil?
181
196
  when 'scope' then parent_element(element).nil?
@@ -206,12 +221,40 @@ module CSS
206
221
  end
207
222
  end
208
223
 
209
- def match_selector_list_arg(element, arg, cache)
210
- arg.is_a?(SelectorList) && matches?(element, arg, cache: cache)
224
+ # `:hover` / `:active` / `:focus-within` propagate up the ancestor
225
+ # chain per Selectors §10 — the Set members are the *source* nodes
226
+ # (e.g. the deepest hovered element) and any of their ancestors
227
+ # also matches. Other stateful pseudos match only the explicit
228
+ # elements in the Set.
229
+ def match_stateful_pseudo?(name, element, state)
230
+ return false if state.nil?
231
+
232
+ value = state[name.to_sym] || state[name]
233
+
234
+ return false if value.nil? || value == false
235
+ return true if value == true
236
+
237
+ return value.include?(element) unless PROPAGATING_STATEFUL_PSEUDOS.include?(name)
238
+
239
+ value.each do |source|
240
+ cur = source
241
+
242
+ while cur
243
+ return true if cur == element
244
+
245
+ cur = parent_element(cur)
246
+ end
247
+ end
248
+
249
+ false
250
+ end
251
+
252
+ def match_selector_list_arg(element, arg, cache, state)
253
+ arg.is_a?(SelectorList) && matches?(element, arg, cache: cache, state: state)
211
254
  end
212
255
 
213
- def negate_selector_list_arg(element, arg, cache)
214
- arg.is_a?(SelectorList) && !matches?(element, arg, cache: cache)
256
+ def negate_selector_list_arg(element, arg, cache, state)
257
+ arg.is_a?(SelectorList) && !matches?(element, arg, cache: cache, state: state)
215
258
  end
216
259
 
217
260
  def match_nth(element, anb, of_type:, from_end:)
data/lib/css/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module CSS
2
- VERSION = '0.1.4'
2
+ VERSION = '0.1.5'
3
3
  end
data/lib/css.rb CHANGED
@@ -50,7 +50,7 @@ module CSS
50
50
 
51
51
  def specificity(selector) = Selectors::SpecificityCalculator.calculate(selector)
52
52
 
53
- def matches?(element, selector) = Selectors::Matcher.matches?(element, selector)
53
+ def matches?(element, selector, state: nil) = Selectors::Matcher.matches?(element, selector, state: state)
54
54
 
55
55
  def parse_media_query_list(input) = MediaQueries::Parser.parse(input)
56
56
 
data/sig/css/cascade.rbs CHANGED
@@ -17,6 +17,6 @@ module CSS
17
17
  # Returns Hash<String, Declaration> of winning declarations after
18
18
  # !important > inline > stylesheet > specificity > source-order
19
19
  # sorting.
20
- def resolve: (untyped element, ?inline_style: inline_style?) -> Hash[String, Nodes::Declaration]
20
+ def resolve: (untyped element, ?inline_style: inline_style?, ?state: matcher_state?) -> Hash[String, Nodes::Declaration]
21
21
  end
22
22
  end
@@ -151,7 +151,10 @@ module CSS
151
151
  end
152
152
 
153
153
  module Matcher
154
- def self.matches?: (untyped element, untyped selector, ?cache: Hash[Integer, untyped]) -> bool
154
+ STATEFUL_PSEUDOS: Set[String]
155
+ PROPAGATING_STATEFUL_PSEUDOS: Set[String]
156
+
157
+ def self.matches?: (untyped element, untyped selector, ?cache: Hash[Integer, untyped], ?state: matcher_state?) -> bool
155
158
 
156
159
  def self.tag_of: (untyped element, ?Hash[Integer, untyped]?) -> String
157
160
  def self.id_of: (untyped element, ?Hash[Integer, untyped]?) -> String?
data/sig/css.rbs CHANGED
@@ -68,7 +68,12 @@ module CSS
68
68
 
69
69
  type selector = Selectors::SelectorList | Selectors::ComplexSelector | Selectors::CompoundSelector
70
70
 
71
- def self.matches?: (untyped element, String | selector selector, ?cache: Hash[Integer, untyped]) -> bool
71
+ # Per-pseudo state. Keys are pseudo-class names (Symbol or String);
72
+ # values are `true` (match every element), a `Set` / `Array` of the
73
+ # specific elements in that state, or falsy (no match).
74
+ type matcher_state = Hash[Symbol | String, bool | Set[untyped] | Array[untyped]]
75
+
76
+ def self.matches?: (untyped element, String | selector selector, ?cache: Hash[Integer, untyped], ?state: matcher_state?) -> bool
72
77
 
73
78
  # Media queries (Media Queries 4)
74
79
  # ---------------------------------------------------------------
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: p_css
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Keita Urashima