CSS::Tiny::Style - Object oriented interface to CSS stylesheets
This document describes CSS::Tiny::Style version 0.0.3
#------------------------------------------------------ # simple example #------------------------------------------------------ use CSS::Tiny; use CSS::Tiny::Style; use HTML::Element; $el = HTML::Element->new('p'); $el->attr('class', 'myclass'); $cts = CSS::Tiny::Style->new('p.myclass') $cts->match($el) # return true #------------------------------------------------------ # more interesting example #------------------------------------------------------ # inline a stylesheet onto the single HTML elements #------------------------------------------------------ use HTML::TreeBuilder; my $tree = HTML::TreeBuilder->new(); $tree->parse_file('filename.html'); $css = CSS::Tiny->read( 'stylesheet.css' ); for my $el ($tree->descendants) { for my $st ($css->styles) { if ($st->match($el)) { $st->add_style($el); } } } print $tree->as_HTML;
my $selector_string = 'div.index li.new'; my $style = CSS::Tiny::Style->new($selector_string);
Returns a new CSS::Tiny::Style object
CSS::Tiny::Styles provides two additional methods to CSS::Tiny, making it possible to generate CSS::Tiny::Styles on the fly by parsing a stylesheet like this:
$css = CSS::Tiny->read( 'stylesheet.css' );
@styles = CSS::Tiny->styles
Returns a list of CSS::Tiny::Style objects made up of all the style definitions in CSS::Tiny
@selectors = CSS::Tiny->selectors
Returns a list of selector strings in the CSS::Tiny object
my $style = CSS::Tiny::Style->new('pre'); print $style->tag; # prints "pre"
Returns the tag part of the selector
my $style = CSS::Tiny::Style->new('li#leftmenu'); print $style->id; # prints "leftmenu"
Returns the id part of the selector
my $style = CSS::Tiny::Style->new('p.big'); print $style->class; # prints "big"
Returns the class part of the selector
my $style = CSS::Tiny::Style->new('div.sidebar p.new'); print $style->selector; # prints "div.sidebar p.new"
Returns the selector string
my $style = CSS::Tiny::Style->new('div.main div.sidebar > p.new'); @sel_arr = $style->selector_array; print join "::" @sel_arr; # prints
Returns an array of element tags (i.e: p.important) alternated with strings derived from relative selectors (i.e.: the ">" or "+" or " " between element tags.
Relative selectors are transformed as follows:
'+' becomes 'left'
'>' becomes 'parent'
' ' becomes 'lineage'
selarr is an alias for selector_array
my $style = CSS::Tiny::Style->new('h1.menu li.myclass'); print $style->specificity # prints 22
Returns the selector's specificity as described in http://www.w3.org/TR/CSS21/cascade.html#specificity. A base 100 is used.
my $style = CSS::Tiny::Style->new('h1.menu li.myclass #foo'); print $style->count_tags # prints 2
Return the number of tags in the selector
my $style = CSS::Tiny::Style->new('h1#bar li.myclass #foo'); print $style->count_ids # prints 2
Return the number of ids in the selector
my $style = CSS::Tiny::Style->new('h1.menu li #foo'); print $style->count_attributes # prints 1
Return the number of attributes in the selector
$el = HTML::Element->new('p'); $el->attr('class', 'myclass'); $cts = CSS::Tiny::Style->new('p.myclass') $cts->match($el) # return true
Returns true if the style selector matches on the HTML::Element object, taking into account all its relatives (siblings, ancestors, parent).
$el = HTML::Element->new('p'); $el->attr('class', 'myclass'); $cts = CSS::Tiny::Style->new('p.myclass') $cts->element_match($el) # return true
Returns true if the style selector matches on the HTML::Element object without taking into account the ancestors.
Inlines the style definition into the element.
CSS::Tiny HTML::Element
None reported.
No bugs have yet been reported.
Please report any bugs or feature requests to bug-css-tiny-style@rt.cpan.org, or through the web interface at http://rt.cpan.org.
bug-css-tiny-style@rt.cpan.org
Does not handle E[foo] type selectors
Allows duplicated setting of the same property in a inline style definition
Does not handle inherited properties
Simone Cesano <scesano@cpan.org>
<scesano@cpan.org>
Copyright (c) 2007, Simone Cesano <scesano@cpan.org>. All rights reserved.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic.
BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
To install CSS::Tiny, copy and paste the appropriate command in to your terminal.
cpanm
cpanm CSS::Tiny
CPAN shell
perl -MCPAN -e shell install CSS::Tiny
For more information on module installation, please visit the detailed CPAN module installation guide.