Subject: bug#68508: [PATCH] ; (dom-print): Use HTML entities for reserved characters.

From: Eshel Yaron <[email-masked]>
Date: Tue, 16 Jan 2024 17:29:12 +0100

Eli Zaretskii <[email-masked]> writes:

>> Date: Tue, 16 Jan 2024 14:24:40 +0100
>> From:  Eshel Yaron via "Bug reports for GNU Emacs,
>>  the Swiss army knife of text editors" <[email-masked]>
>>
>> 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).
>
> Thanks, but could you please also add tests for this?

Sure, I've added a test to dom-tests.el in the updated patch below.

>From 8d60074053ee1ebc04fc3fda417d53ddc5a4fac9 Mon Sep 17 00:00:00 2001
From: Eshel Yaron <[email-masked]>
Date: Fri, 5 Jan 2024 16:40:44 +0100
Subject: [PATCH v2] ; Use HTML entities for reserved characters in 'dom-print'

* lisp/dom.el (dom-print): Encode HTML reserved characters in strings.
* test/lisp/dom-tests.el (dom-tests-print): New test.  (Bug#68508)
---
 lisp/dom.el            |  2 +-
 test/lisp/dom-tests.el | 10 ++++++++++
 2 files changed, 11 insertions(+), 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)))
diff --git a/test/lisp/dom-tests.el b/test/lisp/dom-tests.el
index 8cbfb9ad9df..a4e913541bf 100644
--- a/test/lisp/dom-tests.el
+++ b/test/lisp/dom-tests.el
@@ -209,6 +209,16 @@ dom-tests-pp
       (dom-pp node t)
       (should (equal (buffer-string) "(\"foo\" nil)")))))
 
+(ert-deftest dom-tests-print ()
+  "Test that `dom-print' correctly encodes HTML reserved characters."
+  (with-temp-buffer
+    (dom-print '(samp ((class . "samp")) "<div class=\"default\"> </div>"))
+    (should (equal
+             (buffer-string)
+             (concat "<samp class=\"samp\">"
+                     "&lt;div class=&quot;default&quot;&gt; &lt;/div&gt;"
+                     "</samp>")))))
+
 (ert-deftest dom-test-search ()
   (let ((dom '(a nil (b nil (c nil)))))
     (should (equal (dom-search dom (lambda (d) (eq (dom-tag d) 'a)))
-- 
2.42.0