/ traversal /
/traversal/dom.lisp
 1 (in-package #:oh-ducks.traversal)
 2 
 3 (defmethod unify::occurs-in-p ((var symbol) (pat dom:element) env)
 4   (declare (ignore var pat env))
 5   nil)
 6 
 7 (defmethod unify:unify ((template oh-ducks::css-selector-template)
 8                         (document dom:document)
 9                         &optional (env (unify:make-empty-environment))
10                         &key)
11   (unify:unify template (dom:document-element document) env))
12 
13 ;;; general accessors
14 
15 (defmethod element-children ((element dom:element))
16   (remove-if-not #'dom:element-p (coerce (dom:child-nodes element) 'list)))
17 
18 (defmethod element-parent ((element dom:element))
19   (let ((parent (dom:parent-node element)))
20     (unless (typep parent 'dom:document)
21       parent)))
22 
23 (defmethod element-attribute ((attribute symbol) (element dom:element))
24   (element-attribute (string-downcase (symbol-name attribute)) element))
25 (defmethod element-attribute ((attribute string) (element dom:element))
26   (when-let* ((attribute-node (dom:get-attribute-node element attribute)))
27     (dom:value attribute-node)))
28 
29 (defmethod element-type ((element dom:element))
30   (dom:tag-name element))
31 
32 (defmethod element-content ((element dom:element))
33   (mapcar (lambda (node)
34             (typecase node
35               (dom:element node)
36               (dom:text (dom:data node))
37               (t (error "Unsure what to do."))))
38           (coerce (dom:child-nodes element) 'list)))
39 
40 ;;; special accessors in case something special needs to happen
41 (defmethod element-id ((element dom:element))
42   (element-attribute "id" element))
43 
44 (defmethod element-classes ((element dom:element))
45   (split-sequence:split-sequence #\Space (element-attribute "class" element) :remove-empty-subseqs t))