| 1 |
package xpath |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"fmt" |
| 6 |
"hash/fnv" |
| 7 |
"reflect" |
| 8 |
"strconv" |
| 9 |
) |
| 10 |
|
| 11 |
// The return type of the XPath expression. |
| 12 |
type resultType int |
| 13 |
|
| 14 |
var xpathResultType = struct { |
| 15 |
Boolean resultType |
| 16 |
// A numeric value |
| 17 |
Number resultType |
| 18 |
String resultType |
| 19 |
// A node collection. |
| 20 |
NodeSet resultType |
| 21 |
// Any of the XPath node types. |
| 22 |
Any resultType |
| 23 |
}{ |
| 24 |
Boolean: 0, |
| 25 |
Number: 1, |
| 26 |
String: 2, |
| 27 |
NodeSet: 3, |
| 28 |
Any: 4, |
| 29 |
} |
| 30 |
|
| 31 |
type queryProp int |
| 32 |
|
| 33 |
var queryProps = struct { |
| 34 |
None queryProp |
| 35 |
Position queryProp |
| 36 |
Count queryProp |
| 37 |
Cached queryProp |
| 38 |
Reverse queryProp |
| 39 |
Merge queryProp |
| 40 |
}{ |
| 41 |
None: 0, |
| 42 |
Position: 1, |
| 43 |
Count: 2, |
| 44 |
Cached: 4, |
| 45 |
Reverse: 8, |
| 46 |
Merge: 16, |
| 47 |
} |
| 48 |
|
| 49 |
type iterator interface { |
| 50 |
Current() NodeNavigator |
| 51 |
} |
| 52 |
|
| 53 |
// An XPath query interface. |
| 54 |
type query interface { |
| 55 |
// Select traversing iterator returns a query matched node NodeNavigator. |
| 56 |
Select(iterator) NodeNavigator |
| 57 |
|
| 58 |
// Evaluate evaluates query and returns values of the current query. |
| 59 |
Evaluate(iterator) interface{} |
| 60 |
|
| 61 |
Clone() query |
| 62 |
|
| 63 |
// ValueType returns the value type of the current query. |
| 64 |
ValueType() resultType |
| 65 |
|
| 66 |
Properties() queryProp |
| 67 |
} |
| 68 |
|
| 69 |
// nopQuery is an empty query that always return nil for any query. |
| 70 |
type nopQuery struct{} |
| 71 |
|
| 72 |
func (nopQuery) Select(iterator) NodeNavigator { return nil } |
| 73 |
|
| 74 |
func (nopQuery) Evaluate(iterator) interface{} { return nil } |
| 75 |
|
| 76 |
func (nopQuery) Clone() query { return nopQuery{} } |
| 77 |
|
| 78 |
func (nopQuery) ValueType() resultType { return xpathResultType.NodeSet } |
| 79 |
|
| 80 |
func (nopQuery) Properties() queryProp { |
| 81 |
return queryProps.Merge | queryProps.Position | queryProps.Count | queryProps.Cached |
| 82 |
} |
| 83 |
|
| 84 |
// contextQuery is returns current node on the iterator object query. |
| 85 |
type contextQuery struct { |
| 86 |
count int |
| 87 |
} |
| 88 |
|
| 89 |
func (c *contextQuery) Select(t iterator) NodeNavigator { |
| 90 |
if c.count > 0 { |
| 91 |
return nil |
| 92 |
} |
| 93 |
c.count++ |
| 94 |
return t.Current().Copy() |
| 95 |
} |
| 96 |
|
| 97 |
func (c *contextQuery) Evaluate(iterator) interface{} { |
| 98 |
c.count = 0 |
| 99 |
return c |
| 100 |
} |
| 101 |
|
| 102 |
func (c *contextQuery) Clone() query { |
| 103 |
return &contextQuery{} |
| 104 |
} |
| 105 |
|
| 106 |
func (c *contextQuery) ValueType() resultType { |
| 107 |
return xpathResultType.NodeSet |
| 108 |
} |
| 109 |
|
| 110 |
func (c *contextQuery) Properties() queryProp { |
| 111 |
return queryProps.Merge | queryProps.Position | queryProps.Count | queryProps.Cached |
| 112 |
} |
| 113 |
|
| 114 |
type absoluteQuery struct { |
| 115 |
count int |
| 116 |
} |
| 117 |
|
| 118 |
func (a *absoluteQuery) Select(t iterator) (n NodeNavigator) { |
| 119 |
if a.count > 0 { |
| 120 |
return |
| 121 |
} |
| 122 |
a.count++ |
| 123 |
n = t.Current().Copy() |
| 124 |
n.MoveToRoot() |
| 125 |
return |
| 126 |
} |
| 127 |
|
| 128 |
func (a *absoluteQuery) Evaluate(t iterator) interface{} { |
| 129 |
a.count = 0 |
| 130 |
return a |
| 131 |
} |
| 132 |
|
| 133 |
func (a *absoluteQuery) Clone() query { |
| 134 |
return &absoluteQuery{} |
| 135 |
} |
| 136 |
|
| 137 |
func (a *absoluteQuery) ValueType() resultType { |
| 138 |
return xpathResultType.NodeSet |
| 139 |
} |
| 140 |
|
| 141 |
func (a *absoluteQuery) Properties() queryProp { |
| 142 |
return queryProps.Merge | queryProps.Position | queryProps.Count | queryProps.Cached |
| 143 |
} |
| 144 |
|
| 145 |
// ancestorQuery is an XPath ancestor node query.(ancestor::*|ancestor-self::*) |
| 146 |
type ancestorQuery struct { |
| 147 |
name string |
| 148 |
iterator func() NodeNavigator |
| 149 |
table map[uint64]bool |
| 150 |
pos int |
| 151 |
|
| 152 |
Self bool |
| 153 |
Input query |
| 154 |
Predicate func(NodeNavigator) bool |
| 155 |
} |
| 156 |
|
| 157 |
func (a *ancestorQuery) Select(t iterator) NodeNavigator { |
| 158 |
if a.table == nil { |
| 159 |
a.table = make(map[uint64]bool) |
| 160 |
} |
| 161 |
|
| 162 |
for { |
| 163 |
if a.iterator == nil { |
| 164 |
node := a.Input.Select(t) |
| 165 |
if node == nil { |
| 166 |
return nil |
| 167 |
} |
| 168 |
// Reset position for a new input context node |
| 169 |
a.pos = 0 |
| 170 |
first := true |
| 171 |
node = node.Copy() |
| 172 |
a.iterator = func() NodeNavigator { |
| 173 |
if first { |
| 174 |
first = false |
| 175 |
if a.Self && a.Predicate(node) { |
| 176 |
return node |
| 177 |
} |
| 178 |
} |
| 179 |
for node.MoveToParent() { |
| 180 |
if a.Predicate(node) { |
| 181 |
return node |
| 182 |
} |
| 183 |
} |
| 184 |
return nil |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
for node := a.iterator(); node != nil; node = a.iterator() { |
| 189 |
node_id := getHashCode(node.Copy()) |
| 190 |
if _, ok := a.table[node_id]; !ok { |
| 191 |
a.table[node_id] = true |
| 192 |
// Increase position for each matched node in current input context |
| 193 |
a.pos++ |
| 194 |
return node |
| 195 |
} |
| 196 |
} |
| 197 |
a.iterator = nil |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
func (a *ancestorQuery) Evaluate(t iterator) interface{} { |
| 202 |
a.Input.Evaluate(t) |
| 203 |
a.iterator = nil |
| 204 |
// Reset the table when re-evaluating to ensure clean state |
| 205 |
a.table = nil |
| 206 |
return a |
| 207 |
} |
| 208 |
|
| 209 |
func (a *ancestorQuery) Test(n NodeNavigator) bool { |
| 210 |
return a.Predicate(n) |
| 211 |
} |
| 212 |
|
| 213 |
func (a *ancestorQuery) Clone() query { |
| 214 |
return &ancestorQuery{name: a.name, Self: a.Self, Input: a.Input.Clone(), Predicate: a.Predicate} |
| 215 |
} |
| 216 |
|
| 217 |
func (a *ancestorQuery) ValueType() resultType { |
| 218 |
return xpathResultType.NodeSet |
| 219 |
} |
| 220 |
|
| 221 |
func (a *ancestorQuery) Properties() queryProp { |
| 222 |
return queryProps.Position | queryProps.Count | queryProps.Cached | queryProps.Merge | queryProps.Reverse |
| 223 |
} |
| 224 |
|
| 225 |
// position returns the ordinal of the current matched node within the axis |
| 226 |
// traversal for the current input context node. This is required so numeric |
| 227 |
// predicates like [1] or [2] on the ancestor axis resolve in axis order. |
| 228 |
func (a *ancestorQuery) position() int { |
| 229 |
return a.pos |
| 230 |
} |
| 231 |
|
| 232 |
// attributeQuery is an XPath attribute node query.(@*) |
| 233 |
type attributeQuery struct { |
| 234 |
name string |
| 235 |
iterator func() NodeNavigator |
| 236 |
|
| 237 |
Input query |
| 238 |
Predicate func(NodeNavigator) bool |
| 239 |
} |
| 240 |
|
| 241 |
func (a *attributeQuery) Select(t iterator) NodeNavigator { |
| 242 |
for { |
| 243 |
if a.iterator == nil { |
| 244 |
node := a.Input.Select(t) |
| 245 |
if node == nil { |
| 246 |
return nil |
| 247 |
} |
| 248 |
node = node.Copy() |
| 249 |
a.iterator = func() NodeNavigator { |
| 250 |
for { |
| 251 |
onAttr := node.MoveToNextAttribute() |
| 252 |
if !onAttr { |
| 253 |
return nil |
| 254 |
} |
| 255 |
if a.Predicate(node) { |
| 256 |
return node |
| 257 |
} |
| 258 |
} |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
if node := a.iterator(); node != nil { |
| 263 |
return node |
| 264 |
} |
| 265 |
a.iterator = nil |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
func (a *attributeQuery) Evaluate(t iterator) interface{} { |
| 270 |
a.Input.Evaluate(t) |
| 271 |
a.iterator = nil |
| 272 |
return a |
| 273 |
} |
| 274 |
|
| 275 |
func (a *attributeQuery) Test(n NodeNavigator) bool { |
| 276 |
return a.Predicate(n) |
| 277 |
} |
| 278 |
|
| 279 |
func (a *attributeQuery) Clone() query { |
| 280 |
return &attributeQuery{name: a.name, Input: a.Input.Clone(), Predicate: a.Predicate} |
| 281 |
} |
| 282 |
|
| 283 |
func (a *attributeQuery) ValueType() resultType { |
| 284 |
return xpathResultType.NodeSet |
| 285 |
} |
| 286 |
|
| 287 |
func (a *attributeQuery) Properties() queryProp { |
| 288 |
return queryProps.Merge |
| 289 |
} |
| 290 |
|
| 291 |
// childQuery is an XPath child node query.(child::*) |
| 292 |
type childQuery struct { |
| 293 |
name string |
| 294 |
posit int |
| 295 |
iterator func() NodeNavigator |
| 296 |
|
| 297 |
Input query |
| 298 |
Predicate func(NodeNavigator) bool |
| 299 |
} |
| 300 |
|
| 301 |
func (c *childQuery) Select(t iterator) NodeNavigator { |
| 302 |
for { |
| 303 |
if c.iterator == nil { |
| 304 |
c.posit = 0 |
| 305 |
node := c.Input.Select(t) |
| 306 |
if node == nil { |
| 307 |
return nil |
| 308 |
} |
| 309 |
node = node.Copy() |
| 310 |
first := true |
| 311 |
c.iterator = func() NodeNavigator { |
| 312 |
for { |
| 313 |
if (first && !node.MoveToChild()) || (!first && !node.MoveToNext()) { |
| 314 |
return nil |
| 315 |
} |
| 316 |
first = false |
| 317 |
if c.Predicate(node) { |
| 318 |
return node |
| 319 |
} |
| 320 |
} |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
if node := c.iterator(); node != nil { |
| 325 |
c.posit++ |
| 326 |
return node |
| 327 |
} |
| 328 |
c.iterator = nil |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
func (c *childQuery) Evaluate(t iterator) interface{} { |
| 333 |
c.Input.Evaluate(t) |
| 334 |
c.iterator = nil |
| 335 |
return c |
| 336 |
} |
| 337 |
|
| 338 |
func (c *childQuery) Test(n NodeNavigator) bool { |
| 339 |
return c.Predicate(n) |
| 340 |
} |
| 341 |
|
| 342 |
func (c *childQuery) Clone() query { |
| 343 |
return &childQuery{name: c.name, Input: c.Input.Clone(), Predicate: c.Predicate} |
| 344 |
} |
| 345 |
|
| 346 |
func (c *childQuery) ValueType() resultType { |
| 347 |
return xpathResultType.NodeSet |
| 348 |
} |
| 349 |
|
| 350 |
func (c *childQuery) Properties() queryProp { |
| 351 |
return queryProps.Merge |
| 352 |
} |
| 353 |
|
| 354 |
// position returns a position of current NodeNavigator. |
| 355 |
func (c *childQuery) position() int { |
| 356 |
return c.posit |
| 357 |
} |
| 358 |
|
| 359 |
type cachedChildQuery struct { |
| 360 |
name string |
| 361 |
posit int |
| 362 |
iterator func() NodeNavigator |
| 363 |
|
| 364 |
Input query |
| 365 |
Predicate func(NodeNavigator) bool |
| 366 |
} |
| 367 |
|
| 368 |
func (c *cachedChildQuery) Select(t iterator) NodeNavigator { |
| 369 |
for { |
| 370 |
if c.iterator == nil { |
| 371 |
c.posit = 0 |
| 372 |
node := c.Input.Select(t) |
| 373 |
if node == nil { |
| 374 |
return nil |
| 375 |
} |
| 376 |
node = node.Copy() |
| 377 |
first := true |
| 378 |
c.iterator = func() NodeNavigator { |
| 379 |
for { |
| 380 |
if (first && !node.MoveToChild()) || (!first && !node.MoveToNext()) { |
| 381 |
return nil |
| 382 |
} |
| 383 |
first = false |
| 384 |
if c.Predicate(node) { |
| 385 |
return node |
| 386 |
} |
| 387 |
} |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
if node := c.iterator(); node != nil { |
| 392 |
c.posit++ |
| 393 |
return node |
| 394 |
} |
| 395 |
c.iterator = nil |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
func (c *cachedChildQuery) Evaluate(t iterator) interface{} { |
| 400 |
c.Input.Evaluate(t) |
| 401 |
c.iterator = nil |
| 402 |
return c |
| 403 |
} |
| 404 |
|
| 405 |
func (c *cachedChildQuery) position() int { |
| 406 |
return c.posit |
| 407 |
} |
| 408 |
|
| 409 |
func (c *cachedChildQuery) Test(n NodeNavigator) bool { |
| 410 |
return c.Predicate(n) |
| 411 |
} |
| 412 |
|
| 413 |
func (c *cachedChildQuery) Clone() query { |
| 414 |
return &childQuery{name: c.name, Input: c.Input.Clone(), Predicate: c.Predicate} |
| 415 |
} |
| 416 |
|
| 417 |
func (c *cachedChildQuery) ValueType() resultType { |
| 418 |
return xpathResultType.NodeSet |
| 419 |
} |
| 420 |
|
| 421 |
func (c *cachedChildQuery) Properties() queryProp { |
| 422 |
return queryProps.Merge |
| 423 |
} |
| 424 |
|
| 425 |
// descendantQuery is an XPath descendant node query.(descendant::* | descendant-or-self::*) |
| 426 |
type descendantQuery struct { |
| 427 |
name string |
| 428 |
iterator func() NodeNavigator |
| 429 |
posit int |
| 430 |
level int |
| 431 |
|
| 432 |
Self bool |
| 433 |
Input query |
| 434 |
Predicate func(NodeNavigator) bool |
| 435 |
} |
| 436 |
|
| 437 |
func (d *descendantQuery) Select(t iterator) NodeNavigator { |
| 438 |
for { |
| 439 |
if d.iterator == nil { |
| 440 |
d.posit = 0 |
| 441 |
node := d.Input.Select(t) |
| 442 |
if node == nil { |
| 443 |
return nil |
| 444 |
} |
| 445 |
node = node.Copy() |
| 446 |
d.level = 0 |
| 447 |
first := true |
| 448 |
d.iterator = func() NodeNavigator { |
| 449 |
if first { |
| 450 |
first = false |
| 451 |
if d.Self && d.Predicate(node) { |
| 452 |
return node |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
for { |
| 457 |
if node.MoveToChild() { |
| 458 |
d.level = d.level + 1 |
| 459 |
} else { |
| 460 |
for { |
| 461 |
if d.level == 0 { |
| 462 |
return nil |
| 463 |
} |
| 464 |
if node.MoveToNext() { |
| 465 |
break |
| 466 |
} |
| 467 |
node.MoveToParent() |
| 468 |
d.level = d.level - 1 |
| 469 |
} |
| 470 |
} |
| 471 |
if d.Predicate(node) { |
| 472 |
return node |
| 473 |
} |
| 474 |
} |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
if node := d.iterator(); node != nil { |
| 479 |
d.posit++ |
| 480 |
return node |
| 481 |
} |
| 482 |
d.iterator = nil |
| 483 |
} |
| 484 |
} |
| 485 |
|
| 486 |
func (d *descendantQuery) Evaluate(t iterator) interface{} { |
| 487 |
d.Input.Evaluate(t) |
| 488 |
d.iterator = nil |
| 489 |
return d |
| 490 |
} |
| 491 |
|
| 492 |
func (d *descendantQuery) Test(n NodeNavigator) bool { |
| 493 |
return d.Predicate(n) |
| 494 |
} |
| 495 |
|
| 496 |
// position returns a position of current NodeNavigator. |
| 497 |
func (d *descendantQuery) position() int { |
| 498 |
return d.posit |
| 499 |
} |
| 500 |
|
| 501 |
func (d *descendantQuery) depth() int { |
| 502 |
return d.level |
| 503 |
} |
| 504 |
|
| 505 |
func (d *descendantQuery) Clone() query { |
| 506 |
return &descendantQuery{name: d.name, Self: d.Self, Input: d.Input.Clone(), Predicate: d.Predicate} |
| 507 |
} |
| 508 |
|
| 509 |
func (d *descendantQuery) ValueType() resultType { |
| 510 |
return xpathResultType.NodeSet |
| 511 |
} |
| 512 |
|
| 513 |
func (d *descendantQuery) Properties() queryProp { |
| 514 |
return queryProps.Merge |
| 515 |
} |
| 516 |
|
| 517 |
// followingQuery is an XPath following node query.(following::*|following-sibling::*) |
| 518 |
type followingQuery struct { |
| 519 |
posit int |
| 520 |
iterator func() NodeNavigator |
| 521 |
|
| 522 |
Input query |
| 523 |
Sibling bool // The matching sibling node of current node. |
| 524 |
Predicate func(NodeNavigator) bool |
| 525 |
} |
| 526 |
|
| 527 |
func (f *followingQuery) Select(t iterator) NodeNavigator { |
| 528 |
for { |
| 529 |
if f.iterator == nil { |
| 530 |
f.posit = 0 |
| 531 |
node := f.Input.Select(t) |
| 532 |
if node == nil { |
| 533 |
return nil |
| 534 |
} |
| 535 |
node = node.Copy() |
| 536 |
if f.Sibling { |
| 537 |
f.iterator = func() NodeNavigator { |
| 538 |
for { |
| 539 |
if !node.MoveToNext() { |
| 540 |
return nil |
| 541 |
} |
| 542 |
if f.Predicate(node) { |
| 543 |
f.posit++ |
| 544 |
return node |
| 545 |
} |
| 546 |
} |
| 547 |
} |
| 548 |
} else { |
| 549 |
var q *descendantQuery // descendant query |
| 550 |
f.iterator = func() NodeNavigator { |
| 551 |
for { |
| 552 |
if q == nil { |
| 553 |
for !node.MoveToNext() { |
| 554 |
if !node.MoveToParent() { |
| 555 |
return nil |
| 556 |
} |
| 557 |
} |
| 558 |
q = &descendantQuery{ |
| 559 |
Self: true, |
| 560 |
Input: &contextQuery{}, |
| 561 |
Predicate: f.Predicate, |
| 562 |
} |
| 563 |
t.Current().MoveTo(node) |
| 564 |
} |
| 565 |
if node := q.Select(t); node != nil { |
| 566 |
f.posit = q.posit |
| 567 |
return node |
| 568 |
} |
| 569 |
q = nil |
| 570 |
} |
| 571 |
} |
| 572 |
} |
| 573 |
} |
| 574 |
|
| 575 |
if node := f.iterator(); node != nil { |
| 576 |
return node |
| 577 |
} |
| 578 |
f.iterator = nil |
| 579 |
} |
| 580 |
} |
| 581 |
|
| 582 |
func (f *followingQuery) Evaluate(t iterator) interface{} { |
| 583 |
f.Input.Evaluate(t) |
| 584 |
return f |
| 585 |
} |
| 586 |
|
| 587 |
func (f *followingQuery) Test(n NodeNavigator) bool { |
| 588 |
return f.Predicate(n) |
| 589 |
} |
| 590 |
|
| 591 |
func (f *followingQuery) Clone() query { |
| 592 |
return &followingQuery{Input: f.Input.Clone(), Sibling: f.Sibling, Predicate: f.Predicate} |
| 593 |
} |
| 594 |
|
| 595 |
func (f *followingQuery) ValueType() resultType { |
| 596 |
return xpathResultType.NodeSet |
| 597 |
} |
| 598 |
|
| 599 |
func (f *followingQuery) Properties() queryProp { |
| 600 |
return queryProps.Merge |
| 601 |
} |
| 602 |
|
| 603 |
func (f *followingQuery) position() int { |
| 604 |
return f.posit |
| 605 |
} |
| 606 |
|
| 607 |
// precedingQuery is an XPath preceding node query.(preceding::*) |
| 608 |
type precedingQuery struct { |
| 609 |
iterator func() NodeNavigator |
| 610 |
posit int |
| 611 |
Input query |
| 612 |
Sibling bool // The matching sibling node of current node. |
| 613 |
Predicate func(NodeNavigator) bool |
| 614 |
} |
| 615 |
|
| 616 |
func (p *precedingQuery) Select(t iterator) NodeNavigator { |
| 617 |
for { |
| 618 |
if p.iterator == nil { |
| 619 |
p.posit = 0 |
| 620 |
node := p.Input.Select(t) |
| 621 |
if node == nil { |
| 622 |
return nil |
| 623 |
} |
| 624 |
node = node.Copy() |
| 625 |
if p.Sibling { |
| 626 |
p.iterator = func() NodeNavigator { |
| 627 |
for { |
| 628 |
for !node.MoveToPrevious() { |
| 629 |
return nil |
| 630 |
} |
| 631 |
if p.Predicate(node) { |
| 632 |
p.posit++ |
| 633 |
return node |
| 634 |
} |
| 635 |
} |
| 636 |
} |
| 637 |
} else { |
| 638 |
var q query |
| 639 |
p.iterator = func() NodeNavigator { |
| 640 |
for { |
| 641 |
if q == nil { |
| 642 |
for !node.MoveToPrevious() { |
| 643 |
if !node.MoveToParent() { |
| 644 |
return nil |
| 645 |
} |
| 646 |
p.posit = 0 |
| 647 |
} |
| 648 |
q = &descendantQuery{ |
| 649 |
Self: true, |
| 650 |
Input: &contextQuery{}, |
| 651 |
Predicate: p.Predicate, |
| 652 |
} |
| 653 |
t.Current().MoveTo(node) |
| 654 |
} |
| 655 |
if node := q.Select(t); node != nil { |
| 656 |
p.posit++ |
| 657 |
return node |
| 658 |
} |
| 659 |
q = nil |
| 660 |
} |
| 661 |
} |
| 662 |
} |
| 663 |
} |
| 664 |
if node := p.iterator(); node != nil { |
| 665 |
return node |
| 666 |
} |
| 667 |
p.iterator = nil |
| 668 |
} |
| 669 |
} |
| 670 |
|
| 671 |
func (p *precedingQuery) Evaluate(t iterator) interface{} { |
| 672 |
p.Input.Evaluate(t) |
| 673 |
return p |
| 674 |
} |
| 675 |
|
| 676 |
func (p *precedingQuery) Test(n NodeNavigator) bool { |
| 677 |
return p.Predicate(n) |
| 678 |
} |
| 679 |
|
| 680 |
func (p *precedingQuery) Clone() query { |
| 681 |
return &precedingQuery{Input: p.Input.Clone(), Sibling: p.Sibling, Predicate: p.Predicate} |
| 682 |
} |
| 683 |
|
| 684 |
func (p *precedingQuery) ValueType() resultType { |
| 685 |
return xpathResultType.NodeSet |
| 686 |
} |
| 687 |
|
| 688 |
func (p *precedingQuery) Properties() queryProp { |
| 689 |
return queryProps.Merge | queryProps.Reverse |
| 690 |
} |
| 691 |
|
| 692 |
func (p *precedingQuery) position() int { |
| 693 |
return p.posit |
| 694 |
} |
| 695 |
|
| 696 |
// parentQuery is an XPath parent node query.(parent::*) |
| 697 |
type parentQuery struct { |
| 698 |
Input query |
| 699 |
Predicate func(NodeNavigator) bool |
| 700 |
} |
| 701 |
|
| 702 |
func (p *parentQuery) Select(t iterator) NodeNavigator { |
| 703 |
for { |
| 704 |
node := p.Input.Select(t) |
| 705 |
if node == nil { |
| 706 |
return nil |
| 707 |
} |
| 708 |
node = node.Copy() |
| 709 |
if node.MoveToParent() && p.Predicate(node) { |
| 710 |
return node |
| 711 |
} |
| 712 |
} |
| 713 |
} |
| 714 |
|
| 715 |
func (p *parentQuery) Evaluate(t iterator) interface{} { |
| 716 |
p.Input.Evaluate(t) |
| 717 |
return p |
| 718 |
} |
| 719 |
|
| 720 |
func (p *parentQuery) Clone() query { |
| 721 |
return &parentQuery{Input: p.Input.Clone(), Predicate: p.Predicate} |
| 722 |
} |
| 723 |
|
| 724 |
func (p *parentQuery) ValueType() resultType { |
| 725 |
return xpathResultType.NodeSet |
| 726 |
} |
| 727 |
|
| 728 |
func (p *parentQuery) Properties() queryProp { |
| 729 |
return queryProps.Position | queryProps.Count | queryProps.Cached | queryProps.Merge |
| 730 |
} |
| 731 |
|
| 732 |
func (p *parentQuery) Test(n NodeNavigator) bool { |
| 733 |
return p.Predicate(n) |
| 734 |
} |
| 735 |
|
| 736 |
// selfQuery is an Self node query.(self::*) |
| 737 |
type selfQuery struct { |
| 738 |
Input query |
| 739 |
Predicate func(NodeNavigator) bool |
| 740 |
} |
| 741 |
|
| 742 |
func (s *selfQuery) Select(t iterator) NodeNavigator { |
| 743 |
for { |
| 744 |
node := s.Input.Select(t) |
| 745 |
if node == nil { |
| 746 |
return nil |
| 747 |
} |
| 748 |
|
| 749 |
if s.Predicate(node) { |
| 750 |
return node |
| 751 |
} |
| 752 |
} |
| 753 |
} |
| 754 |
|
| 755 |
func (s *selfQuery) Evaluate(t iterator) interface{} { |
| 756 |
s.Input.Evaluate(t) |
| 757 |
return s |
| 758 |
} |
| 759 |
|
| 760 |
func (s *selfQuery) Test(n NodeNavigator) bool { |
| 761 |
return s.Predicate(n) |
| 762 |
} |
| 763 |
|
| 764 |
func (s *selfQuery) Clone() query { |
| 765 |
return &selfQuery{Input: s.Input.Clone(), Predicate: s.Predicate} |
| 766 |
} |
| 767 |
|
| 768 |
func (s *selfQuery) ValueType() resultType { |
| 769 |
return xpathResultType.NodeSet |
| 770 |
} |
| 771 |
|
| 772 |
func (s *selfQuery) Properties() queryProp { |
| 773 |
return queryProps.Merge |
| 774 |
} |
| 775 |
|
| 776 |
// filterQuery is an XPath query for predicate filter. |
| 777 |
type filterQuery struct { |
| 778 |
Input query |
| 779 |
Predicate query |
| 780 |
NoPosition bool |
| 781 |
|
| 782 |
posit int |
| 783 |
positmap map[int]int |
| 784 |
} |
| 785 |
|
| 786 |
func (f *filterQuery) do(t iterator) bool { |
| 787 |
val := reflect.ValueOf(f.Predicate.Evaluate(t)) |
| 788 |
switch val.Kind() { |
| 789 |
case reflect.Bool: |
| 790 |
return val.Bool() |
| 791 |
case reflect.String: |
| 792 |
return len(val.String()) > 0 |
| 793 |
case reflect.Float64: |
| 794 |
pt := getNodePosition(f.Input) |
| 795 |
return int(val.Float()) == pt |
| 796 |
default: |
| 797 |
if f.Predicate != nil { |
| 798 |
return f.Predicate.Select(t) != nil |
| 799 |
} |
| 800 |
} |
| 801 |
return false |
| 802 |
} |
| 803 |
|
| 804 |
func (f *filterQuery) position() int { |
| 805 |
return f.posit |
| 806 |
} |
| 807 |
|
| 808 |
func (f *filterQuery) Select(t iterator) NodeNavigator { |
| 809 |
if f.positmap == nil { |
| 810 |
f.positmap = make(map[int]int) |
| 811 |
} |
| 812 |
for { |
| 813 |
|
| 814 |
node := f.Input.Select(t) |
| 815 |
if node == nil { |
| 816 |
return nil |
| 817 |
} |
| 818 |
node = node.Copy() |
| 819 |
|
| 820 |
t.Current().MoveTo(node) |
| 821 |
if f.do(t) { |
| 822 |
// fix https://github.com/antchfx/htmlquery/issues/26 |
| 823 |
// Calculate and keep the each of matching node's position in the same depth. |
| 824 |
level := getNodeDepth(f.Input) |
| 825 |
f.positmap[level]++ |
| 826 |
f.posit = f.positmap[level] |
| 827 |
return node |
| 828 |
} |
| 829 |
} |
| 830 |
} |
| 831 |
|
| 832 |
func (f *filterQuery) Evaluate(t iterator) interface{} { |
| 833 |
f.Input.Evaluate(t) |
| 834 |
// Reset the position map when re-evaluating to ensure clean state |
| 835 |
f.positmap = nil |
| 836 |
return f |
| 837 |
} |
| 838 |
|
| 839 |
func (f *filterQuery) Clone() query { |
| 840 |
return &filterQuery{Input: f.Input.Clone(), Predicate: f.Predicate.Clone()} |
| 841 |
} |
| 842 |
|
| 843 |
func (f *filterQuery) ValueType() resultType { |
| 844 |
return xpathResultType.NodeSet |
| 845 |
} |
| 846 |
|
| 847 |
func (f *filterQuery) Properties() queryProp { |
| 848 |
return (queryProps.Position | f.Input.Properties()) & (queryProps.Reverse | queryProps.Merge) |
| 849 |
} |
| 850 |
|
| 851 |
// functionQuery is an XPath function that returns a computed value for |
| 852 |
// the Evaluate call of the current NodeNavigator node. Select call isn't |
| 853 |
// applicable for functionQuery. |
| 854 |
type functionQuery struct { |
| 855 |
Input query // Node Set |
| 856 |
Func func(query, iterator) interface{} // The xpath function. |
| 857 |
} |
| 858 |
|
| 859 |
func (f *functionQuery) Select(t iterator) NodeNavigator { |
| 860 |
return nil |
| 861 |
} |
| 862 |
|
| 863 |
// Evaluate call a specified function that will returns the |
| 864 |
// following value type: number,string,boolean. |
| 865 |
func (f *functionQuery) Evaluate(t iterator) interface{} { |
| 866 |
return f.Func(f.Input, t) |
| 867 |
} |
| 868 |
|
| 869 |
func (f *functionQuery) Clone() query { |
| 870 |
if f.Input == nil { |
| 871 |
return &functionQuery{Func: f.Func} |
| 872 |
} |
| 873 |
return &functionQuery{Input: f.Input.Clone(), Func: f.Func} |
| 874 |
} |
| 875 |
|
| 876 |
func (f *functionQuery) ValueType() resultType { |
| 877 |
return xpathResultType.Any |
| 878 |
} |
| 879 |
|
| 880 |
func (f *functionQuery) Properties() queryProp { |
| 881 |
return queryProps.Merge |
| 882 |
} |
| 883 |
|
| 884 |
// transformFunctionQuery diffs from functionQuery where the latter computes a scalar |
| 885 |
// value (number,string,boolean) for the current NodeNavigator node while the former |
| 886 |
// (transformFunctionQuery) performs a mapping or transform of the current NodeNavigator |
| 887 |
// and returns a new NodeNavigator. It is used for non-scalar XPath functions such as |
| 888 |
// reverse(), remove(), subsequence(), unordered(), etc. |
| 889 |
type transformFunctionQuery struct { |
| 890 |
Input query |
| 891 |
Func func(query, iterator) func() NodeNavigator |
| 892 |
iterator func() NodeNavigator |
| 893 |
} |
| 894 |
|
| 895 |
func (f *transformFunctionQuery) Select(t iterator) NodeNavigator { |
| 896 |
if f.iterator == nil { |
| 897 |
f.iterator = f.Func(f.Input, t) |
| 898 |
} |
| 899 |
return f.iterator() |
| 900 |
} |
| 901 |
|
| 902 |
func (f *transformFunctionQuery) Evaluate(t iterator) interface{} { |
| 903 |
f.Input.Evaluate(t) |
| 904 |
f.iterator = nil |
| 905 |
return f |
| 906 |
} |
| 907 |
|
| 908 |
func (f *transformFunctionQuery) Clone() query { |
| 909 |
return &transformFunctionQuery{Input: f.Input.Clone(), Func: f.Func} |
| 910 |
} |
| 911 |
|
| 912 |
func (f *transformFunctionQuery) ValueType() resultType { |
| 913 |
return xpathResultType.Any |
| 914 |
} |
| 915 |
|
| 916 |
func (f *transformFunctionQuery) Properties() queryProp { |
| 917 |
return queryProps.Merge |
| 918 |
} |
| 919 |
|
| 920 |
// constantQuery is an XPath constant operand. |
| 921 |
type constantQuery struct { |
| 922 |
Val interface{} |
| 923 |
} |
| 924 |
|
| 925 |
func (c *constantQuery) Select(t iterator) NodeNavigator { |
| 926 |
return nil |
| 927 |
} |
| 928 |
|
| 929 |
func (c *constantQuery) Evaluate(t iterator) interface{} { |
| 930 |
return c.Val |
| 931 |
} |
| 932 |
|
| 933 |
func (c *constantQuery) Clone() query { |
| 934 |
return c |
| 935 |
} |
| 936 |
|
| 937 |
func (c *constantQuery) ValueType() resultType { |
| 938 |
return getXPathType(c.Val) |
| 939 |
} |
| 940 |
|
| 941 |
func (c *constantQuery) Properties() queryProp { |
| 942 |
return queryProps.Position | queryProps.Count | queryProps.Cached | queryProps.Merge |
| 943 |
} |
| 944 |
|
| 945 |
type groupQuery struct { |
| 946 |
posit int |
| 947 |
|
| 948 |
Input query |
| 949 |
} |
| 950 |
|
| 951 |
func (g *groupQuery) Select(t iterator) NodeNavigator { |
| 952 |
node := g.Input.Select(t) |
| 953 |
if node == nil { |
| 954 |
return nil |
| 955 |
} |
| 956 |
g.posit++ |
| 957 |
return node |
| 958 |
} |
| 959 |
|
| 960 |
func (g *groupQuery) Evaluate(t iterator) interface{} { |
| 961 |
return g.Input.Evaluate(t) |
| 962 |
} |
| 963 |
|
| 964 |
func (g *groupQuery) Clone() query { |
| 965 |
return &groupQuery{Input: g.Input.Clone()} |
| 966 |
} |
| 967 |
|
| 968 |
func (g *groupQuery) ValueType() resultType { |
| 969 |
return g.Input.ValueType() |
| 970 |
} |
| 971 |
|
| 972 |
func (g *groupQuery) Properties() queryProp { |
| 973 |
return queryProps.Position |
| 974 |
} |
| 975 |
|
| 976 |
func (g *groupQuery) position() int { |
| 977 |
return g.posit |
| 978 |
} |
| 979 |
|
| 980 |
// logicalQuery is an XPath logical expression. |
| 981 |
type logicalQuery struct { |
| 982 |
Left, Right query |
| 983 |
|
| 984 |
Do func(iterator, interface{}, interface{}) interface{} |
| 985 |
} |
| 986 |
|
| 987 |
func (l *logicalQuery) Select(t iterator) NodeNavigator { |
| 988 |
return nil |
| 989 |
} |
| 990 |
|
| 991 |
func (l *logicalQuery) Evaluate(t iterator) interface{} { |
| 992 |
m := l.Left.Evaluate(t) |
| 993 |
n := l.Right.Evaluate(t) |
| 994 |
return l.Do(t, m, n) |
| 995 |
} |
| 996 |
|
| 997 |
func (l *logicalQuery) Clone() query { |
| 998 |
return &logicalQuery{Left: l.Left.Clone(), Right: l.Right.Clone(), Do: l.Do} |
| 999 |
} |
| 1000 |
|
| 1001 |
func (l *logicalQuery) ValueType() resultType { |
| 1002 |
return xpathResultType.Boolean |
| 1003 |
} |
| 1004 |
|
| 1005 |
func (l *logicalQuery) Properties() queryProp { |
| 1006 |
return queryProps.Merge |
| 1007 |
} |
| 1008 |
|
| 1009 |
// numericQuery is an XPath numeric operator expression. |
| 1010 |
type numericQuery struct { |
| 1011 |
Left, Right query |
| 1012 |
|
| 1013 |
Do func(iterator, interface{}, interface{}) interface{} |
| 1014 |
} |
| 1015 |
|
| 1016 |
func (n *numericQuery) Select(t iterator) NodeNavigator { |
| 1017 |
return nil |
| 1018 |
} |
| 1019 |
|
| 1020 |
func (n *numericQuery) Evaluate(t iterator) interface{} { |
| 1021 |
// Snapshot the operator's context so each operand evaluates and coerces |
| 1022 |
// against equivalent context. filterQuery (predicates) mutates t.Current() |
| 1023 |
// while selecting the left operand; without restore, a context-sensitive |
| 1024 |
// right operand (e.g. count(self::*)) observes the moved navigator. |
| 1025 |
// Eager asNumber materializes node-set coercion before restore so Do's |
| 1026 |
// lazy conversion cannot re-Select against the wrong context either. |
| 1027 |
root := t.Current().Copy() |
| 1028 |
m := asNumber(t, n.Left.Evaluate(t)) |
| 1029 |
t.Current().MoveTo(root) |
| 1030 |
k := asNumber(t, n.Right.Evaluate(t)) |
| 1031 |
t.Current().MoveTo(root) |
| 1032 |
return n.Do(t, m, k) |
| 1033 |
} |
| 1034 |
|
| 1035 |
func (n *numericQuery) Clone() query { |
| 1036 |
return &numericQuery{Left: n.Left.Clone(), Right: n.Right.Clone(), Do: n.Do} |
| 1037 |
} |
| 1038 |
|
| 1039 |
func (n *numericQuery) ValueType() resultType { |
| 1040 |
return xpathResultType.Number |
| 1041 |
} |
| 1042 |
|
| 1043 |
func (n *numericQuery) Properties() queryProp { |
| 1044 |
return queryProps.Merge |
| 1045 |
} |
| 1046 |
|
| 1047 |
type booleanQuery struct { |
| 1048 |
IsOr bool |
| 1049 |
Left, Right query |
| 1050 |
iterator func() NodeNavigator |
| 1051 |
} |
| 1052 |
|
| 1053 |
func (b *booleanQuery) Select(t iterator) NodeNavigator { |
| 1054 |
if b.iterator == nil { |
| 1055 |
var list []NodeNavigator |
| 1056 |
i := 0 |
| 1057 |
root := t.Current().Copy() |
| 1058 |
if b.IsOr { |
| 1059 |
for { |
| 1060 |
node := b.Left.Select(t) |
| 1061 |
if node == nil { |
| 1062 |
break |
| 1063 |
} |
| 1064 |
node = node.Copy() |
| 1065 |
list = append(list, node) |
| 1066 |
} |
| 1067 |
t.Current().MoveTo(root) |
| 1068 |
for { |
| 1069 |
node := b.Right.Select(t) |
| 1070 |
if node == nil { |
| 1071 |
break |
| 1072 |
} |
| 1073 |
node = node.Copy() |
| 1074 |
list = append(list, node) |
| 1075 |
} |
| 1076 |
} else { |
| 1077 |
var m []NodeNavigator |
| 1078 |
var n []NodeNavigator |
| 1079 |
for { |
| 1080 |
node := b.Left.Select(t) |
| 1081 |
if node == nil { |
| 1082 |
break |
| 1083 |
} |
| 1084 |
node = node.Copy() |
| 1085 |
list = append(m, node) |
| 1086 |
} |
| 1087 |
t.Current().MoveTo(root) |
| 1088 |
for { |
| 1089 |
node := b.Right.Select(t) |
| 1090 |
if node == nil { |
| 1091 |
break |
| 1092 |
} |
| 1093 |
node = node.Copy() |
| 1094 |
list = append(n, node) |
| 1095 |
} |
| 1096 |
for _, k := range m { |
| 1097 |
for _, j := range n { |
| 1098 |
if k == j { |
| 1099 |
list = append(list, k) |
| 1100 |
} |
| 1101 |
} |
| 1102 |
} |
| 1103 |
} |
| 1104 |
|
| 1105 |
b.iterator = func() NodeNavigator { |
| 1106 |
if i >= len(list) { |
| 1107 |
return nil |
| 1108 |
} |
| 1109 |
node := list[i] |
| 1110 |
i++ |
| 1111 |
return node |
| 1112 |
} |
| 1113 |
} |
| 1114 |
return b.iterator() |
| 1115 |
} |
| 1116 |
|
| 1117 |
func (b *booleanQuery) Evaluate(t iterator) interface{} { |
| 1118 |
n := t.Current().Copy() |
| 1119 |
|
| 1120 |
m := b.Left.Evaluate(t) |
| 1121 |
left := asBool(t, m) |
| 1122 |
if b.IsOr && left { |
| 1123 |
return true |
| 1124 |
} else if !b.IsOr && !left { |
| 1125 |
return false |
| 1126 |
} |
| 1127 |
|
| 1128 |
t.Current().MoveTo(n) |
| 1129 |
m = b.Right.Evaluate(t) |
| 1130 |
return asBool(t, m) |
| 1131 |
} |
| 1132 |
|
| 1133 |
func (b *booleanQuery) Clone() query { |
| 1134 |
return &booleanQuery{IsOr: b.IsOr, Left: b.Left.Clone(), Right: b.Right.Clone()} |
| 1135 |
} |
| 1136 |
|
| 1137 |
func (b *booleanQuery) ValueType() resultType { |
| 1138 |
return xpathResultType.Boolean |
| 1139 |
} |
| 1140 |
|
| 1141 |
func (b *booleanQuery) Properties() queryProp { |
| 1142 |
return queryProps.Merge |
| 1143 |
} |
| 1144 |
|
| 1145 |
type unionQuery struct { |
| 1146 |
Left, Right query |
| 1147 |
iterator func() NodeNavigator |
| 1148 |
} |
| 1149 |
|
| 1150 |
func (u *unionQuery) Select(t iterator) NodeNavigator { |
| 1151 |
if u.iterator == nil { |
| 1152 |
var list []NodeNavigator |
| 1153 |
var m = make(map[uint64]bool) |
| 1154 |
root := t.Current().Copy() |
| 1155 |
for { |
| 1156 |
node := u.Left.Select(t) |
| 1157 |
if node == nil { |
| 1158 |
break |
| 1159 |
} |
| 1160 |
code := getHashCode(node.Copy()) |
| 1161 |
if _, ok := m[code]; !ok { |
| 1162 |
m[code] = true |
| 1163 |
list = append(list, node.Copy()) |
| 1164 |
} |
| 1165 |
} |
| 1166 |
t.Current().MoveTo(root) |
| 1167 |
for { |
| 1168 |
node := u.Right.Select(t) |
| 1169 |
if node == nil { |
| 1170 |
break |
| 1171 |
} |
| 1172 |
code := getHashCode(node.Copy()) |
| 1173 |
if _, ok := m[code]; !ok { |
| 1174 |
m[code] = true |
| 1175 |
list = append(list, node.Copy()) |
| 1176 |
} |
| 1177 |
} |
| 1178 |
var i int |
| 1179 |
u.iterator = func() NodeNavigator { |
| 1180 |
if i >= len(list) { |
| 1181 |
return nil |
| 1182 |
} |
| 1183 |
node := list[i] |
| 1184 |
i++ |
| 1185 |
return node |
| 1186 |
} |
| 1187 |
} |
| 1188 |
return u.iterator() |
| 1189 |
} |
| 1190 |
|
| 1191 |
func (u *unionQuery) Evaluate(t iterator) interface{} { |
| 1192 |
u.iterator = nil |
| 1193 |
u.Left.Evaluate(t) |
| 1194 |
u.Right.Evaluate(t) |
| 1195 |
return u |
| 1196 |
} |
| 1197 |
|
| 1198 |
func (u *unionQuery) Clone() query { |
| 1199 |
return &unionQuery{Left: u.Left.Clone(), Right: u.Right.Clone()} |
| 1200 |
} |
| 1201 |
|
| 1202 |
func (u *unionQuery) ValueType() resultType { |
| 1203 |
return xpathResultType.NodeSet |
| 1204 |
} |
| 1205 |
|
| 1206 |
func (u *unionQuery) Properties() queryProp { |
| 1207 |
return queryProps.Merge |
| 1208 |
} |
| 1209 |
|
| 1210 |
type lastFuncQuery struct { |
| 1211 |
buffer []NodeNavigator |
| 1212 |
counted bool |
| 1213 |
|
| 1214 |
Input query |
| 1215 |
} |
| 1216 |
|
| 1217 |
func (q *lastFuncQuery) Select(t iterator) NodeNavigator { |
| 1218 |
return nil |
| 1219 |
} |
| 1220 |
|
| 1221 |
func (q *lastFuncQuery) Evaluate(t iterator) interface{} { |
| 1222 |
if !q.counted { |
| 1223 |
for { |
| 1224 |
node := q.Input.Select(t) |
| 1225 |
if node == nil { |
| 1226 |
break |
| 1227 |
} |
| 1228 |
q.buffer = append(q.buffer, node.Copy()) |
| 1229 |
} |
| 1230 |
q.counted = true |
| 1231 |
} |
| 1232 |
return float64(len(q.buffer)) |
| 1233 |
} |
| 1234 |
|
| 1235 |
func (q *lastFuncQuery) Clone() query { |
| 1236 |
return &lastFuncQuery{Input: q.Input.Clone()} |
| 1237 |
} |
| 1238 |
|
| 1239 |
func (q *lastFuncQuery) ValueType() resultType { |
| 1240 |
return xpathResultType.Number |
| 1241 |
} |
| 1242 |
|
| 1243 |
func (q *lastFuncQuery) Properties() queryProp { |
| 1244 |
return queryProps.Merge |
| 1245 |
} |
| 1246 |
|
| 1247 |
type descendantOverDescendantQuery struct { |
| 1248 |
name string |
| 1249 |
level int |
| 1250 |
posit int |
| 1251 |
currentNode NodeNavigator |
| 1252 |
|
| 1253 |
Input query |
| 1254 |
MatchSelf bool |
| 1255 |
Predicate func(NodeNavigator) bool |
| 1256 |
} |
| 1257 |
|
| 1258 |
func (d *descendantOverDescendantQuery) moveToFirstChild() bool { |
| 1259 |
if d.currentNode.MoveToChild() { |
| 1260 |
d.level++ |
| 1261 |
return true |
| 1262 |
} |
| 1263 |
return false |
| 1264 |
} |
| 1265 |
|
| 1266 |
func (d *descendantOverDescendantQuery) moveUpUntilNext() bool { |
| 1267 |
for !d.currentNode.MoveToNext() { |
| 1268 |
d.level-- |
| 1269 |
if d.level == 0 { |
| 1270 |
return false |
| 1271 |
} |
| 1272 |
d.currentNode.MoveToParent() |
| 1273 |
} |
| 1274 |
return true |
| 1275 |
} |
| 1276 |
|
| 1277 |
func (d *descendantOverDescendantQuery) Select(t iterator) NodeNavigator { |
| 1278 |
for { |
| 1279 |
if d.level == 0 { |
| 1280 |
node := d.Input.Select(t) |
| 1281 |
if node == nil { |
| 1282 |
return nil |
| 1283 |
} |
| 1284 |
d.currentNode = node.Copy() |
| 1285 |
d.posit = 0 |
| 1286 |
if d.MatchSelf && d.Predicate(d.currentNode) { |
| 1287 |
d.posit = 1 |
| 1288 |
return d.currentNode |
| 1289 |
} |
| 1290 |
d.moveToFirstChild() |
| 1291 |
} else if !d.moveUpUntilNext() { |
| 1292 |
continue |
| 1293 |
} |
| 1294 |
for ok := true; ok; ok = d.moveToFirstChild() { |
| 1295 |
if d.Predicate(d.currentNode) { |
| 1296 |
d.posit++ |
| 1297 |
return d.currentNode |
| 1298 |
} |
| 1299 |
} |
| 1300 |
} |
| 1301 |
} |
| 1302 |
|
| 1303 |
func (d *descendantOverDescendantQuery) Evaluate(t iterator) interface{} { |
| 1304 |
d.Input.Evaluate(t) |
| 1305 |
return d |
| 1306 |
} |
| 1307 |
|
| 1308 |
func (d *descendantOverDescendantQuery) Clone() query { |
| 1309 |
return &descendantOverDescendantQuery{Input: d.Input.Clone(), Predicate: d.Predicate, MatchSelf: d.MatchSelf} |
| 1310 |
} |
| 1311 |
|
| 1312 |
func (d *descendantOverDescendantQuery) ValueType() resultType { |
| 1313 |
return xpathResultType.NodeSet |
| 1314 |
} |
| 1315 |
|
| 1316 |
func (d *descendantOverDescendantQuery) Properties() queryProp { |
| 1317 |
return queryProps.Merge |
| 1318 |
} |
| 1319 |
|
| 1320 |
func (d *descendantOverDescendantQuery) position() int { |
| 1321 |
return d.posit |
| 1322 |
} |
| 1323 |
|
| 1324 |
type mergeQuery struct { |
| 1325 |
Input query |
| 1326 |
Child query |
| 1327 |
|
| 1328 |
iterator func() NodeNavigator |
| 1329 |
} |
| 1330 |
|
| 1331 |
func (m *mergeQuery) Select(t iterator) NodeNavigator { |
| 1332 |
for { |
| 1333 |
if m.iterator == nil { |
| 1334 |
root := m.Input.Select(t) |
| 1335 |
if root == nil { |
| 1336 |
return nil |
| 1337 |
} |
| 1338 |
m.Child.Evaluate(t) |
| 1339 |
root = root.Copy() |
| 1340 |
t.Current().MoveTo(root) |
| 1341 |
var list []NodeNavigator |
| 1342 |
for node := m.Child.Select(t); node != nil; node = m.Child.Select(t) { |
| 1343 |
list = append(list, node.Copy()) |
| 1344 |
} |
| 1345 |
i := 0 |
| 1346 |
m.iterator = func() NodeNavigator { |
| 1347 |
if i >= len(list) { |
| 1348 |
return nil |
| 1349 |
} |
| 1350 |
result := list[i] |
| 1351 |
i++ |
| 1352 |
return result |
| 1353 |
} |
| 1354 |
} |
| 1355 |
|
| 1356 |
if node := m.iterator(); node != nil { |
| 1357 |
return node |
| 1358 |
} |
| 1359 |
m.iterator = nil |
| 1360 |
} |
| 1361 |
} |
| 1362 |
|
| 1363 |
func (m *mergeQuery) Evaluate(t iterator) interface{} { |
| 1364 |
m.Input.Evaluate(t) |
| 1365 |
m.iterator = nil |
| 1366 |
return m |
| 1367 |
} |
| 1368 |
|
| 1369 |
func (m *mergeQuery) Clone() query { |
| 1370 |
return &mergeQuery{Input: m.Input.Clone(), Child: m.Child.Clone()} |
| 1371 |
} |
| 1372 |
|
| 1373 |
func (m *mergeQuery) ValueType() resultType { |
| 1374 |
return xpathResultType.NodeSet |
| 1375 |
} |
| 1376 |
|
| 1377 |
func (m *mergeQuery) Properties() queryProp { |
| 1378 |
return queryProps.Position | queryProps.Count | queryProps.Cached | queryProps.Merge |
| 1379 |
} |
| 1380 |
|
| 1381 |
func getHashCode(n NodeNavigator) uint64 { |
| 1382 |
var sb bytes.Buffer |
| 1383 |
switch n.NodeType() { |
| 1384 |
case AttributeNode, TextNode, CommentNode: |
| 1385 |
sb.WriteString(n.LocalName()) |
| 1386 |
sb.WriteByte('=') |
| 1387 |
sb.WriteString(n.Value()) |
| 1388 |
// https://github.com/antchfx/htmlquery/issues/25 |
| 1389 |
d := 1 |
| 1390 |
for n.MoveToPrevious() { |
| 1391 |
d++ |
| 1392 |
} |
| 1393 |
sb.WriteByte('-') |
| 1394 |
sb.WriteString(strconv.Itoa(d)) |
| 1395 |
for n.MoveToParent() { |
| 1396 |
d = 1 |
| 1397 |
for n.MoveToPrevious() { |
| 1398 |
d++ |
| 1399 |
} |
| 1400 |
sb.WriteByte('-') |
| 1401 |
sb.WriteString(strconv.Itoa(d)) |
| 1402 |
} |
| 1403 |
case ElementNode: |
| 1404 |
sb.WriteString(n.Prefix() + n.LocalName()) |
| 1405 |
d := 1 |
| 1406 |
for n.MoveToPrevious() { |
| 1407 |
d++ |
| 1408 |
} |
| 1409 |
sb.WriteByte('-') |
| 1410 |
sb.WriteString(strconv.Itoa(d)) |
| 1411 |
|
| 1412 |
for n.MoveToParent() { |
| 1413 |
d = 1 |
| 1414 |
for n.MoveToPrevious() { |
| 1415 |
d++ |
| 1416 |
} |
| 1417 |
sb.WriteByte('-') |
| 1418 |
sb.WriteString(strconv.Itoa(d)) |
| 1419 |
} |
| 1420 |
} |
| 1421 |
h := fnv.New64a() |
| 1422 |
h.Write(sb.Bytes()) |
| 1423 |
return h.Sum64() |
| 1424 |
} |
| 1425 |
|
| 1426 |
func getNodePosition(q query) int { |
| 1427 |
type Position interface { |
| 1428 |
position() int |
| 1429 |
} |
| 1430 |
if count, ok := q.(Position); ok { |
| 1431 |
return count.position() |
| 1432 |
} |
| 1433 |
return 1 |
| 1434 |
} |
| 1435 |
|
| 1436 |
func getNodeDepth(q query) int { |
| 1437 |
type Depth interface { |
| 1438 |
depth() int |
| 1439 |
} |
| 1440 |
if count, ok := q.(Depth); ok { |
| 1441 |
return count.depth() |
| 1442 |
} |
| 1443 |
return 0 |
| 1444 |
} |
| 1445 |
|
| 1446 |
func getXPathType(i interface{}) resultType { |
| 1447 |
v := reflect.ValueOf(i) |
| 1448 |
switch v.Kind() { |
| 1449 |
case reflect.Float64: |
| 1450 |
return xpathResultType.Number |
| 1451 |
case reflect.String: |
| 1452 |
return xpathResultType.String |
| 1453 |
case reflect.Bool: |
| 1454 |
return xpathResultType.Boolean |
| 1455 |
default: |
| 1456 |
if _, ok := i.(query); ok { |
| 1457 |
return xpathResultType.NodeSet |
| 1458 |
} |
| 1459 |
} |
| 1460 |
panic(fmt.Errorf("xpath unknown value type: %v", v.Kind())) |
| 1461 |
} |