Add attribute-equal selector
Wed Feb 10 08:50:16 UTC 2010 pix@kepibu.org
* Add attribute-equal selector
diff -rN -u old-Oh, Ducks!/notes new-Oh, Ducks!/notes
--- old-Oh, Ducks!/notes 2013-07-24 10:35:26.000000000 +0000
+++ new-Oh, Ducks!/notes 2013-07-24 10:35:26.000000000 +0000
@@ -155,9 +155,9 @@
* [X] :only-child
* [X] :only-of-type
* [X] :empty
-*** attribute selectors [1/7]
+*** attribute selectors [2/7]
* [X] attribute-present [att]
- * [ ] attribute-equal [att=val]
+ * [X] attribute-equal [att=val]
* [ ] attribute-member [att~=val]
* [ ] attribute-lang [att|=val]
* [ ] attribute-begins [att^=val]
diff -rN -u old-Oh, Ducks!/selectors.lisp new-Oh, Ducks!/selectors.lisp
--- old-Oh, Ducks!/selectors.lisp 2013-07-24 10:35:26.000000000 +0000
+++ new-Oh, Ducks!/selectors.lisp 2013-07-24 10:35:26.000000000 +0000
@@ -42,8 +42,10 @@
(defclass nth-last-of-type-selector (nth-of-type-selector) ())
(defclass empty-selector (simple-selector) ())
-(defclass attribute-selector (simple-selector) ())
+(defclass attribute-selector (simple-selector)
+ ((val :reader attribute-value :initarg :value)))
(defclass attribute-present-selector (attribute-selector) ())
+(defclass attribute-equal-selector (attribute-selector) ())
(defmethod initialize-instance :after ((selector nth-child-selector)
&key (asign "+") a
@@ -110,6 +112,9 @@
(#T(regexp$ ("[" $name "]") (?attribute))
(cons (make-instance 'attribute-present-selector :arg attribute)
(parse-selector &rest)))
+ (#T(regexp$ ("[" $name "=" $name "]") (?attribute ?value))
+ (cons (make-instance 'attribute-equal-selector :arg attribute :value value)
+ (parse-selector &rest)))
;; cyclic (An+B, n+B)
(#T(regexp$ (":nth-child(" \s* an+b \s* ")")
(?asign ?a ?bsign ?b))
@@ -271,6 +276,10 @@
(defmethod subject-p ((selector attribute-present-selector) element)
(element-attribute (selector-arg selector) element))
+(defmethod subject-p ((selector attribute-equal-selector) element)
+ (when-let* ((val (element-attribute (selector-arg selector) element)))
+ (string= val (attribute-value selector))))
+
(defmethod subject-p ((selector %implicit-element-selector) element)
(eq element *implicit-element*))
diff -rN -u old-Oh, Ducks!/tests.lisp new-Oh, Ducks!/tests.lisp
--- old-Oh, Ducks!/tests.lisp 2013-07-24 10:35:26.000000000 +0000
+++ new-Oh, Ducks!/tests.lisp 2013-07-24 10:35:26.000000000 +0000
@@ -157,6 +157,12 @@
"<div><i id=''>blank id</i>foo<b>no id</b>bar<i id='id'>id id</i></div>")
ids))
+(serialize-values
+ (match (#T(html (:model dom)
+ ("[id=foo]" . ?id))
+ "<div><i id='bar'>bar id</i><i>no id</i><i id='foo'>foo id</i></div>")
+ id))
+
#+LATER?
(match (#t(html ("div::content" . #t(regexp+ "^f(o+)" (?o))))
"<div>barbaz</div><div>fooooooobar</div>")