aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLars Ingebrigtsen2019-09-27 00:32:37 +0200
committerLars Ingebrigtsen2019-09-27 00:33:45 +0200
commit45727c4e6de7f6f6f61a4ae3589d5dd271ea8803 (patch)
treea853f0eadc96373bef56e7d870cf8ceb0df275cc
parented9402f81907f5810d70fc421b456791d624ba1a (diff)
downloademacs-45727c4e6de7f6f6f61a4ae3589d5dd271ea8803.tar.gz
emacs-45727c4e6de7f6f6f61a4ae3589d5dd271ea8803.zip
Add a new `dom-search' function
* doc/lispref/text.texi (Document Object Model): Document it. * lisp/dom.el (dom-search): New function.
-rw-r--r--doc/lispref/text.texi5
-rw-r--r--etc/NEWS6
-rw-r--r--lisp/dom.el12
3 files changed, 23 insertions, 0 deletions
diff --git a/doc/lispref/text.texi b/doc/lispref/text.texi
index d7b04d2934f..8d78a9b24ff 100644
--- a/doc/lispref/text.texi
+++ b/doc/lispref/text.texi
@@ -5162,6 +5162,11 @@ which is a regular expression.
5162Return all nodes in @var{dom} that have IDs that match @var{match}, 5162Return all nodes in @var{dom} that have IDs that match @var{match},
5163which is a regular expression. 5163which is a regular expression.
5164 5164
5165@item dom-search @var{dom} @var{predicate}
5166Return all nodes in @var{dom} where @var{predicate} returns a
5167non-@code{nil} value. @var{predicate} is called with the node to be
5168tested as its parameter.
5169
5165@item dom-strings @var{dom} 5170@item dom-strings @var{dom}
5166Return all strings in @var{dom}. 5171Return all strings in @var{dom}.
5167 5172
diff --git a/etc/NEWS b/etc/NEWS
index 0a4ada3cc6c..afeb3877739 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -276,6 +276,12 @@ used to remove comments before calling the libxml functions to parse
276the data. 276the data.
277 277
278+++ 278+++
279** A new DOM (the XML/HTML document structure returned by functions
280such as 'libxml-parse-html-region') traversal function has been added:
281'dom-search', which takes a DOM and a predicate and returns all nodes
282that match.
283
284+++
279** The Network Security Manager now allows more fine-grained control 285** The Network Security Manager now allows more fine-grained control
280of what checks to run via the 'network-security-protocol-checks' 286of what checks to run via the 'network-security-protocol-checks'
281variable. 287variable.
diff --git a/lisp/dom.el b/lisp/dom.el
index d8c44339985..f01da5221b8 100644
--- a/lisp/dom.el
+++ b/lisp/dom.el
@@ -108,6 +108,18 @@ A name is a symbol like `td'."
108 (cons dom matches) 108 (cons dom matches)
109 matches))) 109 matches)))
110 110
111(defun dom-search (dom predicate)
112 "Return elements in DOM where PREDICATE is non-nil.
113PREDICATE is called with the node as its only parameter."
114 (let ((matches (cl-loop for child in (dom-children dom)
115 for matches = (and (not (stringp child))
116 (dom-search child predicate))
117 when matches
118 append matches)))
119 (if (funcall predicate dom)
120 (cons dom matches)
121 matches)))
122
111(defun dom-strings (dom) 123(defun dom-strings (dom)
112 "Return elements in DOM that are strings." 124 "Return elements in DOM that are strings."
113 (cl-loop for child in (dom-children dom) 125 (cl-loop for child in (dom-children dom)