Add sibling and adjacent combinators
selectors.lisp
Thu Dec 3 00:12:02 UTC 2009 pix@kepibu.org
* Add sibling and adjacent combinators
--- old-Oh, Ducks!/selectors.lisp 2015-09-23 01:38:31.000000000 +0000
+++ new-Oh, Ducks!/selectors.lisp 2015-09-23 01:38:31.000000000 +0000
@@ -51,8 +51,10 @@
(defun parse-selector (selector)
(match-case (selector)
;; combinators
- #+TODO (#T(regexp$ "[ ]*[~][ ]*" ()) (list (make-instance 'sibling-combinator :matcher (parse-selector &rest))))
- #+TODO (#T(regexp$ "[ ]*[+][ ]*" ()) (list (make-instance 'adjacent-combinator :matcher (parse-selector &rest))))
+ (#T(regexp$ "[ ]*[~][ ]*" ())
+ (list (make-instance 'sibling-combinator :matcher (parse-selector &rest))))
+ (#T(regexp$ "[ ]*[+][ ]*" ())
+ (list (make-instance 'adjacent-combinator :matcher (parse-selector &rest))))
(#T(regexp$ "[ ]*[>][ ]*" ())
(list (make-instance 'child-combinator :matcher (parse-selector &rest))))
(#T(regexp$ "[ ]+" ())
@@ -143,10 +145,18 @@
(defmethod element-matches-p (element (selector descendant-combinator))
(some (rcurry #'element-matches-p (matcher selector)) (element-ancestors element)))
-#+TODO
(defmethod element-matches-p (element (selector adjacent-combinator))
- ...)
+ (let* ((parent (element-parent element))
+ (siblings (element-children parent))
+ (ourpos (position element siblings :test #'eq)))
+ (and ourpos
+ (> ourpos 0)
+ (element-matches-p (elt siblings (1- ourpos)) (matcher selector)))))
-#+TODO
(defmethod element-matches-p (element (selector sibling-combinator))
- ...)
+ (let* ((parent (element-parent element))
+ (siblings (element-children parent))
+ (ourpos (position element siblings :test #'eq)))
+ (and ourpos
+ (> ourpos 0)
+ (find-if (rcurry #'element-matches-p (matcher selector)) siblings :end ourpos))))