CSS::Object(3)
π¦ 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 aCSS::Object::Formatobject or one of its child modules.parserβ this is aCSS::Object::Parserobject 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 π
CSS::Object- Mozilla documentation on Custom CSS Properties
https://developer.mozilla.org/en-US/docs/Web/CSS/--*
βοΈ COPYRIGHT & LICENSE βοΈ
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, dated2026-02-15, and this iconic page isCSS::Object(3). Bye! π
π¦ where this page comes from
- alpine 3.24: perl-css-object-doc 0.2.0-r1 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms