From 3ceaa1815d15554f4dbcf4ded36686d1cda30c6d Mon Sep 17 00:00:00 2001 From: Lars Magne Ingebrigtsen Date: Mon, 10 Jun 2013 15:43:56 +0200 Subject: [PATCH] Make form submission work * eww.el (eww-submit): Make form submission work. (shr-expand-url): Strip query strings from URLs before expanding them. --- lisp/ChangeLog | 3 +++ lisp/eww.el | 30 ++++++++++++++++++++++++------ lisp/shr.el | 31 +++++++++++++++++-------------- 3 files changed, 44 insertions(+), 20 deletions(-) diff --git a/lisp/ChangeLog b/lisp/ChangeLog index 3bb93f7a6..693bb9243 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog @@ -1,7 +1,10 @@ 2013-06-10 Lars Magne Ingebrigtsen + * eww.el (eww-submit): Make form submission work. + * shr.el (shr-descend): Allow other packages to override (or provide) rendering of elements. + (shr-expand-url): Strip query strings from URLs before expanding them. * eww.el: Don't require cl-lib. (eww-tag-form): Start form support. diff --git a/lisp/eww.el b/lisp/eww.el index badf2a0ae..f1758a330 100644 --- a/lisp/eww.el +++ b/lisp/eww.el @@ -27,6 +27,7 @@ (eval-when-compile (require 'cl)) (require 'shr) (require 'url) +(require 'mm-url) (defvar eww-current-url nil) (defvar eww-history nil) @@ -135,7 +136,7 @@ mode-name "eww") (set (make-local-variable 'eww-current-url) 'author) (set (make-local-variable 'browse-url-browser-function) 'eww-browse-url) - (setq buffer-read-only t) + ;;(setq buffer-read-only t) (use-local-map eww-mode-map)) (defun eww-browse-url (url &optional new-window) @@ -173,21 +174,38 @@ 'eww-form eww-form))) (defun eww-tag-input (cont) - (push (cons (cdr (assq :name cont)) - (cdr (assq :value cont))) - eww-form) (let ((start (point)) (widget (list 'editable-field :size (string-to-number (or (cdr (assq :size cont)) "40")) - :value (or "____" (cdr (assq :value cont)) "") - :action 'eww-submit))) + :value (or (cdr (assq :value cont)) "") + :action 'eww-submit + :name (cdr (assq :name cont)) + :eww-form eww-form))) (apply 'widget-create widget) (shr-generic cont) (put-text-property start (point) 'eww-widget widget))) +(defun eww-submit (widget dummy) + (let ((form (getf (cdr widget) :eww-form)) + values) + (dolist (overlay (overlays-in (point-min) (point-max))) + (let ((field (getf (overlay-properties overlay) 'field))) + (when (eq (getf (cdr field) :eww-form) form) + (let ((name (getf (cdr field) :name))) + (when name + (push (cons name (widget-value field)) + values)))))) + (let ((shr-base eww-current-url)) + (eww-browse-url + (shr-expand-url + (concat + (getf form :action) + "?" + (mm-url-encode-www-form-urlencoded values))))))) + (defun eww-convert-widgets () (let ((start (point-min)) widget) diff --git a/lisp/shr.el b/lisp/shr.el index 1d6a8ca65..bf9f5a4e3 100644 --- a/lisp/shr.el +++ b/lisp/shr.el @@ -484,20 +484,23 @@ size, and full-buffer size." (not failed))) (defun shr-expand-url (url) - (cond - ;; Absolute URL. - ((or (not url) - (string-match "\\`[a-z]*:" url) - (not shr-base)) - url) - ((and (string-match "\\`//" url) - (string-match "\\`[a-z]*:" shr-base)) - (concat (match-string 0 shr-base) url)) - ((and (not (string-match "/\\'" shr-base)) - (not (string-match "\\`/" url))) - (concat shr-base "/" url)) - (t - (concat shr-base url)))) + (if (or (not url) + (string-match "\\`[a-z]*:" url) + (not shr-base)) + ;; Absolute URL. + url + (let ((base shr-base)) + (when (string-match "^\\([^?]+\\)[?]" base) + (setq base (match-string 1 base))) + (cond + ((and (string-match "\\`//" url) + (string-match "\\`[a-z]*:" base)) + (concat (match-string 0 base) url)) + ((and (not (string-match "/\\'" base)) + (not (string-match "\\`/" url))) + (concat base "/" url)) + (t + (concat base url)))))) (defun shr-ensure-newline () (unless (zerop (current-column)) -- 2.25.1