manticore-smash 3.1.0

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.
@@ -0,0 +1,103 @@
1
+ # frozen_string_literal: false
2
+
3
+ # Copyright (C) 2024 Manticore Authors
4
+ #
5
+ # This program is free software: you can redistribute it and/or modify
6
+ # it under the terms of the GNU Affero General Public License as published
7
+ # by the Free Software Foundation, either version 3 of the License, or
8
+ # (at your option) any later version.
9
+ #
10
+ # This program is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU Affero General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU Affero General Public License
16
+ # along with this program. If not, see <https://www.gnu.org/licenses/>.
17
+
18
+ module XmlUtils
19
+ class XPath
20
+ def self.match(element, path)
21
+ return [element] if path.nil? || path.strip.empty? || path == '.'
22
+ return [element] if path == '/'
23
+
24
+ nodes = [element]
25
+ parts = path.split('/').reject(&:empty?)
26
+
27
+ parts.each do |part|
28
+ new_nodes = []
29
+ nodes.each do |node|
30
+ new_nodes.concat(match_step(node, part))
31
+ end
32
+ nodes = new_nodes
33
+ end
34
+
35
+ nodes
36
+ end
37
+
38
+ def self.first(element, path)
39
+ match(element, path).first
40
+ end
41
+
42
+ def self.each(element, path, &block)
43
+ match(element, path).each(&block)
44
+ end
45
+
46
+ def self.match_step(node, step)
47
+ return [] unless node.is_a?(Element) || node.is_a?(Document)
48
+
49
+ if step == '*'
50
+ return node.children.select { |c| c.is_a?(Element) }
51
+ end
52
+
53
+ if step == '..'
54
+ return node.parent ? [node.parent] : []
55
+ end
56
+
57
+ if step.start_with?('@')
58
+ attr_name = step[1..]
59
+ if node.is_a?(Element)
60
+ attr = node.attribute(attr_name)
61
+ return attr ? [attr] : []
62
+ end
63
+ return []
64
+ end
65
+
66
+ if step.include?('[')
67
+ name = step[/^[^\[]+/]
68
+ predicate = step[/\[(.*?)\]/, 1]
69
+ else
70
+ name = step
71
+ predicate = nil
72
+ end
73
+
74
+ if node.is_a?(Document)
75
+ candidates = node.children.select { |c| c.is_a?(Element) && c.name == name }
76
+ else
77
+ candidates = node.children.select { |c| c.is_a?(Element) && c.name == name }
78
+ end
79
+
80
+ return candidates unless predicate
81
+
82
+ apply_predicate(candidates, predicate)
83
+ end
84
+
85
+ def self.apply_predicate(candidates, predicate)
86
+ if predicate =~ /^\d+$/
87
+ idx = predicate.to_i - 1
88
+ idx >= 0 && candidates[idx] ? [candidates[idx]] : []
89
+ elsif predicate =~ /^@(\S+)$/
90
+ attr_name = $1
91
+ candidates.select { |c| c.has_attribute?(attr_name) }
92
+ elsif predicate =~ /^@(\S+)\s*=\s*['"]([^'"]*)['"]$/
93
+ attr_name = $1
94
+ attr_value = $2
95
+ candidates.select { |c| c[attr_name] == attr_value }
96
+ elsif predicate =~ /^contains\(\s*(\S+)\s*,\s*['"]([^'"]*)['"]\s*\)$/
97
+ candidates # Simplified: not fully implemented
98
+ else
99
+ candidates
100
+ end
101
+ end
102
+ end
103
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: manticore-smash
3
+ version: !ruby/object:Gem::Version
4
+ version: 3.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Frampt
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: A multi-format library for parsing, formatting, interacting and more.
13
+ executables: []
14
+ extensions: []
15
+ extra_rdoc_files: []
16
+ files:
17
+ - LICENSE
18
+ - README.md
19
+ - lib/manticore.rb
20
+ - lib/mdutils/rediscount.rb
21
+ - lib/xmlutils/formatters.rb
22
+ - lib/xmlutils/node.rb
23
+ - lib/xmlutils/tokenizer.rb
24
+ - lib/xmlutils/tree_parser.rb
25
+ - lib/xmlutils/xml_doc.rb
26
+ - lib/xmlutils/xpath.rb
27
+ homepage: https://github.com/ChenMeng1365/manticore
28
+ licenses:
29
+ - AGPL-3.0-or-later
30
+ metadata: {}
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '3.0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubygems_version: 4.0.10
46
+ specification_version: 4
47
+ summary: A multi-format toolkit
48
+ test_files: []