NAME
CSS::Tiny::Style - Object oriented interface to CSS stylesheets
VERSION
This document describes CSS::Tiny::Style version 0.0.3
SYNOPSIS
#------------------------------------------------------
# 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;
DESCRIPTION
INTERFACE
Constructor
new
my $selector_string = 'div.index li.new';
my $style = CSS::Tiny::Style->new($selector_string);
Returns a new CSS::Tiny::Style object
Using with CSS::Tiny
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' );
CSS::Tiny->styles
@styles = CSS::Tiny->styles
Returns a list of CSS::Tiny::Style objects made up of all the style definitions in CSS::Tiny
CSS::Tiny->selectors
@selectors = CSS::Tiny->selectors
Returns a list of selector strings in the CSS::Tiny object
Selectors
tag
my $style = CSS::Tiny::Style->new('pre');
print $style->tag; # prints "pre"
Returns the tag part of the selector
id
my $style = CSS::Tiny::Style->new('li#leftmenu');
print $style->id; # prints "leftmenu"
Returns the id part of the selector
class
my $style = CSS::Tiny::Style->new('p.big');
print $style->class; # prints "big"
Returns the class part of the selector
selector
my $style = CSS::Tiny::Style->new('div.sidebar p.new');
print $style->selector; # prints "div.sidebar p.new"
Returns the selector string
selector_array
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
selarr is an alias for selector_array
Specificity
specificity
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.
count_tags
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
count_ids
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
count_attributes
my $style = CSS::Tiny::Style->new('h1.menu li #foo');
print $style->count_attributes # prints 1
Return the number of attributes in the selector
Matching and inlining
match
$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).
element_match
$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.
add_to
Inlines the style definition into the element.
DEPENDENCIES
CSS::Tiny
HTML::Element
INCOMPATIBILITIES
None reported.
BUGS
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.
LIMITATIONS
Attribute selectors
Does not handle E[foo] type selectors
Repeated properties
Allows duplicated setting of the same property in a inline style definition
Inheritance
Does not handle inherited properties
AUTHOR
Simone Cesano <scesano@cpan.org>
LICENCE AND COPYRIGHT
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.
DISCLAIMER OF WARRANTY
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.