Skip to content

Commit 0b00a07

Browse files
committed
Merge pull request #10 from ruricolist/stp
Fixes for STP.
2 parents 20530ed + e97a28f commit 0b00a07

File tree

2 files changed

+81
-74
lines changed

2 files changed

+81
-74
lines changed

src/compile.lisp

+59-62
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@
66

77
(defun attrib-includes? (node attrib value)
88
(member value
9-
(cl-ppcre:split "\\s+" (get-attribute node attrib))
10-
:test #'string-equal))
9+
(cl-ppcre:split "\\s+" (get-attribute node attrib))
10+
:test #'string-equal))
1111

1212
(defun make-or-matcher (forms)
1313
(let ((matchers (mapcar (lambda (f) (make-matcher-aux f)) forms)))
1414
(lambda (%node%)
1515
"or-matcher"
1616
(iter (for matcher in matchers)
17-
(thereis (funcall matcher %node%))))))
17+
(thereis (funcall matcher %node%))))))
1818

1919
(defun make-and-matcher (forms )
2020
(let ((matchers (mapcar (lambda (f) (make-matcher-aux f)) forms)))
2121
(lambda (%node%)
2222
"and-matcher"
2323
(iter (for matcher in matchers)
24-
(always (funcall matcher %node%))))))
24+
(always (funcall matcher %node%))))))
2525

2626
(defun make-class-matcher ( class )
2727
(lambda (%node%)
@@ -31,7 +31,7 @@
3131
(defun make-hash-matcher ( id )
3232
(lambda (%node%)
3333
"hash-matcher"
34-
(string-equal (buildnode:get-attribute %node% :id) id)))
34+
(string-equal (get-attribute %node% "id") id)))
3535

3636
(defun make-elt-matcher ( tag )
3737
(lambda (%node%)
@@ -46,35 +46,35 @@
4646
"attrib-matcher"
4747
(lambda (%node%)
4848
(case match-type
49-
(:equals (string-equal (buildnode:get-attribute %node% attrib) match-to))
49+
(:equals (string-equal (get-attribute %node% attrib) match-to))
5050
(:includes (attrib-includes? %node% attrib match-to))
5151
(:dashmatch (member match-to
52-
(cl-ppcre:split "-" (buildnode:get-attribute %node% attrib))
53-
:test #'string-equal))
52+
(cl-ppcre:split "-" (get-attribute %node% attrib))
53+
:test #'string-equal))
5454
(:begins-with (alexandria:starts-with-subseq
55-
match-to
56-
(buildnode:get-attribute %node% attrib)
57-
:test #'char-equal))
55+
match-to
56+
(get-attribute %node% attrib)
57+
:test #'char-equal))
5858
(:ends-with (alexandria:ends-with-subseq
59-
match-to
60-
(buildnode:get-attribute %node% attrib)
61-
:test #'char-equal))
62-
(:substring (search match-to (buildnode:get-attribute %node% attrib)
63-
:test #'string-equal ))
64-
(:exists (buildnode:get-attribute %node% attrib)))))
59+
match-to
60+
(get-attribute %node% attrib)
61+
:test #'char-equal))
62+
(:substring (search match-to (get-attribute %node% attrib)
63+
:test #'string-equal ))
64+
(:exists (get-attribute %node% attrib)))))
6565

6666

6767

6868
(defun make-immediate-child-matcher (parent-matcher child-matcher)
6969
(lambda (%node%)
7070
(and (funcall child-matcher %node%)
71-
(parent-element %node%)
72-
(funcall parent-matcher (parent-element %node%)))))
71+
(parent-element %node%)
72+
(funcall parent-matcher (parent-element %node%)))))
7373

7474
(defun make-child-matcher (parent-matcher child-matcher )
7575
(lambda (%node%)
7676
(and (funcall child-matcher %node%)
77-
(iter (for n in (parent-elements %node%))
77+
(iter (for n in (parent-elements %node%))
7878
;; the root is/could be document node
7979
;; we can really only test on elements, so
8080
;; this seems pretty valid, solves github issue:5
@@ -84,16 +84,16 @@
8484
(defun make-immediatly-preceded-by-matcher (this-matcher sibling-matcher )
8585
(lambda (%node%)
8686
(and (funcall this-matcher %node%)
87-
(previous-sibling %node%)
88-
(funcall sibling-matcher (previous-sibling %node%)))))
87+
(previous-sibling %node%)
88+
(funcall sibling-matcher (previous-sibling %node%)))))
8989

9090
(defun make-preceded-by-matcher (this-matcher sibling-matcher )
9191
(lambda (%node%)
9292
(and (funcall this-matcher %node%)
93-
(iter (for n initially (previous-sibling %node%)
94-
then (previous-sibling n))
95-
(while n)
96-
(thereis (funcall sibling-matcher n))))))
93+
(iter (for n initially (previous-sibling %node%)
94+
then (previous-sibling n))
95+
(while n)
96+
(thereis (funcall sibling-matcher n))))))
9797

9898
(defun make-pseudo-matcher (pseudo submatcher)
9999
(lambda (%node%) (funcall pseudo %node% submatcher)))
@@ -103,8 +103,8 @@
103103

104104
(defun make-matcher-aux (tree)
105105
(ecase (typecase tree
106-
(atom tree)
107-
(list (car tree)))
106+
(atom tree)
107+
(list (car tree)))
108108
(:or (make-or-matcher (rest tree) ))
109109
(:and (make-and-matcher (rest tree) ))
110110
(:class (make-class-matcher (second tree) ))
@@ -113,44 +113,44 @@
113113
(:everything (lambda (%node%) (declare (ignore %node%)) T))
114114
(:attribute
115115
(let ((attrib (second tree)))
116-
(ecase (length tree)
117-
(2 (make-attrib-matcher attrib :exists nil ))
118-
(3 (destructuring-bind (match-type match-to) (third tree)
119-
(make-attrib-matcher attrib match-type match-to ))))))
116+
(ecase (length tree)
117+
(2 (make-attrib-matcher attrib :exists nil ))
118+
(3 (destructuring-bind (match-type match-to) (third tree)
119+
(make-attrib-matcher attrib match-type match-to ))))))
120120
(:immediate-child
121121
(make-immediate-child-matcher
122-
(make-matcher-aux (second tree))
123-
(make-matcher-aux (third tree))
124-
))
122+
(make-matcher-aux (second tree))
123+
(make-matcher-aux (third tree))
124+
))
125125
(:child
126126
(make-child-matcher
127-
(make-matcher-aux (second tree))
128-
(make-matcher-aux (third tree))
129-
))
127+
(make-matcher-aux (second tree))
128+
(make-matcher-aux (third tree))
129+
))
130130
(:immediatly-preceded-by
131131
(make-immediatly-preceded-by-matcher
132-
(make-matcher-aux (third tree))
133-
(make-matcher-aux (second tree))
134-
))
132+
(make-matcher-aux (third tree))
133+
(make-matcher-aux (second tree))
134+
))
135135
(:preceded-by
136136
(make-preceded-by-matcher
137-
(make-matcher-aux (third tree))
138-
(make-matcher-aux (second tree))
139-
))
137+
(make-matcher-aux (third tree))
138+
(make-matcher-aux (second tree))
139+
))
140140
(:pseudo
141141
(destructuring-bind (pseudo name &optional subselector) tree
142-
(declare (ignore pseudo ))
143-
(make-pseudo-matcher
144-
(fdefinition (intern (string-upcase name) :pseudo))
145-
(when subselector
146-
(make-matcher-aux subselector ))
147-
)))
142+
(declare (ignore pseudo ))
143+
(make-pseudo-matcher
144+
(fdefinition (intern (string-upcase name) :pseudo))
145+
(when subselector
146+
(make-matcher-aux subselector ))
147+
)))
148148
(:nth-pseudo
149149
(destructuring-bind (pseudo name mul add) tree
150-
(declare (ignore pseudo ))
151-
(make-nth-pseudo-matcher
152-
(fdefinition (intern (string-upcase name) :pseudo))
153-
mul add )))))
150+
(declare (ignore pseudo ))
151+
(make-nth-pseudo-matcher
152+
(fdefinition (intern (string-upcase name) :pseudo))
153+
mul add )))))
154154

