* doc/lispref/text.texi (Document Object Model): Mention `dom-pp'.
* lisp/dom.el (dom-pp): New function.
+2014-11-27 Lars Magne Ingebrigtsen <larsi@gnus.org>
+
+ * text.texi (Document Object Model): Mention `dom-pp'.
+
2014-11-26 Lars Magne Ingebrigtsen <larsi@gnus.org>
* text.texi (Document Object Model): New node to document dom.el.
(document object model):
@example
-(html ()
- (head ())
- (body ((width . "101"))
- (div ((class . "thing"))
- "Foo"
- (div ()
- "Yes"))))
+(html nil
+ (head nil)
+ (body ((width . "101"))
+ (div ((class . "thing"))
+ "Foo"
+ (div nil
+ "Yes"))))
@end example
@end defun
nodes are either strings or @acronym{DOM} objects.
@example
-(body
- ((width . "101"))
- (div
- ((class . "thing"))
+(body ((width . "101"))
+ (div ((class . "thing"))
"Foo"
- (div
- nil
+ (div nil
"Yes")))
@end example
@item dom-children @var{node}
Return all the children of the node.
+@item dom-non-text-children @var{node}
+Return all the non-string children of the node.
+
@item dom-attributes @var{node}
Return the key/value pair list of attributes of the node.
@end table
+Utility functions:
+
+@table @code
+@item dom-pp @var{dom} &optional @var{remove-empty}
+Pretty-print @var{dom} at point. If @var{remove-empty}, don't print
+textual nodes that just contain white-space.
+@end table
+
@node Atomic Changes
@section Atomic Change Groups
+2014-11-27 Lars Magne Ingebrigtsen <larsi@gnus.org>
+
+ * dom.el (dom-pp): New function.
+
2014-11-17 Eli Zaretskii <eliz@gnu.org>
* vc/vc-bzr.el (vc-bzr-print-log, vc-bzr-expanded-log-entry):
(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