all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* bug#52594: [PATCH] Add image whiltelist to shr.el
@ 2021-12-18  8:22 LdBeth
  2021-12-19 11:26 ` Lars Ingebrigtsen
  0 siblings, 1 reply; 4+ messages in thread
From: LdBeth @ 2021-12-18  8:22 UTC (permalink / raw)
  To: 52594

[-- Attachment #1: Type: text/plain, Size: 417 bytes --]

shr.el has already a blacklist feature via `shr-blocked-images'
variable. However in the setting of using shr as an email pager to
display HTML contents, it is much more useful to have a "whitelist"
feature.

Summary of changes:

Added variable `shr-safe-images', nil means all images are
allowed. This can be overriden by setting existed `shr-blocked-images'.

Added subst function `shr-image-is-blocked'.

-- 
LDB


[-- Attachment #2: shr_whitelist.patch --]
[-- Type: text/plain, Size: 2358 bytes --]

--- shr.el.old	2021-12-18 16:04:43.000000000 +0800
+++ shr.el	2021-12-18 16:18:30.000000000 +0800
@@ -56,8 +56,16 @@
   :version "24.1"
   :type 'float)
 
+(defcustom shr-safe-images nil
+  "Only images that have URLs matching this regexp will be displayed.
+The nil value means all URLs are allowed. This can be overrided
+by `shr-blocked-images'."
+  :version "29.1"
+  :type '(choice (const nil) regexp))
+
 (defcustom shr-blocked-images nil
-  "Images that have URLs matching this regexp will be blocked."
+  "Images that have URLs matching this regexp will be blocked.
+This take effect in prior to `shr-safe-images'."
   :version "24.1"
   :type '(choice (const nil) regexp))
 
@@ -483,6 +491,12 @@
 	(shr-insert sub)
       (shr-descend sub))))
 
+(defsubst shr-image-is-blocked (url)
+  (or (and shr-blocked-images
+           (string-match shr-blocked-images url))
+      (and shr-safe-images
+           (not (string-match shr-safe-images url)))))
+
 (defun shr-indirect-call (tag-name dom &rest args)
   (let ((function (intern (concat "shr-tag-" (symbol-name tag-name)) obarray))
 	;; Allow other packages to override (or provide) rendering
@@ -1186,7 +1200,7 @@
     ;; SVG images may contain references to further images that we may
     ;; want to block.  So special-case these by parsing the XML data
     ;; and remove anything that looks like a blocked bit.
-    (when (and shr-blocked-images
+    (when (and (or shr-safe-images shr-blocked-images)
                (eq content-type 'image/svg+xml))
       (setq data
             ;; Note that libxml2 doesn't parse everything perfectly,
@@ -1380,8 +1394,7 @@
        ((or (not (eq (dom-tag elem) 'image))
 	    ;; Filter out blocked elements inside the SVG image.
 	    (not (setq url (dom-attr elem ':xlink:href)))
-	    (not shr-blocked-images)
-	    (not (string-match shr-blocked-images url)))
+	    (not (shr-image-is-blocked url)))
 	(insert " ")
 	(shr-dom-print elem)))))
   (insert (format "</%s>" (dom-tag dom))))
@@ -1657,8 +1670,7 @@
 	      (funcall shr-put-image-function image alt
                        (list :width width :height height)))))
 	 ((or shr-inhibit-images
-	      (and shr-blocked-images
-		   (string-match shr-blocked-images url)))
+	      (shr-image-is-blocked url))
 	  (setq shr-start (point))
           (shr-insert alt))
 	 ((and (not shr-ignore-cache)

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-12-20 10:18 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-12-18  8:22 bug#52594: [PATCH] Add image whiltelist to shr.el LdBeth
2021-12-19 11:26 ` Lars Ingebrigtsen
2021-12-20  6:18   ` LdBeth
2021-12-20 10:18     ` Lars Ingebrigtsen

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.