File Coverage

blib/lib/Rose/HTML/Text.pm
Criterion Covered Total %
statement 31 32 96.8
branch 10 12 83.3
condition n/a
subroutine 14 15 93.3
pod 8 8 100.0
total 63 67 94.0


line stmt bran cond sub pod time code
1             package Rose::HTML::Text;
2              
3 43     43   84658 use strict;
  43         125  
  43         1399  
4              
5 43     43   284 use base 'Rose::HTML::Object';
  43         1256  
  43         24119  
6              
7 43     43   346 use Rose::HTML::Util();
  43         97  
  43         3835  
8              
9             our $VERSION = '0.602';
10              
11             __PACKAGE__->valid_html_attrs([]);
12              
13             use overload
14             (
15 85     85   2689 '""' => sub { shift->html },
16 2636     2636   6663 'bool' => sub { 1 },
17 0     0   0 '0+' => sub { 1 },
18 43         655 fallback => 1,
19 43     43   294 );
  43         109  
20              
21             # XXX: When Class::XSAccessor is installed, the (apparent) combination of
22             # XXX: overload and Rose::Object::MakeMethods::Generic's method creation
23             # XXX: for plain scalar attributes causes things to go awry and tests to
24             # XXX" fail (e.g., t/text.t)
25             # use Rose::Object::MakeMethods::Generic
26             # (
27             # { override_existing => 1 },
28             # scalar =>
29             # [
30             # 'html',
31             # ],
32             # );
33              
34             # XXX: Do it the old-fashioned way (see comments above)
35             sub html
36             {
37 2799     2799 1 20165 my($self) = shift;
38 2799 100       7669 return $self->{'html'} = shift if(@_);
39 1975         9534 return $self->{'html'};
40             }
41              
42 862     862 1 1855 sub html_tag { shift->html(@_) }
43 191     191 1 439 sub xhtml_tag { shift->xhtml(@_) }
44              
45 193     193 1 527 sub xhtml { shift->html(@_) }
46              
47             sub init
48             {
49 610     610 1 1053 my($self) = shift;
50 610 100       1447 @_ = (text => @_) if(@_ == 1);
51 610         1604 $self->SUPER::init(@_);
52             }
53              
54             sub text
55             {
56 408     408 1 1942 my($self) = shift;
57 408         1367 local $^W = 0; # XXX: Using a sledgehammer here due to possible stringification overloading on $_[0]
58 408 100       1829 $self->html(defined $_[0] ? Rose::HTML::Util::escape_html(@_) : undef) if(@_);
    100          
59 408         852 return Rose::HTML::Util::unescape_html($self->html);
60             }
61              
62             sub children
63             {
64 3     3 1 6 my($self) = shift;
65 3 50       9 Carp::croak ref($self), " objects cannot have children()" if(@_ > 1);
66 3 50       22 return wantarray ? () : [];
67             }
68              
69 5     5 1 1021 sub push_children { Carp::croak ref($_[0]), " objects cannot have children()" }
70             *unshift_children = \&push_children;
71              
72             1;
73              
74             __END__
75              
76             =head1 NAME
77              
78             Rose::HTML::Text - Object representation of HTML-escaped text.
79              
80             =head1 SYNOPSIS
81              
82             $text = Rose::HTML::Text->new('I <3 HTML');
83              
84             print $text->html; # I &lt;3 HTML
85              
86             # Stringification is overloaded
87             print "$text" # I &lt;3 HTML
88              
89             ...
90              
91             =head1 DESCRIPTION
92              
93             L<Rose::HTML::Text> is an object representation of and HTML-escaped text string. Stringification is L<overloaded|overload> to call the L<html|/html> method.
94              
95             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.
96              
97             =head1 HTML ATTRIBUTES
98              
99             Valid attributes: E<lt>noneE<gt>
100              
101             =head1 CONSTRUCTOR
102              
103             =over 4
104              
105             =item B<new [ PARAMS | TEXT ]>
106              
107             This behaves like standard L<Rose::HTML::Object> L<constructor|Rose::HTML::Object/new> except that if a lone argument is passed, it is taken as the value of L<text|/text>.
108              
109             =back
110              
111             =head1 OBJECT METHODS
112              
113             =over 4
114              
115             =item B<html [HTML]>
116              
117             Get or set the HTML version of the L<text|/text>.
118              
119             =item B<text [TEXT]>
120              
121             Get or set the text.
122              
123             =item B<xhtml [XHTML]>
124              
125             This is an alias for the L<html|/html> method.
126              
127             =back
128              
129             =head1 AUTHOR
130              
131             John C. Siracusa (siracusa@gmail.com)
132              
133             =head1 LICENSE
134              
135             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.