;;; dom.el --- XML/HTML (etc.) DOM manipulation and searching functions
-;; Copyright (C) 2014 Free Software Foundation, Inc.
+;; Copyright (C) 2014-2015 Free Software Foundation, Inc.
;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
;; Keywords: xml, html
(cddr (car node))
(cddr node)))
+(defun dom-non-text-children (node)
+ "Return all non-text-node children of NODE."
+ (cl-loop for child in (dom-children node)
+ unless (stringp child)
+ collect child))
+
(defun dom-set-attributes (node attributes)
"Set the attributes of NODE to ATTRIBUTES."
(setq node (dom-ensure-node node))
(dom-by-tag child tag))
when matches
append matches)))
- (if (eq (dom-tag dom) tag)
+ (if (equal (dom-tag dom) tag)
(cons dom matches)
matches)))
"Find elements matching MATCH (a regexp) in ATTRIBUTE.
ATTRIBUTE would typically be `class', `id' or the like."
(let ((matches (cl-loop for child in (dom-children dom)
- for matches = (dom-elements child attribute match)
+ for matches = (and (not (stringp child))
+ (dom-elements child attribute
+ match))
when matches
append matches))
(attr (dom-attr dom attribute)))
(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