mirror of
https://github.com/PuerkitoBio/goquery.git
synced 2024-04-21 12:31:36 +00:00
Compare commits
62
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3ce74287b9 | ||
|
|
153f1ea15a | ||
|
|
2095230494 | ||
|
|
ce7330a252 | ||
|
|
4cbe087f02 | ||
|
|
a47543523f | ||
|
|
86d245603e | ||
|
|
1fad3d426b | ||
|
|
de2d209e9e | ||
|
|
65c0ed3569 | ||
|
|
b6c7644395 | ||
|
|
11947b0fdf | ||
|
|
0f8eca33fe | ||
|
|
fee38068fd | ||
|
|
6216851ea7 | ||
|
|
2f635a81ad | ||
|
|
527d85980c | ||
|
|
fb60260fc2 | ||
|
|
6c7e5111a3 | ||
|
|
3ceadf7b6c | ||
|
|
361d1ad92e | ||
|
|
2dbc26aca4 | ||
|
|
8b62ea92ad | ||
|
|
03e170457f | ||
|
|
8442cfcc57 | ||
|
|
b5ecb79c0b | ||
|
|
1be7481ad4 | ||
|
|
39fb6d4dc4 | ||
|
|
33955beabb | ||
|
|
015f32a1b4 | ||
|
|
3b7929a0d7 | ||
|
|
53d8b4dd6f | ||
|
|
b103c0e59a | ||
|
|
36e95634e3 | ||
|
|
156fa4f6ae | ||
|
|
c76525c424 | ||
|
|
3920ccc98a | ||
|
|
eef406fe05 | ||
|
|
ed0b238f66 | ||
|
|
93ca487049 | ||
|
|
8c90f82d4f | ||
|
|
5ffcf47d06 | ||
|
|
7a43c97117 | ||
|
|
a8c788f298 | ||
|
|
faf9b4dc84 | ||
|
|
a751e062fd | ||
|
|
b6dc0bd0ea | ||
|
|
4110e175b5 | ||
|
|
d35d4cec3e | ||
|
|
11e1233457 | ||
|
|
98eb88c22b | ||
|
|
6a7f1c4a50 | ||
|
|
7307e2457c | ||
|
|
a7412da07a | ||
|
|
c5eeb05f52 | ||
|
|
372d5bdaa0 | ||
|
|
5dfda0e354 | ||
|
|
d6951d24bb | ||
|
|
c09491e08b | ||
|
|
02e233c33a | ||
|
|
e11510b114 | ||
|
|
a9cfd6779b |
@@ -1,104 +0,0 @@
|
||||
image: fedora/latest
|
||||
packages:
|
||||
- tar
|
||||
environment:
|
||||
GOROOT: /tmp/go
|
||||
GOPATH: /home/build
|
||||
GOBIN: /home/build/bin
|
||||
sources:
|
||||
- https://github.com/PuerkitoBio/goquery
|
||||
tasks:
|
||||
- setup: |
|
||||
mkdir bin
|
||||
mkdir -p src/github.com/PuerkitoBio
|
||||
mv goquery src/github.com/PuerkitoBio/
|
||||
- go1_7: |
|
||||
cd /tmp
|
||||
rm -rf go
|
||||
curl -Lo golang.tar.gz https://golang.org/dl/go1.7.6.linux-amd64.tar.gz
|
||||
tar xvf golang.tar.gz
|
||||
cd /home/build/src/github.com/PuerkitoBio/goquery
|
||||
/tmp/go/bin/go version
|
||||
/tmp/go/bin/go get -t .
|
||||
/tmp/go/bin/go test -v
|
||||
- go1_8: |
|
||||
cd /tmp
|
||||
rm -rf go
|
||||
curl -Lo golang.tar.gz https://golang.org/dl/go1.8.7.linux-amd64.tar.gz
|
||||
tar xvf golang.tar.gz
|
||||
cd /home/build/src/github.com/PuerkitoBio/goquery
|
||||
/tmp/go/bin/go version
|
||||
/tmp/go/bin/go get -t .
|
||||
/tmp/go/bin/go test -v
|
||||
- go1_9: |
|
||||
cd /tmp
|
||||
rm -rf go
|
||||
curl -Lo golang.tar.gz https://golang.org/dl/go1.9.7.linux-amd64.tar.gz
|
||||
tar xvf golang.tar.gz
|
||||
cd /home/build/src/github.com/PuerkitoBio/goquery
|
||||
/tmp/go/bin/go version
|
||||
/tmp/go/bin/go get -t .
|
||||
/tmp/go/bin/go test -v
|
||||
- go1_10: |
|
||||
cd /tmp
|
||||
rm -rf go
|
||||
curl -Lo golang.tar.gz https://golang.org/dl/go1.10.8.linux-amd64.tar.gz
|
||||
tar xvf golang.tar.gz
|
||||
cd /home/build/src/github.com/PuerkitoBio/goquery
|
||||
/tmp/go/bin/go version
|
||||
/tmp/go/bin/go get -t .
|
||||
/tmp/go/bin/go test -v
|
||||
- go1_11: |
|
||||
cd /tmp
|
||||
rm -rf go
|
||||
curl -Lo golang.tar.gz https://golang.org/dl/go1.11.13.linux-amd64.tar.gz
|
||||
tar xvf golang.tar.gz
|
||||
cd /home/build/src/github.com/PuerkitoBio/goquery
|
||||
/tmp/go/bin/go version
|
||||
/tmp/go/bin/go get -t .
|
||||
/tmp/go/bin/go test -v
|
||||
- go1_12: |
|
||||
cd /tmp
|
||||
rm -rf go
|
||||
curl -Lo golang.tar.gz https://golang.org/dl/go1.12.17.linux-amd64.tar.gz
|
||||
tar xvf golang.tar.gz
|
||||
cd /home/build/src/github.com/PuerkitoBio/goquery
|
||||
/tmp/go/bin/go version
|
||||
/tmp/go/bin/go get -t .
|
||||
/tmp/go/bin/go test -v
|
||||
- go1_13: |
|
||||
cd /tmp
|
||||
rm -rf go
|
||||
curl -Lo golang.tar.gz https://golang.org/dl/go1.13.15.linux-amd64.tar.gz
|
||||
tar xvf golang.tar.gz
|
||||
cd /home/build/src/github.com/PuerkitoBio/goquery
|
||||
/tmp/go/bin/go version
|
||||
/tmp/go/bin/go get -t .
|
||||
/tmp/go/bin/go test -v
|
||||
- go1_14: |
|
||||
cd /tmp
|
||||
rm -rf go
|
||||
curl -Lo golang.tar.gz https://golang.org/dl/go1.14.12.linux-amd64.tar.gz
|
||||
tar xvf golang.tar.gz
|
||||
cd /home/build/src/github.com/PuerkitoBio/goquery
|
||||
/tmp/go/bin/go version
|
||||
/tmp/go/bin/go get -t .
|
||||
/tmp/go/bin/go test -v
|
||||
- go1_15: |
|
||||
cd /tmp
|
||||
rm -rf go
|
||||
curl -Lo golang.tar.gz https://golang.org/dl/go1.15.5.linux-amd64.tar.gz
|
||||
tar xvf golang.tar.gz
|
||||
cd /home/build/src/github.com/PuerkitoBio/goquery
|
||||
/tmp/go/bin/go version
|
||||
/tmp/go/bin/go get -t .
|
||||
/tmp/go/bin/go test -v
|
||||
- go1_16: |
|
||||
cd /tmp
|
||||
rm -rf go
|
||||
curl -Lo golang.tar.gz https://golang.org/dl/go1.16beta1.linux-amd64.tar.gz
|
||||
tar xvf golang.tar.gz
|
||||
cd /home/build/src/github.com/PuerkitoBio/goquery
|
||||
/tmp/go/bin/go version
|
||||
/tmp/go/bin/go get -t .
|
||||
/tmp/go/bin/go test -v
|
||||
@@ -0,0 +1,7 @@
|
||||
version: 2
|
||||
updates:
|
||||
# Maintain dependencies for Go
|
||||
- package-ecosystem: "gomod"
|
||||
directory: "/"
|
||||
schedule:
|
||||
interval: "daily"
|
||||
@@ -0,0 +1,25 @@
|
||||
name: test
|
||||
on: [push, pull_request]
|
||||
|
||||
env:
|
||||
GOPROXY: https://proxy.golang.org,direct
|
||||
|
||||
jobs:
|
||||
test:
|
||||
strategy:
|
||||
matrix:
|
||||
go-version: [1.21.x, 1.22.x]
|
||||
os: [ubuntu-latest, macos-latest, windows-latest]
|
||||
runs-on: ${{ matrix.os }}
|
||||
|
||||
steps:
|
||||
- name: Install Go
|
||||
uses: actions/setup-go@v3
|
||||
with:
|
||||
go-version: ${{ matrix.go-version }}
|
||||
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v3
|
||||
|
||||
- name: Test
|
||||
run: go test ./... -v -cover
|
||||
-31
@@ -1,31 +0,0 @@
|
||||
arch:
|
||||
- amd64
|
||||
- ppc64le
|
||||
language: go
|
||||
|
||||
go:
|
||||
- 1.7.x
|
||||
- 1.8.x
|
||||
- 1.9.x
|
||||
- 1.10.x
|
||||
- 1.11.x
|
||||
- 1.12.x
|
||||
- 1.13.x
|
||||
- 1.14.x
|
||||
- 1.15.x
|
||||
- tip
|
||||
|
||||
jobs:
|
||||
exclude:
|
||||
- arch: ppc64le
|
||||
go: 1.7.x
|
||||
- arch: ppc64le
|
||||
go: 1.8.x
|
||||
- arch: ppc64le
|
||||
go: 1.9.x
|
||||
- arch: ppc64le
|
||||
go: 1.10.x
|
||||
- arch: ppc64le
|
||||
go: 1.11.x
|
||||
- arch: ppc64le
|
||||
go: 1.12.x
|
||||
@@ -1,4 +1,4 @@
|
||||
Copyright (c) 2012-2016, Martin Angers & Contributors
|
||||
Copyright (c) 2012-2021, Martin Angers & Contributors
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
# goquery - a little like that j-thing, only in Go
|
||||
[](https://builds.sr.ht/~mna/goquery/commits/fedora.yml?) [](http://travis-ci.org/PuerkitoBio/goquery) [](https://pkg.go.dev/github.com/PuerkitoBio/goquery) [](https://sourcegraph.com/github.com/PuerkitoBio/goquery?badge)
|
||||
|
||||
[](https://github.com/PuerkitoBio/goquery/actions)
|
||||
[](https://pkg.go.dev/github.com/PuerkitoBio/goquery)
|
||||
[](https://sourcegraph.com/github.com/PuerkitoBio/goquery?badge)
|
||||
|
||||
goquery brings a syntax and a set of features similar to [jQuery][] to the [Go language][go]. It is based on Go's [net/html package][html] and the CSS Selector library [cascadia][]. Since the net/html parser returns nodes, and not a full-featured DOM tree, jQuery's stateful manipulation functions (like height(), css(), detach()) have been left off.
|
||||
|
||||
@@ -19,7 +22,7 @@ Syntax-wise, it is as close as possible to jQuery, with the same function names
|
||||
|
||||
## Installation
|
||||
|
||||
Please note that because of the net/html dependency, goquery requires Go1.1+.
|
||||
Please note that starting with version `v1.9.0` of goquery, Go 1.18+ is required due to the use of generics. For previous goquery versions, a Go version of 1.1+ was required because of the `net/html` dependency. Ongoing goquery development is tested on the latest 2 versions of Go.
|
||||
|
||||
$ go get github.com/PuerkitoBio/goquery
|
||||
|
||||
@@ -37,6 +40,12 @@ Please note that because of the net/html dependency, goquery requires Go1.1+.
|
||||
|
||||
**Note that goquery's API is now stable, and will not break.**
|
||||
|
||||
* **2024-02-29 (v1.9.1)** : Improve allocation and performance of the `Map` function and `Selection.Map` method, better document the cascadia differences (thanks [@jwilsson](https://github.com/jwilsson)).
|
||||
* **2024-02-22 (v1.9.0)** : Add a generic `Map` function, **goquery now requires Go version 1.18+** (thanks [@Fesaa](https://github.com/Fesaa)).
|
||||
* **2023-02-18 (v1.8.1)** : Update `go.mod` dependencies, update CI workflow.
|
||||
* **2021-10-25 (v1.8.0)** : Add `Render` function to render a `Selection` to an `io.Writer` (thanks [@anthonygedeon](https://github.com/anthonygedeon)).
|
||||
* **2021-07-11 (v1.7.1)** : Update go.mod dependencies and add dependabot config (thanks [@jauderho](https://github.com/jauderho)).
|
||||
* **2021-06-14 (v1.7.0)** : Add `Single` and `SingleMatcher` functions to optimize first-match selection (thanks [@gdollardollar](https://github.com/gdollardollar)).
|
||||
* **2021-01-11 (v1.6.1)** : Fix panic when calling `{Prepend,Append,Set}Html` on a `Selection` that contains non-Element nodes.
|
||||
* **2020-10-08 (v1.6.0)** : Parse html in context of the container node for all functions that deal with html strings (`AfterHtml`, `AppendHtml`, etc.). Thanks to [@thiemok][thiemok] and [@davidjwilkins][djw] for their work on this.
|
||||
* **2020-02-04 (v1.5.1)** : Update module dependencies.
|
||||
@@ -50,7 +59,7 @@ Please note that because of the net/html dependency, goquery requires Go1.1+.
|
||||
* **2016-08-28 (v1.0.1)** : Optimize performance for large documents.
|
||||
* **2016-07-27 (v1.0.0)** : Tag version 1.0.0.
|
||||
* **2016-06-15** : Invalid selector strings internally compile to a `Matcher` implementation that never matches any node (instead of a panic). So for example, `doc.Find("~")` returns an empty `*Selection` object.
|
||||
* **2016-02-02** : Add `NodeName` utility function similar to the DOM's `nodeName` property. It returns the tag name of the first element in a selection, and other relevant values of non-element nodes (see godoc for details). Add `OuterHtml` utility function similar to the DOM's `outerHTML` property (named `OuterHtml` in small caps for consistency with the existing `Html` method on the `Selection`).
|
||||
* **2016-02-02** : Add `NodeName` utility function similar to the DOM's `nodeName` property. It returns the tag name of the first element in a selection, and other relevant values of non-element nodes (see [doc][] for details). Add `OuterHtml` utility function similar to the DOM's `outerHTML` property (named `OuterHtml` in small caps for consistency with the existing `Html` method on the `Selection`).
|
||||
* **2015-04-20** : Add `AttrOr` helper method to return the attribute's value or a default value if absent. Thanks to [piotrkowalczuk][piotr].
|
||||
* **2015-02-04** : Add more manipulation functions - Prepend* - thanks again to [Andrew Stone][thatguystone].
|
||||
* **2014-11-28** : Add more manipulation functions - ReplaceWith*, Wrap* and Unwrap - thanks again to [Andrew Stone][thatguystone].
|
||||
@@ -79,9 +88,9 @@ jQuery often has many variants for the same function (no argument, a selector st
|
||||
|
||||
Utility functions that are not in jQuery but are useful in Go are implemented as functions (that take a `*Selection` as parameter), to avoid a potential naming clash on the `*Selection`'s methods (reserved for jQuery-equivalent behaviour).
|
||||
|
||||
The complete [godoc reference documentation can be found here][doc].
|
||||
The complete [package reference documentation can be found here][doc].
|
||||
|
||||
Please note that Cascadia's selectors do not necessarily match all supported selectors of jQuery (Sizzle). See the [cascadia project][cascadia] for details. Invalid selector strings compile to a `Matcher` that fails to match any node. Behaviour of the various functions that take a selector string as argument follows from that fact, e.g. (where `~` is an invalid selector string):
|
||||
Please note that Cascadia's selectors do not necessarily match all supported selectors of jQuery (Sizzle). See the [cascadia project][cascadia] for details. Also, the selectors work more like the DOM's `querySelectorAll`, than jQuery's matchers - they have no concept of contextual matching (for some concrete examples of what that means, see [this ticket](https://github.com/andybalholm/cascadia/issues/61)). In practice, it doesn't matter very often but it's something worth mentioning. Invalid selector strings compile to a `Matcher` that fails to match any node. Behaviour of the various functions that take a selector string as argument follows from that fact, e.g. (where `~` is an invalid selector string):
|
||||
|
||||
* `Find("~")` returns an empty selection because the selector string doesn't match anything.
|
||||
* `Add("~")` returns a new selection that holds the same nodes as the original selection, because it didn't add any node (selector string didn't match anything).
|
||||
@@ -148,6 +157,9 @@ func main() {
|
||||
- [Geziyor](https://github.com/geziyor/geziyor), a fast web crawling & scraping framework for Go. Supports JS rendering.
|
||||
- [Pagser](https://github.com/foolin/pagser), a simple, easy, extensible, configurable HTML parser to struct based on goquery and struct tags.
|
||||
- [stitcherd](https://github.com/vhodges/stitcherd), A server for doing server side includes using css selectors and DOM updates.
|
||||
- [goskyr](https://github.com/jakopako/goskyr), an easily configurable command-line scraper written in Go.
|
||||
- [goGetJS](https://github.com/davemolk/goGetJS), a tool for extracting, searching, and saving JavaScript files (with optional headless browser).
|
||||
- [fitter](https://github.com/PxyUp/fitter), a tool for selecting values from JSON, XML, HTML and XPath formatted pages.
|
||||
|
||||
## Support
|
||||
|
||||
@@ -156,7 +168,7 @@ There are a number of ways you can support the project:
|
||||
* Use it, star it, build something with it, spread the word!
|
||||
- If you do build something open-source or otherwise publicly-visible, let me know so I can add it to the [Related Projects](#related-projects) section!
|
||||
* Raise issues to improve the project (note: doc typos and clarifications are issues too!)
|
||||
- Please search existing issues before opening a new one - it may have already been adressed.
|
||||
- Please search existing issues before opening a new one - it may have already been addressed.
|
||||
* Pull requests: please discuss new code in an issue first, unless the fix is really trivial.
|
||||
- Make sure new code is tested.
|
||||
- Be mindful of existing code - PRs that break existing code have a high probability of being declined, unless it fixes a serious issue.
|
||||
@@ -177,10 +189,10 @@ The [BSD 3-Clause license][bsd], the same as the [Go language][golic]. Cascadia'
|
||||
[bsd]: http://opensource.org/licenses/BSD-3-Clause
|
||||
[golic]: http://golang.org/LICENSE
|
||||
[caslic]: https://github.com/andybalholm/cascadia/blob/master/LICENSE
|
||||
[doc]: http://godoc.org/github.com/PuerkitoBio/goquery
|
||||
[doc]: https://pkg.go.dev/github.com/PuerkitoBio/goquery
|
||||
[index]: http://api.jquery.com/index/
|
||||
[gonet]: https://github.com/golang/net/
|
||||
[html]: http://godoc.org/golang.org/x/net/html
|
||||
[html]: https://pkg.go.dev/golang.org/x/net/html
|
||||
[wiki]: https://github.com/PuerkitoBio/goquery/wiki/Tips-and-tricks
|
||||
[thatguystone]: https://github.com/thatguystone
|
||||
[piotr]: https://github.com/piotrkowalczuk
|
||||
|
||||
@@ -46,6 +46,6 @@ func BenchmarkHtml(b *testing.B) {
|
||||
sel := DocW().Find("h2")
|
||||
b.StartTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
sel.Html()
|
||||
_, _ = sel.Html()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
module github.com/PuerkitoBio/goquery
|
||||
|
||||
require (
|
||||
github.com/andybalholm/cascadia v1.1.0
|
||||
golang.org/x/net v0.0.0-20200202094626-16171245cfb2
|
||||
github.com/andybalholm/cascadia v1.3.2
|
||||
golang.org/x/net v0.21.0
|
||||
)
|
||||
|
||||
go 1.13
|
||||
go 1.18
|
||||
|
||||
@@ -1,8 +1,38 @@
|
||||
github.com/andybalholm/cascadia v1.1.0 h1:BuuO6sSfQNFRu1LppgbD25Hr2vLYW25JvxHs5zzsLTo=
|
||||
github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y=
|
||||
github.com/andybalholm/cascadia v1.3.2 h1:3Xi6Dw5lHF15JtdcmAHD3i1+T8plmv7BQ/nsViSLyss=
|
||||
github.com/andybalholm/cascadia v1.3.2/go.mod h1:7gtRlve5FxPPgIgX36uWBX58OdBsSS6lUvCFb+h7KvU=
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20200202094626-16171245cfb2 h1:CCH4IOTTfewWjGOlSp+zGcjutRKlBEZQ6wTn8ozI/nI=
|
||||
golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
|
||||
golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns=
|
||||
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
|
||||
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
|
||||
golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
|
||||
+9
-1
@@ -31,8 +31,16 @@ func (s *Selection) EachWithBreak(f func(int, *Selection) bool) *Selection {
|
||||
// element in that selection starting at 0, and a *Selection that contains
|
||||
// only that element.
|
||||
func (s *Selection) Map(f func(int, *Selection) string) (result []string) {
|
||||
return Map(s, f)
|
||||
}
|
||||
|
||||
// Map is the generic version of Selection.Map, allowing any type to be
|
||||
// returned.
|
||||
func Map[E any](s *Selection, f func(int, *Selection) E) (result []E) {
|
||||
result = make([]E, len(s.Nodes))
|
||||
|
||||
for i, n := range s.Nodes {
|
||||
result = append(result, f(i, newSingleSelection(n, s.document)))
|
||||
result[i] = f(i, newSingleSelection(n, s.document))
|
||||
}
|
||||
|
||||
return result
|
||||
|
||||
@@ -86,3 +86,22 @@ func TestForRange(t *testing.T) {
|
||||
t.Errorf("expected initial selection to still have length %d, got %d", initLen, sel.Length())
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenericMap(t *testing.T) {
|
||||
sel := Doc().Find(".pvk-content")
|
||||
vals := Map(sel, func(i int, s *Selection) *html.NodeType {
|
||||
n := s.Get(0)
|
||||
if n.Type == html.ElementNode {
|
||||
return &n.Type
|
||||
}
|
||||
return nil
|
||||
})
|
||||
for _, v := range vals {
|
||||
if v == nil || *v != html.ElementNode {
|
||||
t.Error("Expected Map array result to be all 'div's.")
|
||||
}
|
||||
}
|
||||
if len(vals) != 3 {
|
||||
t.Errorf("Expected Map array result to have a length of 3, found %v.", len(vals))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,12 @@ const (
|
||||
// Find gets the descendants of each element in the current set of matched
|
||||
// elements, filtered by a selector. It returns a new Selection object
|
||||
// containing these matched elements.
|
||||
//
|
||||
// Note that as for all methods accepting a selector string, the selector is
|
||||
// compiled and applied by the cascadia package and inherits its behavior and
|
||||
// constraints regarding supported selectors. See the note on cascadia in
|
||||
// the goquery documentation here:
|
||||
// https://github.com/PuerkitoBio/goquery?tab=readme-ov-file#api
|
||||
func (s *Selection) Find(selector string) *Selection {
|
||||
return pushStack(s, findWithMatcher(s.Nodes, compileMatcher(selector)))
|
||||
}
|
||||
|
||||
+15
-8
@@ -2,6 +2,7 @@ package goquery
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
|
||||
"golang.org/x/net/html"
|
||||
)
|
||||
@@ -50,28 +51,34 @@ func nodeName(node *html.Node) string {
|
||||
case html.ElementNode, html.DoctypeNode:
|
||||
return node.Data
|
||||
default:
|
||||
if node.Type >= 0 && int(node.Type) < len(nodeNames) {
|
||||
if int(node.Type) < len(nodeNames) {
|
||||
return nodeNames[node.Type]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
// Render renders the HTML of the first item in the selection and writes it to
|
||||
// the writer. It behaves the same as OuterHtml but writes to w instead of
|
||||
// returning the string.
|
||||
func Render(w io.Writer, s *Selection) error {
|
||||
if s.Length() == 0 {
|
||||
return nil
|
||||
}
|
||||
n := s.Get(0)
|
||||
return html.Render(w, n)
|
||||
}
|
||||
|
||||
// OuterHtml returns the outer HTML rendering of the first item in
|
||||
// the selection - that is, the HTML including the first element's
|
||||
// tag and attributes.
|
||||
//
|
||||
// Unlike InnerHtml, this is a function and not a method on the Selection,
|
||||
// Unlike Html, this is a function and not a method on the Selection,
|
||||
// because this is not a jQuery method (in javascript-land, this is
|
||||
// a property provided by the DOM).
|
||||
func OuterHtml(s *Selection) (string, error) {
|
||||
var buf bytes.Buffer
|
||||
|
||||
if s.Length() == 0 {
|
||||
return "", nil
|
||||
}
|
||||
n := s.Get(0)
|
||||
if err := html.Render(&buf, n); err != nil {
|
||||
if err := Render(&buf, s); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return buf.String(), nil
|
||||
|
||||
Reference in New Issue
Block a user