CSS-Tiny-Style
view release on metacpan or search on metacpan
lib/CSS/Tiny/Style.pm view on Meta::CPAN
$sel, # the first selector
$rel, # the relationship, i.e.: '>' or '+' or ' '
@sel # the remaining selector
) = @_;
} else {
($sel, $rel, @sel) = $self->selector_array;
}
#+++++++++++++++++++++++++++++++++++++++++++++++++++++
# 1) loop through elements
# 2) check if one matches
# 3) if it matches, loop through his relatives
# 4) return true if one matches
#+++++++++++++++++++++++++++++++++++++++++++++++++++++
my $match = 0;
for (@el) {
if ($self->element_match($_, $sel)) {
# if element matches, check his relatives
if ($rel) {
my $rellist = $_->$rel;
$match = $self->match($rellist, @sel)
} else {
$match = 1;
}
}
last if $match;
};
return $match;
}
sub selector {
my $self = shift;
$self->{'_selector'};
}
*selarr = *selector_array;
sub selector_array {
my $self = shift;
unless (defined $self->{_selarr}) {
my $selector = $self->selector;
my @sel = _sel_arr($selector);
$self->{_selarr} = [@sel];
}
return @{ $self->{_selarr} };
}
sub add_to {
my $self = shift;
my $element = shift;
my $style = $element->attr('style');
$style = $style eq "" ? $self->stringify : (join ";", ($style, $self->stringify));
#$style =~ s/^;//; # why why why?
$element->attr('style', $style)
}
###########################################################################################################
# from CSS spec at http://www.w3.org/TR/CSS21/cascade.html#specificity
###########################################################################################################
# A selector's specificity is calculated as follows:
#
# * count the number of ID attributes in the selector (= a)
# * count the number of other attributes and pseudo-classes in the selector (= b)
# * count the number of element names in the selector (= c)
# * ignore pseudo-elements.
#
# Concatenating the three numbers a-b-c (in a number system with a large base) gives the specificity.
#
# Example(s):
#
# Some examples:
#
# * {} /* a=0 b=0 c=0 -> specificity = 0 */
# LI {} /* a=0 b=0 c=1 -> specificity = 1 */
# UL LI {} /* a=0 b=0 c=2 -> specificity = 2 */
# UL OL+LI {} /* a=0 b=0 c=3 -> specificity = 3 */
# H1 + *[REL=up]{} /* a=0 b=1 c=1 -> specificity = 11 */
# UL OL LI.red {} /* a=0 b=1 c=3 -> specificity = 13 */
# LI.red.level {} /* a=0 b=2 c=1 -> specificity = 21 */
# #x34y {} /* a=1 b=0 c=0 -> specificity = 100 */
###########################################################################################################
sub specificity {
my $self = shift;
return 0 if $self->selector eq '*';
return (
$self->count_ids * 100 +
$self->count_attributes * 10 +
$self->count_tags
);
}
sub tag { _tag(shift->selector) }
sub id { _id(shift->selector) }
sub class { _class(shift->selector) }
sub count_ids {
my @sel = shift->selarr;
return scalar grep { /\#/ } @sel
}
sub count_attributes {
my @sel = shift->selarr;
return scalar grep { /\./ || /\[/ } @sel
}
sub count_tags {
my @sel = shift->selarr;
return int (((scalar grep { !/\*/ } @sel) / 2) + 1)
}
sub _sel_arr {
local $_ = shift;
my @d;
while ($_) {
lib/CSS/Tiny/Style.pm view on Meta::CPAN
Returns the tag part of the selector
=head3 id
my $style = CSS::Tiny::Style->new('li#leftmenu');
print $style->id; # prints "leftmenu"
Returns the id part of the selector
=head3 class
my $style = CSS::Tiny::Style->new('p.big');
print $style->class; # prints "big"
Returns the class part of the selector
=head3 selector
my $style = CSS::Tiny::Style->new('div.sidebar p.new');
print $style->selector; # prints "div.sidebar p.new"
Returns the selector string
=head3 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'
=head3 selarr
selarr is an alias for selector_array
=head2 Specificity
=head3 specificity
my $style = CSS::Tiny::Style->new('h1.menu li.myclass');
print $style->specificity # prints 22
Returns the selector's specificity as described in
L<http://www.w3.org/TR/CSS21/cascade.html#specificity>. A base 100 is
used.
=head3 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
=head3 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
=head3 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
=head2 Matching and inlining
=head3 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).
=head3 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.
=head3 add_to
Inlines the style definition into the element.
=for author to fill in:
Write a separate section listing the public components of the modules
interface. These normally consist of either subroutines that may be
exported, or methods that may be called on objects belonging to the
( run in 3.273 seconds using v1.01-cache-2.11-cpan-2398b32b56e )