Make descendant combinators work with an implicit parent
selectors.lisp
Fri Dec 4 04:47:58 UTC 2009 pix@kepibu.org
* Make descendant combinators work with an implicit parent
--- old-Oh, Ducks!/selectors.lisp 2015-11-24 09:19:29.000000000 +0000
+++ new-Oh, Ducks!/selectors.lisp 2015-11-24 09:19:29.000000000 +0000
@@ -1,5 +1,8 @@
(in-package #:oh-ducks)
+(defvar *effective-root* nil
+ "The element to be considered as the root element during unification. Is the implicit element to be matched by combinators without a leading qualifier. E.g., \"> a\" will match <a> tags directly under *effective-root*.")
+
#.(set-dispatch-macro-character #\# #\T 'unify::|sharp-T-reader|)
(defclass selector (unify::string-template)
@@ -49,17 +52,23 @@
<http://common-lisp.net/pipermail/cl-unification-devel/attachments/20091201/d5021e15/attachment.obj> ~
to ensure proper functioning of the \"Oh, Ducks!\" library.")
+(defclass %root-selector (simple-selector) ())
+(defparameter %root-selector (make-instance '%root-selector))
+
+(defmethod print-object ((selector %root-selector) stream)
+ (print-unreadable-object (selector stream :type t)))
+
(defun parse-selector (selector)
(match-case (selector)
;; combinators
(#T(regexp$ "[ ]*[~][ ]*" ())
- (list (make-instance 'sibling-combinator :matcher (parse-selector &rest))))
+ (list (make-instance 'sibling-combinator :matcher (or (parse-selector &rest) %root-selector))))
(#T(regexp$ "[ ]*[+][ ]*" ())
- (list (make-instance 'adjacent-combinator :matcher (parse-selector &rest))))
+ (list (make-instance 'adjacent-combinator :matcher (or (parse-selector &rest) %root-selector))))
(#T(regexp$ "[ ]*[>][ ]*" ())
- (list (make-instance 'child-combinator :matcher (parse-selector &rest))))
+ (list (make-instance 'child-combinator :matcher (or (parse-selector &rest) %root-selector))))
(#T(regexp$ "[ ]+" ())
- (list (make-instance 'descendant-combinator :matcher (parse-selector &rest))))
+ (list (make-instance 'descendant-combinator :matcher (or (parse-selector &rest) %root-selector))))
;; simple selectors
;; cyclic (An+B, n+B)
(#T(regexp$ ":nth-child\\([ ]*([+-]?)([0-9]+)?n[ ]*([+-])[ ]*([0-9]+)?[ ]*\\)" (?asign ?a ?bsign ?b))
@@ -138,6 +147,9 @@
(declare (ignore element selector))
t)
+(defmethod element-matches-p (element (selector %root-selector))
+ (eq element *effective-root*))
+
(defmethod element-matches-p (element (selector list))
(every (curry #'element-matches-p element) selector))