2
|
1 ;;; w3-forms.el --- Emacs-w3 forms parsing code for new display engine
|
0
|
2 ;; Author: wmperry
|
2
|
3 ;; Created: 1996/08/10 16:14:08
|
|
4 ;; Version: 1.14
|
0
|
5 ;; Keywords: faces, help, comm, data, languages
|
|
6
|
|
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
2
|
8 ;;; Copyright (c) 1996 by William M. Perry (wmperry@cs.indiana.edu)
|
0
|
9 ;;;
|
|
10 ;;; This file is not part of GNU Emacs, but the same permissions apply.
|
|
11 ;;;
|
|
12 ;;; GNU Emacs is free software; you can redistribute it and/or modify
|
|
13 ;;; it under the terms of the GNU General Public License as published by
|
|
14 ;;; the Free Software Foundation; either version 2, or (at your option)
|
|
15 ;;; any later version.
|
|
16 ;;;
|
|
17 ;;; GNU Emacs is distributed in the hope that it will be useful,
|
|
18 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
19 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
20 ;;; GNU General Public License for more details.
|
|
21 ;;;
|
|
22 ;;; You should have received a copy of the GNU General Public License
|
|
23 ;;; along with GNU Emacs; see the file COPYING. If not, write to
|
|
24 ;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
26
|
|
27 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
28 ;;; FORMS processing for html 2.0/3.0
|
|
29 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
30 (eval-and-compile
|
|
31 (require 'w3-draw))
|
|
32
|
|
33 (require 'widget)
|
|
34
|
|
35 (if (featurep 'mule) (fset 'string-width 'length))
|
|
36
|
|
37 ;; These are things in later versions of the widget package that I don't
|
|
38 ;; have yet.
|
|
39 (defun widget-at (pt)
|
|
40 (or (get-text-property pt 'button)
|
|
41 (get-text-property pt 'field)))
|
|
42
|
|
43 ;; A form entry area is a vector
|
|
44 ;; [ type name default-value value maxlength options widget]
|
|
45 ;; Where:
|
|
46 ;; type = symbol defining what type of form entry area it is
|
|
47 ;; (ie: file, radio)
|
|
48 ;; name = the name of the form element
|
|
49 ;; default-value = the value this started out with
|
|
50
|
|
51 (defsubst w3-form-element-type (obj) (aref obj 0))
|
|
52 (defsubst w3-form-element-name (obj) (aref obj 1))
|
|
53 (defsubst w3-form-element-default-value (obj) (aref obj 2))
|
|
54 (defsubst w3-form-element-value (obj) (aref obj 3))
|
|
55 (defsubst w3-form-element-size (obj) (aref obj 4))
|
|
56 (defsubst w3-form-element-maxlength (obj) (aref obj 5))
|
|
57 (defsubst w3-form-element-options (obj) (aref obj 6))
|
|
58 (defsubst w3-form-element-action (obj) (aref obj 7))
|
|
59 (defsubst w3-form-element-widget (obj) (aref obj 8))
|
|
60
|
|
61 (defsubst w3-form-element-set-type (obj val) (aset obj 0 val))
|
|
62 (defsubst w3-form-element-set-name (obj val) (aset obj 1 val))
|
|
63 (defsubst w3-form-element-set-default-value (obj val) (aset obj 2 val))
|
|
64 (defsubst w3-form-element-set-value (obj val) (aset obj 3 val))
|
|
65 (defsubst w3-form-element-set-size (obj val) (aset obj 4 val))
|
|
66 (defsubst w3-form-element-set-maxlength (obj val) (aset obj 5 val))
|
|
67 (defsubst w3-form-element-set-options (obj val) (aset obj 6 val))
|
|
68 (defsubst w3-form-element-set-action (obj val) (aset obj 7 val))
|
|
69 (defsubst w3-form-element-set-widget (obj val) (aset obj 8 val))
|
|
70
|
|
71 ;; The main function - this adds a single widget to the form
|
|
72 (defun w3-form-add-element (&rest args)
|
|
73 (let* ((widget nil)
|
|
74 (buffer-read-only nil)
|
|
75 (inhibit-read-only t)
|
|
76 (widget-creation-function nil)
|
2
|
77 (action (cons (cons 'form-number (w3-get-state :formnum))
|
|
78 (nth 6 args)))
|
0
|
79 (node (assoc action w3-form-elements))
|
|
80 (name (or (nth 1 args)
|
|
81 (if (memq (nth 0 args) '(submit reset))
|
|
82 nil
|
|
83 (symbol-name (nth 0 args)))))
|
|
84 (val (vector (nth 0 args) ; type
|
|
85 name ; name
|
|
86 (nth 5 args) ; default
|
|
87 (nth 2 args) ; value
|
|
88 (nth 3 args) ; size
|
|
89 (nth 4 args) ; maxlength
|
|
90 (nth 7 args) ; options
|
|
91 action
|
|
92 nil)) ; widget
|
|
93 )
|
|
94 (setq widget-creation-function (or (get (car args)
|
|
95 'w3-widget-creation-function)
|
|
96 'w3-form-default-widget-creator)
|
|
97 widget (funcall widget-creation-function val
|
|
98 (cdr (nth 10 args))))
|
|
99 (if node
|
|
100 (setcdr node (cons val (cdr node)))
|
|
101 (setq w3-form-elements (cons (cons action (list val)) w3-form-elements)))
|
|
102 (if (not widget)
|
|
103 nil
|
|
104 (w3-form-element-set-widget val widget)
|
|
105 (widget-put widget 'w3-form-data val))))
|
|
106
|
|
107 ;; These properties tell the add-element function how to actually create
|
|
108 ;; each type of widget.
|
|
109 (put 'checkbox 'w3-widget-creation-function 'w3-form-create-checkbox)
|
|
110 (put 'multiline 'w3-widget-creation-function 'w3-form-create-multiline)
|
|
111 (put 'radio 'w3-widget-creation-function 'w3-form-create-radio-button)
|
|
112 (put 'reset 'w3-widget-creation-function 'w3-form-create-submit-button)
|
|
113 (put 'submit 'w3-widget-creation-function 'w3-form-create-submit-button)
|
|
114 (put 'hidden 'w3-widget-creation-function 'ignore)
|
|
115 (put 'file 'w3-widget-creation-function 'w3-form-create-file-browser)
|
|
116 (put 'option 'w3-widget-creation-function 'w3-form-create-option-list)
|
|
117 (put 'keygen 'w3-widget-creation-function 'w3-form-create-keygen-list)
|
|
118 (put 'button 'w3-widget-creation-function 'w3-form-create-button)
|
|
119 (put 'image 'w3-widget-creation-function 'w3-form-create-image)
|
|
120
|
|
121 (defun w3-form-create-checkbox (el face)
|
|
122 (widget-create 'checkbox :value-face face
|
|
123 (and (w3-form-element-default-value el) t)))
|
|
124
|
|
125 (defun w3-form-create-radio-button (el face)
|
|
126 (let* ((name (w3-form-element-name el))
|
|
127 (formobj (cdr (assoc name w3-form-radio-elements)))
|
|
128 (widget nil)
|
|
129 )
|
|
130 (if formobj
|
|
131 (progn
|
|
132 (setq widget (w3-form-element-widget formobj))
|
2
|
133 (widget-radio-add-item widget
|
|
134 (list 'item
|
|
135 :format "%t"
|
|
136 :tag ""
|
|
137 :value (w3-form-element-value el)))
|
|
138 (if (w3-form-element-default-value el)
|
|
139 (widget-value-set widget (w3-form-element-value el)))
|
0
|
140 nil)
|
2
|
141 (setq widget (widget-create 'radio
|
|
142 :value (w3-form-element-value el)
|
|
143 (list 'item
|
|
144 :format "%t"
|
|
145 :tag ""
|
|
146 :value (w3-form-element-value el)))
|
0
|
147 w3-form-radio-elements (cons (cons name el)
|
|
148 w3-form-radio-elements))
|
|
149 widget)))
|
|
150
|
|
151 (defun w3-form-create-button (el face)
|
|
152 ;; This handles dealing with the bogus Netscape 'button' input type
|
|
153 ;; that lots of places have been using to slap javascript shit onto
|
|
154 (let ((val (w3-form-element-value el)))
|
|
155 (if (or (not val) (string= val ""))
|
|
156 (setq val "Push Me"))
|
|
157 (widget-create 'push :notify 'ignore :button-face face val)))
|
|
158
|
|
159 (defun w3-form-create-image (el face)
|
|
160 (let ((widget (widget-create 'push
|
|
161 :notify 'w3-form-submit/reset-callback
|
|
162 :value "Form-Image")))
|
|
163 widget))
|
|
164
|
|
165 (defun w3-form-create-submit-button (el face)
|
|
166 (let ((val (w3-form-element-value el)))
|
|
167 (if (or (not val) (string= val ""))
|
|
168 (setq val (if (eq (w3-form-element-type el) 'submit)
|
|
169 "Submit"
|
|
170 "Reset")))
|
|
171 (widget-create 'push :notify 'w3-form-submit/reset-callback
|
|
172 :button-face face val)))
|
|
173
|
|
174 (defun w3-form-create-file-browser (el face)
|
|
175 (widget-create 'file :value-face face :value (w3-form-element-value el)))
|
|
176
|
|
177 (defvar w3-form-valid-key-sizes
|
|
178 '(
|
|
179 ("1024 (Premium)" . 1024)
|
|
180 ("896 (Regular)" . 896)
|
|
181 ("768 (Unleaded)" . 768)
|
|
182 ("512 (Low Grade)" . 512)
|
|
183 ("508 (Woos)" . 508)
|
|
184 ("256 (Test Grade)" . 256)
|
|
185 )
|
|
186 "An assoc list of available key sizes and meaningful descriptions.")
|
|
187
|
|
188 (defun w3-form-create-keygen-list (el face)
|
|
189 (let ((tmp w3-form-valid-key-sizes)
|
|
190 (longest 0)
|
|
191 (options nil))
|
|
192 (while tmp
|
|
193 (if (> (length (caar tmp)) longest)
|
|
194 (setq longest (length (caar tmp))))
|
|
195 (setq options (cons (list 'choice-item :tag (caar tmp)
|
|
196 :value (cdar tmp)) options)
|
|
197 tmp (cdr tmp)))
|
|
198 (apply 'widget-create 'choice :value 1024
|
|
199 :tag "Key Length"
|
|
200 :size (1+ longest)
|
|
201 :value-face face
|
|
202 options)))
|
|
203
|
|
204 (defun w3-form-create-option-list (el face)
|
|
205 (let ((widget (apply 'widget-create 'choice :value (w3-form-element-value el)
|
|
206 :tag "Choose"
|
2
|
207 :format "%v"
|
0
|
208 :size (w3-form-element-size el)
|
|
209 :value-face face
|
|
210 (mapcar
|
|
211 (function
|
|
212 (lambda (x)
|
|
213 (list 'choice-item :format "%[%t%]"
|
2
|
214 :tag (car x) :value (car x))))
|
0
|
215 (reverse (w3-form-element-options el))))))
|
2
|
216 (widget-value-set widget (w3-form-element-value el))
|
0
|
217 widget))
|
|
218
|
|
219 ;(defun w3-form-create-multiline (el face)
|
|
220 ; ;; FIX THIS! - need to padd out with newlines or something...
|
|
221 ; (widget-create 'field :value-face face (w3-form-element-value el)))
|
|
222
|
|
223 (defun w3-form-create-multiline (el face)
|
|
224 (widget-create 'push :notify 'w3-do-text-entry "Multiline text area"))
|
|
225
|
|
226 (defun w3-form-default-widget-creator (el face)
|
|
227 (widget-create 'link
|
|
228 :notify 'w3-form-default-button-callback
|
|
229 :size (w3-form-element-size el)
|
|
230 :tag (w3-truncate-string (w3-form-element-value el)
|
|
231 (w3-form-element-size el) ?_)
|
|
232 :value-face face
|
|
233 (w3-form-element-value el)))
|
|
234
|
|
235 (defun w3-form-default-button-callback (widget &rest ignore)
|
|
236 (let* ((obj (widget-get widget 'w3-form-data))
|
|
237 (typ (w3-form-element-type obj))
|
|
238 (def (widget-value widget))
|
|
239 (val nil)
|
|
240 )
|
|
241 (case typ
|
|
242 (password
|
|
243 (setq val (funcall url-passwd-entry-func "Password: " def))
|
|
244 (widget-put widget :tag (w3-truncate-string
|
|
245 (make-string (length val) ?*)
|
|
246 (w3-form-element-size obj) ?_)))
|
|
247 (otherwise
|
|
248 (setq val (read-string
|
|
249 (concat (capitalize (symbol-name typ)) ": ") def))
|
|
250 (widget-put widget :tag (w3-truncate-string
|
|
251 val (w3-form-element-size obj) ?_))))
|
|
252 (widget-value-set widget val))
|
|
253 (apply 'w3-form-possibly-submit widget ignore))
|
|
254
|
|
255 (defun w3-truncate-string (str len &optional pad)
|
|
256 "Truncate string STR so that string-width of STR is not greater than LEN.
|
|
257 If width of the truncated string is less than LEN, and if a character PAD is
|
|
258 defined, add padding end of it."
|
|
259 (if (featurep 'mule)
|
|
260 (let ((cl (string-to-char-list str)) (n 0) (sw 0))
|
|
261 (if (<= (string-width str) len) str
|
|
262 (while (<= (setq sw (+ (char-width (nth n cl)) sw)) len)
|
|
263 (setq n (1+ n)))
|
|
264 (string-match (make-string n ?.) str)
|
|
265 (setq str (substring str 0 (match-end 0))))
|
|
266 (if pad (concat str (make-string (- len (string-width str)) pad)) str))
|
|
267 (concat (if (> (length str) len) (substring str 0 len) str)
|
|
268 (if (or (null pad) (> (length str) len))
|
|
269 ""
|
|
270 (make-string (- len (length str)) pad)))))
|
|
271
|
|
272 (defun w3-form-possibly-submit (widget &rest ignore)
|
|
273 (let* ((formobj (widget-get widget 'w3-form-data))
|
|
274 (ident (w3-form-element-action formobj))
|
|
275 (widgets (w3-all-widgets ident))
|
|
276 (text-fields 0)
|
|
277 (text-p nil))
|
|
278 ;;
|
|
279 ;; Gack. Netscape auto-submits forms of one text field
|
|
280 ;; here we go through the list of widgets in this form and
|
|
281 ;; determine which are not submit/reset/button inputs.
|
|
282 ;; If the # == 1, then submit the form.
|
|
283 ;;
|
|
284 (while widgets
|
|
285 (setq text-fields (+
|
|
286 text-fields
|
|
287 (case (w3-form-element-type (car widgets))
|
|
288 ((submit reset image button)
|
|
289 0)
|
|
290 (text
|
|
291 (setq text-p t)
|
|
292 1)
|
|
293 (otherwise
|
|
294 1)))
|
|
295 widgets (cdr widgets)))
|
|
296 (if (and (= text-fields 1) text-p)
|
|
297 (w3-submit-form ident))))
|
|
298
|
|
299 (defun w3-form-submit/reset-callback (widget &rest ignore)
|
|
300 (let* ((formobj (widget-get widget 'w3-form-data))
|
|
301 (w3-submit-button formobj))
|
|
302 (case (w3-form-element-type formobj)
|
|
303 (submit (w3-submit-form (w3-form-element-action formobj)))
|
|
304 (reset (w3-revert-form (w3-form-element-action formobj)))
|
|
305 (image (w3-submit-form (w3-form-element-action formobj)))
|
|
306 (otherwise
|
|
307 (error
|
|
308 "Impossible widget type %s triggered w3-form-submit/reset-callback"
|
|
309 (w3-form-element-type formobj))))))
|
|
310
|
|
311 (defun w3-do-text-entry (widget &rest ignore)
|
|
312 (let* ((data (list widget (current-buffer)))
|
|
313 (formobj (widget-get widget 'w3-form-data))
|
|
314 (buff (get-buffer-create (format "Form Entry: %s"
|
|
315 (w3-form-element-name formobj)))))
|
|
316 (switch-to-buffer-other-window buff)
|
|
317 (indented-text-mode)
|
|
318 (erase-buffer)
|
|
319 (if (w3-form-element-value formobj)
|
|
320 (insert (w3-form-element-value formobj)))
|
|
321 (setq w3-current-last-buffer data)
|
|
322 (message "Press C-c C-c when finished with text entry.")
|
|
323 (local-set-key "\C-c\C-c" 'w3-finish-text-entry)))
|
|
324
|
|
325 (defun w3-finish-text-entry ()
|
|
326 (interactive)
|
|
327 (if w3-current-last-buffer
|
|
328 (let* ((widget (nth 0 w3-current-last-buffer))
|
|
329 (formobj (widget-get widget 'w3-form-data))
|
|
330 (buff (nth 1 w3-current-last-buffer))
|
|
331 (valu (buffer-string))
|
|
332 (inhibit-read-only t)
|
|
333 )
|
|
334 (local-set-key "\C-c\C-c" 'undefined)
|
|
335 (kill-buffer (current-buffer))
|
|
336 (condition-case ()
|
|
337 (delete-window)
|
|
338 (error nil))
|
|
339 (if (not (and buff (bufferp buff) (buffer-name buff)))
|
|
340 (message "Could not find the form buffer for this text!")
|
|
341 (switch-to-buffer buff)
|
|
342 (w3-form-element-set-value formobj valu)))))
|
|
343
|
|
344 (defsubst w3-all-widgets (actn)
|
|
345 ;; Return a list of data entry widgets in form number ACTN
|
|
346 (cdr-safe (assoc actn w3-form-elements)))
|
|
347
|
|
348 (defun w3-revert-form (actn)
|
|
349 (save-excursion
|
|
350 (let* ((formobjs (w3-all-widgets actn))
|
|
351 (inhibit-read-only t)
|
|
352 deft type widget formobj)
|
|
353 (while formobjs
|
|
354 (setq formobj (car formobjs)
|
|
355 widget (w3-form-element-widget formobj)
|
|
356 formobjs (cdr formobjs)
|
|
357 deft (w3-form-element-default-value formobj)
|
|
358 type (w3-form-element-type formobj))
|
|
359 (case type
|
|
360 ((submit reset image) nil)
|
|
361 (radio
|
|
362 ;; Ack - how!?
|
|
363 )
|
|
364 (checkbox
|
|
365 (if deft
|
|
366 (widget-value-set widget t)
|
|
367 (widget-value-set widget nil)))
|
|
368 (file
|
|
369 (widget-value-set widget deft))
|
|
370 (otherwise
|
|
371 (widget-value-set widget deft)))))))
|
|
372
|
|
373 (defun w3-form-encode-helper (formobjs)
|
|
374 (let (
|
|
375 (submit-button-data w3-submit-button)
|
|
376 formobj result widget temp type)
|
|
377 (while formobjs
|
|
378 (setq formobj (car formobjs)
|
|
379 type (w3-form-element-type formobj)
|
|
380 widget (w3-form-element-widget formobj)
|
|
381 formobjs (cdr formobjs)
|
|
382 temp (case type
|
|
383 (reset nil)
|
|
384 (image
|
|
385 (if (and (eq submit-button-data formobj)
|
|
386 (w3-form-element-name formobj))
|
|
387 (setq result (append
|
|
388 (list
|
|
389 (cons
|
|
390 (concat (w3-form-element-name formobj)
|
|
391 ".x") "0")
|
|
392 (cons
|
|
393 (concat (w3-form-element-name formobj)
|
|
394 ".y") "0"))
|
|
395 result)))
|
|
396 nil)
|
|
397 (submit
|
|
398 (if (and (eq submit-button-data formobj)
|
|
399 (w3-form-element-name formobj))
|
|
400 (cons (w3-form-element-name formobj)
|
|
401 (w3-form-element-value formobj))))
|
|
402 (radio
|
2
|
403 (let* ((radio-name (w3-form-element-name formobj))
|
|
404 (radio-object (cdr-safe
|
|
405 (assoc radio-name
|
|
406 w3-form-radio-elements)))
|
|
407 (chosen-widget (and radio-object
|
|
408 (widget-radio-chosen
|
|
409 (w3-form-element-widget
|
|
410 radio-object)))))
|
|
411 (if (assoc radio-name result)
|
0
|
412 nil
|
2
|
413 (cons radio-name (widget-value chosen-widget)))))
|
0
|
414 (checkbox
|
|
415 (if (widget-value widget)
|
|
416 (cons (w3-form-element-name formobj)
|
|
417 (w3-form-element-value formobj))))
|
|
418 (file
|
|
419 (let ((dat nil)
|
|
420 (fname (widget-value widget)))
|
|
421 (save-excursion
|
|
422 (set-buffer (get-buffer-create " *w3-temp*"))
|
|
423 (erase-buffer)
|
|
424 (setq dat
|
|
425 (condition-case ()
|
|
426 (insert-file-contents-literally fname)
|
|
427 (error (concat "Error accessing " fname))))
|
|
428 (cons (w3-form-element-name formobj) dat))))
|
|
429 (option
|
|
430 (cons (w3-form-element-name formobj)
|
2
|
431 (cdr-safe
|
|
432 (assoc (widget-value widget)
|
|
433 (w3-form-element-options formobj)))))
|
0
|
434 (keygen
|
|
435 (cons (w3-form-element-name formobj)
|
|
436 (format "Should create a %d bit RSA key"
|
|
437 (widget-value widget))))
|
|
438 ((multiline hidden)
|
|
439 (cons (w3-form-element-name formobj)
|
|
440 (w3-form-element-value formobj)))
|
|
441 (otherwise
|
|
442 (cons (w3-form-element-name formobj)
|
|
443 (widget-value widget)))))
|
|
444 (if temp
|
|
445 (setq result (cons temp result))))
|
|
446 result))
|
|
447
|
|
448 (defun w3-form-encode-make-mime-part (id data separator)
|
|
449 (concat separator "\nContent-id: " id
|
|
450 "\nContent-length: " (length data)
|
|
451 "\n\n" data))
|
|
452
|
|
453 (defun w3-form-encode-multipart/x-www-form-data (formobjs)
|
|
454 ;; Create a multipart form submission.
|
|
455 ;; Returns a cons of two strings. Car is the separator used.
|
|
456 ;; cdr is the body of the MIME message."
|
|
457 (let ((separator "---some-separator-for-www-form-data"))
|
|
458 (cons separator
|
|
459 (mapconcat
|
|
460 (function
|
|
461 (lambda (formobj)
|
|
462 (w3-form-encode-make-mime-part (car formobj) (cdr formobj)
|
|
463 separator)))
|
|
464 (w3-form-encode-helper formobjs)
|
|
465 "\n"))))
|
|
466
|
|
467 (fset 'w3-form-encode-multipart/form-data
|
|
468 'w3-form-encode-multipart/x-www-form-data)
|
|
469 (fset 'w3-form-encode- 'w3-form-encode-application/x-www-form-urlencoded)
|
|
470
|
|
471 (defun w3-next-widget (pos)
|
|
472 (let* ((next (cond ((get-text-property pos 'button)
|
|
473 (next-single-property-change pos 'button))
|
|
474 ((get-text-property pos 'field)
|
|
475 (next-single-property-change pos 'field))
|
|
476 (t pos)))
|
|
477 (button (next-single-property-change next 'button))
|
|
478 (field (next-single-property-change next 'field)))
|
|
479 (setq next
|
|
480 (cond
|
|
481 ((and button field) (min button field))
|
|
482 (button button)
|
|
483 (field field)
|
|
484 (t nil)))
|
|
485 (and next
|
|
486 (or (get-text-property next 'button)
|
|
487 (get-text-property next 'field)))))
|
|
488
|
|
489 (defun w3-form-encode (result &optional enctype)
|
|
490 "Create a string suitably encoded for a URL request."
|
|
491 (let ((func (intern (concat "w3-form-encode-" enctype))))
|
2
|
492 (if (fboundp func)
|
|
493 (funcall func result)
|
|
494 (w3-warn 'html (format "Bad encoding type for form data: %s" enctype))
|
|
495 (w3-form-encode-application/x-www-form-urlencoded result))))
|
0
|
496
|
|
497 (defun w3-form-encode-text/plain (result)
|
|
498 (let ((query ""))
|
|
499 (setq query
|
|
500 (mapconcat
|
|
501 (function
|
|
502 (lambda (widget)
|
|
503 (let ((nam (car widget))
|
|
504 (val (cdr widget)))
|
|
505 (if (string-match "\n" nam)
|
|
506 (setq nam (mapconcat
|
|
507 (function
|
|
508 (lambda (x)
|
|
509 (if (= x ?\n) "," (char-to-string x))))
|
|
510 nam "")))
|
|
511 (concat nam " " val))))
|
|
512 (w3-form-encode-helper result) "\n"))
|
|
513 query))
|
|
514
|
2
|
515 (defun w3-form-encode-application/x-w3-wais (result)
|
|
516 (cdr (car (w3-form-encode-helper result))))
|
|
517
|
0
|
518 (defun w3-form-encode-application/x-gopher-query (result)
|
2
|
519 (concat "\t" (cdr (car (w3-form-encode-helper result)))))
|
0
|
520
|
|
521 (defconst w3-xwfu-acceptable-chars
|
|
522 (list
|
|
523 ?a ?b ?c ?d ?e ?f ?g ?h ?i ?j ?k ?l ?m ?n ?o ?p ?q ?r ?s ?t ?u ?v ?w ?x ?y ?z
|
|
524 ?A ?B ?C ?D ?E ?F ?G ?H ?I ?J ?K ?L ?M ?N ?O ?P ?Q ?R ?S ?T ?U ?V ?W ?X ?Y ?Z
|
|
525 ?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9
|
2
|
526 ?_ ;; BOGUS! This is for #!%#@!ing netscape compatibility
|
|
527 ?. ;; BOGUS! This is for #!%#@!ing netscape compatibility
|
0
|
528 )
|
|
529 "A list of characters that we do not have to escape in the media type
|
2
|
530 application/x-www-form-urlencoded")
|
0
|
531
|
|
532 (defun w3-form-encode-xwfu (chunk)
|
|
533 "Escape characters in a string for application/x-www-form-urlencoded.
|
|
534 Blasphemous crap because someone didn't think %20 was good enough for encoding
|
|
535 spaces. Die Die Die."
|
|
536 (if (and (featurep 'mule) chunk)
|
2
|
537 (setq chunk (if w3-running-xemacs
|
|
538 (decode-coding-string
|
|
539 chunk url-mule-retrieval-coding-system)
|
|
540 (code-convert-string
|
|
541 chunk *internal* url-mule-retrieval-coding-system))))
|
0
|
542 (mapconcat
|
|
543 (function
|
|
544 (lambda (char)
|
|
545 (cond
|
|
546 ((= char ? ) "+")
|
|
547 ((memq char w3-xwfu-acceptable-chars) (char-to-string char))
|
2
|
548 (t (upcase (format "%%%02x" char))))))
|
0
|
549 chunk ""))
|
|
550
|
|
551 (defun w3-form-encode-application/x-www-form-urlencoded (result)
|
|
552 (mapconcat
|
|
553 (function
|
|
554 (lambda (data)
|
|
555 (concat (w3-form-encode-xwfu (car data)) "="
|
|
556 (w3-form-encode-xwfu (cdr data)))))
|
|
557 (w3-form-encode-helper result) "&"))
|
|
558
|
|
559 (defun w3-form-encode-application/x-w3-isindex (result)
|
|
560 (let* ((info (w3-form-encode-helper result))
|
|
561 (query (cdr-safe (assoc "isindex" info))))
|
|
562 (if query
|
|
563 (url-hexify-string query)
|
|
564 "")))
|
|
565
|
|
566 (defun w3-form-encode-application/gopher-ask-block (result)
|
|
567 (let ((query ""))
|
|
568 ;;; gopher+ will expect all the checkboxes/etc, even if they are
|
|
569 ;;; not turned on. Should still ignore RADIO boxes that are not
|
|
570 ;;; active though.
|
|
571 (while result
|
|
572 (if (and (not (and (string= (nth 2 (car result)) "RADIO")
|
|
573 (not (nth 6 (car result)))))
|
|
574 (not (member (nth 2 (car result)) '("SUBMIT" "RESET"))))
|
|
575 (setq query (format "%s\r\n%s" query (nth 5 (car result)))))
|
|
576 (setq result (cdr result)))
|
|
577 (concat query "\r\n.\r\n")))
|
|
578
|
|
579 (defun w3-submit-form (ident)
|
|
580 ;; Submit form entry fields matching ACTN as their action identifier.
|
|
581 (let* ((result (w3-all-widgets ident))
|
|
582 (enctype (cdr (assq 'enctype ident)))
|
|
583 (query (w3-form-encode result enctype))
|
|
584 (themeth (upcase (or (cdr (assq 'method ident)) "get")))
|
|
585 (theurl (cdr (assq 'action ident))))
|
|
586 (if (and (string= "GET" themeth)
|
|
587 (string-match "\\([^\\?]*\\)\\?" theurl))
|
|
588 (setq theurl (url-match theurl 1)))
|
|
589 (cond
|
|
590 ((or (string= "POST" themeth)
|
|
591 (string= "PUT" themeth))
|
|
592 (if (consp query)
|
|
593 (setq enctype (concat enctype "; separator=\""
|
|
594 (substring (car query) 3 nil)
|
|
595 "\"")
|
|
596 query (cdr query)))
|
|
597 (let ((url-request-method themeth)
|
|
598 (url-request-data query)
|
|
599 (url-request-extra-headers
|
|
600 (cons (cons "Content-type" enctype) url-request-extra-headers)))
|
|
601 (w3-fetch theurl)))
|
|
602 ((string= "GET" themeth)
|
2
|
603 (let ((theurl (concat theurl (if (string-match "gopher" enctype)
|
|
604 "" "?") query)))
|
0
|
605 (w3-fetch theurl)))
|
|
606 (t
|
|
607 (w3-warn 'html (format "Unknown submit method: %s" themeth))
|
|
608 (let ((theurl (concat theurl "?" query)))
|
|
609 (w3-fetch theurl))))))
|
|
610
|
|
611 (provide 'w3-forms)
|