File Coverage

blib/lib/CSS/DOM/MediaList.pm
Criterion Covered Total %
statement 19 19 100.0
branch 6 6 100.0
condition n/a
subroutine 5 5 100.0
pod 3 3 100.0
total 33 33 100.0


line stmt bran cond sub pod time code
1             package CSS::DOM::MediaList;
2              
3             $VERSION = '0.15';
4              
5             require CSS::DOM::Array;
6             @ISA = 'CSS::DOM::Array';
7              
8 9     9   2019 use CSS::DOM::Exception 'NOT_FOUND_ERR';
  9         22  
  9         1466  
9              
10             sub mediaText {
11 47     47 1 85 my $list = shift;
12 47 100       217 my $old = join ', ', @$list unless not defined wantarray;
13 47 100       146 if (@_) {
14             # This parser is based on the description in the HTML spec.
15 8     8   6885 @$list = map /^\p{IsSpacePerl}*([A-Za-z0-9-]*)/,
  8         74  
  8         375  
  4         52  
16             split /,/, shift;
17             }
18             $old
19 47         210 }
20              
21             sub deleteMedium {
22 2     2 1 4 my ($list, $medium) = @_;
23 2         5 my $length = @$list;
24 2         9 @$list = grep $_ ne $medium, @$list;
25 2 100       20 @$list == $length and die CSS::DOM::Exception->new(
26             NOT_FOUND_ERR,
27             qq'The medium "$medium" cannot be found in the list'
28             );
29             return # nothing;
30 1         7 }
31              
32             sub appendMedium { # ~~~ If someone passes ‘foo>>@#’ as an argument, are we
33             # supposed to truncate it or throw an exception? The
34             # DOM Style spec. is vague and refers to the ‘under-
35             # lying style language’. The HTML spec. has a sec-
36             # tion on parsing of entire lists. Does that apply
37             # to this, or do I have to hunt through the CSS
38             # spec. to find that which I seek?
39 2     2 1 272 my ($list ,$medium) = @_;
40 2         8 @$list = (grep($_ ne $medium, @$list), $medium);
41             return # nothing;
42 2         8 }
43              
44             !()__END__()!
45              
46             =head1 NAME
47              
48             CSS::DOM::MediaList - Medium list class for CSS::DOM
49              
50             =head1 VERSION
51              
52             Version 0.15
53              
54             =head1 SYNOPSIS
55              
56             use CSS::DOM;
57             $media_list = new CSS::DOM ->media;
58              
59             use CSS::DOM::MediaList;
60             $media_list = new MediaList 'screen', 'papyrus';
61              
62             @media = @$media_list; # use as array
63             $media_list->mediaText; # returns a string
64             $media_list->mediaText('print, screen'); # change it
65             $media_list->deleteMedium('print');
66             $media_list->appendMedium('tv');
67              
68             =head1 DESCRIPTION
69              
70             This module implements medium lists for L<CSS::DOM>. It implements the
71             CSSMediaList DOM interface and inherits from L<CSS::DOM::Array>.
72              
73             =head1 METHODS
74              
75             =head2 DOM Attributes
76              
77             =over 4
78              
79             =item mediaText
80              
81             A comma-and-space-separated string of the individual media in the list,
82             e.g., 'screen, print'.
83              
84             =back
85              
86             =head2 DOM Methods
87              
88             =over 4
89              
90             =item deleteMedium ( $name )
91              
92             Deletes the named medium from the list, or throws an error if it cannot
93             be found.
94              
95             =item appendMedium ( $name )
96              
97             Adds a medium to the end of the list.
98              
99             =back
100              
101             =head1 SEE ALSO
102              
103             L<CSS::DOM>
104              
105             L<CSS::DOM::Array>