(eval-when-compile (require 'cl))
(require 'shr)
(require 'url)
+(require 'mm-url)
(defvar eww-current-url nil)
(defvar eww-history nil)
(libxml-parse-html-region (point) (point-max)))))
(eww-setup-buffer)
(setq eww-current-url url)
- (let ((inhibit-read-only t))
- (shr-insert-document document))
+ (let ((inhibit-read-only t)
+ (shr-external-rendering-functions
+ '((form . eww-tag-form)
+ (input . eww-tag-input)
+ (submit . eww-tag-submit))))
+ (shr-insert-document document)
+ (eww-convert-widgets))
(goto-char (point-min))))
(defun eww-display-raw (charset)
(defun eww-setup-buffer ()
(pop-to-buffer (get-buffer-create "*eww*"))
+ (remove-overlays)
+ (setq widget-field-list nil)
(let ((inhibit-read-only t))
(erase-buffer))
(eww-mode))
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)
(let ((prev (pop eww-history)))
(url-retrieve (car prev) 'eww-render (list (car prev) (cadr prev)))))
+;; Form support.
+
+(defvar eww-form nil)
+
+(defun eww-tag-form (cont)
+ (let ((eww-form
+ (list (assq :method cont)
+ (assq :action cont)))
+ (start (point)))
+ (shr-ensure-paragraph)
+ (shr-generic cont)
+ (shr-ensure-paragraph)
+ (put-text-property start (1+ start)
+ 'eww-form eww-form)))
+
+(defun eww-tag-input (cont)
+ (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
+ :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))
+ (if (and (stringp (getf form :method))
+ (equal (downcase (getf form :method)) "post"))
+ (let ((url-request-method "POST")
+ (url-request-data (mm-url-encode-www-form-urlencoded values)))
+ (eww-browse-url (shr-expand-url (getf form :action))))
+ (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)
+ (while (setq start (next-single-property-change start 'eww-widget))
+ (setq widget (get-text-property start 'eww-widget))
+ (goto-char start)
+ (delete-region start (next-single-property-change start 'eww-widget))
+ (apply 'widget-create widget)
+ (put-text-property start (point) 'not-read-only t))
+ (widget-setup)))
+
(provide 'eww)
;;; eww.el ends here
(defvar shr-stylesheet nil)
(defvar shr-base nil)
(defvar shr-ignore-cache nil)
+(defvar shr-external-rendering-functions nil)
(defvar shr-map
(let ((map (make-sparse-keymap)))
(nreverse result)))
(defun shr-descend (dom)
- (let ((function (intern (concat "shr-tag-" (symbol-name (car dom))) obarray))
+ (let ((function
+ (or
+ ;; Allow other packages to override (or provide) rendering
+ ;; of elements.
+ (cdr (assq (car dom) shr-external-rendering-functions))
+ (intern (concat "shr-tag-" (symbol-name (car dom))) obarray)))
(style (cdr (assq :style (cdr dom))))
(shr-stylesheet shr-stylesheet)
(start (point)))
(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))