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