]> code.delx.au - gnu-emacs/blobdiff - lisp/net/shr.el
Doc fixes for menu-bar.el
[gnu-emacs] / lisp / net / shr.el
index 0703171a436d5cbc584ef5421f09ff4646360bbc..2c8ff79763f3cc36219d49f864cb0bac17f1c53c 100644 (file)
@@ -35,6 +35,7 @@
 (require 'browse-url)
 (require 'subr-x)
 (require 'dom)
+(require 'seq)
 
 (defgroup shr nil
   "Simple HTML Renderer"
@@ -782,8 +783,13 @@ size, and full-buffer size."
         (url-expand-file-name url (concat (car base) (cadr base))))))
 
 (defun shr-ensure-newline ()
-  (unless (zerop (current-column))
-    (insert "\n")))
+  (unless (bobp)
+    (let ((prefix (get-text-property (line-beginning-position)
+                                    'shr-prefix-length)))
+      (unless (or (zerop (current-column))
+                  (and prefix
+                       (= prefix (- (point) (line-beginning-position)))))
+        (insert "\n")))))
 
 (defun shr-ensure-paragraph ()
   (unless (bobp)
@@ -811,6 +817,10 @@ size, and full-buffer size."
                                                    (line-end-position))
                       (line-end-position)))))
        (delete-region (match-beginning 0) (match-end 0)))
+       ;; We have a single blank line.
+       ((and (eolp) (bolp))
+        (insert "\n"))
+       ;; Insert new paragraph.
        (t
        (insert "\n\n"))))))
 
@@ -914,7 +924,8 @@ If EXTERNAL, browse the URL using `shr-external-browser'."
     (let ((param (match-string 4 data))
          (payload (url-unhex-string (match-string 5 data))))
       (when (string-match "^.*\\(;[ \t]*base64\\)$" param)
-       (setq payload (base64-decode-string payload)))
+       (setq payload (ignore-errors
+                        (base64-decode-string payload))))
       payload)))
 
 ;; Behind display-graphic-p test.
@@ -965,7 +976,7 @@ element is the data blob and the second element is the content-type."
                             (image-animated-p image))))
             (image-animate image nil 60)))
        image)
-    (insert alt)))
+    (insert (or alt ""))))
 
 (defun shr-rescale-image (data &optional content-type)
   "Rescale DATA, if too big, to fit the current buffer."
@@ -1408,9 +1419,7 @@ The preference is a float determined from `shr-prefer-media-type'."
              (and shr-blocked-images
                   (string-match shr-blocked-images url)))
          (setq shr-start (point))
-         (if (> (string-width alt) 8)
-             (shr-insert (truncate-string-to-width alt 8))
-           (shr-insert alt)))
+          (shr-insert alt))
         ((and (not shr-ignore-cache)
               (url-is-cached (shr-encode-url url)))
          (funcall shr-put-image-function (shr-get-image-data url) alt))
@@ -1471,6 +1480,10 @@ The preference is a float determined from `shr-prefer-media-type'."
   (shr-ensure-paragraph)
   (let ((shr-list-mode 'ul))
     (shr-generic dom))
+  ;; If we end on an empty <li>, then make sure we really end on a new
+  ;; paragraph.
+  (unless (bolp)
+    (insert "\n"))
   (shr-ensure-paragraph))
 
 (defun shr-tag-ol (dom)
@@ -1600,7 +1613,9 @@ The preference is a float determined from `shr-prefer-media-type'."
     (shr-insert-table (shr-make-table dom sketch-widths t) sketch-widths)))
 
 (defun shr-table-body (dom)
-  (let ((tbodies (dom-by-tag dom 'tbody)))
+  (let ((tbodies (seq-filter (lambda (child)
+                               (eq (dom-tag child) 'tbody))
+                             (dom-non-text-children dom))))
     (cond
      ((null tbodies)
       dom)
@@ -1751,17 +1766,18 @@ The preference is a float determined from `shr-prefer-media-type'."
                                 align)))
              (dolist (line lines)
                (end-of-line)
-               (let ((start (point)))
-                 (insert
-                  line
-                  (propertize " "
-                              'display `(space :align-to (,pixel-align))
-                              'face (and (> (length line) 0)
-                                         (shr-face-background
-                                          (get-text-property
-                                           (1- (length line)) 'face line)))
-                              'shr-table-indent shr-table-id)
-                  shr-table-vertical-line)
+               (let ((start (point))
+                      (background (and (> (length line) 0)
+                                       (shr-face-background
+                                        (get-text-property
+                                         (1- (length line)) 'face line))))
+                      (space (propertize
+                              " "
+                              'display `(space :align-to (,pixel-align))
+                              'shr-table-indent shr-table-id)))
+                  (when background
+                    (setq space (propertize space 'face background)))
+                 (insert line space shr-table-vertical-line)
                  (shr-colorize-region
                   start (1- (point)) (nth 5 column) (nth 6 column)))
                (forward-line 1))
@@ -1784,13 +1800,16 @@ The preference is a float determined from `shr-prefer-media-type'."
 
 (defun shr-face-background (face)
   (and (consp face)
-       (let ((background nil))
-        (dolist (elem face)
-          (when (and (consp elem)
-                     (eq (car elem) :background))
-            (setq background (cadr elem))))
-        (and background
-             (list :background background)))))
+       (or (and (plist-get face :background)
+                (list :background (plist-get face :background)))
+           (let ((background nil))
+             (dolist (elem face)
+               (when (and (consp elem)
+                          (eq (car elem) :background)
+                          (not background))
+                 (setq background (cadr elem))))
+             (and background
+                  (list :background background))))))
 
 (defun shr-expand-alignments (start end)
   (while (< (setq start (next-single-property-change