CSS::Object(3)

πŸ“¦ version 0.2.0, in alpine 3.24

πŸ¦‹ CSS::Object(3) β€” the bestie-coded manual from the User Contributed Perl Documentation πŸ¦‹

🌸 NAME 🌸

CSS::Object β€” CSS Object Oriented, no cap.


✨ SYNOPSIS ✨

Okay bestie, here’s the starter-pack energy from the original page, copied exactly so none of the important bits get lost πŸ’–

use CSS::Object;
use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
my $resp = $ua->get( $style_uri );
die( $resp->message ) if( $resp->is_error );
my $style = $resp->decoded_content;
my $css = CSS::Object->new;
$css->read_string( $style );
$css->rules->foreach(sub
{
    my $rule = shift( @_ );
    # more processing
});

or, parsing inline stylesheets from a remote document:

use HTML::Object;
use HTML::Object::XQuery;
use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
my $resp = $ua->get( $document_uri );
die( $resp->message ) if( $resp->is_error );
my $html = $resp->decoded_content;
my $parser = HTML::Object->new;
my $doc = $parser->parse( $html ) || die( $parser->error );
my $styles = $doc->find( 'style' ) || die( $doc->error );
say "Nothing found", exit(0) unless( $styles->length > 0 );
my $data = $styles->children->first->text();
my $css = CSS::Object->new;
$css->read_string( $data );
$css->rules->foreach(sub
{
    my $rule = shift( @_ );
    # more processing
});

Creating dynamically rules:

use CSS::Object;
my $css = CSS::Object->new ||
    die( CSS::Object->error );
my $b = $css->builder;
$b->select( ['#main_section > .article', 'section .article'] )
    ->display( 'none' )
    ->font_size( '+0.2rem' )
    ->comment( ['Some multiline comment', 'that are made possible with array reference'] )
    ->text_align( 'center' )
    ->comment( 'Making it look pretty' )
    ->padding( 5 );
$b->charset( 'UTF-8' );
$b->at( _webkit_keyframes => 'error' )
    ->frame( 0, { _webkit_transform => 'translateX( 0px )' })
    ->frame( 25, { _webkit_transform => 'translateX( 30px )' })
    ->frame( 45, { _webkit_transform => 'translateX( -30px )' })
    ->frame( 65, { _webkit_transform => 'translateX( 30px )' })
    ->frame( 82, { _webkit_transform => 'translateX( -30px )' })
    ->frame( 94, { _webkit_transform => 'translateX( 30px )' })
    ->frame( [qw( 35 55 75 87 97 100 )], { _webkit_transform => 'translateX( 0px )' } );
say $css->as_string;

πŸŽ€ VERSION πŸŽ€

This one is serving version v0.2.0 ✨


πŸ’œ DESCRIPTION πŸ’œ

CSS::Object is a object oriented CSS parser and manipulation interface. It’s giving structure, it’s giving control, it’s giving CSS but make it an object πŸ‘‘


πŸ‘‘ CONSTRUCTOR πŸ‘‘

🧚 new 🧚

To instantiate a new CSS::Object object, pass a hash reference of these parameters:

  • debug β€” this is an integer. The bigger it is, the more verbose the output is.
  • format β€” this is a CSS::Object::Format object or one of its child modules.
  • parser β€” this is a CSS::Object::Parser object or one of its child modules.

πŸ’€ EXCEPTION HANDLING πŸ’€

Whenever an error has occurred, CSS::Object will set a Module::Generic::Exception object containing the detail of the error and return undef.

The error object can be retrieved with the inherited "error" in Module::Generic method. For example:

my $css = CSS::Object->new( debug => 3 ) || die( CSS::Object->error );

🌈 METHODS 🌈

🧸 add_element 🧸

Provided with a CSS::Object::Element object, this adds it to the list of css elements.

It uses an array object called elements, which is a Module::Generic::Array object.

πŸ“ add_rule πŸ“

Provided with a CSS::Object::Rule object, this adds it to our list of rules. It returns the rule object that was added.

