Debian Codegraph @

src/.svn/text-base/forms.pl.svn-base

raw
1 #BSD Copyright (c) 2009, Jeff Forristal (wiretrip.net)
2 #BSD All rights reserved.
3 #BSD
4 #BSD Redistribution and use in source and binary forms, with or without
5 #BSD modification, are permitted provided that the following conditions
6 #BSD are met:
7 #BSD
8 #BSD - Redistributions of source code must retain the above copyright
9 #BSD notice, this list of conditions and the following disclaimer.
10 #BSD
11 #BSD - Redistributions in binary form must reproduce the above copyright
12 #BSD notice, this list of conditions and the following disclaimer in the
13 #BSD documentation and/or other materials provided with the distribution.
14 #BSD
15 #BSD THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 #BSD "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 #BSD LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 #BSD FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 #BSD COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 #BSD INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 #BSD BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 #BSD LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 #BSD CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 #BSD LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 #BSD ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 #BSD POSSIBILITY OF SUCH DAMAGE.
27
28 =item B<FORMS FUNCTIONS>
29
30 The goal is to parse the variable, human-readable HTML into concrete
31 structures useable by your program. The forms functions does do a good job
32 at making these structures, but I will admit: they are not exactly simple,
33 and thus not a cinch to work with. But then again, representing something
34 as complex as a HTML form is not a simple thing either. I think the
35 results are acceptable for what's trying to be done. Anyways...
36
37 Forms are stored in perl hashes, with elements in the following format:
38
39 $form{'element_name'}=@([ 'type', 'value', @params ])
40
41 Thus every element in the hash is an array of anonymous arrays. The first
42 array value contains the element type (which is 'select', 'textarea',
43 'button', or an 'input' value of the form 'input-text', 'input-hidden',
44 'input-radio', etc).
45
46 The second value is the value, if applicable (it could be undef if no
47 value was specified). Note that select elements will always have an undef
48 value--the actual values are in the subsequent options elements.
49
50 The third value, if defined, is an anonymous array of additional tag
51 parameters found in the element (like 'onchange="blah"', 'size="20"',
52 'maxlength="40"', 'selected', etc).
53
54 The array does contain one special element, which is stored in the hash
55 under a NULL character ("\0") key. This element is of the format:
56
57 $form{"\0"}=['name', 'method', 'action', @parameters];
58
59 The element is an anonymous array that contains strings of the form's
60 name, method, and action (values can be undef), and a @parameters array
61 similar to that found in normal elements (above).
62
63 Accessing individual values stored in the form hash becomes a test of your
64 perl referencing skills. Hint: to access the 'value' of the third element
65 named 'choices', you would need to do:
66
67 $form{'choices'}->[2]->[1];
68
69 The '[2]' is the third element (normal array starts with 0), and the
70 actual value is '[1]' (the type is '[0]', and the parameter array is
71 '[2]').
72
73 =cut
74
75 ################################################################
76
77 # Cluster global variables
78 %_forms_ELEMENTS = (
79 'form' => 1,
80 'input' => 1,
81 'textarea' => 1,
82 'button' => 1,
83 'select' => 1,
84 'option' => 1,
85 '/select' => 1
86 );
87
88 ################################################################
89
90 =item B<forms_read>
91
92 Params: \$html_data
93
94 Return: \@found_forms
95
96 This function parses the given $html_data into libwhisker form hashes.
97 It returns a reference to an array of hash references to the found
98 forms.
99
100 =cut
101
102 sub forms_read {
103 my $dr = shift;
104 return undef if ( !ref($dr) || length($$dr) == 0 );
105
106 my $A = [ {}, [] ];
107
108 html_find_tags( $dr, \&_forms_parse_callback, 0, $A, \%_forms_ELEMENTS );
109
110 if ( scalar %{ $A->[0] } ) {
111 push( @{ $A->[1] }, $A->[0] );
112 }
113
114 return $A->[1];
115 }
116
117 ################################################################
118
119 =item B<forms_write>
120
121 Params: \%form_hash
122
123 Return: $html_of_form [undef on error]
124
125 This function will take the given %form hash and compose a generic HTML
126 representation of it, formatted with tabs and newlines in order to make it
127 neat and tidy for printing.
128
129 Note: this function does *not* escape any special characters that were
130 embedded in the element values.
131
132 =cut
133
134 sub forms_write {
135 my $hr = shift;
136 return undef if ( !ref($hr) || !( scalar %$hr ) );
137 return undef if ( !defined $$hr{"\0"} );
138
139 my $t = '<form name="' . $$hr{"\0"}->[0] . '" method="';
140 $t .= $$hr{"\0"}->[1] . '" action="' . $$hr{"\0"}->[2] . '"';
141 if ( defined $$hr{"\0"}->[3] ) {
142 $t .= ' ' . join( ' ', @{ $$hr{"\0"}->[3] } );
143 }
144 $t .= ">\n";
145
146 my ( $name, $ar );
147 while ( ( $name, $ar ) = each(%$hr) ) {
148 next if ( $name eq "\0" );
149 next if ( $name eq '' && $ar->[0]->[0] eq '' );
150 foreach $a (@$ar) {
151 my $P = '';
152 $P = ' ' . join( ' ', @{ $$a[2] } ) if ( defined $$a[2] );
153 $t .= "\t";
154
155 if ( $$a[0] eq 'textarea' ) {
156 $t .= "<textarea name=\"$name\"$P>$$a[1]";
157 $t .= "</textarea>\n";
158
159 }
160 elsif ( $$a[0] =~ m/^input-(.+)$/ ) {
161 $t .= "<input type=\"$1\" name=\"$name\" ";
162 $t .= "value=\"$$a[1]\"$P>\n";
163
164 }
165 elsif ( $$a[0] eq 'option' ) {
166 $t .= "\t<option value=\"$$a[1]\"$P>$$a[1]\n";
167
168 }
169 elsif ( $$a[0] eq 'select' ) {
170 $t .= "<select name=\"$name\"$P>\n";
171
172 }
173 elsif ( $$a[0] eq '/select' ) {
174 $t .= "</select$P>\n";
175
176 }
177 else { # button
178 $t .= "<button name=\"$name\" value=\"$$a[1]\">\n";
179 }
180 }
181 }
182
183 $t .= "</form>\n";
184 return $t;
185 }
186
187 ################################################################
188
189 { # these are 'private' static variables for &_forms_parse_html
190 my $CURRENT_SELECT = undef;
191 my $UNKNOWNS = 0;
192
193 sub _forms_parse_callback {
194 my ( $TAG, $hr, $dr, $start, $len, $ar ) = ( lc(shift), @_ );
195 my ( $saveparam, $parr, $key ) = ( 0, undef, '' );
196
197 my $_forms_CURRENT = $ar->[0];
198 my $_forms_FOUND = $ar->[1];
199
200 if ( scalar %$hr ) {
201 while ( my ( $key, $val ) = each %$hr ) {
202 if ( $key =~ tr/A-Z// ) {
203 delete $$hr{$key};
204 if ( defined $val ) { $$hr{ lc($key) } = $val; }
205 else { $$hr{ lc($key) } = undef; }
206 }
207 }
208 }
209
210 if ( $TAG eq 'form' ) {
211 if ( scalar %$_forms_CURRENT ) { # save last form
212 push( @$_forms_FOUND, $_forms_CURRENT );
213 $ar->[0] = {};
214 $_forms_CURRENT = $ar->[0];
215 }
216
217 $_forms_CURRENT->{"\0"} =
218 [ $$hr{name}, $$hr{method}, $$hr{action}, [] ];
219 delete $$hr{'name'};
220 delete $$hr{'method'};
221 delete $$hr{'action'};
222 $key = "\0";
223 $UNKNOWNS = 0;
224
225 }
226 elsif ( $TAG eq 'input' ) {
227 $$hr{type} = 'text' if ( !defined $$hr{type} );
228 $$hr{name} = 'unknown' . $UNKNOWNS++ if ( !defined $$hr{name} );
229 $$hr{value} = undef if ( !defined $$hr{value} );
230 $key = $$hr{name};
231
232 push @{ $_forms_CURRENT->{$key} },
233 [ 'input-' . $$hr{type}, $$hr{value}, [] ];
234 delete $$hr{'name'};
235 delete $$hr{'type'};
236 delete $$hr{'value'};
237
238 }
239 elsif ( $TAG eq 'select' ) {
240 $$hr{name} = 'unknown' . $UNKNOWNS++ if ( !defined $$hr{name} );
241 $key = $$hr{name};
242 push @{ $_forms_CURRENT->{$key} }, [ 'select', undef, [] ];
243 $CURRENT_SELECT = $key;
244 delete $$hr{name};
245
246 }
247 elsif ( $TAG eq '/select' ) {
248 push @{ $_forms_CURRENT->{$CURRENT_SELECT} },
249 [ '/select', undef, [] ];
250 $CURRENT_SELECT = undef;
251 return undef;
252
253 }
254 elsif ( $TAG eq 'option' ) {
255 return undef if ( !defined $CURRENT_SELECT );
256 if ( !defined $$hr{value} ) {
257 my $stop = index( $$dr, '<', $start + $len );
258 return undef if ( $stop == -1 ); # MAJOR PUKE
259 $$hr{value} =
260 substr( $$dr, $start + $len, ( $stop - $start - $len ) );
261 $$hr{value} =~ tr/\r\n//d;
262 }
263 push @{ $_forms_CURRENT->{$CURRENT_SELECT} },
264 [ 'option', $$hr{value}, [] ];
265 delete $$hr{value};
266
267 }
268 elsif ( $TAG eq 'textarea' ) {
269 my $stop = $start + $len;
270 $$hr{value} = $$hr{'='};
271 delete $$hr{'='};
272 $$hr{name} = 'unknown' . $UNKNOWNS++ if ( !defined $$hr{name} );
273 $key = $$hr{name};
274 push @{ $_forms_CURRENT->{$key} }, [ 'textarea', $$hr{value}, [] ];
275 delete $$hr{'name'};
276 delete $$hr{'value'};
277
278 }
279 else { # button
280 $$hr{name} = 'unknown' . $UNKNOWNS++ if ( !defined $$hr{name} );
281 $$hr{value} = undef if ( !defined $$hr{value} );
282 $key = $$hr{name};
283 push @{ $_forms_CURRENT->{$key} }, [ 'button', $$hr{value}, [] ];
284 delete $$hr{'name'};
285 delete $$hr{'value'};
286 }
287
288 if ( scalar %$hr ) {
289 if ( $TAG eq 'form' ) { $parr = $_forms_CURRENT->{$key}->[3]; }
290 else {
291 $parr = $_forms_CURRENT->{$key}->[-1];
292 $parr = $parr->[2];
293 }
294
295 my ( $k, $v );
296 while ( ( $k, $v ) = each(%$hr) ) {
297 if ( defined $v ) { push @$parr, "$k=\"$v\""; }
298 else { push @$parr, $k; }
299 }
300 }
301
302 return undef;
303 }
304 }
305