rexml-css_selector 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +7 -0
  2. data/.editorconfig +8 -0
  3. data/.rubocop.yml +48 -0
  4. data/CODE_OF_CONDUCT.md +84 -0
  5. data/LICENSE.txt +21 -0
  6. data/README.md +74 -0
  7. data/Rakefile +27 -0
  8. data/example/prism.rb +13 -0
  9. data/lib/rexml/css_selector/adapters/prism_adapter.rb +77 -0
  10. data/lib/rexml/css_selector/adapters/rexml_adapter.rb +77 -0
  11. data/lib/rexml/css_selector/ast.rb +107 -0
  12. data/lib/rexml/css_selector/base_adapter.rb +88 -0
  13. data/lib/rexml/css_selector/compiler.rb +287 -0
  14. data/lib/rexml/css_selector/parser.rb +430 -0
  15. data/lib/rexml/css_selector/pseudo_class_def.rb +19 -0
  16. data/lib/rexml/css_selector/queries/adjacent_query.rb +18 -0
  17. data/lib/rexml/css_selector/queries/attribute_matcher_query.rb +58 -0
  18. data/lib/rexml/css_selector/queries/attribute_presence_query.rb +20 -0
  19. data/lib/rexml/css_selector/queries/checked_query.rb +18 -0
  20. data/lib/rexml/css_selector/queries/child_query.rb +18 -0
  21. data/lib/rexml/css_selector/queries/class_name_query.rb +18 -0
  22. data/lib/rexml/css_selector/queries/descendant_query.rb +41 -0
  23. data/lib/rexml/css_selector/queries/disabled_query.rb +18 -0
  24. data/lib/rexml/css_selector/queries/empty_query.rb +17 -0
  25. data/lib/rexml/css_selector/queries/has_query.rb +34 -0
  26. data/lib/rexml/css_selector/queries/id_query.rb +18 -0
  27. data/lib/rexml/css_selector/queries/nested_query.rb +18 -0
  28. data/lib/rexml/css_selector/queries/not_query.rb +18 -0
  29. data/lib/rexml/css_selector/queries/nth_child_of_query.rb +40 -0
  30. data/lib/rexml/css_selector/queries/nth_child_query.rb +32 -0
  31. data/lib/rexml/css_selector/queries/nth_last_child_of_query.rb +40 -0
  32. data/lib/rexml/css_selector/queries/nth_last_child_query.rb +33 -0
  33. data/lib/rexml/css_selector/queries/nth_last_of_type_query.rb +44 -0
  34. data/lib/rexml/css_selector/queries/nth_of_type_query.rb +44 -0
  35. data/lib/rexml/css_selector/queries/one_of_query.rb +17 -0
  36. data/lib/rexml/css_selector/queries/only_child_query.rb +23 -0
  37. data/lib/rexml/css_selector/queries/only_of_type_query.rb +34 -0
  38. data/lib/rexml/css_selector/queries/root_query.rb +17 -0
  39. data/lib/rexml/css_selector/queries/scope_query.rb +17 -0
  40. data/lib/rexml/css_selector/queries/sibling_query.rb +21 -0
  41. data/lib/rexml/css_selector/queries/tag_name_type_query.rb +30 -0
  42. data/lib/rexml/css_selector/queries/true_query.rb +15 -0
  43. data/lib/rexml/css_selector/queries/universal_type_query.rb +22 -0
  44. data/lib/rexml/css_selector/query_context.rb +25 -0
  45. data/lib/rexml/css_selector/version.rb +8 -0
  46. data/lib/rexml/css_selector.rb +197 -0
  47. data/sig/rexml/css_selector.rbs +5 -0
  48. metadata +137 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c991ed27ec6b41e8379c850568907e58fc06408dec1e02eae211a11fec8180b0
