lightningcss_rb 0.0.1
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 +7 -0
- data/CHANGELOG.md +4 -0
- data/Cargo.lock +1212 -0
- data/Cargo.toml +7 -0
- data/LICENSE.txt +21 -0
- data/NOTICE.md +17 -0
- data/README.md +57 -0
- data/Rakefile +25 -0
- data/ext/lightningcss_rb/Cargo.toml +15 -0
- data/ext/lightningcss_rb/extconf.rb +6 -0
- data/ext/lightningcss_rb/src/lib.rs +57 -0
- data/lib/lightningcss_rb/ast/selectors/attribute.rb +47 -0
- data/lib/lightningcss_rb/ast/selectors/base.rb +33 -0
- data/lib/lightningcss_rb/ast/selectors/class_selector.rb +13 -0
- data/lib/lightningcss_rb/ast/selectors/combinator.rb +25 -0
- data/lib/lightningcss_rb/ast/selectors/id.rb +13 -0
- data/lib/lightningcss_rb/ast/selectors/namespace.rb +22 -0
- data/lib/lightningcss_rb/ast/selectors/nesting.rb +11 -0
- data/lib/lightningcss_rb/ast/selectors/pseudo_class.rb +73 -0
- data/lib/lightningcss_rb/ast/selectors/pseudo_element.rb +67 -0
- data/lib/lightningcss_rb/ast/selectors/type_selector.rb +13 -0
- data/lib/lightningcss_rb/ast/selectors/universal.rb +11 -0
- data/lib/lightningcss_rb/ast/selectors.rb +39 -0
- data/lib/lightningcss_rb/ast.rb +56 -0
- data/lib/lightningcss_rb/error.rb +5 -0
- data/lib/lightningcss_rb/unknown_error.rb +20 -0
- data/lib/lightningcss_rb/version.rb +5 -0
- data/lib/lightningcss_rb.rb +65 -0
- data/lib/tasks/.keep +0 -0
- data/sig/lightningcss_rb.rbs +16 -0
- metadata +90 -0
data/Cargo.toml
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Marc Heiligers
|
|
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/NOTICE.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# Third-party notices
|
|
2
|
+
|
|
3
|
+
The Ruby and Rust binding code in this gem (`lightningcss_rb`) is licensed under
|
|
4
|
+
the MIT License, see `LICENSE.txt`.
|
|
5
|
+
|
|
6
|
+
This gem links the **LightningCSS** Rust library, which is licensed under the
|
|
7
|
+
**Mozilla Public License 2.0 (MPL-2.0)**:
|
|
8
|
+
|
|
9
|
+
- Project: https://github.com/parcel-bundler/lightningcss
|
|
10
|
+
- License: https://www.mozilla.org/en-US/MPL/2.0/
|
|
11
|
+
- Version: 1.0.0-alpha.71
|
|
12
|
+
|
|
13
|
+
The MPL-2.0 covers the LightningCSS source files. Under the MPL-2.0, the source
|
|
14
|
+
for any modified MPL-covered files must be made available; this gem uses an
|
|
15
|
+
unmodified upstream release obtained from crates.io, whose source is available
|
|
16
|
+
at the project URL above. The MIT license applies only to the binding code in
|
|
17
|
+
this repository, not to the bundled LightningCSS sources.
|
data/README.md
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# LightningcssRb
|
|
2
|
+
|
|
3
|
+
TODO: Delete this and the text below, and describe your gem
|
|
4
|
+
|
|
5
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/lightningcss_rb`. To experiment with that code, run `bin/console` for an interactive prompt.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
|
|
10
|
+
|
|
11
|
+
Install the gem and add to the application's Gemfile by executing:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
TODO: Write usage instructions here
|
|
26
|
+
|
|
27
|
+
## Development
|
|
28
|
+
|
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Because this gem has a Rust extension, compile it with `bundle exec rake compile` before running the tests (or run `bundle exec rake`, which compiles, tests, and lints). You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
30
|
+
|
|
31
|
+
### Tests
|
|
32
|
+
|
|
33
|
+
`bin/test` runs the test suite, and can scope down to a single file or a single test:
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
bin/test # all tests
|
|
37
|
+
bin/test test/test_lightningcss_rb.rb # a single file
|
|
38
|
+
bin/test test/test_lightningcss_rb.rb:26 # the test defined at or above line 26
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Flags are passed through to minitest (e.g. `bin/test -v`). The shims run against the currently compiled extension, so re-run `bundle exec rake compile` after changing Rust code.
|
|
42
|
+
|
|
43
|
+
### Linting
|
|
44
|
+
|
|
45
|
+
`bin/rubocop` runs RuboCop, forwarding any arguments (e.g. `bin/rubocop -a` to autocorrect).
|
|
46
|
+
|
|
47
|
+
### Install & Release
|
|
48
|
+
|
|
49
|
+
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).
|
|
50
|
+
|
|
51
|
+
## Contributing
|
|
52
|
+
|
|
53
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/marcheiligers/lightningcss_rb.
|
|
54
|
+
|
|
55
|
+
## License
|
|
56
|
+
|
|
57
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "bundler/gem_tasks"
|
|
4
|
+
|
|
5
|
+
Dir.glob("lib/tasks/**/*.rake").each { |f| load f }
|
|
6
|
+
require "minitest/test_task"
|
|
7
|
+
|
|
8
|
+
Minitest::TestTask.create
|
|
9
|
+
|
|
10
|
+
require "rubocop/rake_task"
|
|
11
|
+
|
|
12
|
+
RuboCop::RakeTask.new
|
|
13
|
+
|
|
14
|
+
require "rb_sys/extensiontask"
|
|
15
|
+
|
|
16
|
+
desc "Compile the extension"
|
|
17
|
+
task build: :compile
|
|
18
|
+
|
|
19
|
+
GEMSPEC = Gem::Specification.load("lightningcss_rb.gemspec")
|
|
20
|
+
|
|
21
|
+
RbSys::ExtensionTask.new("lightningcss_rb", GEMSPEC) do |ext|
|
|
22
|
+
ext.lib_dir = "lib/lightningcss_rb"
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
task default: %i[compile test rubocop]
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
[package]
|
|
2
|
+
name = "lightningcss_rb"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
edition = "2021"
|
|
5
|
+
authors = ["Marc Heiligers <marc@fascination.works>"]
|
|
6
|
+
license = "MIT"
|
|
7
|
+
publish = false
|
|
8
|
+
|
|
9
|
+
[lib]
|
|
10
|
+
crate-type = ["cdylib"]
|
|
11
|
+
|
|
12
|
+
[dependencies]
|
|
13
|
+
magnus = { version = "0.8.2" }
|
|
14
|
+
lightningcss = { version = "=1.0.0-alpha.71", features = ["serde"] }
|
|
15
|
+
serde_json = "1"
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
use lightningcss::stylesheet::{MinifyOptions, ParserOptions, PrinterOptions, StyleSheet};
|
|
2
|
+
use magnus::{function, prelude::*, ExceptionClass, Error, RModule, Ruby};
|
|
3
|
+
|
|
4
|
+
/// Build a `LightningcssRb::Error` from any displayable error. Falls back to
|
|
5
|
+
/// StandardError if the custom class can't be resolved, so this never panics
|
|
6
|
+
/// across the FFI boundary.
|
|
7
|
+
fn error(message: impl std::fmt::Display) -> Error {
|
|
8
|
+
let ruby = Ruby::get().unwrap();
|
|
9
|
+
let class: ExceptionClass = ruby
|
|
10
|
+
.class_object()
|
|
11
|
+
.const_get::<_, RModule>("LightningcssRb")
|
|
12
|
+
.and_then(|m| m.const_get("Error"))
|
|
13
|
+
.unwrap_or_else(|_| ruby.exception_standard_error());
|
|
14
|
+
Error::new(class, message.to_string())
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/// Parse CSS into LightningCSS's AST, serialized as a JSON string.
|
|
18
|
+
fn parse(css: String) -> Result<String, Error> {
|
|
19
|
+
let stylesheet = StyleSheet::parse(&css, ParserOptions::default()).map_err(error)?;
|
|
20
|
+
serde_json::to_string(&stylesheet).map_err(error)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/// Serialize a JSON AST (as produced by `parse`) back to CSS.
|
|
24
|
+
fn to_css(ast_json: String, minify: bool) -> Result<String, Error> {
|
|
25
|
+
let stylesheet: StyleSheet = serde_json::from_str(&ast_json).map_err(error)?;
|
|
26
|
+
let res = stylesheet
|
|
27
|
+
.to_css(PrinterOptions {
|
|
28
|
+
minify,
|
|
29
|
+
..Default::default()
|
|
30
|
+
})
|
|
31
|
+
.map_err(error)?;
|
|
32
|
+
Ok(res.code)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/// Parse, run the optimization pass, and print CSS. With `minify: true`
|
|
36
|
+
/// whitespace is also removed.
|
|
37
|
+
fn transform(css: String, minify: bool) -> Result<String, Error> {
|
|
38
|
+
let mut stylesheet = StyleSheet::parse(&css, ParserOptions::default()).map_err(error)?;
|
|
39
|
+
stylesheet.minify(MinifyOptions::default()).map_err(error)?;
|
|
40
|
+
let res = stylesheet
|
|
41
|
+
.to_css(PrinterOptions {
|
|
42
|
+
minify,
|
|
43
|
+
..Default::default()
|
|
44
|
+
})
|
|
45
|
+
.map_err(error)?;
|
|
46
|
+
Ok(res.code)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
#[magnus::init]
|
|
50
|
+
fn init(ruby: &Ruby) -> Result<(), Error> {
|
|
51
|
+
let module = ruby.define_module("LightningcssRb")?;
|
|
52
|
+
module.define_error("Error", ruby.exception_standard_error())?;
|
|
53
|
+
module.define_singleton_method("_parse", function!(parse, 1))?;
|
|
54
|
+
module.define_singleton_method("_to_css", function!(to_css, 2))?;
|
|
55
|
+
module.define_singleton_method("_transform", function!(transform, 2))?;
|
|
56
|
+
Ok(())
|
|
57
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module LightningcssRb
|
|
4
|
+
class AST
|
|
5
|
+
module Selectors
|
|
6
|
+
class Attribute < Base
|
|
7
|
+
node_attr_reader :name, :namespace, :operation
|
|
8
|
+
|
|
9
|
+
OPERATORS = {
|
|
10
|
+
"equal" => "=",
|
|
11
|
+
"includes" => "~=",
|
|
12
|
+
"dash-match" => "|=",
|
|
13
|
+
"prefix" => "^=",
|
|
14
|
+
"substring" => "*=",
|
|
15
|
+
"suffix" => "$="
|
|
16
|
+
}.freeze
|
|
17
|
+
|
|
18
|
+
def to_css # rubocop:disable Metrics/MethodLength
|
|
19
|
+
ns = namespace_prefix
|
|
20
|
+
s = "[#{ns}#{name}"
|
|
21
|
+
if operation
|
|
22
|
+
op = OPERATORS[operation[:operator]]
|
|
23
|
+
val = operation[:value]
|
|
24
|
+
s += "#{op}\"#{val}\""
|
|
25
|
+
case operation[:caseSensitivity]
|
|
26
|
+
when "ascii-case-insensitive" then s += " i"
|
|
27
|
+
when "explicit-case-sensitive" then s += " s"
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
"#{s}]"
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
private
|
|
34
|
+
|
|
35
|
+
def namespace_prefix
|
|
36
|
+
return "" if namespace.nil?
|
|
37
|
+
|
|
38
|
+
case namespace[:type]
|
|
39
|
+
when "any" then "*|"
|
|
40
|
+
when "specific" then "#{namespace[:prefix]}|"
|
|
41
|
+
else ""
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module LightningcssRb
|
|
4
|
+
class AST
|
|
5
|
+
module Selectors
|
|
6
|
+
class Base
|
|
7
|
+
attr_reader :node
|
|
8
|
+
|
|
9
|
+
def self.node_attr_reader(*names)
|
|
10
|
+
names.each do |name|
|
|
11
|
+
define_method name do
|
|
12
|
+
node[name]
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def self.from_ast(node)
|
|
18
|
+
new(node: node)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def initialize(node:)
|
|
22
|
+
@node = node
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def to_css
|
|
26
|
+
raise NotImplementedError, "#{self.class.name} must implement #{to_css}"
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def to_s = to_css
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module LightningcssRb
|
|
4
|
+
class AST
|
|
5
|
+
module Selectors
|
|
6
|
+
class Combinator < Base
|
|
7
|
+
node_attr_reader :value
|
|
8
|
+
|
|
9
|
+
STRINGS = {
|
|
10
|
+
"descendant" => " ",
|
|
11
|
+
"child" => " > ",
|
|
12
|
+
"next-sibling" => " + ",
|
|
13
|
+
"later-sibling" => " ~ ",
|
|
14
|
+
"pseudo-element" => "",
|
|
15
|
+
"slot-assignment" => "",
|
|
16
|
+
"part" => "",
|
|
17
|
+
"deep-descendant" => " >>> ",
|
|
18
|
+
"deep" => " /deep/ "
|
|
19
|
+
}.freeze
|
|
20
|
+
|
|
21
|
+
def to_css = STRINGS.fetch(value, " ")
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module LightningcssRb
|
|
4
|
+
class AST
|
|
5
|
+
module Selectors
|
|
6
|
+
UnknownNamespaceKind = Class.new(UnknownError)
|
|
7
|
+
|
|
8
|
+
class Namespace < Base
|
|
9
|
+
node_attr_reader :kind, :prefix
|
|
10
|
+
|
|
11
|
+
def to_css
|
|
12
|
+
case kind
|
|
13
|
+
when "none" then "|"
|
|
14
|
+
when "any" then "*|"
|
|
15
|
+
when "named" then "#{prefix}|"
|
|
16
|
+
else raise UnknownNamespaceKind(kind, node)
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module LightningcssRb
|
|
4
|
+
class AST
|
|
5
|
+
module Selectors
|
|
6
|
+
class PseudoClass < Base
|
|
7
|
+
node_attr_reader :kind
|
|
8
|
+
|
|
9
|
+
def to_css # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/AbcSize
|
|
10
|
+
case kind
|
|
11
|
+
when "not" then ":not(#{render_selector_list(node[:selectors])})"
|
|
12
|
+
when "is" then ":is(#{render_selector_list(node[:selectors])})"
|
|
13
|
+
when "where" then ":where(#{render_selector_list(node[:selectors])})"
|
|
14
|
+
when "has" then ":has(#{render_selector_list(node[:selectors])})"
|
|
15
|
+
when "any" then ":#{vendor_prefix_str(node[:vendorPrefix])}any(#{render_selector_list(node[:selectors])})"
|
|
16
|
+
when "host" then node[:selectors] ? ":host(#{render_selector(node[:selectors])})" : ":host"
|
|
17
|
+
when "local" then ":local(#{render_selector(node[:selector])})"
|
|
18
|
+
when "global" then ":global(#{render_selector(node[:selector])})"
|
|
19
|
+
when "lang" then ":lang(#{node[:languages].join(", ")})"
|
|
20
|
+
when "dir" then ":dir(#{node[:direction]})"
|
|
21
|
+
when "webkit-scrollbar" then ":#{node[:value]}"
|
|
22
|
+
when "nth-child" then ":nth-child(#{nth_str}#{of_str})"
|
|
23
|
+
when "nth-last-child" then ":nth-last-child(#{nth_str}#{of_str})"
|
|
24
|
+
when "nth-of-type" then ":nth-of-type(#{nth_str})"
|
|
25
|
+
when "nth-last-of-type" then ":nth-last-of-type(#{nth_str})"
|
|
26
|
+
when "nth-col" then ":nth-col(#{nth_str})"
|
|
27
|
+
when "nth-last-col" then ":nth-last-col(#{nth_str})"
|
|
28
|
+
when "active-view-transition-type" then ":active-view-transition-type(#{node[:type].join(", ")})"
|
|
29
|
+
when "state" then ":state(#{node[:state]})"
|
|
30
|
+
when "custom" then ":#{node[:name]}"
|
|
31
|
+
when "custom-function" then ":#{node[:name]}()"
|
|
32
|
+
else ":#{kind}"
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
private
|
|
37
|
+
|
|
38
|
+
def render_selector_list(selectors)
|
|
39
|
+
selectors.map { |sel| render_selector(sel) }.join(", ")
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def render_selector(components)
|
|
43
|
+
components.map { |h| Selectors.from_ast(h).to_css }.join
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def nth_str
|
|
47
|
+
a = node[:a]
|
|
48
|
+
b = node[:b]
|
|
49
|
+
return b.to_s if a == 0
|
|
50
|
+
return "n" if a == 1 && b == 0
|
|
51
|
+
return "n+#{b}" if a == 1
|
|
52
|
+
|
|
53
|
+
s = "#{a}n"
|
|
54
|
+
s += "+#{b}" if b.positive?
|
|
55
|
+
s += b.to_s if b.negative?
|
|
56
|
+
s
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def of_str
|
|
60
|
+
return "" unless node[:of]
|
|
61
|
+
|
|
62
|
+
" of #{render_selector_list(node[:of])}"
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def vendor_prefix_str(prefixes)
|
|
66
|
+
return "" if prefixes.nil? || prefixes.empty?
|
|
67
|
+
|
|
68
|
+
"-#{prefixes.first}-"
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module LightningcssRb
|
|
4
|
+
class AST
|
|
5
|
+
module Selectors
|
|
6
|
+
class PseudoElement < Base
|
|
7
|
+
node_attr_reader :kind
|
|
8
|
+
|
|
9
|
+
SIMPLE = %w[
|
|
10
|
+
after before first-line first-letter details-content target-text
|
|
11
|
+
search-text marker cue cue-region view-transition picker-icon
|
|
12
|
+
checkmark grammar-error spelling-error
|
|
13
|
+
].freeze
|
|
14
|
+
|
|
15
|
+
VENDOR_PREFIXED = %w[selection placeholder backdrop file-selector-button].freeze
|
|
16
|
+
|
|
17
|
+
def to_css # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/AbcSize
|
|
18
|
+
if SIMPLE.include?(kind)
|
|
19
|
+
"::#{kind}"
|
|
20
|
+
elsif VENDOR_PREFIXED.include?(kind)
|
|
21
|
+
"::#{vendor_prefix_str(node[:vendorPrefix])}#{kind}"
|
|
22
|
+
else
|
|
23
|
+
case kind
|
|
24
|
+
when "highlight-function" then "::highlight(#{node[:name]})"
|
|
25
|
+
when "cue-function" then "::cue(#{render_selector(node[:selector])})"
|
|
26
|
+
when "cue-region-function" then "::cue-region(#{render_selector(node[:selector])})"
|
|
27
|
+
when "picker-function" then "::picker(#{node[:identifier]})"
|
|
28
|
+
when "slotted" then "::slotted(#{render_selector(node[:selector])})"
|
|
29
|
+
when "part" then "::part(#{node[:names].join(" ")})"
|
|
30
|
+
when "custom" then "::#{node[:name]}"
|
|
31
|
+
when "custom-function" then "::#{node[:name]}()"
|
|
32
|
+
when "view-transition-group", "view-transition-image-pair",
|
|
33
|
+
"view-transition-old", "view-transition-new"
|
|
34
|
+
"::#{kind}(#{view_transition_part(node[:part])})"
|
|
35
|
+
when "webkit-scrollbar"
|
|
36
|
+
"::-webkit-scrollbar#{webkit_scrollbar_suffix(node[:value])}"
|
|
37
|
+
else
|
|
38
|
+
"::#{kind}"
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def to_s = to_css
|
|
44
|
+
|
|
45
|
+
private
|
|
46
|
+
|
|
47
|
+
def render_selector(components)
|
|
48
|
+
components.map { |h| Selectors.from_ast(h).to_css }.join
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def vendor_prefix_str(prefixes)
|
|
52
|
+
return "" if prefixes.nil? || prefixes.empty?
|
|
53
|
+
|
|
54
|
+
"-#{prefixes.first}-"
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def view_transition_part(part)
|
|
58
|
+
part[:name] || "*"
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def webkit_scrollbar_suffix(value)
|
|
62
|
+
value == "scrollbar" ? "" : "-#{value}"
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "selectors/base"
|
|
4
|
+
require_relative "selectors/combinator"
|
|
5
|
+
require_relative "selectors/universal"
|
|
6
|
+
require_relative "selectors/namespace"
|
|
7
|
+
require_relative "selectors/type_selector"
|
|
8
|
+
require_relative "selectors/id"
|
|
9
|
+
require_relative "selectors/class_selector"
|
|
10
|
+
require_relative "selectors/attribute"
|
|
11
|
+
require_relative "selectors/pseudo_class"
|
|
12
|
+
require_relative "selectors/pseudo_element"
|
|
13
|
+
require_relative "selectors/nesting"
|
|
14
|
+
|
|
15
|
+
module LightningcssRb
|
|
16
|
+
class AST
|
|
17
|
+
module Selectors
|
|
18
|
+
TYPE_MAP = {
|
|
19
|
+
"combinator" => Combinator,
|
|
20
|
+
"universal" => Universal,
|
|
21
|
+
"namespace" => Namespace,
|
|
22
|
+
"type" => TypeSelector,
|
|
23
|
+
"id" => Id,
|
|
24
|
+
"class" => ClassSelector,
|
|
25
|
+
"attribute" => Attribute,
|
|
26
|
+
"pseudo-class" => PseudoClass,
|
|
27
|
+
"pseudo-element" => PseudoElement,
|
|
28
|
+
"nesting" => Nesting
|
|
29
|
+
}.freeze
|
|
30
|
+
|
|
31
|
+
def self.from_ast(node)
|
|
32
|
+
klass = TYPE_MAP[node[:type]]
|
|
33
|
+
raise UnknownSelectorType.new("Unknown selector type: #{node[:type].inspect}", node) unless klass
|
|
34
|
+
|
|
35
|
+
klass.from_ast(node)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "ast/selectors"
|
|
4
|
+
|
|
5
|
+
module LightningcssRb
|
|
6
|
+
class AST
|
|
7
|
+
UnknownSelectorType = Class.new(UnknownError)
|
|
8
|
+
|
|
9
|
+
attr_reader :ast
|
|
10
|
+
|
|
11
|
+
def initialize(ast)
|
|
12
|
+
@ast = ast.is_a?(String) ? LightningcssRb.parse(ast) : ast
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def rules
|
|
16
|
+
ast[:rules]
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def selectors
|
|
20
|
+
collect_selectors(rules)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
private
|
|
24
|
+
|
|
25
|
+
# Recursively collect fully-resolved selector strings, descending into nested
|
|
26
|
+
# rules and expanding each `&` (nesting) token against the parent selector(s).
|
|
27
|
+
def collect_selectors(rules, parents = nil)
|
|
28
|
+
rules.flat_map do |rule|
|
|
29
|
+
case rule[:type]
|
|
30
|
+
when "style"
|
|
31
|
+
resolved = resolve_nesting(rule.dig(:value, :selectors), parents)
|
|
32
|
+
resolved.map { |selector| selector_to_s(selector) } +
|
|
33
|
+
collect_selectors(rule.dig(:value, :rules), resolved)
|
|
34
|
+
when "media"
|
|
35
|
+
collect_selectors(rule.dig(:value, :rules), parents)
|
|
36
|
+
end
|
|
37
|
+
end.compact
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Replace the `nesting` token in each selector with each parent selector,
|
|
41
|
+
# producing the cartesian product when a parent has multiple selectors.
|
|
42
|
+
def resolve_nesting(selectors, parents)
|
|
43
|
+
return selectors if parents.nil?
|
|
44
|
+
|
|
45
|
+
selectors.flat_map do |selector|
|
|
46
|
+
parents.map do |parent|
|
|
47
|
+
selector.flat_map { |part| part[:type] == "nesting" ? parent : [part] }
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def selector_to_s(selector)
|
|
53
|
+
selector.map { |part| Selectors.from_ast(part).to_css }.join
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|