blib/lib/Embperl/Form/Control/textarea.pm untested

filter: Statementstmt 0.0% 0 / 110 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 25 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 203 CC 12 SCAR 22.6
CC
12
Cov
0%
get_display_text
20.0
init_data
20.0
prepare_fdat
12.0
CRAP 9.6
1  
2 ###################################################################################
3 #
4 # Embperl - Copyright (c) 1997-2008 Gerald Richter / ecos gmbh www.ecos.de
5 # Embperl - Copyright (c) 2008-2015 Gerald Richter
6 # Embperl - Copyright (c) 2015-2023 actevy.io
7 #
8 # You may distribute under the terms of either the GNU General Public
9 # License or the Artistic License, as specified in the Perl README file.
10 #
11 # THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
12 # IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
13 # WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
14 #
15 # $Id$
16 #
17 ###################################################################################
18  
19 package Embperl::Form::Control::textarea ;
20  
21 use strict ;
22 use base 'Embperl::Form::Control' ;
23  
24 use Embperl::Inline ;
25  
26 use vars qw{%fdat} ;
27  
28 use constant ALLOWED_HTML_TAGS => {map { $_ => 1 } qw{p br img ol ul td th tr td div h1 h2 h3 h4 h5 h6 pre strong em u s sub sup li blockquote caption table tbody tfoot thead hr span big small tt code kbd samp var del ins cite address } };
29  
30  
31 # ---------------------------------------------------------------------------
32 #
33 # init - init the new control
34 #
35  
36 sub init
37  
38 {
39 my ($self) = @_ ;
40  
41 return $self ;
42 }
43  
44 # ------------------------------------------------------------------------------------------
45 #
46 # _filter_html - remove all not allowed html tags
47 #
48  
49 sub _filter_html
50 {
51 my ($self, $dataref) = @_ ;
52  
53 $$dataref =~ s/(<\/?(\w+).*?>)/ALLOWED_HTML_TAGS->{$2}?$1:''/ge ;
54 }
55  
56 # ------------------------------------------------------------------------------------------
57 #
58 # _text2html - convert plain text to html
59 #
60  
61 sub _text2html
62 {
63 my ($self, $dataref) = @_ ;
64  
65
66 my $is_html = $self -> {format} eq 'html' && ($$dataref =~ /^<[-a-zA-Z0-9 "'=:;,]+?>/) ;
67 return if ($is_html) ;
68  
69 my @text = split (/\n/, $$dataref) ;
70
71 $$dataref = '<p>' . join ("<br>\n", @text) . "</p>\n" ;
72 }
73  
74
75 # ------------------------------------------------------------------------------------------
76 #
77 # _text2pre - convert plain text to html pre
78 #
79  
80 sub _text2pre
81 {
82 my ($self, $dataref) = @_ ;
83  
84
85 my $is_html = $self -> {format} eq 'html' && ($$dataref =~ /^<[-a-zA-Z0-9 "'=:;,]+?>/) ;
86 return if ($is_html) ;
87  
88 $$dataref =~ s/<\/pre>/<_pre>/g ;
89 $$dataref = '<pre>' . $$dataref . "</pre>\n" ;
90 }
91  
92
93 # ------------------------------------------------------------------------------------------
94 #
95 # _html2text - convert html to plain text
96 #
97  
98 sub _html2text
99 {
100 my ($self, $dataref) = @_ ;
101  
102 return if ($self -> {format} ne 'html') ;
103
104 use utf8 ;
105 $$dataref =~ s/<.+?>/ /g ;
106 $$dataref =~ s/&auml;/ä/g ;
107 $$dataref =~ s/&ouml;/ö/g ;
108 $$dataref =~ s/&uuml;/ü/g ;
109 $$dataref =~ s/&Auml;/Ä/g ;
110 $$dataref =~ s/&Ouml;/Ö/g ;
111 $$dataref =~ s/&Uuml;/Ü/g ;
112 $$dataref =~ s/&szlig;/ß/g ;
113 $$dataref =~ s/&gt;/>/g ;
114 $$dataref =~ s/&lt;/</g ;
115 $$dataref =~ s/&quot;/"/g ;
116 $$dataref =~ s/&apos;/'/g ;
117 $$dataref =~ s/&#39;/'/g ;
118 $$dataref =~ s/&amp;/&/g ;
119 $$dataref =~ s/&nbsp;/ /g ;
120 }
121  
122 # ------------------------------------------------------------------------------------------
123 #
124 # get_display_text - returns the text that should be displayed
125 #
126  
127 sub get_display_text
128 {
129 my ($self, $req, $value, $compact) = @_ ;
130
131 $value = $self -> get_value ($req) if (!defined ($value)) ;
132 return $value if ($self -> {format} ne 'html') ;
133  
134 if ($compact)
135 {
136 $self -> _html2text (\$value) ;
137 }
138 else
139 {
140 $self -> _filter_html (\$value) ;
141 $self -> _text2html (\$value) ;
142 }
143
144 return $value ;
145 }
146  
147  
148
149 # ------------------------------------------------------------------------------------------
150 #
151 # init_data - daten aufteilen
152 #
153  
154 sub init_data
155 {
156 my ($self, $req, $parentctrl, $force) = @_ ;
157  
158  
159 my $fdat = $req -> {docdata} || \%fdat ;
160 my $name = $self->{name} ;
161 return if (!exists $fdat->{$name} || $req -> {"ef_textarea_init_done_$name"}) ;
162  
163 if ($self -> {format} ne 'html')
164 {
165 if ($self -> is_readonly ($req))
166 {
167 $self -> _text2pre (\$fdat->{$name}) ;
168 }
169 }
170 else
171 {
172 $self -> _filter_html (\$fdat->{$name}) ;
173 $self -> _text2html (\$fdat->{$name}) ;
174 }
175
176 $req -> {"ef_textarea_init_done_$name"} = 1 ;
177 return ;
178 }
179  
180 # ---------------------------------------------------------------------------
181 #
182 # init_markup - add any dynamic markup to the form data
183 #
184  
185 sub init_markup
186  
187 {
188 my ($self, $req, $parentctl, $method) = @_ ;
189  
190 return $self -> init_data ($req, $parentctl) ;
191 }
192  
193 # ------------------------------------------------------------------------------------------
194 #
195 # prepare_fdat - daten zusammenfuehren
196 #
197  
198 sub prepare_fdat
199 {
200 my ($self, $req) = @_ ;
201  
202 my $fdat = $req -> {form} || \%Embperl::fdat ;
203 my $name = $self->{name} ;
204 return if (!exists $fdat->{$name}) ;
205  
206 if ($self -> {format} ne 'html')
207 {
208 return ;
209 }
210  
211 $self -> _filter_html (\$fdat->{$name}) ;
212 $self -> _text2html (\$fdat->{$name}) ;
213  
214 return ;
215 }
216  
217 1 ;
218  
219 __EMBPERL__
220
221 [# ---------------------------------------------------------------------------
222 #
223 # show - output the control
224 #]
225  
226 [$ sub show ($self, $req)
227 $]
228  
229 [$if !$self -> {fullwidth} || $self -> is_readonly ($req) $]
230 [- $self -> SUPER::show ($req) -]
231 [$else$]
232  
233 [#
234 <table class="ef-element ef-element-width-[+ $self -> {width_percent} +] ef-element-[+ $self -> {type} +] [+ $self -> {state} +]" style="width: 348px">
235 #]
236 <table class="ef-element ef-element-width-[+ $self -> {width_percent} +] ef-element-[+ $self -> {type} +] [+ $self -> {state} +]">
237 <tr>
238 <td class="ef-label-box ef-label-box-width-full [$ if $self->{labelclass} $][+ " $self->{labelclass}" +][$ endif $]" _ef_attr="[+ $self -> {name} +]">
239 [-
240 $fdat{$name} = $self -> {default} if ($fdat{$name} eq '' && exists ($self -> {default})) ;
241 my $span = 0 ;
242 $self -> show_label ($req);
243 -]
244 </td>
245 </tr>
246 <tr>
247 <td class="ef-control-box ef-control-box-width-full">
248 [-
249 local $self -> {width_percent} = 'full' ;
250 $self -> show_control ($req)
251 -]
252 </td>
253 </tr>
254 </table>
255 [$endif$]
256 [$endsub$]
257
258 [# ---------------------------------------------------------------------------
259 #
260 # show_control - output the control
261 #]
262  
263 [$ sub show_control ($self, $req)
264 my $class = $self -> {class} ||= '' ;
265 my ($attrs, $ctrlid, $name) = $self -> get_std_control_attr($req) ;
266 my $ro = $self ->{no_edit} ? 'readOnly="1"' : '' ;
267 $]
268  
269 <textarea [+ $ro +] type="text" name="[+ $self -> {force_name} || $self -> {name} +]" [+ do { local $escmode = 0 ; $attrs} +]
270 [# [$if $self -> {cols} $]cols="[+ $self->{cols} +]"[$endif$] #]
271 [$if $self -> {rows} $]rows="[+ $self->{rows} +]"[$endif$]
272 [$if $self -> {format} eq 'html' $]_ef_attach="ef_ckeditor"[$endif$]
273 ></textarea>
274 [* return $ctrlid ; *]
275 [$endsub$]
276  
277  
278 [# ---------------------------------------------------------------------------
279 #
280 # show_control_readonly - output the control as readonly
281 #]
282  
283 [$ sub show_control_readonly ($self, $req, $value, $suffix)
284  
285 my $text = $self -> get_display_text ($req, $value) ;
286 $text =~ s/\s*$// ;
287 $text =~ s/^\s*// ;
288 my $name = $self -> {force_name} || $self -> {name} ;
289 my $is_html = $self -> {format} eq 'html' && ($text =~ /^<[-a-zA-Z0-9 "'=:;,]+?>/) ;
290 my @text = $is_html?($text):split (/\n/, $text) ;
291 $]
292 <div [+ do { local $escmode = 0 ; $self -> get_std_control_attr($req, '', 'readonly') } +] _ef_divname="[+ $name +]">
293 [$ foreach my $val (@text) $]
294 [$ if ($val =~ /^\s*$/) $]<br>[$else$]
295 [$if $is_html $][+ do { local $escmode = 0 ; $val } +][$else$][+ $val +]<br>[$endif$]
296 [$endif$]
297 [$endforeach$]
298 </div>
299 [$ if $self->{hidden} $]
300 <input type="hidden" name="[+ $name +]" value="[+ $value +]">
301 [$endif$]
302 [$endsub$]
303  
304 __END__