Subject: 27.0.60; [PATCH] eww/shr: Ensure faces of enclosing elements apply to <code> elements

From: Kévin Le Gouguec <[email-masked]>
Date: Sat, 08 Feb 2020 01:06:11 +0100

Hello!

I have a minor gripe with the way eww/shr render <code> elements within
other inline elements.  My main frustration stems from code inside
links[1], but this extends to other elements, e.g. <strong> and <em>:

Variable fixed variable

Variable fixed variable

Variable fixed variable

Variable fixed variable

Here is how Emacs 26.3 rendered it:

Here is how Emacs 27 and 28 render it:

Both Emacs variants fail to do what I'd expect:

- Emacs 26 uses variable-pitch for <code> elements,
- Emacs 27+ uses the default face, which does ensure a monospace font,
  but also neutralizes any effects of enclosing elements, e.g.
    - no bold inside <strong>,
    - no italics inside <em>,
    - no blue color nor underline inside <a>.

For example, here is how Firefox 72 does it:

Here is a first, simple patch that uses fixed-pitch instead of default
to ensure other face attributes are not overridden:

>From 36629660e9adf4cd500b081e8b45c76cb1d8cb4d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?K=C3=A9vin=20Le=20Gouguec?= <[email-masked]>
Date: Sat, 8 Feb 2020 00:08:13 +0100
Subject: [PATCH] Ensure faces of enclosing elements apply to <code> elements
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The default face overrides the attributes of every enclosing element.
E.g. with <a href="…">foo <code>bar</code> baz</a>, since shr applies
'default' top of 'shr-link', "bar" has neither a blue foreground nor
an underline.

* lisp/net/shr.el (shr-tag-code): Use a face that only sets relevant
attributes.
---
 lisp/net/shr.el | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lisp/net/shr.el b/lisp/net/shr.el
index 241180d471..87106b0bd6 100644
--- a/lisp/net/shr.el
+++ b/lisp/net/shr.el
@@ -1438,7 +1438,7 @@ shr-tag-u
   (shr-fontize-dom dom 'underline))
 
 (defun shr-tag-code (dom)
-  (let ((shr-current-font 'default))
+  (let ((shr-current-font 'fixed-pitch))
     (shr-generic dom)))
 
 (defun shr-tag-tt (dom)
-- 
2.20.1

Here is an alternative patch which allows customizing the face used for
<code>, in case the user wants e.g. fixed-pitch-serif instead of
fixed-pitch:

>From b648822ac4e401984f967fdc2d78e177697089ab Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?K=C3=A9vin=20Le=20Gouguec?= <[email-masked]>
Date: Sat, 8 Feb 2020 00:08:13 +0100
Subject: [PATCH] Introduce face for <code> elements
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The default face overrides the attributes of every enclosing element.
E.g. with <a href="…">foo <code>bar</code> baz</a>, since shr applies
'default' top of 'shr-link', "bar" has neither a blue foreground nor
an underline.

* lisp/net/shr.el (shr-code): New face.
(shr-tag-code): Use it.
---
 lisp/net/shr.el | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/lisp/net/shr.el b/lisp/net/shr.el
index 241180d471..1215fbddd0 100644
--- a/lisp/net/shr.el
+++ b/lisp/net/shr.el
@@ -142,6 +142,10 @@ shr-strike-through
   "Face for <s> elements."
   :version "24.1")
 
+(defface shr-code '((t :inherit fixed-pitch))
+  "Face for <code> elements."
+  :version "27.1")
+
 (defface shr-link
   '((t :inherit link))
   "Face for link elements."
@@ -1438,7 +1442,7 @@ shr-tag-u
   (shr-fontize-dom dom 'underline))
 
 (defun shr-tag-code (dom)
-  (let ((shr-current-font 'default))
+  (let ((shr-current-font 'shr-code))
     (shr-generic dom)))
 
 (defun shr-tag-tt (dom)
-- 
2.20.1

Both patches give the same results:

If the second patch seems like a worthwhile addition, I can augment it
with some documentation.

Thank you for your time.

[1] Cf. for example This Week In Rust:
    https://this-week-in-rust.org/blog/2020/02/04/this-week-in-rust-324/#updates-from-rust-core

In GNU Emacs 28.0.50 (build 2, x86_64-pc-linux-gnu, GTK+ Version 3.24.5, cairo version 1.16.0)
 of 2020-01-31 built on hirondell
Repository revision: d3ead375092e2690c1d1d6a5dd82e6e89cdf4f4c
Repository branch: master
Windowing system distributor 'The X.Org Foundation', version 11.0.12004000
System Description: Debian GNU/Linux 10 (buster)

Configured using:
 'configure --with-xwidgets --with-cairo'

Configured features:
XPM JPEG TIFF GIF PNG RSVG CAIRO SOUND GPM DBUS GSETTINGS GLIB NOTIFY
INOTIFY ACL LIBSELINUX GNUTLS LIBXML2 FREETYPE HARFBUZZ M17N_FLT LIBOTF
ZLIB TOOLKIT_SCROLL_BARS GTK3 X11 XDBE XIM MODULES THREADS XWIDGETS
LIBSYSTEMD JSON PDUMPER LCMS2 GMP

Important settings:
  value of $LANG: en_US.UTF-8
  locale-coding-system: utf-8-unix