Function: dom-print

dom-print is a byte-compiled function defined in dom.el.gz.

Signature

(dom-print DOM &optional PRETTY XML)

Documentation

Print DOM at point as HTML/XML.

If PRETTY, indent the HTML/XML logically. If XML, generate XML instead of HTML.

Probably introduced at or before Emacs version 28.1.

Source Code

;; Defined in /usr/src/emacs/lisp/dom.el.gz
(defun dom-print (dom &optional pretty xml)
  "Print DOM at point as HTML/XML.
If PRETTY, indent the HTML/XML logically.
If XML, generate XML instead of HTML."
  (let ((column (current-column))
        (indent-tabs-mode nil)) ;; Indent with spaces
    (insert (format "<%s" (dom-tag dom)))
    (pcase-dolist (`(,attr . ,value) (dom-attributes dom))
      ;; Don't print attributes without a value.
      (when value
        (insert
         ;; HTML boolean attributes should not have an = value.  The
         ;; presence of a boolean attribute on an element represents
         ;; the true value, and the absence of the attribute
         ;; represents the false value.
         (if (and (not xml) (dom--html-boolean-attribute-p attr))
             (format " %s" attr)
           (format " %s=%S" attr (url-insert-entities-in-string
                                  (format "%s" value)))))))
    (let* ((children (dom-children dom))
	   (non-text nil)
           (indent (+ column 2)))
      (if (null children)
	  (insert " />")
	(insert ">")
        (dolist (child children)
	  (if (stringp child)
	      (insert (url-insert-entities-in-string child))
	    (setq non-text t)
	    (when pretty
              (insert "\n")
              (indent-line-to indent))
	    (dom-print child pretty xml)))
	;; If we inserted non-text child nodes, or a text node that
	;; ends with a newline, then we indent the end tag.
        (when (and pretty (or (bolp) non-text))
	  (or (bolp) (insert "\n"))
	  (indent-line-to column))
        (insert (format "</%s>" (dom-tag dom)))))))