node_query 1.3.0 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/Gemfile.lock +1 -1
- data/README.md +1 -1
- data/lib/node_query/compiler/expression.rb +7 -4
- data/lib/node_query/compiler/expression_list.rb +7 -4
- data/lib/node_query/compiler/selector.rb +22 -5
- data/lib/node_query/helper.rb +1 -1
- data/lib/node_query/node_rules.rb +21 -6
- data/lib/node_query/version.rb +1 -1
- data/lib/node_query.rb +7 -4
- data/sig/node_query.rbs +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: 95dcf07d501c704e59bb792454be0bae1ddfafb915c77b6a062c6331d654564b
|
4
|
+
data.tar.gz: 7b835b8479ff5239ea77dcb5ed09f4868b1a09cbdf5edc2f4ab9413d352030be
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e2dbea64418363ad059ea2b6e9d93b3c2d010bdba59317327904b6a48411517a511ab6737fe40bdf4847b2833ec4d81eb5d474be7db7d831a6119c89fe904fea
|
7
|
+
data.tar.gz: c2cf0bfec6edfa28b644a38957dce1276cc287507c3c3034a39e0ce018f25e420faded6ad0c7c3c3838757df5107082c30d05fcf9f4323c5d5d3f32bc68337f1
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -62,7 +62,7 @@ It provides two apis: `query_nodes` and `match_node?`
|
|
62
62
|
|
63
63
|
```ruby
|
64
64
|
node_query = NodeQuery.new(nqlOrRules: String | Hash) # Initialize NodeQuery
|
65
|
-
node_query.query_nodes(node: Node,
|
65
|
+
node_query.query_nodes(node: Node, options = { including_self: true, stop_on_match: false, recursive: true }): Node[] # Get the matching nodes.
|
66
66
|
node_query.match_node?(node: Node): boolean # Check if the node matches nql or rules.
|
67
67
|
```
|
68
68
|
|
@@ -13,14 +13,17 @@ module NodeQuery::Compiler
|
|
13
13
|
|
14
14
|
# Query nodes by the selector and the rest expression.
|
15
15
|
# @param node [Node] node to match
|
16
|
-
# @
|
16
|
+
# @param options [Hash] if query the current node
|
17
|
+
# @option options [boolean] :including_self if query the current node, default is ture
|
18
|
+
# @option options [boolean] :stop_on_match if stop on first match, default is false
|
19
|
+
# @option options [boolean] :recursive if stop on first match, default is true
|
17
20
|
# @return [Array<Node>] matching nodes.
|
18
|
-
def query_nodes(node,
|
19
|
-
matching_nodes = @selector.query_nodes(node,
|
21
|
+
def query_nodes(node, options = {})
|
22
|
+
matching_nodes = @selector.query_nodes(node, options)
|
20
23
|
return matching_nodes if @rest.nil?
|
21
24
|
|
22
25
|
matching_nodes.flat_map do |matching_node|
|
23
|
-
@rest.query_nodes(matching_node,
|
26
|
+
@rest.query_nodes(matching_node, options)
|
24
27
|
end
|
25
28
|
end
|
26
29
|
|
@@ -13,13 +13,16 @@ module NodeQuery::Compiler
|
|
13
13
|
|
14
14
|
# Query nodes by the current and the rest expression.
|
15
15
|
# @param node [Node] node to match
|
16
|
-
# @
|
16
|
+
# @param options [Hash] if query the current node
|
17
|
+
# @option options [boolean] :including_self if query the current node, default is ture
|
18
|
+
# @option options [boolean] :stop_on_match if stop on first match, default is false
|
19
|
+
# @option options [boolean] :recursive if stop on first match, default is true
|
17
20
|
# @return [Array<Node>] matching nodes.
|
18
|
-
def query_nodes(node,
|
19
|
-
matching_nodes = @expression.query_nodes(node,
|
21
|
+
def query_nodes(node, options = {})
|
22
|
+
matching_nodes = @expression.query_nodes(node, options)
|
20
23
|
return matching_nodes if @rest.nil?
|
21
24
|
|
22
|
-
matching_nodes + @rest.query_nodes(node,
|
25
|
+
matching_nodes + @rest.query_nodes(node, options)
|
23
26
|
end
|
24
27
|
|
25
28
|
# Check if the node matches the expression list.
|
@@ -33,9 +33,13 @@ module NodeQuery::Compiler
|
|
33
33
|
# * If relationship is next sibling, it try to match next sibling node.
|
34
34
|
# * If relationship is subsequent sibling, it will match in all sibling nodes.
|
35
35
|
# @param node [Node] node to match
|
36
|
-
# @
|
36
|
+
# @param options [Hash] if query the current node
|
37
|
+
# @option options [boolean] :including_self if query the current node, default is ture
|
38
|
+
# @option options [boolean] :stop_on_match if stop on first match, default is false
|
39
|
+
# @option options [boolean] :recursive if stop on first match, default is true
|
37
40
|
# @return [Array<Node>] matching nodes.
|
38
|
-
def query_nodes(node,
|
41
|
+
def query_nodes(node, options = {})
|
42
|
+
options = { including_self: true, stop_on_match: false, recursive: true }.merge(options)
|
39
43
|
return find_nodes_by_relationship(node) if @relationship
|
40
44
|
|
41
45
|
if node.is_a?(::Array)
|
@@ -45,12 +49,25 @@ module NodeQuery::Compiler
|
|
45
49
|
return find_nodes_by_goto_scope(node) if @goto_scope
|
46
50
|
|
47
51
|
nodes = []
|
48
|
-
if including_self && match?(node)
|
52
|
+
if options[:including_self] && match?(node)
|
49
53
|
nodes << node
|
54
|
+
return matching_nodes if options[:stop_on_match]
|
50
55
|
end
|
51
56
|
if @basic_selector
|
52
|
-
|
53
|
-
|
57
|
+
if options[:recursive]
|
58
|
+
NodeQuery::Helper.handle_recursive_child(node) do |child_node|
|
59
|
+
if match?(child_node)
|
60
|
+
nodes << child_node
|
61
|
+
break if options[:stop_on_match]
|
62
|
+
end
|
63
|
+
end
|
64
|
+
else
|
65
|
+
NodeQuery.adapter.get_children(node).each do |child_node|
|
66
|
+
if match?(child_node)
|
67
|
+
nodes << child_node
|
68
|
+
break if options[:stop_on_match]
|
69
|
+
end
|
70
|
+
end
|
54
71
|
end
|
55
72
|
end
|
56
73
|
nodes
|
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}}}", NodeQuery.adapter.get_source(target_node))
|
54
54
|
end
|
@@ -11,16 +11,31 @@ class NodeQuery::NodeRules
|
|
11
11
|
|
12
12
|
# Query nodes by the rules.
|
13
13
|
# @param node [Node] node to query
|
14
|
-
# @
|
14
|
+
# @param options [Hash] if query the current node
|
15
|
+
# @option options [boolean] :including_self if query the current node, default is ture
|
16
|
+
# @option options [boolean] :stop_on_match if stop on first match, default is false
|
17
|
+
# @option options [boolean] :recursive if stop on first match, default is true
|
15
18
|
# @return [Array<Node>] matching nodes.
|
16
|
-
def query_nodes(node,
|
19
|
+
def query_nodes(node, options = {})
|
20
|
+
options = { including_self: true, stop_on_match: false, recursive: true }.merge(options)
|
17
21
|
matching_nodes = []
|
18
|
-
if
|
22
|
+
if options[:including_self] && match_node?(node)
|
19
23
|
matching_nodes.push(node)
|
24
|
+
return matching_nodes if options[:stop_on_match]
|
20
25
|
end
|
21
|
-
|
22
|
-
|
23
|
-
|
26
|
+
if options[:recursive]
|
27
|
+
NodeQuery::Helper.handle_recursive_child(node) do |child_node|
|
28
|
+
if match_node?(child_node)
|
29
|
+
matching_nodes.push(child_node)
|
30
|
+
break if options[:stop_on_match]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
else
|
34
|
+
NodeQuery.adapter.get_children(node).each do |child_node|
|
35
|
+
if match_node?(child_node)
|
36
|
+
matching_nodes.push(child_node)
|
37
|
+
break if options[:stop_on_match]
|
38
|
+
end
|
24
39
|
end
|
25
40
|
end
|
26
41
|
matching_nodes
|
data/lib/node_query/version.rb
CHANGED
data/lib/node_query.rb
CHANGED
@@ -38,13 +38,16 @@ class NodeQuery
|
|
38
38
|
|
39
39
|
# Query matching nodes.
|
40
40
|
# @param node [Node] ast node
|
41
|
-
# @param
|
41
|
+
# @param options [Hash] if query the current node
|
42
|
+
# @option options [boolean] :including_self if query the current node, default is ture
|
43
|
+
# @option options [boolean] :stop_on_match if stop on first match, default is false
|
44
|
+
# @option options [boolean] :recursive if stop on first match, default is true
|
42
45
|
# @return [Array<Node>] matching child nodes
|
43
|
-
def query_nodes(node,
|
46
|
+
def query_nodes(node, options = {})
|
44
47
|
if @expression
|
45
|
-
@expression.query_nodes(node,
|
48
|
+
@expression.query_nodes(node, options)
|
46
49
|
elsif @rules
|
47
|
-
@rules.query_nodes(node,
|
50
|
+
@rules.query_nodes(node, options)
|
48
51
|
else
|
49
52
|
[]
|
50
53
|
end
|
data/sig/node_query.rbs
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.4.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-09-
|
11
|
+
date: 2022-09-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|