Subject: Re: bug#24831: shr mangling messages

From: Katsumi Yamaoka <[email-masked]>
Date: Tue, 01 Nov 2016 18:59:39 +0900

On Tue, 01 Nov 2016 10:39:12 +0900, Katsumi Yamaoka wrote:
> I found the cause of the problem that shr does not display the
> "Hi, you have a new email..."
> statement contained in the example message.  That is, the message
> has a table in which the td element is omitted or lost.

I tried fixing it.  A patch is below.  But I feel it somewhat
awkward, so I hope Lars or someone will review it.  My patch
simply adds the missing td tag as follows:

(table nil (tr nil contents))
	↓
(table nil (tr nil (td nil contents)))

Thanks.
--- shr.el~	2016-11-01 02:35:57.788777000 +0000
+++ shr.el	2016-11-01 09:51:32.251984400 +0000
@@ -1759,6 +1759,7 @@
 ;; we then render everything again with the new widths, and finally
 ;; insert all these boxes into the main buffer.
 (defun shr-tag-table-1 (dom)
+  (shr-add-missing-td dom)
   (setq dom (or (dom-child-by-tag dom 'tbody) dom))
   (let* ((shr-inhibit-images t)
 	 (shr-table-depth (1+ shr-table-depth))
@@ -1787,6 +1788,19 @@
     ;; Then render the table again with these new "hard" widths.
     (shr-insert-table (shr-make-table dom sketch-widths t) sketch-widths)))
 
+(defun shr-add-missing-td (dom)
+  "Add missing td tag to table."
+  (let (tr td)
+    (dolist (elem (dom-children dom))
+      (when (eq (car-safe elem) 'tr)
+	(setq tr elem
+	      td nil
+	      elem (cddr elem))
+	(while (and (not td) elem)
+	  (setq td (eq (car-safe (pop elem)) 'td)))
+	(unless td
+	  (setcdr (cdr tr) (list (cons 'td (cons nil (cddr tr))))))))))
+
 (defun shr-table-body (dom)
   (let ((tbodies (seq-filter (lambda (child)
                                (eq (dom-tag child) 'tbody))