4
+ data.tar.gz: 0a382373b033ca770e4a066baba811fb697444025598a63656c96dbccfac5f05
5
+ SHA512:
6
+ metadata.gz: b002f7a494d55005b28d4f596074052191fd271333f09a950945b8626b7219620707db3e99fcc149452488b5476d2e3d099b2142734c391e99c659c57581fbd0
7
+ data.tar.gz: b7cee0862576b898a5fac77d9602dd06086a7382f5f07e576c14fa6b5be05e3a6d021ba2fdf0bd6bf6bf3b935e500e96b9e1e6ee4b0e6ac11f7530b193ac1f36
data/.editorconfig ADDED
@@ -0,0 +1,8 @@
1
+ root = true
2
+
3
+ [*]
4
+ end_of_line = lf
5
+ insert_final_newline = true
6
+ charset = utf-8
7
+ indent_style = space
8
+ indent_size = 2
data/.rubocop.yml ADDED
@@ -0,0 +1,48 @@
1
+ inherit_gem:
2
+ syntax_tree: config/rubocop.yml
3
+
4
+ require:
5
+ - rubocop-minitest
6
+ - rubocop-rake
7
+
8
+ AllCops:
9
+ TargetRubyVersion: 3.1
10
+ NewCops: enable
11
+
12
+ Lint/DuplicateBranch:
13
+ Enabled: false
14
+
15
+ Lint/ShadowingOuterLocalVariable:
16
+ Enabled: false
17
+
18
+ Lint/UnreachableLoop:
19
+ Enabled: false
20
+
21
+ Metrics/AbcSize:
22
+ Enabled: false
23
+
24
+ Metrics/ClassLength:
25
+ Enabled: false
26
+
27
+ Metrics/CyclomaticComplexity:
28
+ Enabled: false
29
+
30
+ Metrics/MethodLength:
31
+ Enabled: false
32
+
33
+ Metrics/ParameterLists:
34
+ Enabled: false
35
+
36
+ Metrics/PerceivedComplexity:
37
+ Enabled: false
38
+
39
+ Naming/MethodParameterName:
40
+ Enabled: false
41
+
42
+ Metrics/ModuleLength:
43
+ Enabled: false
44
+
45
+ Style/Documentation:
46
+ Exclude:
47
+ - lib/rexml/css_selector/queries/*.rb
48
+ - test/**/*.rb
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at make.just.on@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Hiroya Fujinami (a.k.a. TSUYUSATO "MakeNowJust" Kitsune)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,74 @@
1
+ # `REXML::CSSSelector`
2
+
3
+ > A REXML extension for supporting CSS selector.
4
+
5
+ ## Installation
6
+
7
+ This library has not been published on RubyGems yet.
8
+
9
+ You can use this library via GitHub source.
10
+ Please add the following line to your `Gemfile`.
11
+
12
+ ```ruby
13
+ gem 'rexml-css_selector', :git => 'git://github.com/makenowjust/rexml-css_selector.git'
14
+ ```
15
+
16
+ ## Usage
17
+
18
+ The main API of this library is `REXML::CSSSelector.each_select`.
19
+ `REXML::CSSSelector.each_select(scope, selector)` takes two arguments: `scope` is a scope node which it starts matching from, and `selector` is a CSS selector string.
20
+ Then, it calls the given block with a matched node.
21
+
22
+ See the example.
23
+
24
+ ```ruby
25
+ require "rexml/document"
26
+ require "rexml/css_selector"
27
+
28
+ # From https://www.w3schools.com/xml/note.xml.
29
+ doc = REXML::Document.new(<<~XML)
30
+ <?xml version="1.0" encoding="UTF-8"?>
31
+ <note>
32
+ <script/>
33
+ <to>Tove</to>
34
+ <from>Jani</from>
35
+ <heading>Reminder</heading>
36
+ <body>Don't forget me this weekend!</body>
37
+ </note>
38
+ XML
39
+
40
+ # "script ~ *" selects sibling elements after a `<script>` tag.
41
+ REXML::CSSSelector.each_select(doc, "script ~ *") do |element|
42
+ p element
43
+ end
44
+
45
+ # Output:
46
+ # <to> ... </>
47
+ # <from> ... </>
48
+ # <heading> ... </>
49
+ # <body> ... </>
50
+ ```
51
+
52
+ This library also provides the following APIs:
53
+
54
+ - `REXML::CSSSelector.select(scope, selector)`: Returns the first matched element.
55
+ - `REXML::CSSSelector.select_all(scope, selector)`: Returns an array of matched elements.
56
+ - `REXML::CSSSelector.is(node, selector, scope: node.document)`: Checks whether `node` matches `selector` from `scope`.
57
+
58
+ ## Development
59
+
60
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
61
+
62
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
63
+
64
+ ## Contributing
65
+
66
+ Bug reports and pull requests are welcome on GitHub at <https://github.com/makenowjust/rexml-css_selector>. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/makenowjust/rexml-css_selector/blob/main/CODE_OF_CONDUCT.md).
67
+
68
+ ## License
69
+
70
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
71
+
72
+ ## Code of Conduct
73
+
74
+ Everyone interacting in the Rexml::Css::Selector project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/makenowjust/rexml-css_selector/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
5
+ require "rdoc/task"
6
+ require "rubocop/rake_task"
7
+ require "syntax_tree/rake_tasks"
8
+
9
+ Minitest::TestTask.create
10
+
11
+ RDoc::Task.new do |t|
12
+ t.main = "README.md"
13
+ t.rdoc_files.include("README.md", "lib/**/*.rb")
14
+ end
15
+
16
+ RuboCop::RakeTask.new { |t| t.options = %w[--fail-level W] }
17
+
18
+ [SyntaxTree::Rake::WriteTask, SyntaxTree::Rake::CheckTask].each do |task|
19
+ task.new do |t|
20
+ t.source_files =
21
+ FileList[%w[Gemfile Rakefile *.gemspec bin/**/{console,rake} lib/**/*.rb test/**/*.rb example/**/*.rb]]
22
+ t.print_width = 120
23
+ end
24
+ end
25
+
26
+ task format: %w[rubocop:autocorrect_all stree:write]
27
+ task lint: %w[rubocop stree:check]
data/example/prism.rb ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "prism"
4
+ require "rexml/css_selector"
5
+ require "rexml/css_selector/adapters/prism_adapter"
6
+
7
+ program = Prism.parse(File.read(__FILE__)).value
8
+ doc = REXML::CSSSelector::Adapters::PrismAdapter::PrismDOM.new(program)
9
+ adapter = REXML::CSSSelector::Adapters::PrismAdapter::INSTANCE
10
+
11
+ REXML::CSSSelector.each_select(doc, 'call[name="require"] > arguments > string:first-child', adapter:) do |element|
12
+ p element.node.content
13
+ end
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "prism"
4
+
5
+ module REXML
6
+ module CSSSelector
7
+ module Adapters
8
+ class PrismAdapter < BaseAdapter
9
+ class PrismDOM
10
+ def initialize(node, parent: nil, document: nil, index: 0)
11
+ @node = node
12
+ @parent = parent
13
+ @document = document || self
14
+ @index = index
15
+ end
16
+
17
+ attr_reader :node, :parent, :document
18
+
19
+ def type
20
+ @node.type.to_s.gsub(/_node$/, "")
21
+ end
22
+
23
+ def attribute(name)
24
+ @node.deconstruct_keys([])[name.intern]&.to_s
25
+ end
26
+
27
+ def children
28
+ @children ||=
29
+ begin
30
+ children = []
31
+ @node.compact_child_nodes.each_with_index do |child, index|
32
+ children << PrismDOM.new(child, parent: self, document:, index:)
33
+ end
34
+ children
35
+ end
36
+ end
37
+
38
+ def previous_sibling
39
+ return if index.zero?
40
+ parent.children[index - 1]
41
+ end
42
+ end
43
+
44
+ def element?(node)
45
+ node.is_a?(PrismDOM)
46
+ end
47
+
48
+ def get_tag_name(element)
49
+ element.type
50
+ end
51
+
52
+ def get_attribute(element, name, _namespace = nil, _attribute_name_case = :sensitive)
53
+ element.attribute(name)
54
+ end
55
+
56
+ def get_document_node(element)
57
+ element.document
58
+ end
59
+
60
+ def get_parent_node(element)
61
+ element.parent
62
+ end
63
+
64
+ def get_previous_sibling_element(element)
65
+ element.previous_sibling
66
+ end
67
+
68
+ def each_child_element(element, &)
69
+ element.children.each(&)
70
+ end
71
+
72
+ # INSTANCE is the default instance.
73
+ INSTANCE = new
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ module REXML
4
+ module CSSSelector
5
+ module Adapters
6
+ # REXMLAdapter is an adapter implementation for +REXML+.
7
+ class REXMLAdapter < BaseAdapter
8
+ def element?(node)
9
+ node.instance_of?(::REXML::Element)
10
+ end
11
+
12
+ def empty?(node)
13
+ node.children.all? do |child|
14
+ case child
15
+ when ::REXML::Element
16
+ false
17
+ when ::REXML::Text
18
+ child.to_s.match?(/\A\s*\z/)
19
+ else
20
+ true
21
+ end
22
+ end
23
+ end
24
+
25
+ def get_tag_name(element)
26
+ element.name
27
+ end
28
+
29
+ def get_namespace(element)
30
+ element.prefix
31
+ end
32
+
33
+ def get_attribute(element, name, namespace = nil, attribute_name_case = :sensitive)
34
+ namespace = element.namespace(namespace) if namespace
35
+
36
+ case attribute_name_case
37
+ in :sensitive
38
+ element.attribute(name, namespace)&.value
39
+ in :insensitive
40
+ name = name.downcase(:ascii)
41
+ target_attr = nil
42
+ element.attributes.each_attribute do |attr|
43
+ if attr.name.downcase(:ascii) == name && (!namespace || attr.namespace == namespace)
44
+ target_attr = attr
45
+ break
46
+ end
47
+ end
48
+ target_attr&.value
49
+ end
50
+ end
51
+
52
+ def get_document_node(node)
53
+ node.root_node
54
+ end
55
+
56
+ def get_parent_node(element)
57
+ element.parent
58
+ end
59
+
60
+ def get_previous_sibling_element(element)
61
+ element.previous_element
62
+ end
63
+
64
+ def each_child_element(element, &)
65
+ element.each_child { yield _1 if element?(_1) }
66
+ end
67
+
68
+ def each_recursive_element(element, &)
69
+ element.each_recursive(&)
70
+ end
71
+
72
+ # INSTANCE is the default instance.
73
+ INSTANCE = new
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,107 @@
1
+ # frozen_string_literal: true
2
+
3
+ module REXML
4
+ module CSSSelector
5
+ # Complex/compound/relative selectors:
6
+
7
+ # SelectorList is a list of selectors.
8
+ #
9
+ # This is corresponding to a comma operator (<tt>..., ...</tt>) in CSS selector.
10
+ SelectorList = Data.define(:selectors)
11
+
12
+ # ComplexSelector is a pair of selectors with a combinator.
13
+ #
14
+ # This is corresponding to descendant/adjacent/sibling operators in CSS selector.
15
+ ComplexSelector = Data.define(:left, :combinator, :right)
16
+
17
+ # CompoundSelector is a compound selector.
18
+ CompoundSelector = Data.define(:type, :subclasses, :pseudo_elements)
19
+
20
+ # RelativeSelector is a pair of a combinator and a selector.
21
+ RelativeSelector = Data.define(:combinator, :right)
22
+
23
+ # Type selectors:
24
+
25
+ # TagNameType is a type selector of a tag name.
26
+ TagNameType = Data.define(:namespace, :tag_name)
27
+
28
+ # UniversalType is the universal type selector (<tt>*</tt>).
29
+ UniversalType = Data.define(:namespace)
30
+
31
+ # Subclass selectors:
32
+
33
+ # Id is an ID selector (e.g. <tt>#name</tt>).
34
+ Id = Data.define(:name)
35
+
36
+ # ClassName is a class name selector (e.g. <tt>.name</tt>).
37
+ ClassName = Data.define(:name)
38
+
39
+ # Attribute is an attribute selector (e.g. <tt>[name]</tt>, <tt>[name=value]</tt>).
40
+ #
41
+ # If +matcher+, +value+, and +modifier+ is +nil+, it means an attribute presence selector (e.g. <tt>[name]</tt>).
42
+ #
43
+ # +matcher+ takes one of the following values:
44
+ #
45
+ # - <tt>:"="</tt>
46
+ # - <tt>:"~="</tt>
47
+ # - <tt>:"|="</tt>
48
+ # - <tt>:"^="</tt>
49
+ # - <tt>:"$="</tt>
50
+ # - <tt>:"*="</tt>
51
+ #
52
+ # +modifier+ takes one of the following values:
53
+ #
54
+ # - <tt>:i</tt>
55
+ # - <tt>:s</tt>
56
+ # - <tt>nil</tt>
57
+ Attribute = Data.define(:namespace, :name, :matcher, :value, :modifier)
58
+
59
+ # PseudoClass ia a pseudo class selector (e.g. <tt>:first-child</tt>).
60
+ PseudoClass = Data.define(:name, :argument)
61
+
62
+ # Namespace is a namespace in CSS selector.
63
+ Namespace = Data.define(:name)
64
+
65
+ # UniversalNamespace is the universal namespace in CSS selector.
66
+ UniversalNamespace = Data.define
67
+
68
+ # Pseudo element:
69
+
70
+ # PseudoElement is a pseudo element.
71
+ PseudoElement = Data.define(:name, :argument, :pseudo_classes)
72
+
73
+ # Arguments:
74
+
75
+ # RelativeSelectorList is a list of relative selectors.
76
+ RelativeSelectorList = Data.define(:selectors)
77
+
78
+ # Odd is +odd+ in a <tt>:nth-child</tt> argument.
79
+ Odd = Data.define
80
+
81
+ # Even is +even+ in a <tt>:nth-child</tt> argument.
82
+ Even = Data.define
83
+
84
+ # Nth is <tt>An+B</tt> in a <tt>:nth-child</tt> argument.
85
+ Nth = Data.define(:a, :b)
86
+
87
+ # NthOfSelectorList is <tt>An+B of S</tt> in a <tt>:nth-child</tt> argument.
88
+ NthOfSelectorList = Data.define(:nth, :selector_list)
89
+
90
+ # ValueList is a list of values.
91
+ ValueList = Data.define(:values)
92
+
93
+ # Value:
94
+
95
+ # Substitution is a placeholder value which is replaced by +substitutions[name]+ on run-time.
96
+ Substitution = Data.define(:name)
97
+
98
+ # Ident is a ident value.
99
+ Ident = Data.define(:value)
100
+
101
+ # String is a string value.
102
+ String = Data.define(:value)
103
+
104
+ # Bare is a bare value.
105
+ Bare = Data.define(:value)
106
+ end
107
+ end
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ module REXML
4
+ module CSSSelector
5
+ # BaseAdapter is a base class of adapters.
6
+ #
7
+ # An adapter is an abstraction of tree traversal operations for CSS selectors.
8
+ # We need to implement the following methods minimally:
9
+ #
10
+ # - <tt>element?(node)</tt>
11
+ # - <tt>get_tag_name(element)</tt>
12
+ # - <tt>get_attribute(element, name, namespace = nil, attribute_name_case = :sensitive)</tt>
13
+ # - <tt>get_document_node(element)</tt>
14
+ # - <tt>get_parent_node(element)</tt>
15
+ # - <tt>get_previous_sibling_node(element)</tt>
16
+ # - <tt>each_child_element(element, &)</tt>
17
+ class BaseAdapter
18
+ # Checks whether +element+ is the root element.
19
+ #
20
+ # This method is used for <tt>:root</tt>.
21
+ def root?(element)
22
+ get_parent_node(element) == get_document_node(element)
23
+ end
24
+
25
+ # Checks whether +element+ is empty.
26
+ #
27
+ # This method is used for <tt>:empty</tt>.
28
+ def empty?(element)
29
+ each_child_element(element) { return false }
30
+ true
31
+ end
32
+
33
+ # Checks whether +element+ is checked.
34
+ #
35
+ # This method is used for <tt>:checked</tt>.
36
+ def checked?(element)
37
+ !!get_attribute(element, "checked")
38
+ end
39
+
40
+ # Checks whether +element+ is disabled.
41
+ #
42
+ # This method is used for <tt>:disabled</tt>.
43
+ def disabled?(element)
44
+ !!get_attribute(element, "disabled")
45
+ end
46
+
47
+ # Returns a namespace of +element+.
48
+ def get_namespace(_element)
49
+ nil
50
+ end
51
+
52
+ # Returns an array of children elements of +element+.
53
+ def get_children_elements(element)
54
+ elements = []
55
+ each_child_element(element) { elements << _1 }
56
+ elements
57
+ end
58
+
59
+ # Returns the index of +element+ in the children of +parent+.
60
+ def get_element_index(parent, element)
61
+ i = 0
62
+ each_child_element(parent) do |child|
63
+ return i if element == child
64
+ i += 1
65
+ end
66
+ nil
67
+ end
68
+
69
+ # Returns class names of +element+.
70
+ def get_class_names(element)
71
+ get_attribute(element, "class")&.split(/\s+/) || []
72
+ end
73
+
74
+ # Returns the ID name of +element+.
75
+ def get_id(element)
76
+ get_attribute(element, "id")
77
+ end
78
+
79
+ # Enumerates the elements in +element+
80
+ def each_recursive_element(element, &)
81
+ each_child_element(element) do |child|
82
+ yield child
83
+ each_recursive_element(child, &)
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end