| | 35 | end |
| | 36 | |
| | 37 | # Puts together an array of neighboring nodes based on their proximity |
| | 38 | # to this node. So, for example, to get the next node, you could use |
| | 39 | # <tt>nodes_at(1). Or, to get the previous node, use <tt>nodes_at(1)</tt>. |
| | 40 | # |
| | 41 | # This method also accepts ranges and sets of numbers. |
| | 42 | # |
| | 43 | # ele.nodes_at(-3..-1, 1..3) # gets three nodes before and three after |
| | 44 | # ele.nodes_at(1, 5, 7) # gets three nodes at offsets below the current node |
| | 45 | # ele.nodes_at(0, 5..6) # the current node and two others |
| | 46 | def nodes_at(*pos) |
| | 47 | sib = parent.children |
| | 48 | i, si = 0, sib.index(self) |
| | 49 | Elements[* |
| | 50 | sib.select do |x| |
| | 51 | sel = case i - si when *pos |
| | 52 | true |
| | 53 | end |
| | 54 | i += 1 |
| | 55 | sel |
| | 56 | end |
| | 57 | ] |
| | 341 | end |
| | 342 | |
| | 343 | # Puts together an array of neighboring sibling elements based on their proximity |
| | 344 | # to this element. |
| | 345 | # |
| | 346 | # This method accepts ranges and sets of numbers. |
| | 347 | # |
| | 348 | # ele.siblings_at(-3..-1, 1..3) # gets three elements before and three after |
| | 349 | # ele.siblings_at(1, 5, 7) # gets three elements at offsets below the current element |
| | 350 | # ele.siblings_at(0, 5..6) # the current element and two others |
| | 351 | # |
| | 352 | # Like the other "sibling" methods, this doesn't find text and comment nodes. |
| | 353 | # Use nodes_at to include those nodes. |
| | 354 | def siblings_at(*pos) |
| | 355 | sib = parent.containers |
| | 356 | i, si = 0, sib.index(self) |
| | 357 | Elements[* |
| | 358 | sib.select do |x| |
| | 359 | sel = case i - si when *pos |
| | 360 | true |
| | 361 | end |
| | 362 | i += 1 |
| | 363 | sel |
| | 364 | end |
| | 365 | ] |