File Coverage

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


line stmt bran cond sub pod time code
1 15     15   6920 use strict;
  15         21  
  15         543  
2 15     15   60 use warnings;
  15         18  
  15         612  
3             package HTML::Widget::Plugin::Link;
4             # ABSTRACT: a hyperlink
5             $HTML::Widget::Plugin::Link::VERSION = '0.203';
6 15     15   60 use parent 'HTML::Widget::Plugin';
  15         16  
  15         61  
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   751 use Carp ();
  15         22  
  15         212  
29 15     15   48 use HTML::Element;
  15         20  
  15         69  
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 38 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   47 sub _attribute_args { qw(href title) }
68              
69             sub link { ## no critic Builtin
70 6     6 1 13 my ($self, $factory, $arg) = @_;
71              
72 6 100       29 $arg->{attr}{name} = $arg->{attr}{id} if not defined $arg->{attr}{name};
73              
74 6 100       202 Carp::croak "can't create a link without an href"
75             unless $arg->{attr}{href};
76              
77 5 100 100     143 Carp::croak "text and html arguments for link widget are mutually exclusive"
78             if $arg->{text} and $arg->{html};
79              
80 4         21 my $widget = HTML::Element->new('a');
81 4         127 $widget->attr($_ => $arg->{attr}{$_}) for keys %{ $arg->{attr} };
  4         33  
82              
83 4         117 my $content;
84 4 100       15 if ($arg->{html}) {
85 2 100       13 $content = ref $arg->{html}
86             ? $arg->{html}
87             : HTML::Element->new('~literal' => text => $arg->{html});
88             } else {
89 2 100       9 $content = defined $arg->{text} ? $arg->{text} : $arg->{attr}{href};
90             }
91              
92 4         47 $widget->push_content($content);
93              
94             # We chomp this to avoid significant whitespace. -- rjbs, 2008-09-22
95 4         91 my $xml = $widget->as_XML;
96 4         1472 chomp $xml;
97 4         42 return $xml;
98             }
99              
100             1;
101              
102             __END__