nokolexbor 0.3.1-x86_64-darwin → 0.3.2-x86_64-darwin

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: f01016194dfcfb9cb81bca65c5e606c981bfea10ca241fb5c791957f00aa4b0e
4
- data.tar.gz: e95070cc353c7991be74f891c5c857218b5bb9e2c2b2506086c413ae511bd73f
3
+ metadata.gz: 643714048497a0e912335327ed7f21270dabacc9bd6eb3b5a80619e07d80f471
4
+ data.tar.gz: 477fe7e93c8099d17b918cc5ec8bd308ab7b65537246cefbcbaafdbf8af24435
5
5
  SHA512:
6
- metadata.gz: 439d129a0b6cbd075f4b970f66e3234bfd8a278b373a9e53d8a91d94e861e7d977e05a34a8b532ec59725cd396be70d4db0a1d9705ab9254565451e35bc7b0ca
7
- data.tar.gz: '08b14ae65c496e31aaff33219173a8014f8e253c2965eb8af7556bd52626b1be03235d8473e3914233f67f90a1401cc4e23fb0cb0c7f13fabd2ade8d482dc753'
6
+ metadata.gz: 8c0f49471e4db5159f2358ca1884c2792c496c55a5ae5eae109db89b773f44755942b78e077ea0751c8575c5613b40e32069fbcb604f28f0232332a5c980da6d
7
+ data.tar.gz: 439cacd2f57207e3b8225a893309ee1e764ff0512e041fa6ff20ca85216e9c9520d3df3be9f433653b8e56ffe955d3de920ed54e983a69d57f487b252591f546
Binary file
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: x86_64-darwin
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