| 1 | package CSS::DOM::Rule::Media; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 6 | $VERSION = '0.16'; | |||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use warnings; no warnings qw 'utf8 parenthesis'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | 6 | ▶ | use CSS::DOM; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 6 | ▶ | use CSS::DOM::Exception qw/ SYNTAX_ERR INDEX_SIZE_ERR /; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | 6 | ▶ | use CSS::DOM::Rule; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | 6 | our @ISA = 'CSS::DOM::Rule'; | |||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | 6 | use constant 1.03 our $_const = { | |||||||||||||||||||||||||||||||
| 15 | # Don't let this conflict with the superclass. | ||||||||||||||||||||||||||||||||
| 16 | ruls => 2, | ||||||||||||||||||||||||||||||||
| 17 | medi => 3, | ||||||||||||||||||||||||||||||||
| 18 | 6 | ▶ | }; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 19 | 6 | ▶ | { no strict; delete @{__PACKAGE__.'::'}{_const => keys %{our $_const}} } | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||
| 21 | # overrides: | ||||||||||||||||||||||||||||||||
| 22 | 3 | ▶ | sub type { CSS::DOM::Rule::MEDIA_RULE } | ||||||||||||||||||||||||||||||
|
Subroutine
type: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 23 | sub cssText { | ||||||||||||||||||||||||||||||||
| 24 | 39 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
cssText: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||||||
| 25 | 39 | my $old; | |||||||||||||||||||||||||||||||
| 26 | 39 | ▶ | if(defined wantarray) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 27 | 38 | ▶ | $old = '@media ' . ( | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 28 | $self->[medi] ? $self->[medi]->mediaText : '' | ||||||||||||||||||||||||||||||||
| 29 | ) . " {\n"; | ||||||||||||||||||||||||||||||||
| 30 | 38 | ▶ | (my $rules = join '',map $_->cssText, @{$self->[ruls]||[]}) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 31 | =~ s/^(?!$)/\t/gm; | ||||||||||||||||||||||||||||||||
| 32 | 38 | $old .= "$rules}\n"; | |||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||
| 34 | 39 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 35 | 2 | my $new_rule = $self->_parse(shift); | |||||||||||||||||||||||||||||||
| 36 | 1 | @$self[ruls,medi] = @$new_rule[ruls,medi]; | |||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||
| 38 | 38 | $old; | |||||||||||||||||||||||||||||||
| 39 | }; | ||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||
| 42 | # CSSMediaRule interface: | ||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||
| 44 | # These methods are identical to those in CSS::DOM, but we’ve had to copy & | ||||||||||||||||||||||||||||||||
| 45 | # paste them here, because the constants have different values. Even if we | ||||||||||||||||||||||||||||||||
| 46 | # were to change them to have the same values, it would be a maintenance | ||||||||||||||||||||||||||||||||
| 47 | # nightmare. | ||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||
| 49 | sub media { | ||||||||||||||||||||||||||||||||
| 50 | 53 | ▶ | wantarray ? @{$_[0]->[medi]||return} : | ||||||||||||||||||||||||||||||
|
Subroutine
media: called CC=4 SCAR=14.3
Pod
documented
Branch: 2 branches
Condition66%2/3
$_[0][3] ||= (require CSS::DOM::MediaList, "CSS::DOM::MediaList"->new)
MC/DC0%0/2
$_[0][3] ||= (require CSS::DOM::MediaList, "CSS::DOM::MediaList"->new)
$_[0][3]
(require CSS::DOM::MediaList, "CSS::DOM::MediaList"->new)
Truth table66%2/3
A ||= B
A= $_[0][3]B= (require CSS::DOM::MediaList, "CSS::DOM::MediaList"->new)
| |||||||||||||||||||||||||||||||||
| 51 | ($_[0]->[medi] ||= ( | ||||||||||||||||||||||||||||||||
| 52 | require CSS::DOM::MediaList, | ||||||||||||||||||||||||||||||||
| 53 | CSS::DOM::MediaList->new | ||||||||||||||||||||||||||||||||
| 54 | )) | ||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | sub cssRules { | ||||||||||||||||||||||||||||||||
| 58 | wantarray | ||||||||||||||||||||||||||||||||
| 59 | 64 | ▶ | ? @{shift->[ruls]||return} | ||||||||||||||||||||||||||||||
|
Subroutine
cssRules: called CC=4 SCAR=14.3
Pod
documented
Branch: 2 branches
Condition66%2/3
+(shift())->[2] ||= "CSS::DOM::RuleList"->new
MC/DC0%0/2
+(shift())->[2] ||= "CSS::DOM::RuleList"->new
+(shift())->[2]
"CSS::DOM::RuleList"->new
Truth table66%2/3
A ||= B
A= +(shift())->[2]B= "CSS::DOM::RuleList"->new
| |||||||||||||||||||||||||||||||||
| 60 | : (shift->[ruls]||=new CSS::DOM::RuleList); | ||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||
| 63 | sub insertRule { # This is supposed to raise an HIERARCHY_REQUEST_ERR if | ||||||||||||||||||||||||||||||||
| 64 | # the rule cannot be inserted at the specified index; | ||||||||||||||||||||||||||||||||
| 65 | # e.g., if an @import rule is inserted after a stan- | ||||||||||||||||||||||||||||||||
| 66 | # dard rule. But we don’t do that, in order to maintain | ||||||||||||||||||||||||||||||||
| 67 | # future compatibility. | ||||||||||||||||||||||||||||||||
| 68 | 5 | ▶ | my ($self, $rule_string, $index) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
insertRule: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||
| 70 | 5 | require CSS::DOM::Parser; | |||||||||||||||||||||||||||||||
| 71 | 5 | my ($at,$rule); | |||||||||||||||||||||||||||||||
| 72 | { | ||||||||||||||||||||||||||||||||
| 73 | 5 | local *@; | |||||||||||||||||||||||||||||||
| 74 | 5 | $rule = CSS::DOM::Parser::parse_statement($rule_string); | |||||||||||||||||||||||||||||||
| 75 | 5 | $at = $@ | |||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||
| 77 | 5 | ▶ | $at and die new CSS::DOM::Exception SYNTAX_ERR, $at; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||
| 79 | 4 | $rule->_set_parentStyleSheet($self->parentStyleSheet); | |||||||||||||||||||||||||||||||
| 80 | 4 | $rule->_set_parentRule($self); | |||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||
| 82 | 4 | my $list = $self->cssRules; # cssRules takes care of ||= | |||||||||||||||||||||||||||||||
| 83 | 4 | splice @$list, $index, 0, $rule; | |||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||
| 85 | 4 | ▶ | $index < 0 ? $#$list + $index : | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 86 | $index <= $#$list ? $index : | ||||||||||||||||||||||||||||||||
| 87 | $#$list | ||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||
| 90 | sub deleteRule { | ||||||||||||||||||||||||||||||||
| 91 | 2 | ▶ | my ($self,$index) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
deleteRule: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 92 | 2 | my $list = $self->[ruls]; | |||||||||||||||||||||||||||||||
| 93 | 2 | ▶ | $index > $#$list and die CSS::DOM::Exception->new( | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 94 | INDEX_SIZE_ERR, | ||||||||||||||||||||||||||||||||
| 95 | "The index passed to deleteRule ($index) is too large" | ||||||||||||||||||||||||||||||||
| 96 | ); | ||||||||||||||||||||||||||||||||
| 97 | 1 | splice @$list, $index, 1; | |||||||||||||||||||||||||||||||
| 98 | return # nothing; | ||||||||||||||||||||||||||||||||
| 99 | 1 | } | |||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||
| 101 | sub _set_parentStyleSheet { | ||||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
_set_parentStyleSheet: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||
| 103 | 0 | $self->SUPER::_set_parentStyleSheet(@_); | |||||||||||||||||||||||||||||||
| 104 | 0 | $_->_set_parentStyleSheet(@_) for @{$self->[ruls]}; | |||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||
| 107 | 6 | !()__END__()! | |||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||
| 109 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||
| 111 | CSS::DOM::Rule::Media - CSS @media rule class for CSS::DOM | ||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||
| 113 | =head1 VERSION | ||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||
| 115 | Version 0.16 | ||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||
| 117 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||
| 119 | use CSS::DOM; | ||||||||||||||||||||||||||||||||
| 120 | my $media_rule = CSS::DOM->parse( | ||||||||||||||||||||||||||||||||
| 121 | '@media print { body { background: none } }' | ||||||||||||||||||||||||||||||||
| 122 | )->cssRules->[0]; | ||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||
| 124 | # OR: | ||||||||||||||||||||||||||||||||
| 125 | use CSS::DOM::Rule::Media; | ||||||||||||||||||||||||||||||||
| 126 | my $media_rule = new CSS::DOM::Rule::Media $parent; | ||||||||||||||||||||||||||||||||
| 127 | push @{$media_rule->media}, 'print'; | ||||||||||||||||||||||||||||||||
| 128 | $media_rule->insertRule('body { background: none }') | ||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||
| 130 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||
| 132 | This module implements CSS C<@media> rules for L<CSS::DOM>. It inherits | ||||||||||||||||||||||||||||||||
| 133 | from | ||||||||||||||||||||||||||||||||
| 134 | L<CSS::DOM::Rule> and implements | ||||||||||||||||||||||||||||||||
| 135 | the CSSMediaRule DOM interface. | ||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||
| 137 | =head1 METHODS | ||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||
| 139 | =over 4 | ||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||
| 141 | =item media | ||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||
| 143 | Returns the MediaList associated with the @media rule (or a plain list in | ||||||||||||||||||||||||||||||||
| 144 | list context). This defaults to an | ||||||||||||||||||||||||||||||||
| 145 | empty list. You can pass a comma-delimited string to the MediaList's | ||||||||||||||||||||||||||||||||
| 146 | C<mediaText> method to set it. | ||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||
| 148 | =item cssRules | ||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||
| 150 | In scalar context, this returns a L<CSS::DOM::RuleList> object (simply a | ||||||||||||||||||||||||||||||||
| 151 | blessed | ||||||||||||||||||||||||||||||||
| 152 | array reference) of L<CSS::DOM::Rule> objects. In list context it returns a | ||||||||||||||||||||||||||||||||
| 153 | list. | ||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||
| 155 | =item insertRule ( $css_code, $index ) | ||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||
| 157 | Parses the rule contained in the C<$css_code>, inserting it the @media | ||||||||||||||||||||||||||||||||
| 158 | rule's list of subrules at the given C<$index>. | ||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||
| 160 | =item deleteRule ( $index ) | ||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||
| 162 | Deletes the rule at the given C<$index>. | ||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||
| 164 | =back | ||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||
| 166 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||
| 168 | L<CSS::DOM> | ||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||
| 170 | L<CSS::DOM::Rule> | ||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||
| 172 | L<CSS::DOM::MediaList> | ||||||||||||||||||||||||||||||||