NAME

Sumi::CSS - Mojo-like access to cascading style sheets

SYNOPSIS

use Sumi::CSS;

# Parse CSS text
my $css = Sumi::CSS->new->parse(<<'CSS');
@media screen {
  body { color: black; background: white; }
}
.foo { color: red; }
CSS

# Walk the rules
$css->walk(sub ($rule) {
    say $rule->to_string;
});

# Get the full CSS back as string
say $css->to_string;

# Minify output
say $css->to_string({ minimize => 1 });

# Indented output (custom depth)
say $css->to_string({ indent => 2 });

# Create a localized copy of the CSS
my $localized = $css->localized('.en');

DESCRIPTION

Sumi::CSS provides a Mojolicious-style object interface for parsing, walking, and reconstructing CSS documents with support of nested at-rules.

ATTRIBUTES

rules

my $rules = $css->rules;
$css->rules([ $rule1, $rule2 ]);

An array reference of Sumi::CSS::Rule objects representing parsed CSS rules.

METHODS

new

my $css = Sumi::CSS->new;
my $css = Sumi::CSS->new->parse($css_text);
my $css = Sumi::CSS->new($other_css);

Creates a new Sumi::CSS object. If called with a string as the first argument, it automatically parses that string as CSS content.

parse

$css = $css->parse($css_text);

Parses a CSS string and populates the internal rule list.

walk

$css->walk(sub ($rule) { ... });

Walks over all CSS rules (recursively for nested at-rules) and applies the provided callback. Returns the invocant for chaining.

to_string

my $string = $css->to_string;
my $string = $css->to_string({ minimize => 1 });
my $string = $css->to_string({ indent => 2 });

Serializes the parsed CSS back into text. Takes an optional hash reference with the following keys:

  • minimize

    If true, removes extra whitespace and newlines for compact output.

  • indent

    Controls indentation depth (default: 4 spaces).

localized

my $localized = $css->localized('.en');

Returns a deep-cloned version of the CSS tree with selectors localized using the given prefix (e.g. prepending .en to class selectors). This can be used for namespacing or scoping CSS for localization or components.

INTERNALS

_parse_rules, _walk, _remove_comments

These internal helper functions are provided by Sumi::CSS::Util and are not intended for external use.

SEE ALSO

Mojolicious, Mojo::DOM, CSS::Tiny, CSS::SAC::Parser

AUTHOR

Simone Cesano

LICENSE

This module is released under the same terms as Perl itself.