Make descendant combinators work with an implicit parent
Fri Dec 4 04:47:58 UTC 2009 pix@kepibu.org
* Make descendant combinators work with an implicit parent
hunk ./selectors.lisp 3
+(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*.")
+
hunk ./selectors.lisp 55
+(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)))
+
hunk ./selectors.lisp 65
- (list (make-instance 'sibling-combinator :matcher (parse-selector &rest))))
+ (list (make-instance 'sibling-combinator :matcher (or (parse-selector &rest) %root-selector))))
hunk ./selectors.lisp 67
- (list (make-instance 'adjacent-combinator :matcher (parse-selector &rest))))
+ (list (make-instance 'adjacent-combinator :matcher (or (parse-selector &rest) %root-selector))))
hunk ./selectors.lisp 69
- (list (make-instance 'child-combinator :matcher (parse-selector &rest))))
+ (list (make-instance 'child-combinator :matcher (or (parse-selector &rest) %root-selector))))
hunk ./selectors.lisp 71
- (list (make-instance 'descendant-combinator :matcher (parse-selector &rest))))
+ (list (make-instance 'descendant-combinator :matcher (or (parse-selector &rest) %root-selector))))
hunk ./selectors.lisp 150
+(defmethod element-matches-p (element (selector %root-selector))
+ (eq element *effective-root*))
+
hunk ./tests.lisp 72
+;; Sometimes, you want to match a thing inside a thing, in which case
+;; combinators should implicitly assume an unspecified right side means
+;; "whatever element I gave you".
+(match (#T(html (:model dom)
+ ("q" . ?q))
+ "<div><i>ham</i> foo <q>bar <i>baz</i></q> quuz <i>spam</i></div>")
+ (match (#t(html ("> i" . ?i))
+ (first q))
+ i))
+
+;; Note, however, that searches are strictly recursive. So a sibling
+;; combinator won't match.
+;; FIXME: should it?
+(match (#T(html (:model dom)
+ ("q" . ?q))
+ "<div><i>ham</i> foo <q>bar <i>baz</i></q> quuz <i>spam</i></div>")
+ (match (#t(html ("+ i" . ?i))
+ (first q))
+ i))
+
hunk ./unify.lisp 21
- (let ((val (find-matching-elements css-specifier document)))
+ (let* ((*effective-root* document)
+ (val (find-matching-elements css-specifier document)))