Subject: bug#18267: eww: should trim <title /> contents, which affects history

From: Ivan Shmakov <[email-masked]>
Date: Thu, 14 Aug 2014 21:15:20 +0000

Package:  emacs
Severity: minor
Tags: patch

	EWW currently fails to trim any leading and trailing whitespace
	/and/ newline codes off the <title /> contents, as in the
	following (valid) HTML document:

<!DOCTYPE html>
<title>
A title with newlines.
</title>

	Here, eww-current-title will be "\nA title with newlines.\n",
	which causes the *eww-history* buffer contents to be formatted like:

A title with newlines.
   http://example.org/

	Which, in turn, causes eww-history-browse to fail (unless used
	on the first line of such an entry), due to the use of
	(line-beginning-position):

  (let ((history (get-text-property (line-beginning-position) 'eww-history)))
    (unless history
      (error "No history on the current line"))
    (quit-window)

	I suggest that this issue be resolved as follows:

	• eww-tag-title is changed to strip any [[:blank:]\r\n]+ from
	  the <title /> element textual content; (it may also turn any
	  embedded [\r\n] codes into ordinary blanks, though it isn’t
	  /strictly/ necessary given the rest of the changes below);

	• eww-list-histories turns any control characters in :title into
	  something printable (say, using the caret notation; ^J for \n,
	  etc.; this doesn’t cover non-ASCII controls, though);

	• the eww-history property is made to cover the whole
	  *eww-history* entry, – not just its initial character;
	  conversely, eww-history-browse is changed to use (point)
	  instead of (line-beginning-position).

	Also, while we’re at it, I suggest getting rid of the
	right-padding of the /last/ (as in: second) field of the history
	records, for it’s entirely unnecessary, and makes copying the
	URIs from *eww-history* harder.

	A possible patch is MIMEd.  (Note that the eww-tag-title change
	looks a bit too verbose, though.)

-- 
FSF associate member #7257  http://boycottsystemd.org/  … 3013 B6A0 230E 334A
--- a/lisp/net/eww.el
+++ b/lisp/net/eww.el
@@ -344,10 +387,20 @@ word(s) will be searched for via `eww-search-prefix'."
     (setq header-line-format nil)))
 
 (defun eww-tag-title (cont)
-  (setq eww-current-title "")
-  (dolist (sub cont)
-    (when (eq (car sub) 'text)
-      (setq eww-current-title (concat eww-current-title (cdr sub)))))
+  (setq eww-current-title
+	(with-temp-buffer
+	  (dolist (sub cont)
+	    (when (eq (car sub) 'text)
+	      (insert (cdr sub))))
+	  ;; trim leading and trailing whitespace
+	  (goto-char (point-min))
+	  (when (re-search-forward "^[[:blank:]\r\n]+" nil t)
+	    (replace-match ""))
+	  (goto-char (point-max))
+	  (when (re-search-backward "[[:blank:]\r\n]+$" nil t)
+	    (replace-match ""))
+	  ;; .
+	  (buffer-string)))
   (eww-update-header-line-format))
 
 (defun eww-tag-body (cont)
@@ -1303,23 +1370,29 @@ Differences in #targets are ignored."
 	(setq start (point))
 	(setq domain-length (max domain-length (length (plist-get history :url))))
 	(setq title-length (max title-length (length (plist-get history :title)))))
-      (setq format (format "%%-%ds %%-%ds" title-length domain-length)
+      (setq format (format "%%-%ds %%s" title-length)
 	    header-line-format
 	    (concat " " (format format "Title" "URL")))
       (dolist (history eww-history-trans)
 	(setq start (point))
 	(setq url (plist-get history :url))
-	(setq title (plist-get history :title))
+	(with-temp-buffer
+	  (insert (plist-get history :title))
+	  (goto-char (point-min))
+	  (while (re-search-forward "[\000-\037]" nil t)
+	    (replace-match
+	     (string ?^ (logior ?@ (aref (match-string 0) 0)))))
+	  (setq title (buffer-string)))
 	(insert (format format title url))
 	(insert "\n")
-	(put-text-property start (1+ start) 'eww-history history))
+	(put-text-property start (point) 'eww-history history))
       (goto-char (point-min)))
     (pop-to-buffer "*eww history*")))
 
 (defun eww-history-browse ()
   "Browse the history under point in eww."
   (interactive)
-  (let ((history (get-text-property (line-beginning-position) 'eww-history)))
+  (let ((history (get-text-property (point) 'eww-history)))
     (unless history
       (error "No history on the current line"))
     (quit-window)