mirror of
https://github.com/PuerkitoBio/goquery
synced 2025-10-06 01:07:06 +08:00
update to use new exp/html package
This commit is contained in:
@@ -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].
|
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:
|
Once this is done, install GoQuery:
|
||||||
|
|
||||||
`go get github.com/PuerkitoBio/goquery`
|
`go get github.com/PuerkitoBio/goquery`
|
||||||
@@ -86,6 +79,7 @@ Taken from example_test.go:
|
|||||||
## TODOs
|
## TODOs
|
||||||
|
|
||||||
* Implement NextFilteredUntil...() and PrevFilteredUntil...().
|
* 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.
|
* Benchmarks so that future changes have a baseline to compare to.
|
||||||
* Add jQuery's `Closest()`? Other missing functions?
|
* Add jQuery's `Closest()`? Other missing functions?
|
||||||
* Support negative indices in `Slice()`, like jQuery.
|
* Support negative indices in `Slice()`, like jQuery.
|
||||||
|
@@ -45,7 +45,7 @@ func (this *Selection) Html() (ret string, e error) {
|
|||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
|
|
||||||
if len(this.Nodes) > 0 {
|
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)
|
e = html.Render(&buf, c)
|
||||||
if e != nil {
|
if e != nil {
|
||||||
return
|
return
|
||||||
@@ -62,9 +62,9 @@ func getNodeText(node *html.Node) string {
|
|||||||
if node.Type == html.TextNode {
|
if node.Type == html.TextNode {
|
||||||
// Keep newlines and spaces, like jQuery
|
// Keep newlines and spaces, like jQuery
|
||||||
return node.Data
|
return node.Data
|
||||||
} else if len(node.Child) > 0 {
|
} else if node.FirstChild != nil {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
for _, c := range node.Child {
|
for c := node.FirstChild; c != nil; c = c.NextSibling {
|
||||||
buf.WriteString(getNodeText(c))
|
buf.WriteString(getNodeText(c))
|
||||||
}
|
}
|
||||||
return buf.String()
|
return buf.String()
|
||||||
|
11
traversal.go
11
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
|
// Map nodes to find the matches within the children of each node
|
||||||
return mapNodes(nodes, func(i int, n *html.Node) (result []*html.Node) {
|
return mapNodes(nodes, func(i int, n *html.Node) (result []*html.Node) {
|
||||||
// Go down one level, becausejQuery's Find() selects only within descendants
|
// 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 {
|
if c.Type == html.ElementNode {
|
||||||
result = append(result, sel.MatchAll(c)...)
|
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.
|
// from the start and stop once it is found.
|
||||||
if st == siblingPrevAll || st == siblingPrevUntil {
|
if st == siblingPrevAll || st == siblingPrevUntil {
|
||||||
// Find the index of this node within its parent's children
|
// 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 {
|
if c == n {
|
||||||
// Looking for previous nodes, so start at index - 1 backwards
|
// Looking for previous nodes, so start at index - 1 backwards
|
||||||
return getChildrenWithSiblingType(p, st, n, i-1, -1, f)
|
return getChildrenWithSiblingType(p, st, n, i-1, -1, f)
|
||||||
}
|
}
|
||||||
|
i++
|
||||||
}
|
}
|
||||||
// Should never get here.
|
// Should never get here.
|
||||||
panic(errors.New(fmt.Sprintf("Could not find node %+v in his parent's Child slice.", n)))
|
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 prev *html.Node
|
||||||
var nFound bool
|
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 {
|
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
|
// Care only about elements unless we explicitly request all types of elements
|
||||||
if c.Type == html.ElementNode || st == siblingAllIncludingNonElements {
|
if c.Type == html.ElementNode || st == siblingAllIncludingNonElements {
|
||||||
|
@@ -4,6 +4,13 @@ import (
|
|||||||
"exp/html"
|
"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.
|
// Loop through all container nodes to search for the target node.
|
||||||
func sliceContains(container []*html.Node, contained *html.Node) bool {
|
func sliceContains(container []*html.Node, contained *html.Node) bool {
|
||||||
for _, n := range container {
|
for _, n := range container {
|
||||||
|
Reference in New Issue
Block a user