155155
(defun make-node-matcher (expression)
156156
(make-matcher-aux
@@ -176,8 +176,8 @@
176176
`(%node-matches?
177177
,node
178178
,(if (constantp inp e)
179-
`(load-time-value (compile-css-node-matcher ,inp))
180-
inp)))
179+
`(load-time-value (compile-css-node-matcher ,inp))
180+
inp)))
181181

182182
(defgeneric %do-query (matcher node &key first?)
183183
(:documentation "matches selector inp against the node
@@ -196,11 +196,8 @@
196196
(%query inp trees))
197197

198198
(define-compiler-macro query (inp &optional (trees 'buildnode:*document*) &environment e)
199-
`(%query
199+
`(%query
200200
,(if (constantp inp e)
201-
`(load-time-value (compile-css-node-matcher ,inp))
202-
inp)
201+
`(load-time-value (compile-css-node-matcher ,inp))
202+
inp)
203203
,trees))
204-
205-
206-

src/stp.lisp

+22-12
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,19 @@
55

66
(defmethod get-attribute ((elt stp:element) attr)
77
;; TODO: special case namespace-uri
8-
(stp:attribute-value elt attr))
8+
(stp:attribute-value elt (string-downcase attr)))
99

1010
(defmethod element-p ((elt stp:element))
1111
elt)
1212

1313
(defmethod parent-node ((n stp:node))
1414
"gets the parent node"
15-
(stp:parent-node n))
15+
(stp:parent n))
1616

1717
(defmethod previous-sibling ((n stp:element))
1818
"gets the parent dom:element (rather than ever returning the document node)"
19-
(element-p (stp:previous-sibling n)))
19+
(unless (eq n (stp:first-child (stp:parent n)))
20+
(element-p (stp:previous-sibling n))))
2021

2122
(defmethod child-elements ((n stp:node))
2223
(iter (for kid in (stp:list-children n))
@@ -28,14 +29,23 @@
2829
(defmethod document-of ((n stp:node))
2930
(stp:document n))
3031

32+
(defun stp-do-query (matcher elt first?)
33+
(if first?
34+
(stp:find-recursively-if
35+
(lambda (node)
36+
(when (and (element-p node) (funcall matcher node))
37+
node))
38+
elt)
39+
(let ((matches '()))
40+
(stp:filter-recursively
41+
(lambda (node)
42+
(when (and (element-p node) (funcall matcher node))
43+
(push node matches)))
44+
elt)
45+
(nreverse matches))))
46+
3147
(defmethod %do-query (matcher (elt stp:element) &key (first? nil))
32-
(stp:filter-children
33-
(lambda (node)
34-
(let ((res (and (element-p node)
35-
(or (cl:not matcher);; TODO: why would this be nil
36-
(funcall matcher node)))))
37-
(if (and first? res)
38-
(return-from %do-query node)
39-
res)))
40-
elt))
48+
(stp-do-query matcher elt first?))
4149

50+
(defmethod %do-query (matcher (elt stp:document) &key (first? nil))
51+
(stp-do-query matcher elt first?))

0 commit comments

Comments
 (0)