Rose::HTML::Text - Object representation of HTML-escaped text.


Rose-HTML-Objects documentation Contained in the Rose-HTML-Objects distribution.

Index


Code Index:

NAME

Top

Rose::HTML::Text - Object representation of HTML-escaped text.

SYNOPSIS

Top

    $text = Rose::HTML::Text->new('I <3 HTML');

    print $text->html;  # I &lt;3 HTML

    # Stringification is overloaded    
    print "$text" # I &lt;3 HTML

    ...

DESCRIPTION

Top

Rose::HTML::Text is an object representation of and HTML-escaped text string. Stringification is overloaded to call the html method.

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

HTML ATTRIBUTES

Top

Valid attributes: <none>

CONSTRUCTOR

Top

new [ PARAMS | TEXT ]

This behaves like standard Rose::HTML::Object constructor except that if a lone argument is passed, it is taken as the value of text.

OBJECT METHODS

Top

html [HTML]

Get or set the HTML version of the text.

text [TEXT]

Get or set the text.

xhtml [XHTML]

This is an alias for the html method.

AUTHOR

Top

John C. Siracusa (siracusa@gmail.com)

LICENSE

Top

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.


Rose-HTML-Objects documentation Contained in the Rose-HTML-Objects distribution.

package Rose::HTML::Text;

use strict;

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

use Rose::HTML::Util();

our $VERSION = '0.602';

__PACKAGE__->valid_html_attrs([]);

use overload
(
  '""'   => sub { shift->html },
  'bool' => sub { 1 },
  '0+'   => sub { 1 },
   fallback => 1,
);

# XXX: When Class::XSAccessor is installed, the (apparent) combination of
# XXX: overload and Rose::Object::MakeMethods::Generic's method creation
# XXX: for plain scalar attributes causes things to go awry and tests to 
# XXX" fail (e.g., t/text.t)
# use Rose::Object::MakeMethods::Generic
# (
#   { override_existing => 1 },
#   scalar =>
#   [
#     'html',
#   ],
# );

# XXX: Do it the old-fashioned way (see comments above)
sub html
{
  my($self) = shift;
  return $self->{'html'} = shift  if(@_);
  return $self->{'html'};
}

sub html_tag  { shift->html(@_) }
sub xhtml_tag { shift->xhtml(@_) }

sub xhtml { shift->html(@_) }

sub init
{
  my($self) = shift;
  @_ = (text => @_)  if(@_ == 1);
  $self->SUPER::init(@_);
}

sub text
{
  my($self) = shift;
  local $^W = 0; # XXX: Using a sledgehammer here due to possible stringification overloading on $_[0]
  $self->html(defined $_[0] ? Rose::HTML::Util::escape_html(@_) : undef)  if(@_);
  return Rose::HTML::Util::unescape_html($self->html);
}

sub children
{
  my($self) = shift;
  Carp::croak ref($self), " objects cannot have children()"  if(@_ > 1);
  return wantarray ? () : [];
}

sub push_children { Carp::croak ref($_[0]), " objects cannot have children()" }
*unshift_children = \&push_children;

1;

__END__