From: Bjoern H. <der...@gm...> - 2003-08-20 05:18:23
|
Hi, The following code is an extension of CSS::SAC to calculate specifity triples from CSS Selectors. The first entry in the returned array reference is the number of ID selectors, the second the number of attribute selectors and pseudo-classes, the third is the number of element type selectors as defined in the CSS specification. Could we maintain this in CVS? Probably as CSS::SAC::Selector::Specifity.pm or something? sub CSS::SAC::Condition::Specifity { my $cond = shift; if ($cond->is_type(NEGATIVE_CONDITION)) { # @@ workaround for a CSS::SAC bug $cond->Condition->isa('CSS::SAC::Selector::Conditional') ? $cond->Condition->Condition->Specifity : $cond->Condition->Specifity } elsif (($cond->is_type(ID_CONDITION))) { [1, 0, 0] } elsif (!$cond->is_type(AND_CONDITION)) { [0, 1, 0] } else # $cond->is_type(AND_CONDITION) { my $x = $cond->FirstCondition->Specifity; my $y = $cond->SecondCondition->Specifity; return unless defined $x and defined $y; return [map { $x->[$_] + $y->[$_] } 0..2] } } sub CSS::SAC::Selector::Specifity { my $item = shift; my $x; my $y; if ($item->is_type(ELEMENT_NODE_SELECTOR)) { return $item->LocalName ? [0, 0, 1] : [0, 0, 0] } elsif ($item->is_type(PSEUDO_ELEMENT_SELECTOR)) { return [0, 0, 1]; } elsif ($item->is_type(CONDITIONAL_SELECTOR)) { $x = $item->SimpleSelector->Specifity; $y = $item->Condition->Specifity; } elsif ($item->is_type(CHILD_SELECTOR) or $item->is_type(DESCENDANT_SELECTOR)) { $x = $item->AncestorSelector->Specifity; $y = $item->SimpleSelector->Specifity; } elsif ($item->is_type(DIRECT_ADJACENT_SELECTOR) or $item->is_type(INDIRECT_ADJACENT_SELECTOR)) { $x = $item->SiblingSelector->Specifity; $y = $item->Selector->Specifity; } else { return } return unless defined $x and defined $y; return [map { $x->[$_] + $y->[$_] } 0..2]; } regards. |