diff --git a/README.md b/README.md index 040f79b..bcea7d8 100644 --- a/README.md +++ b/README.md @@ -10,13 +10,6 @@ Syntax-wise, it is as close as possible to jQuery, with the same function names Since this library (and cascadia) depends on the experimental branch, this package must be installed first. Both GoQuery and Cascadia expect to find the experimental library with the `"exp/html"` import statement. To install it at this location, please [follow this guide][wikiexp]. -**TEMP** : Due to recent breaking changes in the experimental HTML package (which is to be expected, being experimental and all...), user Zippoxer mentions this on golang-nuts mailing list: - -> Had to clone revision e17cd42dcc09 of Go to get cascadia [and GoQuery] to compile. - -I'll try to bring things up to date Sep. 10th, along with cascadia's author. -**END TEMP** - Once this is done, install GoQuery: `go get github.com/PuerkitoBio/goquery` @@ -86,6 +79,7 @@ Taken from example_test.go: ## TODOs * Implement NextFilteredUntil...() and PrevFilteredUntil...(). +* Refactor Next...() and Prev...() to use the new NextSibling/PrevSibling fields. * Benchmarks so that future changes have a baseline to compare to. * Add jQuery's `Closest()`? Other missing functions? * Support negative indices in `Slice()`, like jQuery. diff --git a/property.go b/property.go index a29c208..87a4e18 100644 --- a/property.go +++ b/property.go @@ -45,7 +45,7 @@ func (this *Selection) Html() (ret string, e error) { var buf bytes.Buffer if len(this.Nodes) > 0 { - for _, c := range this.Nodes[0].Child { + for c := this.Nodes[0].FirstChild; c != nil; c = c.NextSibling { e = html.Render(&buf, c) if e != nil { return @@ -62,9 +62,9 @@ func getNodeText(node *html.Node) string { if node.Type == html.TextNode { // Keep newlines and spaces, like jQuery return node.Data - } else if len(node.Child) > 0 { + } else if node.FirstChild != nil { var buf bytes.Buffer - for _, c := range node.Child { + for c := node.FirstChild; c != nil; c = c.NextSibling { buf.WriteString(getNodeText(c)) } return buf.String() diff --git a/traversal.go b/traversal.go index 474dce2..7e48713 100644 --- a/traversal.go +++ b/traversal.go @@ -288,7 +288,7 @@ func findWithSelector(nodes []*html.Node, selector string) []*html.Node { // Map nodes to find the matches within the children of each node return mapNodes(nodes, func(i int, n *html.Node) (result []*html.Node) { // Go down one level, becausejQuery's Find() selects only within descendants - for _, c := range n.Child { + for c := n.FirstChild; c != nil; c = c.NextSibling { if c.Type == html.ElementNode { result = append(result, sel.MatchAll(c)...) } @@ -354,11 +354,13 @@ func getSiblingNodes(nodes []*html.Node, st siblingType, untilSelector string, // from the start and stop once it is found. if st == siblingPrevAll || st == siblingPrevUntil { // Find the index of this node within its parent's children - for i, c := range p.Child { + var i = 0 + for c := p.FirstChild; c != nil; c = c.NextSibling { if c == n { // Looking for previous nodes, so start at index - 1 backwards return getChildrenWithSiblingType(p, st, n, i-1, -1, f) } + i++ } // Should never get here. panic(errors.New(fmt.Sprintf("Could not find node %+v in his parent's Child slice.", n))) @@ -386,10 +388,11 @@ func getChildrenWithSiblingType(parent *html.Node, st siblingType, skipNode *htm var prev *html.Node var nFound bool - var end = len(parent.Child) + var children = getChildren(parent) + var end = len(children) for i := startIndex; i >= 0 && i < end; i += increment { - c := parent.Child[i] + c := children[i] // Care only about elements unless we explicitly request all types of elements if c.Type == html.ElementNode || st == siblingAllIncludingNonElements { diff --git a/utilities.go b/utilities.go index f1da7d0..c10250c 100644 --- a/utilities.go +++ b/utilities.go @@ -4,6 +4,13 @@ import ( "exp/html" ) +func getChildren(n *html.Node) (result []*html.Node) { + for c := n.FirstChild; c != nil; c = c.NextSibling { + result = append(result, c) + } + return +} + // Loop through all container nodes to search for the target node. func sliceContains(container []*html.Node, contained *html.Node) bool { for _, n := range container {