Bumps the go-modules-updates group with 11 updates in the /backend directory: | Package | From | To | | --- | --- | --- | | [github.com/PuerkitoBio/goquery](https://github.com/PuerkitoBio/goquery) | `1.10.2` | `1.11.0` | | [github.com/alecthomas/chroma/v2](https://github.com/alecthomas/chroma) | `2.15.0` | `2.20.0` | | [github.com/go-chi/chi/v5](https://github.com/go-chi/chi) | `5.2.1` | `5.2.3` | | [github.com/go-chi/cors](https://github.com/go-chi/cors) | `1.2.1` | `1.2.2` | | [github.com/go-pkgz/jrpc](https://github.com/go-pkgz/jrpc) | `0.3.1` | `0.4.0` | | [github.com/go-pkgz/lgr](https://github.com/go-pkgz/lgr) | `0.12.0` | `0.12.1` | | [github.com/go-pkgz/rest](https://github.com/go-pkgz/rest) | `1.20.3` | `1.20.4` | | [github.com/golang-jwt/jwt/v5](https://github.com/golang-jwt/jwt) | `5.2.2` | `5.3.0` | | [github.com/stretchr/testify](https://github.com/stretchr/testify) | `1.10.0` | `1.11.1` | | [go.etcd.io/bbolt](https://github.com/etcd-io/bbolt) | `1.4.0` | `1.4.3` | | [golang.org/x/image](https://github.com/golang/image) | `0.26.0` | `0.33.0` | Updates `github.com/PuerkitoBio/goquery` from 1.10.2 to 1.11.0 - [Release notes](https://github.com/PuerkitoBio/goquery/releases) - [Commits](https://github.com/PuerkitoBio/goquery/compare/v1.10.2...v1.11.0) Updates `github.com/alecthomas/chroma/v2` from 2.15.0 to 2.20.0 - [Release notes](https://github.com/alecthomas/chroma/releases) - [Commits](https://github.com/alecthomas/chroma/compare/v2.15.0...v2.20.0) Updates `github.com/go-chi/chi/v5` from 5.2.1 to 5.2.3 - [Release notes](https://github.com/go-chi/chi/releases) - [Changelog](https://github.com/go-chi/chi/blob/master/CHANGELOG.md) - [Commits](https://github.com/go-chi/chi/compare/v5.2.1...v5.2.3) Updates `github.com/go-chi/cors` from 1.2.1 to 1.2.2 - [Release notes](https://github.com/go-chi/cors/releases) - [Commits](https://github.com/go-chi/cors/compare/v1.2.1...v1.2.2) Updates `github.com/go-pkgz/jrpc` from 0.3.1 to 0.4.0 - [Release notes](https://github.com/go-pkgz/jrpc/releases) - [Commits](https://github.com/go-pkgz/jrpc/compare/v0.3.1...v0.4.0) Updates `github.com/go-pkgz/lgr` from 0.12.0 to 0.12.1 - [Release notes](https://github.com/go-pkgz/lgr/releases) - [Commits](https://github.com/go-pkgz/lgr/compare/v0.12.0...v0.12.1) Updates `github.com/go-pkgz/rest` from 1.20.3 to 1.20.4 - [Release notes](https://github.com/go-pkgz/rest/releases) - [Commits](https://github.com/go-pkgz/rest/compare/v1.20.3...v1.20.4) Updates `github.com/golang-jwt/jwt/v5` from 5.2.2 to 5.3.0 - [Release notes](https://github.com/golang-jwt/jwt/releases) - [Commits](https://github.com/golang-jwt/jwt/compare/v5.2.2...v5.3.0) Updates `github.com/stretchr/testify` from 1.10.0 to 1.11.1 - [Release notes](https://github.com/stretchr/testify/releases) - [Commits](https://github.com/stretchr/testify/compare/v1.10.0...v1.11.1) Updates `go.etcd.io/bbolt` from 1.4.0 to 1.4.3 - [Release notes](https://github.com/etcd-io/bbolt/releases) - [Commits](https://github.com/etcd-io/bbolt/compare/v1.4.0...v1.4.3) Updates `golang.org/x/image` from 0.26.0 to 0.33.0 - [Commits](https://github.com/golang/image/compare/v0.26.0...v0.33.0) --- updated-dependencies: - dependency-name: github.com/PuerkitoBio/goquery dependency-version: 1.11.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: go-modules-updates - dependency-name: github.com/alecthomas/chroma/v2 dependency-version: 2.20.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: go-modules-updates - dependency-name: github.com/go-chi/chi/v5 dependency-version: 5.2.3 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: go-modules-updates - dependency-name: github.com/go-chi/cors dependency-version: 1.2.2 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: go-modules-updates - dependency-name: github.com/go-pkgz/jrpc dependency-version: 0.4.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: go-modules-updates - dependency-name: github.com/go-pkgz/lgr dependency-version: 0.12.1 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: go-modules-updates - dependency-name: github.com/go-pkgz/rest dependency-version: 1.20.4 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: go-modules-updates - dependency-name: github.com/golang-jwt/jwt/v5 dependency-version: 5.3.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: go-modules-updates - dependency-name: github.com/stretchr/testify dependency-version: 1.11.1 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: go-modules-updates - dependency-name: go.etcd.io/bbolt dependency-version: 1.4.3 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: go-modules-updates - dependency-name: golang.org/x/image dependency-version: 0.33.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: go-modules-updates ... Signed-off-by: dependabot[bot] <support@github.com>
178 lines
5.0 KiB
Go
178 lines
5.0 KiB
Go
package goquery
|
|
|
|
import (
|
|
"io"
|
|
"strings"
|
|
|
|
"golang.org/x/net/html"
|
|
)
|
|
|
|
// used to determine if a set (map[*html.Node]bool) should be used
|
|
// instead of iterating over a slice. The set uses more memory and
|
|
// is slower than slice iteration for small N.
|
|
const minNodesForSet = 1000
|
|
|
|
var nodeNames = []string{
|
|
html.ErrorNode: "#error",
|
|
html.TextNode: "#text",
|
|
html.DocumentNode: "#document",
|
|
html.CommentNode: "#comment",
|
|
}
|
|
|
|
// NodeName returns the node name of the first element in the selection.
|
|
// It tries to behave in a similar way as the DOM's nodeName property
|
|
// (https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeName).
|
|
//
|
|
// Go's net/html package defines the following node types, listed with
|
|
// the corresponding returned value from this function:
|
|
//
|
|
// ErrorNode : #error
|
|
// TextNode : #text
|
|
// DocumentNode : #document
|
|
// ElementNode : the element's tag name
|
|
// CommentNode : #comment
|
|
// DoctypeNode : the name of the document type
|
|
func NodeName(s *Selection) string {
|
|
if s.Length() == 0 {
|
|
return ""
|
|
}
|
|
return nodeName(s.Get(0))
|
|
}
|
|
|
|
// nodeName returns the node name of the given html node.
|
|
// See NodeName for additional details on behaviour.
|
|
func nodeName(node *html.Node) string {
|
|
if node == nil {
|
|
return ""
|
|
}
|
|
|
|
switch node.Type {
|
|
case html.ElementNode, html.DoctypeNode:
|
|
return node.Data
|
|
default:
|
|
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 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 builder strings.Builder
|
|
if err := Render(&builder, s); err != nil {
|
|
return "", err
|
|
}
|
|
return builder.String(), nil
|
|
}
|
|
|
|
// Loop through all container nodes to search for the target node.
|
|
func sliceContains(container []*html.Node, contained *html.Node) bool {
|
|
for _, n := range container {
|
|
if nodeContains(n, contained) {
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
// Checks if the contained node is within the container node.
|
|
func nodeContains(container *html.Node, contained *html.Node) bool {
|
|
// Check if the parent of the contained node is the container node, traversing
|
|
// upward until the top is reached, or the container is found.
|
|
for contained = contained.Parent; contained != nil; contained = contained.Parent {
|
|
if container == contained {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// Checks if the target node is in the slice of nodes.
|
|
func isInSlice(slice []*html.Node, node *html.Node) bool {
|
|
return indexInSlice(slice, node) > -1
|
|
}
|
|
|
|
// Returns the index of the target node in the slice, or -1.
|
|
func indexInSlice(slice []*html.Node, node *html.Node) int {
|
|
if node != nil {
|
|
for i, n := range slice {
|
|
if n == node {
|
|
return i
|
|
}
|
|
}
|
|
}
|
|
return -1
|
|
}
|
|
|
|
// Appends the new nodes to the target slice, making sure no duplicate is added.
|
|
// There is no check to the original state of the target slice, so it may still
|
|
// contain duplicates. The target slice is returned because append() may create
|
|
// a new underlying array. If targetSet is nil, a local set is created with the
|
|
// target if len(target) + len(nodes) is greater than minNodesForSet.
|
|
func appendWithoutDuplicates(target []*html.Node, nodes []*html.Node, targetSet map[*html.Node]bool) []*html.Node {
|
|
// if there are not that many nodes, don't use the map, faster to just use nested loops
|
|
// (unless a non-nil targetSet is passed, in which case the caller knows better).
|
|
if targetSet == nil && len(target)+len(nodes) < minNodesForSet {
|
|
for _, n := range nodes {
|
|
if !isInSlice(target, n) {
|
|
target = append(target, n)
|
|
}
|
|
}
|
|
return target
|
|
}
|
|
|
|
// if a targetSet is passed, then assume it is reliable, otherwise create one
|
|
// and initialize it with the current target contents.
|
|
if targetSet == nil {
|
|
targetSet = make(map[*html.Node]bool, len(target))
|
|
for _, n := range target {
|
|
targetSet[n] = true
|
|
}
|
|
}
|
|
for _, n := range nodes {
|
|
if !targetSet[n] {
|
|
target = append(target, n)
|
|
targetSet[n] = true
|
|
}
|
|
}
|
|
|
|
return target
|
|
}
|
|
|
|
// Loop through a selection, returning only those nodes that pass the predicate
|
|
// function.
|
|
func grep(sel *Selection, predicate func(i int, s *Selection) bool) (result []*html.Node) {
|
|
for i, n := range sel.Nodes {
|
|
if predicate(i, newSingleSelection(n, sel.document)) {
|
|
result = append(result, n)
|
|
}
|
|
}
|
|
return result
|
|
}
|
|
|
|
// Creates a new Selection object based on the specified nodes, and keeps the
|
|
// source Selection object on the stack (linked list).
|
|
func pushStack(fromSel *Selection, nodes []*html.Node) *Selection {
|
|
result := &Selection{nodes, fromSel.document, fromSel}
|
|
return result
|
|
}
|