github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/goquery/array.go (about) 1 package goquery 2 3 import ( 4 "code.google.com/p/go.net/html" 5 ) 6 7 // First() reduces the set of matched elements to the first in the set. 8 // It returns a new Selection object. 9 func (this *Selection) First() *Selection { 10 return this.Eq(0) 11 } 12 13 // Last() reduces the set of matched elements to the last in the set. 14 // It returns a new Selection object. 15 func (this *Selection) Last() *Selection { 16 return this.Eq(-1) 17 } 18 19 // Eq() reduces the set of matched elements to the one at the specified index. 20 // If a negative index is given, it counts backwards starting at the end of the 21 // set. It returns a new Selection object, and an empty Selection object if the 22 // index is invalid. 23 func (this *Selection) Eq(index int) *Selection { 24 if index < 0 { 25 index += len(this.Nodes) 26 } 27 return this.Slice(index, index+1) 28 } 29 30 // Slice() reduces the set of matched elements to a subset specified by a range 31 // of indices. 32 func (this *Selection) Slice(start int, end int) *Selection { 33 if start < 0 { 34 start += len(this.Nodes) 35 } 36 if end < 0 { 37 end += len(this.Nodes) 38 } 39 return pushStack(this, this.Nodes[start:end]) 40 } 41 42 // Get() retrieves the underlying node at the specified index. 43 // Get() without parameter is not implemented, since the node array is available 44 // on the Selection object. 45 func (this *Selection) Get(index int) *html.Node { 46 if index < 0 { 47 index += len(this.Nodes) // Negative index gets from the end 48 } 49 return this.Nodes[index] 50 } 51 52 // Index() returns the position of the first element within the Selection object 53 // relative to its sibling elements. 54 func (this *Selection) Index() int { 55 if len(this.Nodes) > 0 { 56 return newSingleSelection(this.Nodes[0], this.document).PrevAll().Length() 57 } 58 return -1 59 } 60 61 // IndexSelector() returns the position of the first element within the 62 // Selection object relative to the elements matched by the selector, or -1 if 63 // not found. 64 func (this *Selection) IndexSelector(selector string) int { 65 if len(this.Nodes) > 0 { 66 sel := this.document.Find(selector) 67 return indexInSlice(sel.Nodes, this.Nodes[0]) 68 } 69 return -1 70 } 71 72 // IndexOfNode() returns the position of the specified node within the Selection 73 // object, or -1 if not found. 74 func (this *Selection) IndexOfNode(node *html.Node) int { 75 return indexInSlice(this.Nodes, node) 76 } 77 78 // IndexOfSelection() returns the position of the first node in the specified 79 // Selection object within this Selection object, or -1 if not found. 80 func (this *Selection) IndexOfSelection(s *Selection) int { 81 if s != nil && len(s.Nodes) > 0 { 82 return indexInSlice(this.Nodes, s.Nodes[0]) 83 } 84 return -1 85 }