File Coverage

blib/lib/Rose/HTML/Script.pm
Criterion Covered Total %
statement 56 58 96.5
branch 15 18 83.3
condition 4 4 100.0
subroutine 15 17 88.2
pod 7 10 70.0
total 97 107 90.6


line stmt bran cond sub pod time code
1              
2             use strict;
3 2     2   87774  
  2         13  
  2         53  
4             use base 'Rose::HTML::Object';
5 2     2   8  
  2         4  
  2         588  
6             our $VERSION = '0.606';
7              
8             use Rose::Class::MakeMethods::Generic
9             (
10             inheritable_scalar => 'default_support_older_browsers',
11 2         13 );
12 2     2   12  
  2         4  
13             __PACKAGE__->default_support_older_browsers(1);
14              
15             __PACKAGE__->add_valid_html_attrs
16             (
17             'charset', # %Charset; #IMPLIED -- char encoding of linked resource
18             'type', # %ContentType; #REQUIRED -- content type of script language
19             'src', # %URI; #IMPLIED -- URI for an external script
20             'defer', # (defer) #IMPLIED -- UA may defer execution of script
21             );
22              
23             __PACKAGE__->add_required_html_attrs(
24             {
25             type => 'text/javascript',
26             });
27              
28             __PACKAGE__->add_boolean_html_attrs
29             (
30             'defer',
31             );
32              
33              
34 7     7 1 531 {
35 1     1 0 8 my($self) = shift;
36              
37             return $self->{'support_older_browsers'} = $_[0] ? 1 : 0 if(@_);
38              
39 6     6 1 11 unless(defined $self->{'support_older_browsers'})
40             {
41 6 50       15 return $self->{'support_older_browsers'} =
    100          
42             (ref($self))->default_support_older_browsers;
43 4 100       11 }
44              
45 2         9 return $self->{'support_older_browsers'};
46             }
47              
48              
49 2         5 {
50             my($self) = shift;
51             $self->children(@_) if(@_);
52 0     0 1 0 return join('', map { $_->html } $self->children)
53 0     0 1 0 }
54              
55             *contents = \&script;
56              
57 7     7 1 22 {
58 7 100       22 my($self) = shift;
59 7         17  
  7         17  
60             my $contents = $self->contents;
61             return $contents unless($contents =~ /\S/);
62              
63             for($contents) { s/\A\n//; s/\n\Z// }
64              
65             if($self->support_older_browsers)
66 2     2 0 5 {
67             return "<!--//--><![CDATA[//><!--\n$contents\n//--><!]]>";
68 2         4 }
69 2 50       7  
70             return "\n//<![CDATA[\n$contents\n//]]>\n";
71 2         5 }
  2         3  
  2         4  
72              
73 2 100       6 {
74             my($self) = shift;
75 1         18  
76             my $contents = $self->contents;
77             return $contents unless($contents =~ /\S/);
78 1         7  
79             for($contents) { s/\A\n//; s/\n\Z// }
80              
81             return "\n<!--\n$contents\n// -->\n";
82             }
83 2     2 0 6  
84             {
85 2         4 my($self) = shift;
86 2 50       8  
87             if(length($self->src || ''))
88 2         5 {
  2         3  
  2         5  
89             no warnings;
90 2         14 return '<script' . $self->html_attrs_string . '></script>';
91             }
92              
93             no warnings;
94             return '<script' . $self->html_attrs_string . '>' .
95 3     3 1 6 $self->html_contents_escaped .
96             '</script>';
97 3 100 100     8 }
98              
99 2     2   1300 {
  2         5  
  2         107  
100 1         3 my($self) = shift;
101              
102             if(length($self->src || ''))
103 2     2   13 {
  2         3  
  2         196  
104 2         8 no warnings;
105             return '<script' . $self->xhtml_attrs_string . ' />';
106             }
107              
108             no warnings;
109             return '<script' . $self->xhtml_attrs_string . '>' .
110             $self->xhtml_contents_escaped .
111 3     3 1 5 '</script>';
112             }
113 3 100 100     6  
114             1;
115 2     2   11  
  2         4  
  2         92  
116 1         4  
117             =head1 NAME
118              
119 2     2   11 Rose::HTML::Script - Object representation of the "script" HTML tag.
  2         4  
  2         146  
120 2         7  
121             =head1 SYNOPSIS
122              
123             $script = Rose::HTML::Script->new(src => '/main.js');
124              
125             print $script->html;
126              
127             $script =
128             Rose::HTML::Script->new(
129             script => 'function addThese(a, b) { return a + b }');
130              
131             print $script->html;
132              
133             ...
134              
135             =head1 DESCRIPTION
136              
137             L<Rose::HTML::Script> is an object representation of a "script" HTML tag used to reference or wrap scripts (e.g., JavaScript).
138              
139             This class inherits from, and follows the conventions of, L<Rose::HTML::Object>. Inherited methods that are not overridden will not be documented a second time here. See the L<Rose::HTML::Object> documentation for more information.
140              
141             =head1 HTML ATTRIBUTES
142              
143             Valid attributes:
144              
145             charset
146             class
147             defer
148             dir
149             id
150             lang
151             onclick
152             ondblclick
153             onkeydown
154             onkeypress
155             onkeyup
156             onmousedown
157             onmousemove
158             onmouseout
159             onmouseover
160             onmouseup
161             src
162             style
163             title
164             type
165             xml:lang
166              
167             Required attributes (default values in parentheses):
168              
169             type (text/javascript)
170              
171             Boolean attributes:
172              
173             defer
174              
175             =head1 CONSTRUCTOR
176              
177             =over 4
178              
179             =item B<new PARAMS>
180              
181             Constructs a new L<Rose::HTML::Script> object based on PARAMS, where PARAMS are name/value pairs. Any object method is a valid parameter name.
182              
183             =back
184              
185             =head1 CLASS METHODS
186              
187             =over 4
188              
189             =item B<default_support_older_browsers [BOOL]>
190              
191             Get or set a boolean value that indicates whether or not the L<XHTML|Rose::HTML::Object/xhtml> produced by objects of this class will, by default, attempt to support older web browsers that have trouble parsing the comments used to wrap script contents. The default value is true. See the L<support_older_browsers|/support_older_browsers> object method for some examples.
192              
193             =back
194              
195             =head1 OBJECT METHODS
196              
197             =over 4
198              
199             =item B<contents [TEXT]>
200              
201             Get or set the contents of the script tag.
202              
203             =item B<script [TEXT]>
204              
205             This is an alias for the L<contents|/contents> method.
206              
207             =item B<src [URI]>
208              
209             Get or set the URI of the script file. If this attribute is set, then the L<contents|/contents> of of the script tag are ignored when it comes time to produce the L<HTML|Rose::HTML::Object/html>.
210              
211             =item B<support_older_browsers [BOOL]>
212              
213             Get or set a boolean value that indicates whether or not the L<XHTML|Rose::HTML::Object/xhtml> produced by this object will attempt to support older web browsers that have trouble parsing the comments used to wrap script contents. If undefined, the value of this attribute is set to the return value of the L<default_support_older_browsers|/default_support_older_browsers> class method.
214              
215             Examples:
216              
217             $script =
218             Rose::HTML::Script->new(script => 'function foo() { return 123; }');
219              
220             print $script->xhtml;
221              
222             This prints the following big mess which helps older browsers while also remaining valid XHTML.
223              
224             <script type="text/javascript"><!--//--><![CDATA[//><!--
225             function foo() { return 123; }
226             //--><!]]></script>
227              
228             Now the other mode:
229              
230             $script->support_older_browsers(0);
231             print $script->xhtml;
232              
233             which prints:
234              
235             <script type="text/javascript">
236             //<![CDATA[
237             function foo() { return 123; }
238             //]]>
239             </script>
240              
241             See L<http://hixie.ch/advocacy/xhtml> for more information on this topic.
242              
243             =back
244              
245             =head1 AUTHOR
246              
247             John C. Siracusa (siracusa@gmail.com)
248              
249             =head1 LICENSE
250              
251             Copyright (c) 2010 by John C. Siracusa. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.