nokolexbor 0.3.1-x64-mingw-ucrt → 0.3.3-x64-mingw-ucrt

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: 26df5e2b997ed24918e747bbfb76a364d46a641becb773f9ded960e26a9982ac
4
- data.tar.gz: b0ccf1180dc8fd0240d987672c2a3ff01da4e363883734e326e4ce9d17800013
3
+ metadata.gz: b3a9ae41e4e72bc88b565954821978a8aceefd5cad7834addd14270aa97d974a
4
+ data.tar.gz: 965ae4b6706d44027dc96453bdcf75a8990257a6e00ec43cfc813a2aa622ee21
5
5
  SHA512:
6
- metadata.gz: 69c121521730467e671e7b16952bf68a6bc2629e6db220f6c28b65e809ffd61fbdbcf27329fbb40920dfd4d1537b892148ee929341ef7243f95dd1d025f19644
7
- data.tar.gz: 6b8c4a60b7636e3137dccfb7acc83bd5f89888986d1fa9d0b85e57e17de32c2d93be74da2f113030d3a561bda13be4ea7b9e37b7c5abdf4d83fd1cce68fd27f0
6
+ metadata.gz: 3c3a908f950c137e317a3030a9e2b0b63d7fd94b688e50a3deac249baff9f3c95aadcb735ec401e59bb28fa563320c7157574852724641ab6fbbf8c846b0846d
7
+ data.tar.gz: 59f4f3b30a2e77d04d4a716f765da0c243fdca766409076fe373a219450240e18e002e8240dc63bc1bb2fe95262ae24f96b958d3f16f5c9813c3c6679f57b330
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
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nokolexbor
4
+ class DocumentFragment < Nokolexbor::Node
5
+ def self.parse(tags)
6
+ new(Nokolexbor::Document.new, tags, nil)
7
+ end
8
+
9
+ def initialize(document, tags = nil, ctx = nil)
10
+ return self unless tags
11
+
12
+ ctx ||= document
13
+ node_set = ctx.parse(tags)
14
+ node_set.each { |child| child.parent = self } unless node_set.empty?
15
+ nil
16
+ end
17
+
18
+ def name
19
+ "#document-fragment"
20
+ end
21
+
22
+ alias_method :outer_html, :inner_html
23
+ alias_method :to_html, :outer_html
24
+ alias_method :to_s, :outer_html
25
+ alias_method :serialize, :outer_html
26
+
27
+ def fragment(data)
28
+ document.fragment(data)
29
+ end
30
+ end
31
+ end
@@ -182,6 +182,10 @@ module Nokolexbor
182
182
  end
183
183
  end
184
184
 
185
+ def fragment(tags)
186
+ Nokolexbor::DocumentFragment.new(document, tags, self)
187
+ end
188
+
185
189
  alias_method :inner_html=, :children=
186
190
 
187
191
  def css(*args)
@@ -192,6 +196,16 @@ module Nokolexbor
192
196
  at_css_impl(args.join(', '))
193
197
  end
194
198
 
199
+ def nokogiri_css(*args)
200
+ rules, handler, ns, _ = extract_params(args)
201
+
202
+ nokogiri_css_internal(self, rules, handler, ns)
203
+ end
204
+
205
+ def nokogiri_at_css(*args)
206
+ nokogiri_css(*args).first
207
+ end
208
+
195
209
  def xpath(*args)
196
210
  paths, handler, ns, binds = extract_params(args)
197
211
 
@@ -299,6 +313,10 @@ module Nokolexbor
299
313
 
300
314
  private
301
315
 
316
+ def nokogiri_css_internal(node, rules, handler, ns)
317
+ xpath_internal(node, css_rules_to_xpath(rules, ns), handler, ns, nil)
318
+ end
319
+
302
320
  def xpath_internal(node, paths, handler, ns, binds)
303
321
  # document = node.document
304
322
  # return NodeSet.new(document) unless document
@@ -326,6 +344,34 @@ module Nokolexbor
326
344
  ctx.evaluate(path, handler)
327
345
  end
328
346
 
347
+ def css_rules_to_xpath(rules, ns)
348
+ rules.map { |rule| xpath_query_from_css_rule(rule, ns) }
349
+ end
350
+
351
+ def ensure_nokogiri
352
+ unless defined?(Nokogiri) && defined?(Nokogiri::CSS)
353
+ require 'nokogiri'
354
+ end
355
+ rescue LoadError
356
+ fail('nokogiri_css and nokogiri_at_css require Nokogiri to be installed')
357
+ end
358
+
359
+ def xpath_query_from_css_rule(rule, ns)
360
+ ensure_nokogiri
361
+ if defined? Nokogiri::CSS::XPathVisitor::BuiltinsConfig
362
+ visitor = Nokogiri::CSS::XPathVisitor.new(
363
+ builtins: Nokogiri::CSS::XPathVisitor::BuiltinsConfig::OPTIMAL,
364
+ doctype: :html4,
365
+ )
366
+ else
367
+ visitor = Nokogiri::CSS::XPathVisitorOptimallyUseBuiltins.new
368
+ end
369
+ self.class::IMPLIED_XPATH_CONTEXTS.map do |implied_xpath_context|
370
+ Nokogiri::CSS.xpath_for(rule.to_s, { prefix: implied_xpath_context, ns: ns,
371
+ visitor: visitor, })
372
+ end.join(" | ")
373
+ end
374
+
329
375
  def extract_params(params)
330
376
  handler = params.find do |param|
331
377
  ![Hash, String, Symbol].include?(param.class)
@@ -344,5 +390,7 @@ module Nokolexbor
344
390
 
345
391
  [params, handler, ns, binds]
346
392
  end
393
+
394
+ IMPLIED_XPATH_CONTEXTS = [".//"].freeze
347
395
  end
348
- end
396
+ 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.3'
5
5
  end
data/lib/nokolexbor.rb CHANGED
@@ -25,6 +25,7 @@ require 'nokolexbor/version'
25
25
  require 'nokolexbor/node'
26
26
  require 'nokolexbor/document'
27
27
  require 'nokolexbor/node_set'
28
+ require 'nokolexbor/document_fragment'
28
29
  require 'nokolexbor/attribute'
29
30
  require 'nokolexbor/xpath'
30
31
  require 'nokolexbor/xpath_context'
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.3
5
5
  platform: x64-mingw-ucrt
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
@@ -49,6 +49,7 @@ files:
49
49
  - lib/nokolexbor/3.1/nokolexbor.so
50
50
  - lib/nokolexbor/attribute.rb
51
51
  - lib/nokolexbor/document.rb
52
+ - lib/nokolexbor/document_fragment.rb
52
53
  - lib/nokolexbor/node.rb
53
54
  - lib/nokolexbor/node_set.rb
54
55
  - lib/nokolexbor/version.rb