Add element-content as a prereq to matching on an element's textual content
Sun Dec 13 05:24:52 UTC 2009 pix@kepibu.org
* Add element-content as a prereq to matching on an element's textual content
diff -rN -u old-Oh, Ducks!/package.lisp new-Oh, Ducks!/package.lisp
--- old-Oh, Ducks!/package.lisp 2015-09-27 00:41:25.000000000 +0000
+++ new-Oh, Ducks!/package.lisp 2015-09-27 00:41:25.000000000 +0000
@@ -12,6 +12,7 @@
#:element-parent
#:element-attribute
#:element-type
+ #:element-content
#:element-id
#:element-classes
diff -rN -u old-Oh, Ducks!/traversal/dom.lisp new-Oh, Ducks!/traversal/dom.lisp
--- old-Oh, Ducks!/traversal/dom.lisp 2015-09-27 00:41:25.000000000 +0000
+++ new-Oh, Ducks!/traversal/dom.lisp 2015-09-27 00:41:25.000000000 +0000
@@ -29,6 +29,14 @@
(defmethod element-type ((element dom:element))
(dom:tag-name element))
+(defmethod element-content ((element dom:element))
+ (mapcar (lambda (node)
+ (typecase node
+ (dom:element node)
+ (dom:text (dom:data node))
+ (t (error "Unsure what to do."))))
+ (coerce (dom:child-nodes element) 'list)))
+
;;; special accessors in case something special needs to happen
(defmethod element-id ((element dom:element))
(element-attribute "id" element))
diff -rN -u old-Oh, Ducks!/traversal/interface.lisp new-Oh, Ducks!/traversal/interface.lisp
--- old-Oh, Ducks!/traversal/interface.lisp 2015-09-27 00:41:25.000000000 +0000
+++ new-Oh, Ducks!/traversal/interface.lisp 2015-09-27 00:41:25.000000000 +0000
@@ -13,6 +13,13 @@
(:documentation "Returns the value of the attribute of element, or nil if no such attribute exists."))
(defgeneric element-type (element)
(:documentation "Returns the tag name (type) of element."))
+(defgeneric element-content (element)
+ (:documentation "Returns a string containing the contents of the element, if it contains only textual nodes, or a sequence containing all of the element's child nodes (textual nodes as strings, tag nodes as whatever they'd be under #'element-children).")
+ (:method :around ((element t))
+ (let ((val (call-next-method)))
+ (if (every #'stringp val)
+ (reduce (curry #'concatenate 'string) val)
+ val))))
;;; special accessors in case something special needs to happen
diff -rN -u old-Oh, Ducks!/traversal/lhtml.lisp new-Oh, Ducks!/traversal/lhtml.lisp
--- old-Oh, Ducks!/traversal/lhtml.lisp 2015-09-27 00:41:25.000000000 +0000
+++ new-Oh, Ducks!/traversal/lhtml.lisp 2015-09-27 00:41:25.000000000 +0000
@@ -23,3 +23,6 @@
(defmethod element-type ((element list))
(car element))
+
+(defmethod element-content ((element list))
+ (cddr element))
diff -rN -u old-Oh, Ducks!/traversal/pt.lisp new-Oh, Ducks!/traversal/pt.lisp
--- old-Oh, Ducks!/traversal/pt.lisp 2015-09-27 00:41:25.000000000 +0000
+++ new-Oh, Ducks!/traversal/pt.lisp 2015-09-27 00:41:25.000000000 +0000
@@ -20,3 +20,13 @@
(defmethod element-type ((element chtml:pt))
(chtml:pt-name element))
+
+(defmethod element-content ((element chtml:pt))
+ (mapcar (lambda (node)
+ (cond
+ ((eq :pcdata (chtml:pt-name node))
+ (chtml:pt-attrs node))
+ (t node)))
+ (remove-if (curry #'eq :comment)
+ (chtml:pt-children element)
+ :key #'chtml:pt-name)))