summaryrefslogtreecommitdiff
path: root/lisp/dom.el
diff options
context:
space:
mode:
authorLars Magne Ingebrigtsen <larsi@gnus.org>2014-11-27 16:57:22 +0100
committerLars Magne Ingebrigtsen <larsi@gnus.org>2014-11-27 16:57:22 +0100
commit2d431afee4061515a593da1f0a29bcd5fb152f07 (patch)
tree003974df1a3052789ea6f7a7a4f747e9a2973fe6 /lisp/dom.el
parent2f5134c2766be5dcc3eb32b391183a229ee57e19 (diff)
downloademacs-2d431afee4061515a593da1f0a29bcd5fb152f07.tar.gz
Add a DOM pretty-printing function
* doc/lispref/text.texi (Document Object Model): Mention `dom-pp'. * lisp/dom.el (dom-pp): New function.
Diffstat (limited to 'lisp/dom.el')
-rw-r--r--lisp/dom.el38
1 files changed, 38 insertions, 0 deletions
diff --git a/lisp/dom.el b/lisp/dom.el
index 04d6c219ec0..6b24e4ffa91 100644
--- a/lisp/dom.el
+++ b/lisp/dom.el
@@ -179,6 +179,44 @@ If BEFORE is nil, make CHILD NODE's first child."
(setcdr node (list nil)))
node)
+(defun dom-pp (dom &optional remove-empty)
+ "Pretty-print DOM at point.
+If REMOVE-EMPTY, ignore textual nodes that contain just
+white-space."
+ (let ((column (current-column)))
+ (insert (format "(%S " (dom-tag dom)))
+ (let* ((attr (dom-attributes dom))
+ (times (length attr))
+ (column (1+ (current-column))))
+ (if (null attr)
+ (insert "nil")
+ (insert "(")
+ (dolist (elem attr)
+ (insert (format "(%S . %S)" (car elem) (cdr elem)))
+ (if (zerop (cl-decf times))
+ (insert ")")
+ (insert "\n" (make-string column ? ))))))
+ (let* ((children (if remove-empty
+ (cl-remove-if
+ (lambda (child)
+ (and (stringp child)
+ (string-match "\\`[\n\r\t  ]*\\'" child)))
+ (dom-children dom))
+ (dom-children dom)))
+ (times (length children)))
+ (if (null children)
+ (insert ")")
+ (insert "\n" (make-string (1+ column) ? ))
+ (dolist (child children)
+ (if (stringp child)
+ (if (or (not remove-empty)
+ (not (string-match "\\`[\n\r\t  ]*\\'" child)))
+ (insert (format "%S" child)))
+ (dom-pp child remove-empty))
+ (if (zerop (cl-decf times))
+ (insert ")")
+ (insert "\n" (make-string (1+ column) ? ))))))))
+
(provide 'dom)
;;; dom.el ends here