github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/goquery/utilities.go (about)

     1  package goquery
     2  
     3  import (
     4  	"code.google.com/p/go.net/html"
     5  )
     6  
     7  func getChildren(n *html.Node) (result []*html.Node) {
     8  	for c := n.FirstChild; c != nil; c = c.NextSibling {
     9  		result = append(result, c)
    10  	}
    11  	return
    12  }
    13  
    14  // Loop through all container nodes to search for the target node.
    15  func sliceContains(container []*html.Node, contained *html.Node) bool {
    16  	for _, n := range container {
    17  		if nodeContains(n, contained) {
    18  			return true
    19  		}
    20  	}
    21  
    22  	return false
    23  }
    24  
    25  // Checks if the contained node is within the container node.
    26  func nodeContains(container *html.Node, contained *html.Node) bool {
    27  	// Check if the parent of the contained node is the container node, traversing
    28  	// upward until the top is reached, or the container is found.
    29  	for contained = contained.Parent; contained != nil; contained = contained.Parent {
    30  		if container == contained {
    31  			return true
    32  		}
    33  	}
    34  	return false
    35  }
    36  
    37  // Checks if the target node is in the slice of nodes.
    38  func isInSlice(slice []*html.Node, node *html.Node) bool {
    39  	return indexInSlice(slice, node) > -1
    40  }
    41  
    42  // Returns the index of the target node in the slice, or -1.
    43  func indexInSlice(slice []*html.Node, node *html.Node) int {
    44  	if node != nil {
    45  		for i, n := range slice {
    46  			if n == node {
    47  				return i
    48  			}
    49  		}
    50  	}
    51  	return -1
    52  }
    53  
    54  // Appends the new nodes to the target slice, making sure no duplicate is added.
    55  // There is no check to the original state of the target slice, so it may still
    56  // contain duplicates. The target slice is returned because append() may create
    57  // a new underlying array.
    58  func appendWithoutDuplicates(target []*html.Node,
    59  	nodes []*html.Node) []*html.Node {
    60  
    61  	for _, n := range nodes {
    62  		if !isInSlice(target, n) {
    63  			target = append(target, n)
    64  		}
    65  	}
    66  
    67  	return target
    68  }
    69  
    70  // Loop through a selection, returning only those nodes that pass the predicate
    71  // function.
    72  func grep(sel *Selection, predicate func(i int, s *Selection) bool) (result []*html.Node) {
    73  
    74  	for i, n := range sel.Nodes {
    75  		if predicate(i, newSingleSelection(n, sel.document)) {
    76  			result = append(result, n)
    77  		}
    78  	}
    79  	return
    80  }
    81  
    82  // Creates a new Selection object based on the specified nodes, and keeps the
    83  // source Selection object on the stack (linked list).
    84  func pushStack(fromSel *Selection, nodes []*html.Node) (result *Selection) {
    85  	result = &Selection{nodes, fromSel.document, fromSel}
    86  	return
    87  }