File: form_demo1.pl

package info (click to toggle)
libwhisker2-perl 2.4-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 664 kB
  • ctags: 303
  • sloc: perl: 7,262; makefile: 52
file content (214 lines) | stat: -rw-r--r-- 5,038 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!perl
#
# form_demo1.pl
#
# This is an example libwhisker script which shows how to parse the
# forms structure.  This program describes a form.
#

use LW2;

$HTML =<<EOT;

<form method="POST" action="/test.cgi">
<input type="text" name="first-input" value="one" onchange="foo();">
<input type="text" name="second-input" value="two" onchange="bar();">
<input type="text" value="three">
<input type="checkbox" name="first-check" value="mycheck">
<input type="radio" name="the-radio" value="1">
<input type="radio" name="the-radio" value="2" selected>
<input type="radio" name="the-radio" value="3">
<textarea name="areatext">my text</textarea>
<select name="myselect">
<option>1</option>
<option>2</option>
<option selected>3</option>
</select>
<input type="submit">
</form>

EOT


# read the form(s)
$FOUND = LW2::forms_read( \$HTML );

# $FOUND is an array of found forms; let's just look at the first one
$FIRST_FORM = $FOUND->[0];

# first we'll deal with the <form> data
print "Form name is: ", $FIRST_FORM->{"\0"}->[0], "\n";
print "Form method is: ", $FIRST_FORM->{"\0"}->[1], "\n";
print "Form action is: ", $FIRST_FORM->{"\0"}->[2], "\n";
print "\n";

# now we just enumerate the keys and handle them
foreach $key (keys %$FIRST_FORM){

	# is this an unnamed key?
	$unnamed = 0;
	if($key =~ m/^unknown[0-9]+$/){
		$unnamed++; }

	# check the element type
	$type = $FIRST_FORM->{$key}->[0]->[0];

	# handle multi-element selects
	if($type eq 'select'){

		# storage for the selected value
		$selected_value=undef;

		# possible values
		@possible_values = ();

		# loop through each element
		foreach ( @{ $FIRST_FORM->{$key} }){

			# we only care about options at this point
			next if($_->[0] ne 'option');

			# the first option will be the selected value if
			# an actual flagged as 'selected' value is not found
			$selected_value = $_->[1] if(!defined $selected_value);

			# check to see if this value is selected
			if(attributes_lookup($_->[2],'selected')){
				$selected_value = $_->[1]; }

			# save this value as a possible value
			push @possible_values, $_->[1];
		}

		if($unnamed){
			print "[$key]\tis an unnamed select with value ",
				$selected_value;
		} else {
			print "[$key]\tis a select with value ",
				$selected_value;
		}

		if(attributes_lookup($attribs,'onchange')){
			print " (w/ javascript onchange())";
		}
		print "\n";

		print "\t\t- Possible values are: ",
			join(',', @possible_values), "\n";

		# next element
		next;
	}

	# handle radio boxes
	if(lc($type) eq 'input-radio'){

		# storage for the selected value
		$selected_value=undef;

		# possible values
		@possible_values = ();

		# loop through each element
		foreach $element ( @{ $FIRST_FORM->{$key} }){
			
			# another way to deference things
			$type = $element->[0];
			$value = $element->[1];
			$attribs = $element->[2];

			# we only care about input-radios
			next if(lc($type) ne 'input-radio');

			# check to see if this value is selected
			if(attributes_lookup($attribs,'selected')){
				$selected_value = $value; }

			# save this value as a possible value
			push @possible_values, $value;
		}

		if($unnamed){
			print "[$key]\tis an unnamed radio input ";
		} else {
			print "[$key]\tis a radio input ";
		}
		
		if(defined $selected_value){
			print "with value $selected_value";
		} else {
			print "with no default value";
		}

		if(attributes_lookup($attribs,'onchange')){
			print " (w/ javascript onchange())";
		}
		print "\n";

		print "\t\t- Possible values are: ",
			join(',', @possible_values), "\n";

		next;
	}


	# handle the other simple types
	foreach $element ( @{ $FIRST_FORM->{$key} } ){

		$type = $element->[0];
		$value = $element->[1];
		$attribs = $element->[2];

		if( $type eq 'textarea'){
			print "[$key]\tis textarea with ",
				length( $value ),
				" chars in default value\n";

		} elsif ($type =~ m/^input-(.+)/){
			$input_type = lc($1);

			if($unnamed){
				print "[$key]\tis an unnamed $input_type input";
			} else {
				print "[$key]\tis a $input_type input";
			}

			if($input_type eq 'text' || $input_type eq 'hidden'){
				print " with value '$value'";

			} elsif($input_type eq 'submit'){

			} elsif($input_type eq 'radio'){
				if(attributes_lookup($attribs,'selected')){
					print " with actual value '$value'";
				} else {
					print " with possible value '$value'";
				}

			} elsif($input_type eq 'checkbox'){
				if(attributes_lookup($attribs,'checked')){
					print " which is checked";
				} else {
					print " which is not checked";
				}
			}
		
			if(attributes_lookup($attribs,'onchange')){
				print " (w/ javascript onchange())";
			}
			print "\n";
		}
	}


}

sub attributes_lookup {
	my ( $attribs_ref, $name ) = @_;

	foreach (@$attribs_ref){
		return $_ if($_ eq $name);
		return $_ if($_ =~ m/^$name=/);
	}
	return undef;
}