minicss 0.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.
Files changed (46) hide show
  1. checksums.yaml +7 -0
  2. data/.editorconfig +10 -0
  3. data/.rspec +3 -0
  4. data/.rubocop.yml +66 -0
  5. data/ACKNOWLEDGMENTS.md +47 -0
  6. data/CODE_OF_CONDUCT.md +132 -0
  7. data/LICENSE +21 -0
  8. data/README.md +178 -0
  9. data/Rakefile +12 -0
  10. data/lib/minicss/ast/at_rule.rb +17 -0
  11. data/lib/minicss/ast/bad_token.rb +14 -0
  12. data/lib/minicss/ast/block.rb +29 -0
  13. data/lib/minicss/ast/decl.rb +17 -0
  14. data/lib/minicss/ast/decl_list.rb +18 -0
  15. data/lib/minicss/ast/dimension.rb +14 -0
  16. data/lib/minicss/ast/function.rb +15 -0
  17. data/lib/minicss/ast/number.rb +14 -0
  18. data/lib/minicss/ast/percentage.rb +8 -0
  19. data/lib/minicss/ast/rule.rb +28 -0
  20. data/lib/minicss/ast/string_token.rb +14 -0
  21. data/lib/minicss/ast/syntax_error.rb +13 -0
  22. data/lib/minicss/ast/unicode_range.rb +13 -0
  23. data/lib/minicss/ast/url.rb +13 -0
  24. data/lib/minicss/ast.rb +72 -0
  25. data/lib/minicss/css/ast/at_rule.rb +19 -0
  26. data/lib/minicss/css/ast/declaration.rb +21 -0
  27. data/lib/minicss/css/ast/declaration_list.rb +11 -0
  28. data/lib/minicss/css/ast/function.rb +20 -0
  29. data/lib/minicss/css/ast/qualified_rule.rb +19 -0
  30. data/lib/minicss/css/ast/simple_block.rb +37 -0
  31. data/lib/minicss/css/ast/stylesheet.rb +17 -0
  32. data/lib/minicss/css/ast.rb +9 -0
  33. data/lib/minicss/css/errors.rb +8 -0
  34. data/lib/minicss/css/parser.rb +360 -0
  35. data/lib/minicss/css/position.rb +15 -0
  36. data/lib/minicss/css/refinements.rb +78 -0
  37. data/lib/minicss/css/token.rb +28 -0
  38. data/lib/minicss/css/token_stream.rb +56 -0
  39. data/lib/minicss/css/tokenizer.rb +572 -0
  40. data/lib/minicss/css.rb +10 -0
  41. data/lib/minicss/errors.rb +6 -0
  42. data/lib/minicss/sel.rb +382 -0
  43. data/lib/minicss/serializer.rb +59 -0
  44. data/lib/minicss/version.rb +5 -0
  45. data/lib/minicss.rb +53 -0
  46. metadata +87 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 64d957ba44321d035f80d2f0a1d1f23d268c33c8a169a869192fa0dc6cf51dcc