πŸ¦‹ as_string πŸ¦‹

This returns the css data structure currently registered as a string.

It takes an optional CSS::Object::Format object as a parameter to control the output. If none are provided, it will use the default one by calling format.

πŸŽ€ builder πŸŽ€

This returns a new CSS::Object::Builder object.

🌸 charset 🌸

This sets or gets the css charset. It stores the value in a Module::Generic::Scalar object.

✨ elements ✨

Sets or gets the array of CSS elements. This is a Module::Generic::Array object that accepts only CSS::Object::Element objects or its child classes, such as CSS::Object::Rule, CSS::Object::Comment, etc.

πŸ’… format πŸ’…

Sets or gets a CSS::Object::Format object. See as_string for more detail about their use.

CSS::Object::Format objects control the stringification of the css structure. By default, it will return the data in a string identical or at least very similar to the one parsed if it was parsed.

πŸ”₯ get_rule_by_selector πŸ”₯

Provided with a selector, this returns a CSS::Object::Rule object or an empty string.

However, if this method is called in an object context, such as chaining, then it returns a Module::Generic::Null object instead of an empty string to prevent the perl error of "xxx method called on an undefined value". For example:

$css->get_rule_by_selector( '.does-not-exists' )->add_element( $elem ) ||
die( "Unable to add css element to rule \".does-not-exists\": ", $css->error );

But, in a non-object context, such as:

my $rule = $css->get_rule_by_selector( '.does-not-exists' ) ||
die( "Unable to add css element to rule \".does-not-exists\": ", $css->error );

get_rule_by_selector will return an empty value.

🧚 load_parser 🧚

This will instantiate a new object based on the parser name specified with parser or during css object instantiation.

It returns a new CSS::Object::Parser object, or one of its child module matching the parser specified.

πŸŽ€ new_comment πŸŽ€

This returns a new CSS::Object::Comment object and passes its instantiation method the provided arguments.

return( $css->new_comment( $array_ref_of_comment_ilnes ) );

πŸ“ new_property πŸ“

This takes a property name, and an optional value or array of values, and returns a new CSS::Object::Property object.

πŸ¦‹ new_rule πŸ¦‹

This returns a new CSS::Object::Rule object.

🌈 new_selector 🌈

This takes a selector name and returns a new CSS::Object::Selector object.

πŸ’– new_value πŸ’–

This takes a property value and returns a new CSS::Object::Value object.

🌟 parse_string 🌟

Provided with some css data, this will instantiate the parser, call parse_string in CSS::Object::Parser, and return an array of CSS::Object::Rule objects. The array is an array object from Module::Generic::Array and can be used as a regular array or as an object.

🫢 parser 🫢

Sets or gets the CSS::Object::Parser object to be used by parse_string to parse css data.

A valid parser object can be from CSS::Object::Parser or any of its sub modules.

It returns the current parser object.

πŸ’€ purge πŸ’€

This empties the array containing all the CSS::Object::Rule objects.

πŸ“ read_file πŸ“

Provided with a css file, this will load it into memory and parse it using the parser name registered with parser.

It can also take an array reference of css files which will each be fed to read_file.

It returns the CSS::Object used to call this method.

🧡 read_string 🧡

Provided with some css data, this will call parse_string. It also accepts an array reference of data.

It returns the css object used to call this method.

πŸ‘‘ rules πŸ‘‘

This sets or gets the Module::Generic::Array object used to store all the CSS::Object::Rule objects.


πŸ–‹οΈ AUTHOR πŸ–‹οΈ

Jacques Deguest <jack@deguest.jp>


πŸ‘€ SEE ALSO πŸ‘€


Copyright (c) 2020 DEGUEST Pte. Ltd.

You can use, copy, modify and redistribute this package and associated files under the same terms as Perl itself.


🫢 Source/version check, bestie: perl v5.42.2, dated 2026-02-15, and this iconic page is CSS::Object(3). Bye! πŸ’–
πŸ“¦ where this page comes from