p_css 0.1.7 → 0.1.9

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: 99fe89569a8cb686b5fa018e7403b55ba6603a46ccad4a8df4821135579b6ada
4
- data.tar.gz: 8474073401ef004e365a6e294dcf35c92338ad6c4688168e999fccecac6a95e5
3
+ metadata.gz: 7c7c770667eefbfabcfd7543684970c971588c7d31ef7eaa8dfde8dfc71382ec
4
+ data.tar.gz: fe5c819bb338d0417fed4e15fbf85f9c8a158038a0fd9d17e78e308c2e1c949f
5
5
  SHA512:
6
- metadata.gz: 7c79fcc55da6b57149893332f9d491912f66d04d8a2f6d6ffef9f57fb30872ecf22b00b4184730088fbcc92639bf7d55204c075147b78174f2f5e48d057f9ba8
7
- data.tar.gz: efb733700697de31e950e90b35f10597d22975c3e0ae64ed8575474c0da175bd80bed92c39eedd3a3154560a0b7a604c79eeb3cb0ccec3b785429d7705918346
6
+ metadata.gz: cf33855007bc4c7691d7a9c726eb51f85215492643ec54c2510438a4d10da5afefa176cc062c8eab5774b4a86a23049c5b7b5f64f121682581f34aceb826870d
7
+ data.tar.gz: c72b82737b2c24a7585022da646135f743dfc9a59dc918267482f57ae2e68cd79dcfd6da106a14b6962ac2fd325ab09032a40e59148191d4146b08cc057ef37f
data/lib/css/cascade.rb CHANGED
@@ -35,8 +35,15 @@ module CSS
35
35
  # `state:` opts into stateful-pseudo matching — see
36
36
  # `Selectors::Matcher#matches?` for the shape. Defaults to the
37
37
  # stateless behavior (`:hover`, `:focus`, etc. never match).
38
- def resolve(element, inline_style: nil, state: nil)
39
- cache = {}
38
+ #
39
+ # `cache:` lets callers share a per-element context cache across many
40
+ # resolves. The default `{}` is local to one call. Pass a persistent
41
+ # Hash when the DOM is stable across many resolves — Context (tag, id,
42
+ # classes) computation runs once per element instead of per resolve.
43
+ # The caller is responsible for clearing/replacing the cache on DOM
44
+ # mutation.
45
+ def resolve(element, inline_style: nil, state: nil, cache: nil)
46
+ cache ||= {}
40
47
  candidates = collect_candidate_indexes(element, cache)
41
48
  order = 0
42
49
  matches = []
@@ -43,7 +43,7 @@ module CSS
43
43
  # the duration of a single matcher invocation.
44
44
  Context = Data.define(:tag, :id, :classes)
45
45
 
46
- EMPTY_CLASS_SET = Set.new.freeze
46
+ EMPTY_CLASSES = [].freeze
47
47
 
48
48
  def matches?(element, selector, cache: nil, state: nil)
49
49
  sel = selector.is_a?(String) ? Parser.parse_selector_list(selector) : selector
@@ -148,11 +148,15 @@ module CSS
148
148
  )
149
149
  end
150
150
 
151
+ # Returns an Array of class names. We deliberately don't wrap in a Set:
152
+ # construction allocates two objects (Array + Set), and on the typical
153
+ # 1–5 classes per element, Array#include? is fast enough that the
154
+ # construction win dominates the lookup penalty.
151
155
  def build_class_set(element)
152
156
  v = attr(element, 'class')
153
- return EMPTY_CLASS_SET if v.nil? || v.empty?
157
+ return EMPTY_CLASSES if v.nil? || v.empty?
154
158
 
155
- v.to_s.split(/\s+/).to_set
159
+ v.to_s.split(' ')
156
160
  end
157
161
 
158
162
  # Attribute matching ----------------------------------------------
@@ -453,9 +457,15 @@ module CSS
453
457
 
454
458
  # Element protocol helpers ---------------------------------------
455
459
 
460
+ # Callers (LINK_TAGS.include?, case statements) compare against
461
+ # lowercase literals, so the result must be lowercase. But Nokogiri's
462
+ # HTML parsers already emit lowercase names — the .downcase only fires
463
+ # in XML / uppercase-tag cases. Skip the allocation when there's
464
+ # nothing to lower.
456
465
  def tag(element)
457
466
  name = element.respond_to?(:tag_name) ? element.tag_name : element.name
458
- name.to_s.downcase
467
+ name = name.to_s
468
+ name.match?(/[A-Z]/) ? name.downcase : name
459
469
  end
460
470
 
461
471
  def attr(element, name)
data/lib/css/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module CSS
2
- VERSION = '0.1.7'
2
+ VERSION = '0.1.9'
3
3
  end
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.7
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Keita Urashima