Subject: bug#73133: 29.2; EWW fails to render some webpages

From: Sebastián Monía <[email-masked]>
Date: Wed, 09 Oct 2024 22:08:14 -0400

Jim Porter <[email-masked]> writes:
> I believe this could be:
>
>   (or (run-hook-with-args-until-success
>        'eww-guess-content-type-functions headers response-buffer)
>       "text/plain")

TIL. I landed in seq-some looking for something like
run-hook-with-args-until-sucess. So I actually learned two days in a
row! :)

Attached a modified patch. I also noticed and corrected another error,
that broke things when using the "g" (reload) command.

As for testing, I used this:

(defun do-ask (headers response)
  (when (y-or-n-p "decide?")
    (if (y-or-n-p "render?")
        "text/html"
      "text/plain")))

(setq eww-guess-content-type-functions
      '(do-ask eww--html-if-doctype))

And then reverse the order of the functions. Using "regular"
pages and the one reported in the bug.
Also tested with no functions.

>From 5239cf0add09f69276ae21c13efb2fe665297234 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Sebasti=C3=A1n=20Mon=C3=ADa?= <[email-masked]>
Date: Tue, 8 Oct 2024 23:26:42 -0400
Subject: [PATCH] Add customization to let EWW guess content-type if needed
 (bug#73133)

---
 lisp/net/eww.el | 39 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 38 insertions(+), 1 deletion(-)

diff --git a/lisp/net/eww.el b/lisp/net/eww.el
index b5d2f20781a..30e780a44d9 100644
--- a/lisp/net/eww.el
+++ b/lisp/net/eww.el
@@ -108,6 +108,19 @@ eww-suggest-uris
              eww-current-url
              eww-bookmark-urls))
 
+(defcustom eww-guess-content-type-functions
+  '(eww--html-if-doctype)
+  "List of functions used to guess a page's content-type.
+These are only used when the page does not have a valid Content-Type
+header.  Functions are called in order, until one of them returns the
+value to be used as Content-Type.  They receive two parameters: an alist
+of headers, and the buffer that holds the complete response.  If the
+list is exhausted, eww assumes \"text/plain\" so the user can see the
+markup."
+  :version "31.1"
+  :group 'eww
+  :type '(repeat function))
+
 (defcustom eww-bookmarks-directory user-emacs-directory
   "Directory where bookmark files will be stored."
   :version "25.1"
@@ -630,6 +643,30 @@ eww-html-p
   (member content-type '("text/html"
 			 "application/xhtml+xml")))
 
+(defun eww--guess-content-type (headers response-buffer)
+  "Use HEADERS and RESPONSE to guess the Content-Type.
+Will call each function in `eww-guess-content-type-functions', until one
+of them returns a value.  This mechanism is used only if there isn't a
+valid Content-Type header.  If none of the functions can guess, return
+\"text/plain\", so at least the mark up is displayed."
+  (or (run-hook-with-args-until-success
+       'eww-guess-content-type-functions headers response-buffer)
+      "text/plain"))
+
+(defun eww--html-if-doctype (headers response-buffer)
+  "Return \"text/html\" if RESPONSE-BUFFER has an HTML doctype declaration.
+HEADERS is unused."
+  ;; https://html.spec.whatwg.org/multipage/syntax.html#the-doctype
+  (let ((case-fold-search t)
+        (target
+         "<!doctype +html *\\(>\\|system +\\(\\\"\\|'\\)+about:legacy-compat\\)"))
+    (with-current-buffer response-buffer
+      (goto-char (point-min))
+      ;; match basic <!doctype html> and also legacy variants as
+      ;; specified in link above
+      (when (re-search-forward target nil t)
+        "text/html"))))
+
 (defun eww--rename-buffer ()
   "Rename the current EWW buffer.
 The renaming scheme is performed in accordance with
@@ -659,7 +696,7 @@ eww-render
 	 (content-type
 	  (mail-header-parse-content-type
            (if (zerop (length (cdr (assoc "content-type" headers))))
-	       "text/plain"
+               (eww--guess-content-type headers (current-buffer))
              (cdr (assoc "content-type" headers)))))
 	 (charset (intern
 		   (downcase
-- 
2.43.0