]> code.delx.au - gnu-emacs/blob - lisp/dom.el
Merge from origin/emacs-24
[gnu-emacs] / lisp / dom.el
1 ;;; dom.el --- XML/HTML (etc.) DOM manipulation and searching functions
2
3 ;; Copyright (C) 2014 Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: xml, html
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 (require 'cl-lib)
28
29 (defsubst dom-tag (node)
30 "Return the NODE tag."
31 ;; Called on a list of nodes. Use the first.
32 (if (consp (car node))
33 (caar node)
34 (car node)))
35
36 (defsubst dom-attributes (node)
37 "Return the NODE attributes."
38 ;; Called on a list of nodes. Use the first.
39 (if (consp (car node))
40 (cadr (car node))
41 (cadr node)))
42
43 (defsubst dom-children (node)
44 "Return the NODE children."
45 ;; Called on a list of nodes. Use the first.
46 (if (consp (car node))
47 (cddr (car node))
48 (cddr node)))
49
50 (defun dom-non-text-children (node)
51 "Return all non-text-node children of NODE."
52 (cl-loop for child in (dom-children node)
53 unless (stringp child)
54 collect child))
55
56 (defun dom-set-attributes (node attributes)
57 "Set the attributes of NODE to ATTRIBUTES."
58 (setq node (dom-ensure-node node))
59 (setcar (cdr node) attributes))
60
61 (defun dom-set-attribute (node attribute value)
62 "Set ATTRIBUTE in NODE to VALUE."
63 (setq node (dom-ensure-node node))
64 (let ((old (assoc attribute (cadr node))))
65 (if old
66 (setcdr old value)
67 (setcar (cdr node) (nconc (cadr node) (list (cons attribute value)))))))
68
69 (defmacro dom-attr (node attr)
70 "Return the attribute ATTR from NODE.
71 A typical attribute is `href'."
72 `(cdr (assq ,attr (dom-attributes ,node))))
73
74 (defun dom-text (node)
75 "Return all the text bits in the current node concatenated."
76 (mapconcat 'identity (cl-remove-if-not 'stringp (dom-children node)) " "))
77
78 (defun dom-texts (node &optional separator)
79 "Return all textual data under NODE concatenated with SEPARATOR in-between."
80 (mapconcat
81 'identity
82 (mapcar
83 (lambda (elem)
84 (if (stringp elem)
85 elem
86 (dom-texts elem separator)))
87 (dom-children node))
88 (or separator " ")))
89
90 (defun dom-child-by-tag (dom tag)
91 "Return the first child of DOM that is of type TAG."
92 (assoc tag (dom-children dom)))
93
94 (defun dom-by-tag (dom tag)
95 "Return elements in DOM that is of type TAG.
96 A name is a symbol like `td'."
97 (let ((matches (cl-loop for child in (dom-children dom)
98 for matches = (and (not (stringp child))
99 (dom-by-tag child tag))
100 when matches
101 append matches)))
102 (if (equal (dom-tag dom) tag)
103 (cons dom matches)
104 matches)))
105
106 (defun dom-by-class (dom match)
107 "Return elements in DOM that have a class name that matches regexp MATCH."
108 (dom-elements dom 'class match))
109
110 (defun dom-by-style (dom match)
111 "Return elements in DOM that have a style that matches regexp MATCH."
112 (dom-elements dom 'style match))
113
114 (defun dom-by-id (dom match)
115 "Return elements in DOM that have an ID that matches regexp MATCH."
116 (dom-elements dom 'id match))
117
118 (defun dom-elements (dom attribute match)
119 "Find elements matching MATCH (a regexp) in ATTRIBUTE.
120 ATTRIBUTE would typically be `class', `id' or the like."
121 (let ((matches (cl-loop for child in (dom-children dom)
122 for matches = (and (not (stringp child))
123 (dom-elements child attribute
124 match))
125 when matches
126 append matches))
127 (attr (dom-attr dom attribute)))
128 (if (and attr
129 (string-match match attr))
130 (cons dom matches)
131 matches)))
132
133 (defun dom-parent (dom node)
134 "Return the parent of NODE in DOM."
135 (if (memq node (dom-children dom))
136 dom
137 (let ((result nil))
138 (dolist (elem (dom-children dom))
139 (when (and (not result)
140 (not (stringp elem)))
141 (setq result (dom-parent elem node))))
142 result)))
143
144 (defun dom-node (tag &optional attributes &rest children)
145 "Return a DOM node with TAG and ATTRIBUTES."
146 (if children
147 `(,tag ,attributes ,@children)
148 (list tag attributes)))
149
150 (defun dom-append-child (node child)
151 "Append CHILD to the end of NODE's children."
152 (setq node (dom-ensure-node node))
153 (nconc node (list child)))
154
155 (defun dom-add-child-before (node child &optional before)
156 "Add CHILD to NODE's children before child BEFORE.
157 If BEFORE is nil, make CHILD NODE's first child."
158 (setq node (dom-ensure-node node))
159 (let ((children (dom-children node)))
160 (when (and before
161 (not (memq before children)))
162 (error "%s does not exist as a child" before))
163 (let ((pos (if before
164 (cl-position before children)
165 0)))
166 (if (zerop pos)
167 ;; First child.
168 (setcdr (cdr node) (cons child (cddr node)))
169 (setcdr (nthcdr (1- pos) children)
170 (cons child (nthcdr pos children))))))
171 node)
172
173 (defun dom-ensure-node (node)
174 "Ensure that NODE is a proper DOM node."
175 ;; Add empty attributes, if none.
176 (when (consp (car node))
177 (setq node (car node)))
178 (when (= (length node) 1)
179 (setcdr node (list nil)))
180 node)
181
182 (defun dom-pp (dom &optional remove-empty)
183 "Pretty-print DOM at point.
184 If REMOVE-EMPTY, ignore textual nodes that contain just
185 white-space."
186 (let ((column (current-column)))
187 (insert (format "(%S " (dom-tag dom)))
188 (let* ((attr (dom-attributes dom))
189 (times (length attr))
190 (column (1+ (current-column))))
191 (if (null attr)
192 (insert "nil")
193 (insert "(")
194 (dolist (elem attr)
195 (insert (format "(%S . %S)" (car elem) (cdr elem)))
196 (if (zerop (cl-decf times))
197 (insert ")")
198 (insert "\n" (make-string column ? ))))))
199 (let* ((children (if remove-empty
200 (cl-remove-if
201 (lambda (child)
202 (and (stringp child)
203 (string-match "\\`[\n\r\t  ]*\\'" child)))
204 (dom-children dom))
205 (dom-children dom)))
206 (times (length children)))
207 (if (null children)
208 (insert ")")
209 (insert "\n" (make-string (1+ column) ? ))
210 (dolist (child children)
211 (if (stringp child)
212 (if (or (not remove-empty)
213 (not (string-match "\\`[\n\r\t  ]*\\'" child)))
214 (insert (format "%S" child)))
215 (dom-pp child remove-empty))
216 (if (zerop (cl-decf times))
217 (insert ")")
218 (insert "\n" (make-string (1+ column) ? ))))))))
219
220 (provide 'dom)
221
222 ;;; dom.el ends here