nokolexbor 0.3.1-x64-mingw32 → 0.3.2-x64-mingw32

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 740355ac1e01b77d40ee25ca0c97510996405199c199545c443c00e931d3c9c3
4
- data.tar.gz: 0d74a513c82a415abfee3eee1f692c0536766efdece0d8d121cec80c389e76ee
3
+ metadata.gz: 99150ff13df9107846b728c5e5671831bf55369b95807ccab622e6325a26ce8d
4
+ data.tar.gz: cea9c9c19dd67f82430a6faf0a295de323397775d4feee5bc783bf7d8ec14707
5
5
  SHA512:
6
- metadata.gz: fe7f36eafe6be686522d998606f11d3387f82c9c57abd0fbcc1bffc142edd68f75ecfb6a1ebaddac5fd82e89d3667b8c212dc944b33b0f293fb340de2072f65f
7
- data.tar.gz: f152da74b4c767f5b332a5b447c43b8d53c9a357de6240cc5a09164230d0de717cb37bd0b20282036082ebcdd587013ad5f340cbb52389bf355d2d147dc2469c
6
+ metadata.gz: 6d5efe7c91f775aab452c61607a593978727f6a245534c787c18ac32ec8920fe6e025d2415a27f74f60fa0fd5bef4e69f02e54287390254a0f3dedd2db47e026
7
+ data.tar.gz: 53a48466dc564f5e8adef50bcff12c249edff7b84f16d55fe793f73745f78e8ba3a9fb4849d54de787028455788d58d93f248b3e8777cb2443042d8ec226cb1a
Binary file
Binary file
Binary file
@@ -93,5 +93,9 @@ module Nokolexbor
93
93
  head.prepend_child(element)
94
94
  end
95
95
  end
96
+
97
+ private
98
+
99
+ IMPLIED_XPATH_CONTEXTS = ["//"].freeze # :nodoc:
96
100
  end
97
101
  end
@@ -192,6 +192,16 @@ module Nokolexbor
192
192
  at_css_impl(args.join(', '))
193
193
  end
194
194
 
195
+ def nokogiri_css(*args)
196
+ rules, handler, ns, _ = extract_params(args)
197
+
198
+ nokogiri_css_internal(self, rules, handler, ns)
199
+ end
200
+
201
+ def nokogiri_at_css(*args)
202
+ nokogiri_css(*args).first
203
+ end
204
+
195
205
  def xpath(*args)
196
206
  paths, handler, ns, binds = extract_params(args)
197
207
 
@@ -299,6 +309,10 @@ module Nokolexbor
299
309
 
300
310
  private
301
311
 
312
+ def nokogiri_css_internal(node, rules, handler, ns)
313
+ xpath_internal(node, css_rules_to_xpath(rules, ns), handler, ns, nil)
314
+ end
315
+
302
316
  def xpath_internal(node, paths, handler, ns, binds)
303
317
  # document = node.document
304
318
  # return NodeSet.new(document) unless document
@@ -326,6 +340,34 @@ module Nokolexbor
326
340
  ctx.evaluate(path, handler)
327
341
  end
328
342
 
343
+ def css_rules_to_xpath(rules, ns)
344
+ rules.map { |rule| xpath_query_from_css_rule(rule, ns) }
345
+ end
346
+
347
+ def ensure_nokogiri
348
+ unless defined?(Nokogiri) && defined?(Nokogiri::CSS)
349
+ require 'nokogiri'
350
+ end
351
+ rescue LoadError
352
+ fail('nokogiri_css and nokogiri_at_css require Nokogiri to be installed')
353
+ end
354
+
355
+ def xpath_query_from_css_rule(rule, ns)
356
+ ensure_nokogiri
357
+ if defined? Nokogiri::CSS::XPathVisitor::BuiltinsConfig
358
+ visitor = Nokogiri::CSS::XPathVisitor.new(
359
+ builtins: Nokogiri::CSS::XPathVisitor::BuiltinsConfig::OPTIMAL,
360
+ doctype: :html4,
361
+ )
362
+ else
363
+ visitor = Nokogiri::CSS::XPathVisitorOptimallyUseBuiltins.new
364
+ end
365
+ self.class::IMPLIED_XPATH_CONTEXTS.map do |implied_xpath_context|
366
+ Nokogiri::CSS.xpath_for(rule.to_s, { prefix: implied_xpath_context, ns: ns,
367
+ visitor: visitor, })
368
+ end.join(" | ")
369
+ end
370
+
329
371
  def extract_params(params)
330
372
  handler = params.find do |param|
331
373
  ![Hash, String, Symbol].include?(param.class)
@@ -344,5 +386,7 @@ module Nokolexbor
344
386
 
345
387
  [params, handler, ns, binds]
346
388
  end
389
+
390
+ IMPLIED_XPATH_CONTEXTS = [".//"].freeze
347
391
  end
348
- end
392
+ end
@@ -126,6 +126,23 @@ module Nokolexbor
126
126
  end
127
127
  end
128
128
  end
129
+
130
+ def nokogiri_css(*args)
131
+ rules, handler, ns, _ = extract_params(args)
132
+ paths = css_rules_to_xpath(rules, ns)
133
+
134
+ NodeSet.new(@document) do |set|
135
+ each do |node|
136
+ node.send(:xpath_internal, node, paths, handler, ns, nil).each do |inner_node|
137
+ set << inner_node
138
+ end
139
+ end
140
+ end
141
+ end
142
+
143
+ private
144
+
145
+ IMPLIED_XPATH_CONTEXTS = [".//", "self::"].freeze # :nodoc:
129
146
 
130
147
  end
131
148
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Nokolexbor
4
- VERSION = '0.3.1'
4
+ VERSION = '0.3.2'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nokolexbor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: x64-mingw32
6
6
  authors:
7
7
  - Yicheng Zhou
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-12-31 00:00:00.000000000 Z
11
+ date: 2023-01-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake-compiler