File Coverage

blib/lib/HTML/Widget/Plugin/Link.pm
Criterion Covered Total %
statement 32 32 100.0
branch 12 12 100.0
condition 2 3 66.6
subroutine 8 8 100.0
pod 2 2 100.0
total 56 57 98.2


line stmt bran cond sub pod time code
1 15     15   9349 use strict;
  15         32  
  15         364  
2 15     15   70 use warnings;
  15         77  
  15         702  
3             package HTML::Widget::Plugin::Link;
4             # ABSTRACT: a hyperlink
5             $HTML::Widget::Plugin::Link::VERSION = '0.204';
6 15     15   69 use parent 'HTML::Widget::Plugin';
  15         26  
  15         73  
7              
8             #pod =head1 SYNOPSIS
9             #pod
10             #pod $widget_factory->link({
11             #pod text => "my favorite D&D pages",
12             #pod href => 'http://rjbs.manxome.org/rubric/entries/tags/dnd',
13             #pod });
14             #pod
15             #pod ...or...
16             #pod
17             #pod $widget_factory->link({
18             #pod html => "some great d&d pages",
19             #pod href => 'http://rjbs.manxome.org/rubric/entries/tags/dnd',
20             #pod });
21             #pod
22             #pod =head1 DESCRIPTION
23             #pod
24             #pod This plugin provides a basic input widget.
25             #pod
26             #pod =cut
27              
28 15     15   813 use Carp ();
  15         35  
  15         269  
29 15     15   74 use HTML::Element;
  15         28  
  15         86  
30              
31             #pod =head1 METHODS
32             #pod
33             #pod =head2 C< provided_widgets >
34             #pod
35             #pod This plugin provides the following widgets: link
36             #pod
37             #pod =cut
38              
39 16     16 1 56 sub provided_widgets { qw(link) }
40              
41             #pod =head2 C< link >
42             #pod
43             #pod This method returns a basic text hyperlink.
44             #pod
45             #pod In addition to the generic L attributes, the following
46             #pod are valid arguments:
47             #pod
48             #pod =over
49             #pod
50             #pod =item href
51             #pod
52             #pod This is the URI to which the link ... um ... links. If no href is supplied, an
53             #pod exception is thrown.
54             #pod
55             #pod =item html
56             #pod
57             #pod =item text
58             #pod
59             #pod Either of these may contain the text of created link. If passed as C, it
60             #pod is not escaped; if passed as C, it is. If no text is supplied, the href
61             #pod is used. If both options are provided, an exception is thrown.
62             #pod
63             #pod =back
64             #pod
65             #pod =cut
66              
67 16     16   64 sub _attribute_args { qw(href title) }
68              
69             sub link { ## no critic Builtin
70 6     6 1 7 my ($self, $factory, $arg) = @_;
71              
72 6 100       26 $arg->{attr}{name} = $arg->{attr}{id} if not defined $arg->{attr}{name};
73              
74             Carp::croak "can't create a link without an href"
75 6 100       191 unless $arg->{attr}{href};
76              
77             Carp::croak "text and html arguments for link widget are mutually exclusive"
78 5 100 66     118 if $arg->{text} and $arg->{html};
79              
80 4         17 my $widget = HTML::Element->new('a');
81 4         94 $widget->attr($_ => $arg->{attr}{$_}) for keys %{ $arg->{attr} };
  4         26  
82              
83 4         123 my $content;
84 4 100       9 if ($arg->{html}) {
85             $content = ref $arg->{html}
86             ? $arg->{html}
87 2 100       9 : HTML::Element->new('~literal' => text => $arg->{html});
88             } else {
89 2 100       7 $content = defined $arg->{text} ? $arg->{text} : $arg->{attr}{href};
90             }
91              
92 4         39 $widget->push_content($content);
93              
94             # We chomp this to avoid significant whitespace. -- rjbs, 2008-09-22
95 4         70 my $xml = $widget->as_XML;
96 4         1227 chomp $xml;
97 4         32 return $xml;
98             }
99              
100             1;
101              
102             __END__