<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Rose::HTML::Script;

use strict;

use base 'Rose::HTML::Object';

our $VERSION = '0.606';

use Rose::Class::MakeMethods::Generic
(
  inheritable_scalar =&gt; 'default_support_older_browsers',
);

__PACKAGE__-&gt;default_support_older_browsers(1);

__PACKAGE__-&gt;add_valid_html_attrs
(
  'charset',  # %Charset;      #IMPLIED  -- char encoding of linked resource
  'type',     # %ContentType;  #REQUIRED -- content type of script language
  'src',      # %URI;          #IMPLIED  -- URI for an external script
  'defer',    # (defer)        #IMPLIED  -- UA may defer execution of script
);

__PACKAGE__-&gt;add_required_html_attrs(
{
  type  =&gt; 'text/javascript',
});

__PACKAGE__-&gt;add_boolean_html_attrs
(
  'defer',
);

sub src  { shift-&gt;html_attr('src', @_) }
sub type { shift-&gt;html_attr('type', @_) }

sub support_older_browsers
{
  my($self) = shift;

  return $self-&gt;{'support_older_browsers'} = $_[0] ? 1 : 0  if(@_);

  unless(defined $self-&gt;{'support_older_browsers'})
  {
    return $self-&gt;{'support_older_browsers'} = 
      (ref($self))-&gt;default_support_older_browsers;
  }

  return $self-&gt;{'support_older_browsers'};
}

sub html_element  { 'script' }
sub xhtml_element { 'script' }

sub script
{
  my($self) = shift; 
  $self-&gt;children(@_)  if(@_);
  return join('', map { $_-&gt;html } $self-&gt;children)
}

*contents = \&amp;script;

sub xhtml_contents_escaped
{
  my($self) = shift;

  my $contents = $self-&gt;contents;
  return $contents  unless($contents =~ /\S/);

  for($contents) { s/\A\n//; s/\n\Z// }

  if($self-&gt;support_older_browsers)
  {
    return "&lt;!--//--&gt;&lt;![CDATA[//&gt;&lt;!--\n$contents\n//--&gt;&lt;!]]&gt;";
  }

  return "\n//&lt;![CDATA[\n$contents\n//]]&gt;\n";
}

sub html_contents_escaped
{
  my($self) = shift;

  my $contents = $self-&gt;contents;
  return $contents  unless($contents =~ /\S/);

  for($contents) { s/\A\n//; s/\n\Z// }

  return "\n&lt;!--\n$contents\n// --&gt;\n";
}

sub html_tag
{
  my($self) = shift;

  if(length($self-&gt;src || ''))
  {
    no warnings;
    return '&lt;script' . $self-&gt;html_attrs_string . '&gt;&lt;/script&gt;';
  }

  no warnings;
  return '&lt;script' . $self-&gt;html_attrs_string . '&gt;' .
         $self-&gt;html_contents_escaped .
         '&lt;/script&gt;';
}

sub xhtml_tag
{
  my($self) = shift;

  if(length($self-&gt;src || ''))
  {
    no warnings;
    return '&lt;script' . $self-&gt;xhtml_attrs_string . ' /&gt;';
  }

  no warnings;
  return '&lt;script' . $self-&gt;xhtml_attrs_string . '&gt;' .
         $self-&gt;xhtml_contents_escaped .
         '&lt;/script&gt;';
}

1;

__END__

=head1 NAME

Rose::HTML::Script - Object representation of the "script" HTML tag.

=head1 SYNOPSIS

    $script = Rose::HTML::Script-&gt;new(src =&gt; '/main.js');

    print $script-&gt;html;

    $script = 
      Rose::HTML::Script-&gt;new(
        script =&gt; 'function addThese(a, b) { return a + b }');

    print $script-&gt;html;

    ...

=head1 DESCRIPTION

L&lt;Rose::HTML::Script&gt; is an object representation of a "script" HTML tag used to reference or wrap scripts (e.g., JavaScript).

This class inherits from, and follows the conventions of, L&lt;Rose::HTML::Object&gt;. Inherited methods that are not overridden will not be documented a second time here.  See the L&lt;Rose::HTML::Object&gt; documentation for more information.

=head1 HTML ATTRIBUTES

Valid attributes:

    charset
    class
    defer
    dir
    id
    lang
    onclick
    ondblclick
    onkeydown
    onkeypress
    onkeyup
    onmousedown
    onmousemove
    onmouseout
    onmouseover
    onmouseup
    src
    style
    title
    type
    xml:lang

Required attributes (default values in parentheses):

    type (text/javascript)

Boolean attributes:

    defer

=head1 CONSTRUCTOR

=over 4

=item B&lt;new PARAMS&gt;

Constructs a new L&lt;Rose::HTML::Script&gt; object based on PARAMS, where PARAMS are name/value pairs.  Any object method is a valid parameter name.

=back

=head1 CLASS METHODS

=over 4

=item B&lt;default_support_older_browsers [BOOL]&gt;

Get or set a boolean value that indicates whether or not the L&lt;XHTML|Rose::HTML::Object/xhtml&gt; 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&lt;support_older_browsers|/support_older_browsers&gt; object method for some examples.

=back

=head1 OBJECT METHODS

=over 4

=item B&lt;contents [TEXT]&gt;

Get or set the contents of the script tag.

=item B&lt;script [TEXT]&gt;

This is an alias for the L&lt;contents|/contents&gt; method.

=item B&lt;src [URI]&gt;

Get or set the URI of the script file.  If this attribute is set, then the L&lt;contents|/contents&gt; of of the script tag are ignored when it comes time to produce the L&lt;HTML|Rose::HTML::Object/html&gt;.

=item B&lt;support_older_browsers [BOOL]&gt;

Get or set a boolean value that indicates whether or not the L&lt;XHTML|Rose::HTML::Object/xhtml&gt; 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&lt;default_support_older_browsers|/default_support_older_browsers&gt; class method.

Examples:

    $script = 
      Rose::HTML::Script-&gt;new(script =&gt; 'function foo() { return 123; }');

    print $script-&gt;xhtml;

This prints the following big mess which helps older browsers while also remaining valid XHTML.

    &lt;script type="text/javascript"&gt;&lt;!--//--&gt;&lt;![CDATA[//&gt;&lt;!--
    function foo() { return 123; }
    //--&gt;&lt;!]]&gt;&lt;/script&gt;

Now the other mode:

    $script-&gt;support_older_browsers(0);
    print $script-&gt;xhtml;

which prints:

    &lt;script type="text/javascript"&gt;
    //&lt;![CDATA[
    function foo() { return 123; }
    //]]&gt;
    &lt;/script&gt;

See L&lt;http://hixie.ch/advocacy/xhtml&gt; for more information on this topic.

=back

=head1 AUTHOR

John C. Siracusa (siracusa@gmail.com)

=head1 LICENSE

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.
</pre></body></html>