node_query 1.6.1 → 1.7.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/Gemfile.lock +2 -2
- data/lib/node_query/compiler/array_value.rb +2 -2
- data/lib/node_query/compiler/attribute.rb +3 -3
- data/lib/node_query/compiler/attribute_list.rb +3 -2
- data/lib/node_query/compiler/basic_selector.rb +3 -2
- data/lib/node_query/compiler/comparable.rb +34 -31
- data/lib/node_query/compiler/regexp.rb +2 -1
- data/lib/node_query/compiler/selector.rb +12 -11
- data/lib/node_query/compiler/string.rb +5 -5
- data/lib/node_query/helper.rb +1 -1
- data/lib/node_query/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 061470a9033fb08485c03c44c3c41ca6ff0daf6a7db2fbe474d82384f9dd731b
|
4
|
+
data.tar.gz: 915e29f0a09147d3b7d06b0e81770b197cd743f4d44cd3da7b7f5ddee8a50a8c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b2184539cae7f69fdc3c53d993394fe4a5cb78bf2fb2e92a7183d61c6de4cab4e9117891859ddbd3c09d54f7905de2ea9c3059279e56d7b28c4f3a286f7247a6
|
7
|
+
data.tar.gz: e040f8691f7d51c07b6f9a7f7c03de42488a4f02815c683fd1a763eff6f985e4899a91c620d0bc37a4c9942e1c7e294c1e7220e9499a4ee573734cddeeb39acc
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
node_query (1.
|
4
|
+
node_query (1.7.0)
|
5
5
|
activesupport (< 7.0.0)
|
6
6
|
|
7
7
|
GEM
|
@@ -78,7 +78,7 @@ GEM
|
|
78
78
|
thor (1.2.1)
|
79
79
|
tzinfo (2.0.5)
|
80
80
|
concurrent-ruby (~> 1.0)
|
81
|
-
zeitwerk (2.6.
|
81
|
+
zeitwerk (2.6.1)
|
82
82
|
|
83
83
|
PLATFORMS
|
84
84
|
x86_64-darwin-21
|
@@ -15,10 +15,10 @@ module NodeQuery::Compiler
|
|
15
15
|
|
16
16
|
# Get the expected value.
|
17
17
|
# @return [Array]
|
18
|
-
def expected_value
|
18
|
+
def expected_value(base_node)
|
19
19
|
expected = []
|
20
20
|
expected.push(@value) if @value
|
21
|
-
expected += @rest.expected_value if @rest
|
21
|
+
expected += @rest.expected_value(base_node) if @rest
|
22
22
|
expected
|
23
23
|
end
|
24
24
|
|
@@ -15,10 +15,10 @@ module NodeQuery::Compiler
|
|
15
15
|
|
16
16
|
# Check if the node matches the attribute.
|
17
17
|
# @param node [Node] the node
|
18
|
+
# @param base_node [Node] the bae node for evaluated value
|
18
19
|
# @return [Boolean]
|
19
|
-
def match?(node)
|
20
|
-
|
21
|
-
node && @value.match?(NodeQuery::Helper.get_target_node(node, @key), @operator)
|
20
|
+
def match?(node, base_node)
|
21
|
+
node && @value.match?(NodeQuery::Helper.get_target_node(node, @key), base_node, @operator)
|
22
22
|
end
|
23
23
|
|
24
24
|
def to_s
|
@@ -13,9 +13,10 @@ module NodeQuery::Compiler
|
|
13
13
|
|
14
14
|
# Check if the node matches the attribute list.
|
15
15
|
# @param node [Node] the node
|
16
|
+
# @param base_node [Node] the base node for evaluated value
|
16
17
|
# @return [Boolean]
|
17
|
-
def match?(node)
|
18
|
-
@attribute.match?(node) && (!@rest || @rest.match?(node))
|
18
|
+
def match?(node, base_node)
|
19
|
+
@attribute.match?(node, base_node) && (!@rest || @rest.match?(node, base_node))
|
19
20
|
end
|
20
21
|
|
21
22
|
def to_s
|
@@ -13,11 +13,12 @@ module NodeQuery::Compiler
|
|
13
13
|
|
14
14
|
# Check if node matches the selector.
|
15
15
|
# @param node [Node] the node
|
16
|
+
# @param base_node [Node] the base node for evaluated value
|
16
17
|
# @return [Boolean]
|
17
|
-
def match?(node, _operator = '==')
|
18
|
+
def match?(node, base_node, _operator = '==')
|
18
19
|
return false unless node
|
19
20
|
|
20
|
-
@node_type.to_sym == NodeQuery.adapter.get_node_type(node) && (!@attribute_list || @attribute_list.match?(node))
|
21
|
+
@node_type.to_sym == NodeQuery.adapter.get_node_type(node) && (!@attribute_list || @attribute_list.match?(node, base_node))
|
21
22
|
end
|
22
23
|
|
23
24
|
def to_s
|
@@ -12,69 +12,71 @@ module NodeQuery::Compiler
|
|
12
12
|
# Check if the actual value matches the expected value.
|
13
13
|
#
|
14
14
|
# @param node [Node] node to calculate actual value
|
15
|
+
# @param base_node [Node] the base node for evaluated value
|
15
16
|
# @param operator [String] operator to compare with expected value, operator can be <code>'=='</code>, <code>'!='</code>, <code>'>'</code>, <code>'>='</code>, <code>'<'</code>, <code>'<='</code>, <code>'includes'</code>, <code>'in'</code>, <code>'not_in'</code>, <code>'=~'</code>, <code>'!~'</code>
|
16
17
|
# @return [Boolean] true if actual value matches the expected value
|
17
18
|
# @raise [NodeQuery::Compiler::InvalidOperatorError] if operator is invalid
|
18
|
-
def match?(node, operator)
|
19
|
+
def match?(node, base_node, operator)
|
19
20
|
raise InvalidOperatorError, "invalid operator #{operator}" unless valid_operator?(operator)
|
20
21
|
|
22
|
+
actual = actual_value(node)
|
23
|
+
expected = expected_value(base_node)
|
21
24
|
case operator
|
22
25
|
when '!='
|
23
|
-
if
|
24
|
-
actual
|
25
|
-
|
26
|
-
actual.zip(expected_value).any? { |actual_node, expected_node| expected_node.match?(actual_node, '!=') }
|
26
|
+
if expected.is_a?(::Array)
|
27
|
+
!actual.is_a?(::Array) || actual.size != expected.size ||
|
28
|
+
actual.zip(expected).any? { |actual_child, expected_child| expected_child.match?(actual_child, base_node, '!=') }
|
27
29
|
else
|
28
|
-
!is_equal?(
|
30
|
+
!is_equal?(actual, expected)
|
29
31
|
end
|
30
32
|
when '=~'
|
31
|
-
|
33
|
+
actual =~ expected
|
32
34
|
when '!~'
|
33
|
-
|
35
|
+
actual !~ expected
|
34
36
|
when '^='
|
35
|
-
|
37
|
+
actual.start_with?(expected)
|
36
38
|
when '$='
|
37
|
-
|
39
|
+
actual.end_with?(expected)
|
38
40
|
when '*='
|
39
|
-
|
41
|
+
actual.include?(expected)
|
40
42
|
when '>'
|
41
|
-
|
43
|
+
actual > expected
|
42
44
|
when '>='
|
43
|
-
|
45
|
+
actual >= expected
|
44
46
|
when '<'
|
45
|
-
|
47
|
+
actual < expected
|
46
48
|
when '<='
|
47
|
-
|
49
|
+
actual <= expected
|
48
50
|
when 'in'
|
49
51
|
if node.is_a?(Array)
|
50
|
-
node.all? { |child|
|
52
|
+
node.all? { |child| expected.any? { |expected_child| expected_child.match?(child, base_node, '==') } }
|
51
53
|
else
|
52
|
-
|
54
|
+
expected.any? { |expected_child| expected_child.match?(node, base_node, '==') }
|
53
55
|
end
|
54
56
|
when 'not_in'
|
55
57
|
if node.is_a?(Array)
|
56
|
-
node.all? { |child|
|
58
|
+
node.all? { |child| expected.all? { |expected_child| expected_child.match?(child, base_node, '!=') } }
|
57
59
|
else
|
58
|
-
|
60
|
+
expected.all? { |expected_child| expected_child.match?(node, base_node, '!=') }
|
59
61
|
end
|
60
62
|
when 'includes'
|
61
|
-
|
63
|
+
actual.any? { |actual_child| actual_child == expected }
|
62
64
|
else
|
63
|
-
if
|
64
|
-
actual
|
65
|
-
|
66
|
-
actual.zip(expected_value).all? { |actual_node, expected_node| expected_node.match?(actual_node, '==') }
|
65
|
+
if expected.is_a?(::Array)
|
66
|
+
actual.is_a?(::Array) && actual.size == expected.size &&
|
67
|
+
actual.zip(expected).all? { |actual_child, expected_child| expected_child.match?(actual_child, base_node, '==') }
|
67
68
|
else
|
68
|
-
is_equal?(
|
69
|
+
is_equal?(actual, expected)
|
69
70
|
end
|
70
71
|
end
|
71
72
|
end
|
72
73
|
|
73
|
-
# Check if the actual value equals the
|
74
|
-
# @param
|
75
|
-
# @
|
76
|
-
|
77
|
-
|
74
|
+
# Check if the actual value equals the expected value.
|
75
|
+
# @param acutal
|
76
|
+
# @param expected
|
77
|
+
# @return [Boolean] true if the actual value equals the expected value.
|
78
|
+
def is_equal?(actual, expected)
|
79
|
+
actual == expected
|
78
80
|
end
|
79
81
|
|
80
82
|
# Get the actual value from ast node.
|
@@ -104,8 +106,9 @@ module NodeQuery::Compiler
|
|
104
106
|
end
|
105
107
|
|
106
108
|
# Get the expected value
|
109
|
+
# @param base_node [Node] the base node for evaluated value
|
107
110
|
# @return expected value, could be integer, float, string, boolean, nil, range, and etc.
|
108
|
-
def expected_value
|
111
|
+
def expected_value(base_node)
|
109
112
|
@value
|
110
113
|
end
|
111
114
|
|
@@ -13,9 +13,10 @@ module NodeQuery::Compiler
|
|
13
13
|
|
14
14
|
# Check if the regexp value matches the node value.
|
15
15
|
# @param node [Node] the node
|
16
|
+
# @param _base_node [Node] the base node for evaluated value
|
16
17
|
# @param operator [String] the operator
|
17
18
|
# @return [Boolean] true if the regexp value matches the node value, otherwise, false.
|
18
|
-
def match?(node, operator = '=~')
|
19
|
+
def match?(node, _base_node, operator = '=~')
|
19
20
|
match =
|
20
21
|
if NodeQuery.adapter.is_node?(node)
|
21
22
|
@value.match(NodeQuery.adapter.get_source(node))
|
@@ -22,8 +22,9 @@ module NodeQuery::Compiler
|
|
22
22
|
|
23
23
|
# Check if node matches the selector.
|
24
24
|
# @param node [Parser::AST::Node] the node
|
25
|
-
|
26
|
-
|
25
|
+
# @param base_node [Parser::AST::Node] the base node for evaluated node
|
26
|
+
def match?(node, base_node)
|
27
|
+
NodeQuery.adapter.is_node?(node) && (!@basic_selector || @basic_selector.match?(node, base_node)) && match_pseudo_class?(node)
|
27
28
|
end
|
28
29
|
|
29
30
|
# Query nodes by the selector.
|
@@ -49,25 +50,25 @@ module NodeQuery::Compiler
|
|
49
50
|
return find_nodes_by_goto_scope(node) if @goto_scope
|
50
51
|
|
51
52
|
if options[:including_self] && !options[:recursive]
|
52
|
-
return match?(node) ? [node] : []
|
53
|
+
return match?(node, node) ? [node] : []
|
53
54
|
end
|
54
55
|
|
55
56
|
nodes = []
|
56
|
-
if options[:including_self] && match?(node)
|
57
|
+
if options[:including_self] && match?(node, node)
|
57
58
|
nodes << node
|
58
59
|
return nodes if options[:stop_at_first_match]
|
59
60
|
end
|
60
61
|
if @basic_selector
|
61
62
|
if options[:recursive]
|
62
63
|
NodeQuery::Helper.handle_recursive_child(node) do |child_node|
|
63
|
-
if match?(child_node)
|
64
|
+
if match?(child_node, child_node)
|
64
65
|
nodes << child_node
|
65
66
|
break if options[:stop_at_first_match]
|
66
67
|
end
|
67
68
|
end
|
68
69
|
else
|
69
70
|
NodeQuery.adapter.get_children(node).each do |child_node|
|
70
|
-
if match?(child_node)
|
71
|
+
if match?(child_node, child_node)
|
71
72
|
nodes << child_node
|
72
73
|
break if options[:stop_at_first_match]
|
73
74
|
end
|
@@ -106,25 +107,25 @@ module NodeQuery::Compiler
|
|
106
107
|
when '>'
|
107
108
|
if node.is_a?(::Array)
|
108
109
|
node.each do |child_node|
|
109
|
-
nodes << child_node if @rest.match?(child_node)
|
110
|
+
nodes << child_node if @rest.match?(child_node, child_node)
|
110
111
|
end
|
111
112
|
else
|
112
113
|
node.children.each do |child_node|
|
113
114
|
if NodeQuery.adapter.is_node?(child_node) && :begin == NodeQuery.adapter.get_node_type(child_node)
|
114
115
|
child_node.children.each do |child_child_node|
|
115
|
-
nodes << child_child_node if @rest.match?(child_child_node)
|
116
|
+
nodes << child_child_node if @rest.match?(child_child_node, child_child_node)
|
116
117
|
end
|
117
|
-
elsif @rest.match?(child_node)
|
118
|
+
elsif @rest.match?(child_node, child_node)
|
118
119
|
nodes << child_node
|
119
120
|
end
|
120
121
|
end
|
121
122
|
end
|
122
123
|
when '+'
|
123
124
|
next_sibling = node.siblings.first
|
124
|
-
nodes << next_sibling if @rest.match?(next_sibling)
|
125
|
+
nodes << next_sibling if @rest.match?(next_sibling, next_sibling)
|
125
126
|
when '~'
|
126
127
|
node.siblings.each do |sibling_node|
|
127
|
-
nodes << sibling_node if @rest.match?(sibling_node)
|
128
|
+
nodes << sibling_node if @rest.match?(sibling_node, sibling_node)
|
128
129
|
end
|
129
130
|
end
|
130
131
|
nodes
|
@@ -5,8 +5,6 @@ module NodeQuery::Compiler
|
|
5
5
|
class String
|
6
6
|
include Comparable
|
7
7
|
|
8
|
-
attr_accessor :base_node
|
9
|
-
|
10
8
|
# Initialize a String.
|
11
9
|
# @param value [String] the string value
|
12
10
|
def initialize(value:)
|
@@ -18,16 +16,18 @@ module NodeQuery::Compiler
|
|
18
16
|
# if the source code of the node is @id = id,
|
19
17
|
# and the @value is "@{{right_vaue}}",
|
20
18
|
# then it returns "@id".
|
19
|
+
# @param base_node [Node] the base node for evaluated value
|
21
20
|
# @return [String] the expected string, if it contains evaluated value, evaluate the node value.
|
22
|
-
def expected_value
|
21
|
+
def expected_value(base_node)
|
23
22
|
NodeQuery::Helper.evaluate_node_value(base_node, @value)
|
24
23
|
end
|
25
24
|
|
26
25
|
# Check if the actual value equals the node value.
|
27
26
|
# @param node [Node] the node
|
27
|
+
# @param base_node [Node] the base node for evaluated value
|
28
28
|
# @return [Boolean] true if the actual value equals the node value.
|
29
|
-
def is_equal?(
|
30
|
-
NodeQuery::Helper.to_string(
|
29
|
+
def is_equal?(actual, expected)
|
30
|
+
NodeQuery::Helper.to_string(actual) == expected
|
31
31
|
end
|
32
32
|
|
33
33
|
# Get valid operators.
|
data/lib/node_query/helper.rb
CHANGED
@@ -48,7 +48,7 @@ class NodeQuery::Helper
|
|
48
48
|
# @param str [String] string to be evaluated
|
49
49
|
# @return [String] evaluated string
|
50
50
|
def evaluate_node_value(node, str)
|
51
|
-
str.scan(/{{(
|
51
|
+
str.scan(/{{(.+?)}}/).each do |match_data|
|
52
52
|
target_node = NodeQuery::Helper.get_target_node(node, match_data.first)
|
53
53
|
str = str.sub("{{#{match_data.first}}}", to_string(target_node))
|
54
54
|
end
|
data/lib/node_query/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: node_query
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Richard Huang
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-10-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|