Subject: [PATCH] ; (dom-print): Use HTML entities for reserved characters.
From: Eshel Yaron <[email-masked]>
To: [email-masked]
Date: Tue, 16 Jan 2024 14:24:40 +0100
List-Id: <debbugs-submit.debbugs.gnu.org>

Tags: patch

This makes `dom-print` encode HTML reserved characters that occur in
string elements of the DOM, to ensure the validity of the result.

For example, put the following in `foo.html`:

--8<---------------cut here---------------start------------->8---
<html><body>
Add ‘<samp class="samp">&lt;div class="default"&gt; &lt;/div&gt;</samp>’ tags around the fontified body.
<body><html>
--8<---------------cut here---------------end--------------->8---
(Fragment from https://www.gnu.org/software/emacs/manual/html_mono/htmlfontify.html)

Open that file in Emacs and say `M-: (require 'dom)` and then
`(dom-print (libxml-parse-html-region))` in the HTML buffer.  This
produces invalid HTML since `libxml-parse-html-region` correctly decodes
HTML entities, but `dom-print` doesn't encode (without this patch).

From 259c0138623c352acc7bcd79a1fda42ec606a0cf Mon Sep 17 00:00:00 2001
From: Eshel Yaron <[email-masked]>
Date: Fri, 5 Jan 2024 16:40:44 +0100
Subject: [PATCH] ; (dom-print): Use HTML entities for reserved characters.

---
 lisp/dom.el | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lisp/dom.el b/lisp/dom.el
index f7043ba8252..b329379fdc3 100644
--- a/lisp/dom.el
+++ b/lisp/dom.el
@@ -288,7 +288,7 @@ dom-print
 	(insert ">")
         (dolist (child children)
 	  (if (stringp child)
-	      (insert child)
+	      (insert (url-insert-entities-in-string child))
 	    (setq non-text t)
 	    (when pretty
               (insert "\n" (make-string (+ column 2) ?\s)))
-- 
2.42.0