4
+ data.tar.gz: 758db44357f78c2f43c2a714bb9cfcfba5e8b001e0ec3fc0c9a10b967e2ef005
5
+ SHA512:
6
+ metadata.gz: c1f6547128ba426bd55bbe0a6fb23302810082216de1df340c8f22a17ef9400c912ab6b2b1a0c362130fd0cee5524a40ff923ac69d6ae55abbef5f5621929dfe
7
+ data.tar.gz: 244a8692b6e8e950551270f82bd7b4a3fddb52ee00a47637f17ad5bd13fbdec78da25f6e1ba1208f29ce641f93350e137b815f08f173e178a99e73b49c6402d8
data/.editorconfig ADDED
@@ -0,0 +1,10 @@
1
+ root = true
2
+
3
+ [*]
4
+ charset = utf-8
5
+ end_of_line = lf
6
+ indent_size = 2
7
+ indent_style = space
8
+ insert_final_newline = true
9
+ tab_width = 2
10
+ trim_trailing_whitespace = true
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,66 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.4
3
+ NewCops: enable
4
+ SuggestExtensions: false
5
+ Exclude:
6
+ - spec/parsing_tests/*
7
+
8
+ Style/StringLiterals:
9
+ EnforcedStyle: double_quotes
10
+
11
+ Style/StringLiteralsInInterpolation:
12
+ EnforcedStyle: double_quotes
13
+
14
+ Metrics/CyclomaticComplexity:
15
+ Enabled: false
16
+
17
+ Metrics/BlockLength:
18
+ Enabled: false
19
+
20
+ Metrics/PerceivedComplexity:
21
+ Enabled: false
22
+
23
+ Metrics/MethodLength:
24
+ Enabled: false
25
+
26
+ Metrics/AbcSize:
27
+ Enabled: false
28
+
29
+ Lint/MixedRegexpCaptureTypes:
30
+ Enabled: false
31
+
32
+ Layout/LineLength:
33
+ Enabled: false
34
+
35
+ Style/Documentation:
36
+ Enabled: false
37
+
38
+ Metrics/ModuleLength:
39
+ Enabled: false
40
+
41
+ Metrics/ClassLength:
42
+ Enabled: false
43
+
44
+ Layout/CaseIndentation:
45
+ EnforcedStyle: end
46
+
47
+ Layout/MultilineOperationIndentation:
48
+ EnforcedStyle: indented
49
+
50
+ Layout/SpaceAroundOperators:
51
+ EnforcedStyleForExponentOperator: space
52
+
53
+ Layout/EndAlignment:
54
+ EnforcedStyleAlignWith: start_of_line
55
+
56
+ Style/SignalException:
57
+ Exclude:
58
+ - spec/**/*
59
+
60
+ Naming/PredicateMethod:
61
+ Exclude:
62
+ - lib/minicss/css/parser.rb
63
+
64
+ Lint/HashCompareByIdentity:
65
+ Exclude:
66
+ - lib/minicss/sel.rb
@@ -0,0 +1,47 @@
1
+ # Acknowledgments
2
+
3
+ MiniCSS makes use of the following open source projects:
4
+
5
+ ## @LeaVerou/parsel
6
+
7
+ `MiniCSS::Sel` is heavily based on Lea Verou's [`parsel`](https://github.com/leaverou/parsel) project:
8
+
9
+ ```
10
+ MIT License
11
+
12
+ Copyright (c) 2020 Lea Verou
13
+
14
+ Permission is hereby granted, free of charge, to any person obtaining a copy
15
+ of this software and associated documentation files (the "Software"), to deal
16
+ in the Software without restriction, including without limitation the rights
17
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18
+ copies of the Software, and to permit persons to whom the Software is
19
+ furnished to do so, subject to the following conditions:
20
+
21
+ The above copyright notice and this permission notice shall be included in all
22
+ copies or substantial portions of the Software.
23
+
24
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30
+ SOFTWARE.
31
+ ```
32
+
33
+ ## @heyvito/css-parsing-tests
34
+
35
+ Test cases were generated by the implementation-independant, JSON-based tests for CSS parsers `css-parsing-tests`, forked from [CourtBouillon/css-parsing-tests](https://github.com/CourtBouillon/css-parsing-tests), currently living at [heyvito/css-parsing-tests](https://github.com/heyvito/css-parsing-tests):
36
+
37
+ ```
38
+ Written in 2013 by Simon Sapin.
39
+ Updated in 2025 by Vito Sartori.
40
+
41
+ To the extent possible under law, the author(s) have dedicated all copyright
42
+ and related and neighboring rights to this work to the public domain worldwide.
43
+ This work is distributed without any warranty.
44
+
45
+ See the CC0 Public Domain Dedication:
46
+ http://creativecommons.org/publicdomain/zero/1.0/
47
+ ```
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Vito Sartori
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,178 @@
1
+ # MiniCSS
2
+
3
+ MiniCSS is a pure Ruby implementation of the CSS Syntax Level 3 tokenizer,
4
+ parser, serializer, and selector toolkit. It provides fast, dependency-free
5
+ primitives for building linters, minifiers, and other CSS-aware tooling
6
+ without reaching for native extensions.
7
+
8
+ ## Features
9
+
10
+ - Pure Ruby tokenizer and parser with zero native dependencies and support
11
+ for Ruby 3.4+.
12
+ - Battle-tested against the JSON-based `css-parsing-tests` suite for
13
+ standards-compliant parsing.
14
+ - High-level AST wrappers (`MiniCSS::AST::Rule`, `Decl`, `AtRule`, `Function`,
15
+ `Number`, and more) ready for programmatic inspection.
16
+ - Bidirectional selector helpers (`MiniCSS::Sel`) for tokenizing, walking,
17
+ computing specificity, and turning selector ASTs back into strings.
18
+ - Serializer that converts AST nodes back into normalized CSS, enabling
19
+ round-tripping and rewrites.
20
+ - Accepts either strings or IO objects, with optional Unicode range
21
+ tokenization via `allow_unicode_ranges`.
22
+
23
+ ## Installation
24
+
25
+ Add MiniCSS to your project with Bundler:
26
+
27
+ ```bash
28
+ bundle add minicss
29
+ ```
30
+
31
+ Or install it directly:
32
+
33
+ ```bash
34
+ gem install minicss
35
+ ```
36
+
37
+ ## Quick Start
38
+
39
+ ```ruby
40
+ require "minicss"
41
+
42
+ css = <<~CSS
43
+ @media (min-width: 768px) {
44
+ .button.primary {
45
+ color: #fff;
46
+ background: rgb(0 0 0 / 80%);
47
+ }
48
+ }
49
+ CSS
50
+
51
+ sheet = MiniCSS.parse(css)
52
+
53
+ sheet.each do |node|
54
+ case node
55
+ when MiniCSS::AST::AtRule
56
+ puts "At-rule: @#{node.name} #{MiniCSS.serialize(node.prelude)}"
57
+ node.child_rules.each { puts " Child: #{MiniCSS.serialize(_1)}" }
58
+ when MiniCSS::AST::Rule
59
+ selector = MiniCSS::Sel.stringify(node.selector)
60
+ puts "Rule: #{selector}"
61
+ node.decls.each do |decl|
62
+ value = MiniCSS.serialize(decl.value)
63
+ important = decl.important? ? " !important" : ""
64
+ puts " #{decl.name}: #{value}#{important}"
65
+ end
66
+ when MiniCSS::AST::SyntaxError
67
+ warn "Syntax error: #{node.reason}"
68
+ end
69
+ end
70
+ ```
71
+
72
+ ## Tokenizing CSS
73
+
74
+ ```ruby
75
+ tokens = MiniCSS.tokenize(".btn { color: #fff; }")
76
+ tokens.map { |token| [token.kind, token.literal] }
77
+ # => [[:delim, "."], [:ident, "btn"], [:whitespace, " "], [:left_curly, "{"],
78
+ # [:whitespace, " "], [:ident, "color"], [:colon, ":"], [:whitespace, " "],
79
+ # [:hash, "#fff"], [:semicolon, ";"], [:whitespace, " "], [:right_curly, "}"]]
80
+
81
+ unicode_tokens = MiniCSS.tokenize("div { content: U+4E00-9FFF; }", allow_unicode_ranges: true)
82
+ ```
83
+
84
+ Every token carries positional information via `token.pos_start` and
85
+ `token.pos_end`, making it easy to surface diagnostics.
86
+
87
+ ## Working with Selectors
88
+
89
+ ```ruby
90
+ selector_ast = MiniCSS::Sel.parse("button#primary.action:hover")
91
+ MiniCSS::Sel.specificity(selector_ast)
92
+ # => [1, 2, 1]
93
+
94
+ MiniCSS::Sel.walk(selector_ast) do |token, parent|
95
+ puts "#{token[:type]} → #{token[:content]}"
96
+ end
97
+ # id → #primary
98
+ # class → .action
99
+ # pseudo-class → :hover
100
+ # type → button
101
+
102
+ MiniCSS::Sel.stringify(selector_ast)
103
+ # => "button#primary.action:hover"
104
+ ```
105
+
106
+ Selectors are represented as nested hashes and arrays, mirroring the structure
107
+ produced by Lea Verou’s parsel, which `MiniCSS::Sel` is based on.
108
+
109
+ ## Serializing CSS
110
+
111
+ `MiniCSS.serialize` accepts any AST node (or array of nodes) returned by
112
+ `MiniCSS.parse`:
113
+
114
+ ```ruby
115
+ ast = MiniCSS.parse(".card { margin: 1rem; padding: 1rem; }")
116
+ MiniCSS.serialize(ast)
117
+ # => ".card{margin:1rem;padding:1rem;}"
118
+ ```
119
+
120
+ This makes it easy to transform stylesheets and emit normalized output.
121
+
122
+ ## Handling Errors
123
+
124
+ When the parser encounters invalid input it emits `MiniCSS::AST::SyntaxError`
125
+ nodes alongside the rest of the AST:
126
+
127
+ ```ruby
128
+ errors = MiniCSS.parse("p { color: }").grep(MiniCSS::AST::SyntaxError)
129
+ errors.each { warn _1.reason }
130
+ # Syntax error details, including source offsets, are preserved from the tokenizer.
131
+ ```
132
+
133
+ You can introspect the original token stream to report accurate diagnostics.
134
+
135
+ ## Development
136
+
137
+ - Install dependencies: `bundle install`
138
+ - Run the test suite: `bundle exec rspec`
139
+ - Check style and linting: `bundle exec rubocop`
140
+ - Run everything: `bundle exec rake`
141
+
142
+ Helpful utilities:
143
+
144
+ - `bin/console` starts an IRB session with MiniCSS preloaded.
145
+ - `bin/gen-specs.rb` regenerates the specs under `spec/parsing_tests` from the
146
+ external `css-parsing-tests` fixtures.
147
+
148
+ ## Contributing
149
+
150
+ Bug reports, feature requests, and pull requests are welcome. By participating
151
+ you agree to abide by the project’s Code of Conduct (see CODE_OF_CONDUCT.md).
152
+ Please include tests whenever you add or change behavior.
153
+
154
+ ## License
155
+
156
+ ```
157
+ The MIT License (MIT)
158
+
159
+ Copyright (c) 2025 Vito Sartori
160
+
161
+ Permission is hereby granted, free of charge, to any person obtaining a copy
162
+ of this software and associated documentation files (the "Software"), to deal
163
+ in the Software without restriction, including without limitation the rights
164
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
165
+ copies of the Software, and to permit persons to whom the Software is
166
+ furnished to do so, subject to the following conditions:
167
+
168
+ The above copyright notice and this permission notice shall be included in
169
+ all copies or substantial portions of the Software.
170
+
171
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
172
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
173
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
174
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
175
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
176
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
177
+ THE SOFTWARE.
178
+ ```
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MiniCSS
4
+ module AST
5
+ class AtRule
6
+ attr_accessor :name, :prelude, :child_rules
7
+
8
+ def initialize(rule)
9
+ @name = rule.name.literal.gsub(/^@/, "")
10
+ @prelude = rule.prelude.map { AST.convert(it) }
11
+ @prelude.pop while @prelude.last == " "
12
+ @prelude.shift while @prelude.first == " "
13
+ @child_rules = rule.child_rules.map { AST.convert it }
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MiniCSS
4
+ module AST
5
+ class BadToken
6
+ attr_accessor :kind, :value
7
+
8
+ def initialize(css)
9
+ @kind = css.kind
10
+ @value = css.literal
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MiniCSS
4
+ module AST
5
+ class Block
6
+ RIGHT_TOKENS = {
7
+ left_parenthesis: ")",
8
+ left_curly: "}",
9
+ left_square_bracket: "]"
10
+ }.freeze
11
+
12
+ LEFT_TOKENS = {
13
+ left_parenthesis: "(",
14
+ left_curly: "{",
15
+ left_square_bracket: "["
16
+ }.freeze
17
+
18
+ attr_accessor :associated_token, :value
19
+
20
+ def initialize(css)
21
+ @associated_token = css.associated_token
22
+ @value = css.value.map { AST.convert(it) }
23
+ end
24
+
25
+ def right_token = RIGHT_TOKENS[@associated_token]
26
+ def left_token = LEFT_TOKENS[@associated_token]
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MiniCSS
4
+ module AST
5
+ class Decl
6
+ attr_accessor :name, :value, :important
7
+
8
+ def initialize(decl)
9
+ @name = decl.name.literal
10
+ @value = decl.value.map { AST.convert(it) }
11
+ @important = decl.important
12
+ end
13
+
14
+ def important? = important
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MiniCSS
4
+ module AST
5
+ class DeclList < Array
6
+ def initialize(old = nil)
7
+ super()
8
+ if old.is_a? Array
9
+ append(*old)
10
+ elsif old
11
+ append(old)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ # Feci quod potui, faciant meliora potentes.
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MiniCSS
4
+ module AST
5
+ class Dimension < Number
6
+ attr_accessor :unit
7
+
8
+ def initialize(css)
9
+ super
10
+ @unit = css.opts[:unit]
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MiniCSS
4
+ module AST
5
+ class Function
6
+ attr_accessor :raw_name, :name, :value
7
+
8
+ def initialize(func)
9
+ @raw_name = func.name.literal
10
+ @name = raw_name.strip.gsub(/\($/, "")
11
+ @value = func.value.map { AST.convert(it) }
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MiniCSS
4
+ module AST
5
+ class Number
6
+ attr_accessor :raw, :value, :type, :sign
7
+
8
+ def initialize(css)
9
+ @raw = css.literal
10
+ @value, @type, @sign = css.opts.slice(:value, :type, :sign_character).values
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MiniCSS
4
+ module AST
5
+ class Percentage < Number
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MiniCSS
4
+ module AST
5
+ class Rule
6
+ attr_accessor :selector, :child_rules, :decls, :raw_selector
7
+
8
+ def initialize(struct)
9
+ raise "Expected CSS::AST::QualifiedRule, found #{struct.class}" unless struct.is_a? CSS::AST::QualifiedRule
10
+
11
+ @raw_selector = struct.prelude.map(&:literal).join
12
+ begin
13
+ @selector = Sel.parse(raw_selector)
14
+ @valid_selector = true
15
+ rescue StandardError => e
16
+ pos = struct.prelude.first.pos_start
17
+ @selector = "Failed parsing selector at #{pos.line}:#{pos.column}: #{e}"
18
+ @valid_selector = false
19
+ end
20
+
21
+ @child_rules = struct.child_rules.map { AST.convert(it) }
22
+ @decls = struct.decls.map { AST.convert(it) }
23
+ end
24
+
25
+ def valid_selector? = @valid_selector
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MiniCSS
4
+ module AST
5
+ class StringToken
6
+ attr_accessor :value, :quoting
7
+
8
+ def initialize(css)
9
+ @value = css.literal
10
+ @quoting = css.opts[:quoting]
11
+ end
12
+ end
13
+ end
14
+ end