-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from nlnwa/doc
Documented basic usage
- Loading branch information
Showing
6 changed files
with
183 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,53 @@ | ||
# whatwg-url | ||
WHATWG conformant url parser for the Go language | ||
Whatwg-url is a spec-compliant URL parser written in Go. See [WHATWG](https://url.spec.whatwg.org/#url-parsing) website | ||
for the specification. | ||
|
||
This is a work in progress and the API is not stable yet | ||
## Status | ||
Whatwg-url parser is up to date as of [24 May 2023](https://url.spec.whatwg.org/commit-snapshots/eee49fdf4f99d59f717cbeb0bce29fda930196d4/) | ||
and passes all relevant tests from [web-platform-tests](https://github.com/web-platform-tests/wpt/tree/master/url) | ||
|
||
## Usage | ||
API is similar to [Chapter 6 in WHATWG URL Standard](https://url.spec.whatwg.org/#api). See [documentation](https://pkg.go.dev/github.com/nlnwa/whatwg-url) for details. | ||
|
||
```go | ||
import "github.com/nlnwa/whatwg-url/url" | ||
|
||
url, _ := url.Parse("http://example.com:80/a?b#c") | ||
fmt.Println(url.Scheme()) // http | ||
fmt.Println(url.Host()) // example.com | ||
fmt.Println(url.Port()) // "" | ||
fmt.Println(url.Pathname()) // "/a" | ||
fmt.Println(url.Href(false)) // http://example.com/a?b#c | ||
fmt.Println(url.Href(true)) // http://example.com/a?b | ||
fmt.Println(url.Hash()) // "#c" | ||
fmt.Println(url.Fragment()) // "c" | ||
fmt.Println(url.Search()) // "?b" | ||
fmt.Println(url.Query()) // "b" | ||
fmt.Println(url) // http://example.com/a?b#c | ||
``` | ||
|
||
### Options | ||
The default parser instance follows the WHATWG URL Standard. To adapt parsing to other needs, create a new parser | ||
instance and configure it with [options](https://pkg.go.dev/github.com/nlnwa/whatwg-url/url#ParserOption). | ||
|
||
example: | ||
|
||
```go | ||
p := url.NewParser(url.WithAcceptInvalidCodepoints(), url.WithCollapseConsecutiveSlashes()) | ||
``` | ||
|
||
### Canonicalization | ||
If you want canonicalization beyond what's described in the standard, you can use the | ||
[Canonicalizer API](https://pkg.go.dev/github.com/nlnwa/whatwg-url/canonicalizer). | ||
You can define your own canonicalization profile: | ||
|
||
```go | ||
c := canonicalizer.New(canonicalizer.WithRemoveUserInfo(), canonicalizer.WithRemoveFragment()) | ||
url, err := c.Parse("http://user@example.com/a?b#c") | ||
``` | ||
|
||
Or use one of the predefined profiles: | ||
|
||
```go | ||
url, err := canonicalizer.GoogleSafeBrowsing.Parse("http://user@example.com/a?b#c") | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright 2021 National Library of Norway. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package canonicalizer_test | ||
|
||
import ( | ||
"fmt" | ||
"github.com/nlnwa/whatwg-url/canonicalizer" | ||
) | ||
|
||
func ExampleNew() { | ||
c := canonicalizer.New(canonicalizer.WithRemoveUserInfo(), canonicalizer.WithRemoveFragment()) | ||
u, err := c.Parse("http://user@example.com/a//d?b#c") | ||
if err == nil { | ||
fmt.Println(u) | ||
} | ||
// Output: http://example.com/a//d?b | ||
} | ||
|
||
func ExampleGoogleSafeBrowsing() { | ||
u, err := canonicalizer.GoogleSafeBrowsing.Parse("http://user@example.com/a//d?b#c") | ||
if err == nil { | ||
fmt.Println(u) | ||
} | ||
// Output: http://user@example.com/a/d?b | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package url_test | ||
|
||
import ( | ||
"fmt" | ||
"github.com/nlnwa/whatwg-url/url" | ||
) | ||
|
||
func ExampleNewParser() { | ||
p := url.NewParser(url.WithAcceptInvalidCodepoints(), url.WithCollapseConsecutiveSlashes()) | ||
u, err := p.Parse("http://example*.com/a//d?b#c") | ||
if err == nil { | ||
fmt.Println(u) | ||
} | ||
// Output: http://example*.com/a/d?b#c | ||
} | ||
|
||
func ExampleUrl_Scheme() { | ||
u, _ := url.Parse("http://example.com:80/a?b#c") | ||
fmt.Println(u.Scheme()) | ||
// Output: http | ||
} | ||
|
||
func ExampleUrl_Host() { | ||
u, _ := url.Parse("http://example.com:80/a?b#c") | ||
fmt.Println(u.Host()) | ||
|
||
// Output: example.com | ||
} | ||
|
||
func ExampleUrl_Port() { | ||
u, _ := url.Parse("http://example.com:80/a?b#c") | ||
fmt.Println(u.Port()) | ||
// Output: | ||
} | ||
|
||
func ExampleUrl_Pathname() { | ||
u, _ := url.Parse("http://example.com:80/a?b#c") | ||
fmt.Println(u.Pathname()) | ||
// Output: /a | ||
} | ||
|
||
func ExampleUrl_Href() { | ||
u, _ := url.Parse("http://example.com:80/a?b#c") | ||
fmt.Println(u.Href(false)) // http://example.com/a?b#c | ||
fmt.Println(u.Href(true)) // http://example.com/a?b | ||
// Output: http://example.com/a?b#c | ||
// http://example.com/a?b | ||
} | ||
|
||
func ExampleUrl_Hash() { | ||
u, _ := url.Parse("http://example.com:80/a?b#c") | ||
fmt.Println(u.Hash()) | ||
// Output: #c | ||
} | ||
|
||
func ExampleUrl_Fragment() { | ||
u, _ := url.Parse("http://example.com:80/a?b#c") | ||
fmt.Println(u.Fragment()) | ||
// Output: c | ||
} | ||
|
||
func ExampleUrl_Search() { | ||
u, _ := url.Parse("http://example.com:80/a?b#c") | ||
fmt.Println(u.Search()) | ||
// Output: ?b | ||
} | ||
|
||
func ExampleUrl_Query() { | ||
u, _ := url.Parse("http://example.com:80/a?b#c") | ||
fmt.Println(u.Query()) | ||
// Output: b | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters