Skip to content

Commit 8ee0487

Browse files
committed
sibling combinators are working now
1 parent 5c5c70b commit 8ee0487

File tree

4 files changed

+36
-28
lines changed

4 files changed

+36
-28
lines changed

README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# q.nim
2-
Simple package for query HTML/XML elements using a CSS3 or jQuery-like selector syntax for [Nim](http://nim-lang.org).
2+
Simple package for query HTML/XML elements using a CSS3 or [jQuery](http://jquery.com)-like selector syntax for [Nim](http://nim-lang.org).
33

4-
This project is under development, just some basic selectors are implemented.
4+
This project is in alpha stage, some features are not supported yet.
55

66
## Selectors
77
- [x] [Type selectors](http://www.w3.org/TR/css3-selectors/#type-selectors)
@@ -11,10 +11,18 @@ This project is under development, just some basic selectors are implemented.
1111
- [x] [Universal selector](http://www.w3.org/TR/css3-selectors/#universal-selector)
1212
- [x] [Attribute selectors](http://www.w3.org/TR/css3-selectors/#attribute-selectors)
1313
- [x] [Child combinator](http://www.w3.org/TR/css3-selectors/#child-combinators)
14-
- [ ] [Adjacent sibling combinator](http://www.w3.org/TR/css3-selectors/#adjacent-sibling-combinators)
15-
- [ ] [General sibling combinator](http://www.w3.org/TR/css3-selectors/#general-sibling-combinators)
14+
- [x] [Adjacent sibling combinator](http://www.w3.org/TR/css3-selectors/#adjacent-sibling-combinators)
15+
- [x] [General sibling combinator](http://www.w3.org/TR/css3-selectors/#general-sibling-combinators)
1616
- [ ] [Structural pseudo-classes](http://www.w3.org/TR/css3-selectors/#structural-pseudos)
1717

18+
##Installation
19+
$ nimble install q
20+
21+
##Changes
22+
0.0.2 - supports sibling combinators and multiple class, attributes selectors
23+
0.0.1 - initial release
24+
25+
1826
##Usage
1927

2028
```nim

q.nim

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -136,28 +136,30 @@ proc searchSimple(parents: var seq[XmlNode], selector: Selector) =
136136

137137
proc searchCombined(parent: XmlNode, selectors: seq[Selector], found: var seq[XmlNode]) =
138138
var starts: seq[int] = @[0]
139-
var matches: seq[int] = @[]
139+
var matches: seq[int]
140140

141-
for i in 0..selectors.len-1: # matching selector by selector
141+
# matching selector by selector
142+
for i in 0..selectors.len-1:
142143
var selector = selectors[i]
144+
matches = @[]
145+
143146
for j in starts:
144147
for k in j..parent.len-1:
145148
var child = parent[k]
146149
if child.kind != xnElement:
147150
continue
148-
echo i, ", ", j, ", ", k
149151

150152
if match(child, selector):
151153
if i < selectors.len-1:
152154
# save current index for next search
153-
matches.add(k)
155+
# next selector will only search for nodes followed by this node
156+
matches.add(k+1)
154157
else:
155158
# no more selector, return matches
156159
if not found.contains(child):
157160
found.add(child)
158161
if selector.combinator == '+':
159162
break
160-
161163
starts = matches
162164

163165
proc searchCombined(parents: var seq[XmlNode], selectors: seq[Selector]) =
@@ -178,8 +180,6 @@ proc parseSelector(token: string): Selector =
178180
if matches[i].isNil:
179181
continue
180182

181-
#echo matches[i]
182-
183183
let ch = matches[i][0]
184184
case ch:
185185
of '#':
@@ -197,7 +197,6 @@ proc parseSelector(token: string): Selector =
197197
discard
198198

199199
proc select*(q: QueryContext, s: string = ""): seq[XmlNode] =
200-
echo "Selectors: ", s
201200
result = q.root
202201

203202
if s.isNil or s == "":
@@ -237,10 +236,8 @@ proc select*(q: QueryContext, s: string = ""): seq[XmlNode] =
237236

238237
nextToken = tokens[pos+i+1]
239238
i += 2
240-
echo "nextCombinator ", nextCombinator, " nextSelector ", nextToken
241239

242240
var tmp = parseSelector(nextToken)
243-
244241
tmp.combinator = nextCombinator[0]
245242
selectors.add(tmp)
246243

tests/test.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424
</ul>
2525
<a href="#">Link 100</a>
2626
<div class="col-2"></div>
27+
<a href="#">Link 104</a>
2728
<div class="col-3"></div>
2829
<a href="#">Link 101</a>
30+
<a href="#">Link 102</a>
2931
</nav>
3032
<form class="form-horizontal">
3133
<div class="form-group">

tests/test.nim

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
import q
1+
import "../q"
22
import xmltree
33

44
var d = q(path="test.html")
5-
#echo d.select("head *")
6-
#echo d.select("ul li a")
7-
#echo d.select("ul.menu > li a")
8-
#echo d.select("ul.menu > li > a")
9-
#echo d.select("ul.menu.and > li > a")
10-
#echo d.select("#link1")
11-
#echo d.select("input[type]")
12-
#echo d.select("input[type=password]")
13-
#echo d.select("input[type='password']")
14-
#echo d.select("input[type=\"password\"]")
15-
#echo d.select("input[type^=pa]")
16-
#echo d.select("input[type$=ord]")
17-
#echo d.select("input[type*=ss]")
5+
6+
echo d.select("head *")
7+
echo d.select("ul li a")
8+
echo d.select("ul.menu > li a")
9+
echo d.select("ul.menu > li > a")
10+
echo d.select("ul.menu.and > li > a")
11+
echo d.select("#link1")
12+
echo d.select("input[type]")
13+
echo d.select("input[type=password]")
14+
echo d.select("input[type='password']")
15+
echo d.select("input[type=\"password\"]")
16+
echo d.select("input[type^=pa]")
17+
echo d.select("input[type$=ord]")
18+
echo d.select("input[type*=ss]")
1819
echo d.select("nav ul.menu ~ div + a")

0 commit comments

Comments
 (0)