ruby_rnv 0.5.2 → 0.5.3

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: 4214fd7cf9852894c4b122a3d2d8bc15a87c4e0aa464bb3bcb359cfe0449c3a4
4
- data.tar.gz: bbb487f231d7cf15affaaab7f794e656100f57a1022682994da12ceb0619b5fc
3
+ metadata.gz: 708b7824fc5d388ab19228ff035c2b2769a1d6847b95d3e56efe8350420ef29b
4
+ data.tar.gz: df218207c1ebfb2ca320ec3bfd03a04180b9eb5a0440676d054beb1fbcf5390d
5
5
  SHA512:
6
- metadata.gz: 0c4d44f5a42521cda63da8f2e4c51b05e7812896e778da25ec1795b69af5f55e60ed58e542c58f96715e40fc697da4063eda47bff4165f68823edd4919c08c91
7
- data.tar.gz: 8c62a8ba7a327c7728b647617f66c4c116a85391b73a72597c29475b8c2e67aa74ab4c73e3b363002027b6390b769c174f0836df6060521ace149370cf54a24c
6
+ metadata.gz: 430139d49d8b6f90fab69c9d62c919b186af0cdd3aac94956fb8b57f406d87d5243fc153c058f942e568e8fc0592b41ab41df8f9b7de6da1971aeebca0b4643a
7
+ data.tar.gz: 60ca45977391df8f28cbe8c9ad2f13627a0c8379141ac4f9b084d4b1a01829b7aeb6b0d73dceda090304f694d8affb9e2d5a5f67442acc13c858bda07478467b
@@ -463,7 +463,7 @@ VALUE ruby_create_error(VALUE self, VALUE line, VALUE col, VALUE xpath, int erno
463
463
  rb_iv_set(err_obj, "@line", line); // set line from sax parser
464
464
  rb_iv_set(err_obj, "@col", col); // set col from sax parser
465
465
 
466
- rb_iv_set(err_obj, "@xpath", xpath); // set line from sax parser
466
+ rb_iv_set(err_obj, "@xpath", xpath); // set xpath from sax parser
467
467
 
468
468
  rb_iv_set(err_obj, "@required", rb_ary_new2(0));
469
469
  rb_iv_set(err_obj, "@allowed", rb_ary_new2(0));
@@ -493,7 +493,6 @@ int ruby_verror_handler(void *data, int erno, char *format, va_list ap)
493
493
  VALUE r_last_col = rb_iv_get(self, "@last_col");
494
494
  int last_line = NUM2INT(r_last_line);
495
495
  int last_col = NUM2INT(r_last_col);
496
- int xpath = NUM2INT(r_last_col);
497
496
 
498
497
  // only one error per line/col
499
498
  // if (document->last_line != last_line || document->last_col != last_col)
data/lib/rnv/validator.rb CHANGED
@@ -8,20 +8,52 @@ module RNV
8
8
  # @!visibility private
9
9
  class XpathNode
10
10
  attr_accessor :name
11
- attr_accessor :uri, :namespaces
11
+ attr_accessor :uri, :namespaces, :rev_namespaces, :xpath_name
12
12
  attr_accessor :parent
13
+ attr_accessor :idx
13
14
  attr_accessor :children
14
15
 
15
16
  def initialize(name)
16
17
  @name = name
18
+ @idx = 0
17
19
  @children = []
20
+ @namespaces = {}
21
+ @rev_namespaces = {}
22
+ @xpath_name = nil
18
23
  end
19
24
 
20
- def add_child(name, uri = nil, namespaces = {})
25
+ def add_child(name, uri = nil, local_namespaces = {})
21
26
  child = XpathNode.new(name)
22
27
  child.uri = uri
23
- child.namespaces = namespaces
28
+
29
+ @rev_namespaces.each do |ns_uri, ns_alias|
30
+ child.rev_namespaces[ns_uri] = ns_alias
31
+ end
32
+ local_namespaces.each do |ns_alias, ns_uri|
33
+ child.rev_namespaces[ns_uri] = ns_alias
34
+ end
35
+
36
+ all_namespaces = {}
37
+ child.rev_namespaces.each do |ns_uri, ns_alias|
38
+ all_namespaces[ns_alias] ||= []
39
+ all_namespaces[ns_alias] << ns_uri
40
+ end
41
+
42
+ child.namespaces = @namespaces.dup
24
43
  child.parent = self
44
+ child.idx = self.children.count { |other_child| other_child.name == name } + 1
45
+
46
+ if child.rev_namespaces[child.uri]
47
+ if all_namespaces[child.rev_namespaces[child.uri]].length == 1
48
+ child.namespaces[child.rev_namespaces[child.uri]] = child.uri
49
+ child.xpath_name = "#{child.rev_namespaces[child.uri]}:#{child.name}[#{child.idx}]"
50
+ else
51
+ child.xpath_name = "*[local-name() = '#{child.name}'][#{child.idx}]" # we can't say which ns it is
52
+ end
53
+ else
54
+ child.xpath_name = "xmlns:#{child.name}[#{child.idx}]"
55
+ end
56
+
25
57
  @children << child
26
58
  child
27
59
  end
@@ -29,30 +61,13 @@ module RNV
29
61
  def to_xpath
30
62
  xpath = []
31
63
  current = self
32
- namespaces = {}
33
- rev_namespaces = {}
34
- ns_current = self
35
- while ns_current.name
36
- ns_current.namespaces.each do |prefix, uri|
37
- rev_namespaces[uri] ||= []
38
- rev_namespaces[uri] << prefix
39
- end
40
- ns_current = ns_current.parent
41
- end
42
-
43
- rev_namespaces.each do |uri, prefixes|
44
- if prefixes.length > 0
45
- prefixes.select! { |prefix| prefix != "xmlns" }
46
- end
47
- namespaces[prefixes.first||"xmlns"] = uri
48
- end
49
64
 
50
65
  while current.name
51
- current_name_with_ns = "#{rev_namespaces[current.uri]&.first||"xmlns"}:#{current.name}"
52
- xpath << "#{current_name_with_ns}[#{current.parent.children.select { |child| child.name == current.name }.index(current) + 1}]"
66
+ xpath << current.xpath_name
53
67
  current = current.parent
54
68
  end
55
- [xpath.reverse, namespaces]
69
+
70
+ [xpath.reverse, self.namespaces]
56
71
  end
57
72
  end
58
73
 
@@ -72,7 +87,7 @@ module RNV
72
87
  tag_attrs = attrs.map { |attr| [attr.uri ? "#{attr.uri}:#{attr.localname}" : attr.localname, attr.value] }
73
88
  tag_name = uri ? "#{uri}:#{name}" : name
74
89
  namespaces = {}
75
- ns.each do |n|
90
+ ns&.each do |n|
76
91
  namespaces[n.first || "xmlns"] = n.last
77
92
  end
78
93
  @xpath_tree = @xpath_tree.add_child(name, uri, namespaces)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_rnv
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.5.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Julien Boulnois
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-09-07 00:00:00.000000000 Z
11
+ date: 2022-10-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler