; Use HTML entities for reserved characters in 'dom-print'
authorEshel Yaron <me@eshelyaron.com>
Fri, 5 Jan 2024 15:40:44 +0000 (16:40 +0100)
committerEli Zaretskii <eliz@gnu.org>
Sat, 20 Jan 2024 09:40:22 +0000 (11:40 +0200)
* 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
test/lisp/dom-tests.el

index f7043ba825220feb2ac6fd330265917f7ec9058d..b329379fdc34cc70b7337a7554264c2e16ebe129 100644 (file)
@@ -288,7 +288,7 @@ If XML, generate XML instead of HTML."
        (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)))
index 8cbfb9ad9df81f7b9764440ba14eac83557a91b4..a4e913541bf47293aaf31f8f980dfdeb158badd7 100644 (file)
@@ -209,6 +209,16 @@ child results in an error."
       (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)))