goquery

package module
v1.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 7, 2018 License: BSD-3-Clause Imports: 9 Imported by: 0

README

goquery - a little like that j-thing, only in Go

build status GoDoc Sourcegraph Badge

goquery brings a syntax and a set of features similar to jQuery to the Go language. It is based on Go's net/html package 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.

Also, because the net/html parser requires UTF-8 encoding, so does goquery: it is the caller's responsibility to ensure that the source document provides UTF-8 encoded HTML. See the wiki for various options to do this.

Syntax-wise, it is as close as possible to jQuery, with the same function names when possible, and that warm and fuzzy chainable interface. jQuery being the ultra-popular library that it is, I felt that writing a similar HTML-manipulating library was better to follow its API than to start anew (in the same spirit as Go's fmt package), even though some of its methods are less than intuitive (looking at you, index()...).

Table of Contents

Installation

Please note that because of the net/html dependency, goquery requires Go1.1+.

$ go get github.com/PuerkitoBio/goquery

(optional) To run unit tests:

$ cd $GOPATH/src/github.com/PuerkitoBio/goquery
$ go test

(optional) To run benchmarks (warning: it runs for a few minutes):

$ cd $GOPATH/src/github.com/PuerkitoBio/goquery
$ go test -bench=".*"

Changelog

Note that goquery's API is now stable, and will not break.

  • 2018-06-07 (v1.4.1) : Add NewDocumentFromReader examples.
  • 2018-03-24 (v1.4.0) : Deprecate NewDocument(url) and NewDocumentFromResponse(response).
  • 2018-01-28 (v1.3.0) : Add ToEnd constant to Slice until the end of the selection (thanks to @davidjwilkins for raising the issue).
  • 2018-01-11 (v1.2.0) : Add AddBack* and deprecate AndSelf (thanks to @davidjwilkins).
  • 2017-02-12 (v1.1.0) : Add SetHtml and SetText (thanks to @glebtv).
  • 2016-12-29 (v1.0.2) : Optimize allocations for Selection.Text (thanks to @radovskyb).
  • 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).
  • 2015-04-20 : Add AttrOr helper method to return the attribute's value or a default value if absent. Thanks to piotrkowalczuk.
  • 2015-02-04 : Add more manipulation functions - Prepend* - thanks again to Andrew Stone.
  • 2014-11-28 : Add more manipulation functions - ReplaceWith*, Wrap* and Unwrap - thanks again to Andrew Stone.
  • 2014-11-07 : Add manipulation functions (thanks to Andrew Stone) and *Matcher functions, that receive compiled cascadia selectors instead of selector strings, thus avoiding potential panics thrown by goquery via cascadia.MustCompile calls. This results in better performance (selectors can be compiled once and reused) and more idiomatic error handling (you can handle cascadia's compilation errors, instead of recovering from panics, which had been bugging me for a long time). Note that the actual type expected is a Matcher interface, that cascadia.Selector implements. Other matcher implementations could be used.
  • 2014-11-06 : Change import paths of net/html to golang.org/x/net/html (see https://groups.google.com/forum/#!topic/golang-nuts/eD8dh3T9yyA). Make sure to update your code to use the new import path too when you call goquery with html.Nodes.
  • v0.3.2 : Add NewDocumentFromReader() (thanks jweir) which allows creating a goquery document from an io.Reader.
  • v0.3.1 : Add NewDocumentFromResponse() (thanks assassingj) which allows creating a goquery document from an http response.
  • v0.3.0 : Add EachWithBreak() which allows to break out of an Each() loop by returning false. This function was added instead of changing the existing Each() to avoid breaking compatibility.
  • v0.2.1 : Make go-getable, now that go.net/html is Go1.0-compatible (thanks to @matrixik for pointing this out).
  • v0.2.0 : Add support for negative indices in Slice(). BREAKING CHANGE Document.Root is removed, Document is now a Selection itself (a selection of one, the root element, just like Document.Root was before). Add jQuery's Closest() method.
  • v0.1.1 : Add benchmarks to use as baseline for refactorings, refactor Next...() and Prev...() methods to use the new html package's linked list features (Next/PrevSibling, FirstChild). Good performance boost (40+% in some cases).
  • v0.1.0 : Initial release.

API

goquery exposes two structs, Document and Selection, and the Matcher interface. Unlike jQuery, which is loaded as part of a DOM document, and thus acts on its containing document, goquery doesn't know which HTML document to act upon. So it needs to be told, and that's what the Document type is for. It holds the root document node as the initial Selection value to manipulate.

jQuery often has many variants for the same function (no argument, a selector string argument, a jQuery object argument, a DOM element argument, ...). Instead of exposing the same features in goquery as a single method with variadic empty interface arguments, statically-typed signatures are used following this naming convention:

  • When the jQuery equivalent can be called with no argument, it has the same name as jQuery for the no argument signature (e.g.: Prev()), and the version with a selector string argument is called XxxFiltered() (e.g.: PrevFiltered())
  • When the jQuery equivalent requires one argument, the same name as jQuery is used for the selector string version (e.g.: Is())
  • The signatures accepting a jQuery object as argument are defined in goquery as XxxSelection() and take a *Selection object as argument (e.g.: FilterSelection())
  • The signatures accepting a DOM element as argument in jQuery are defined in goquery as XxxNodes() and take a variadic argument of type *html.Node (e.g.: FilterNodes())
  • The signatures accepting a function as argument in jQuery are defined in goquery as XxxFunction() and take a function as argument (e.g.: FilterFunction())
  • The goquery methods that can be called with a selector string have a corresponding version that take a Matcher interface and are defined as XxxMatcher() (e.g.: IsMatcher())

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.

Please note that Cascadia's selectors do not necessarily match all supported selectors of jQuery (Sizzle). See the cascadia project 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):

  • 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).
  • ParentsFiltered("~") returns an empty selection because the selector string doesn't match anything.
  • ParentsUntil("~") returns all parents of the selection because the selector string didn't match any element to stop before the top element.

Examples

See some tips and tricks in the wiki.

Adapted from example_test.go:

package main

import (
  "fmt"
  "log"
  "net/http"

  "github.com/PuerkitoBio/goquery"
)

func ExampleScrape() {
  // Request the HTML page.
  res, err := http.Get("http://metalsucks.net")
  if err != nil {
    log.Fatal(err)
  }
  defer res.Body.Close()
  if res.StatusCode != 200 {
    log.Fatalf("status code error: %d %s", res.StatusCode, res.Status)
  }

  // Load the HTML document
  doc, err := goquery.NewDocumentFromReader(res.Body)
  if err != nil {
    log.Fatal(err)
  }

  // Find the review items
  doc.Find(".sidebar-reviews article .content-block").Each(func(i int, s *goquery.Selection) {
    // For each item found, get the band and title
    band := s.Find("a").Text()
    title := s.Find("i").Text()
    fmt.Printf("Review %d: %s - %s\n", i, band, title)
  })
}

func main() {
  ExampleScrape()
}
  • Goq, an HTML deserialization and scraping library based on goquery and struct tags.
  • andybalholm/cascadia, the CSS selector library used by goquery.
  • suntong/cascadia, a command-line interface to the cascadia CSS selector library, useful to test selectors.
  • asciimoo/colly, a lightning fast and elegant Scraping Framework
  • gnulnx/goperf, a website performance test tool that also fetches static assets.

Support

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 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.
  • 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.

If you desperately want to send money my way, I have a BuyMeACoffee.com page:

Buy Me A Coffee

License

The BSD 3-Clause license, the same as the Go language. Cascadia's license is here.

Documentation

Overview

Package goquery implements features similar to jQuery, including the chainable syntax, to manipulate and query an HTML document.

It brings a syntax and a set of features similar to jQuery to the Go language. It is based on Go's net/html package 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.

Also, because the net/html parser requires UTF-8 encoding, so does goquery: it is the caller's responsibility to ensure that the source document provides UTF-8 encoded HTML. See the repository's wiki for various options on how to do this.

Syntax-wise, it is as close as possible to jQuery, with the same method names when possible, and that warm and fuzzy chainable interface. jQuery being the ultra-popular library that it is, writing a similar HTML-manipulating library was better to follow its API than to start anew (in the same spirit as Go's fmt package), even though some of its methods are less than intuitive (looking at you, index()...).

It is hosted on GitHub, along with additional documentation in the README.md file: https://github.com/puerkitobio/goquery

Please note that because of the net/html dependency, goquery requires Go1.1+.

The various methods are split into files based on the category of behavior. The three dots (...) indicate that various "overloads" are available.

* array.go : array-like positional manipulation of the selection.

  • Eq()
  • First()
  • Get()
  • Index...()
  • Last()
  • Slice()

* expand.go : methods that expand or augment the selection's set.

  • Add...()
  • AndSelf()
  • Union(), which is an alias for AddSelection()

* filter.go : filtering methods, that reduce the selection's set.

  • End()
  • Filter...()
  • Has...()
  • Intersection(), which is an alias of FilterSelection()
  • Not...()

* iteration.go : methods to loop over the selection's nodes.

  • Each()
  • EachWithBreak()
  • Map()

* manipulation.go : methods for modifying the document

  • After...()
  • Append...()
  • Before...()
  • Clone()
  • Empty()
  • Prepend...()
  • Remove...()
  • ReplaceWith...()
  • Unwrap()
  • Wrap...()
  • WrapAll...()
  • WrapInner...()

* property.go : methods that inspect and get the node's properties values.

  • Attr*(), RemoveAttr(), SetAttr()
  • AddClass(), HasClass(), RemoveClass(), ToggleClass()
  • Html()
  • Length()
  • Size(), which is an alias for Length()
  • Text()

* query.go : methods that query, or reflect, a node's identity.

  • Contains()
  • Is...()

* traversal.go : methods to traverse the HTML document tree.

  • Children...()
  • Contents()
  • Find...()
  • Next...()
  • Parent[s]...()
  • Prev...()
  • Siblings...()

* type.go : definition of the types exposed by goquery.

  • Document
  • Selection
  • Matcher

* utilities.go : definition of helper functions (and not methods on a *Selection) that are not part of jQuery, but are useful to goquery.

  • NodeName
  • OuterHtml
Example

This example scrapes the reviews shown on the home page of metalsucks.net.

package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/PuerkitoBio/goquery"
)

func main() {
	// Request the HTML page.
	res, err := http.Get("http://metalsucks.net")
	if err != nil {
		log.Fatal(err)
	}
	defer res.Body.Close()
	if res.StatusCode != 200 {
		log.Fatalf("status code error: %d %s", res.StatusCode, res.Status)
	}

	// Load the HTML document
	doc, err := goquery.NewDocumentFromReader(res.Body)
	if err != nil {
		log.Fatal(err)
	}

	// Find the review items
	doc.Find(".sidebar-reviews article .content-block").Each(func(i int, s *goquery.Selection) {
		// For each item found, get the band and title
		band := s.Find("a").Text()
		title := s.Find("i").Text()
		fmt.Printf("Review %d: %s - %s\n", i, band, title)
	})
	// To see the output of the Example while running the test suite (go test), simply
	// remove the leading "x" before Output on the next line. This will cause the
	// example to fail (all the "real" tests should pass).

	// xOutput: voluntarily fail the Example output.
}
Output:

Index

Examples

Constants

View Source
const (

	// ToEnd is a special index value that can be used as end index in a call
	// to Slice so that all elements are selected until the end of the Selection.
	// It is equivalent to passing (*Selection).Length().
	ToEnd = maxInt
)

Variables

This section is empty.

Functions

func NodeName added in v1.0.0

func NodeName(s *Selection) string

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 OuterHtml added in v1.0.0

func OuterHtml(s *Selection) (string, error)

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, because this is not a jQuery method (in javascript-land, this is a property provided by the DOM).

Types

type Document

type Document struct {
	*Selection
	Url *url.URL
	// contains filtered or unexported fields
}

Document represents an HTML document to be manipulated. Unlike jQuery, which is loaded as part of a DOM document, and thus acts upon its containing document, GoQuery doesn't know which HTML document to act upon. So it needs to be told, and that's what the Document class is for. It holds the root document node to manipulate, and can make selections on this document.

func CloneDocument added in v1.0.0

func CloneDocument(doc *Document) *Document

CloneDocument creates a deep-clone of a document.

func NewDocument deprecated

func NewDocument(url string) (*Document, error)

NewDocument is a Document constructor that takes a string URL as argument. It loads the specified document, parses it, and stores the root Document node, ready to be manipulated.

Deprecated: Use the net/http standard library package to make the request and validate the response before calling goquery.NewDocumentFromReader with the response's body.

func NewDocumentFromNode

func NewDocumentFromNode(root *html.Node) *Document

NewDocumentFromNode is a Document constructor that takes a root html Node as argument.

func NewDocumentFromReader added in v0.3.2

func NewDocumentFromReader(r io.Reader) (*Document, error)

NewDocumentFromReader returns a Document from an io.Reader. It returns an error as second value if the reader's data cannot be parsed as html. It does not check if the reader is also an io.Closer, the provided reader is never closed by this call. It is the responsibility of the caller to close it if required.

Example (File)

This example shows how to use NewDocumentFromReader from a file.

package main

import (
	"log"
	"os"

	"github.com/PuerkitoBio/goquery"
)

func main() {
	// create from a file
	f, err := os.Open("some/file.html")
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()
	doc, err := goquery.NewDocumentFromReader(f)
	if err != nil {
		log.Fatal(err)
	}
	// use the goquery document...
	_ = doc.Find("h1")
}
Output:

Example (String)

This example shows how to use NewDocumentFromReader from a string.

package main

import (
	"fmt"
	"log"
	"strings"

	"github.com/PuerkitoBio/goquery"
)

func main() {
	// create from a string
	data := `
<html>
	<head>
		<title>My document</title>
	</head>
	<body>
		<h1>Header</h1>
	</body>
</html>`

	doc, err := goquery.NewDocumentFromReader(strings.NewReader(data))
	if err != nil {
		log.Fatal(err)
	}
	header := doc.Find("h1").Text()
	fmt.Println(header)

}
Output:

Header

func NewDocumentFromResponse deprecated added in v0.3.1

func NewDocumentFromResponse(res *http.Response) (*Document, error)

NewDocumentFromResponse is another Document constructor that takes an http response as argument. It loads the specified response's document, parses it, and stores the root Document node, ready to be manipulated. The response's body is closed on return.

Deprecated: Use goquery.NewDocumentFromReader with the response's body.

type Matcher added in v1.0.0

type Matcher interface {
	Match(*html.Node) bool
	MatchAll(*html.Node) []*html.Node
	Filter([]*html.Node) []*html.Node
}

Matcher is an interface that defines the methods to match HTML nodes against a compiled selector string. Cascadia's Selector implements this interface.

type Selection

type Selection struct {
	Nodes []*html.Node
	// contains filtered or unexported fields
}

Selection represents a collection of nodes matching some criteria. The initial Selection can be created by using Document.Find, and then manipulated using the jQuery-like chainable syntax and methods.

func (*Selection) Add

func (s *Selection) Add(selector string) *Selection

Add adds the selector string's matching nodes to those in the current selection and returns a new Selection object. The selector string is run in the context of the document of the current Selection object.

func (*Selection) AddBack added in v1.2.0

func (s *Selection) AddBack() *Selection

AddBack adds the previous set of elements on the stack to the current set. It returns a new Selection object containing the current Selection combined with the previous one.

func (*Selection) AddBackFiltered added in v1.2.0

func (s *Selection) AddBackFiltered(selector string) *Selection

AddBackFiltered reduces the previous set of elements on the stack to those that match the selector string, and adds them to the current set. It returns a new Selection object containing the current Selection combined with the filtered previous one

func (*Selection) AddBackMatcher added in v1.2.0

func (s *Selection) AddBackMatcher(m Matcher) *Selection

AddBackMatcher reduces the previous set of elements on the stack to those that match the mateher, and adds them to the curernt set. It returns a new Selection object containing the current Selection combined with the filtered previous one

func (*Selection) AddClass added in v1.0.0

func (s *Selection) AddClass(class ...string) *Selection

AddClass adds the given class(es) to each element in the set of matched elements. Multiple class names can be specified, separated by a space or via multiple arguments.

func (*Selection) AddMatcher added in v1.0.0

func (s *Selection) AddMatcher(m Matcher) *Selection

AddMatcher adds the matcher's matching nodes to those in the current selection and returns a new Selection object. The matcher is run in the context of the document of the current Selection object.

func (*Selection) AddNodes

func (s *Selection) AddNodes(nodes ...*html.Node) *Selection

AddNodes adds the specified nodes to those in the current selection and returns a new Selection object.

func (*Selection) AddSelection

func (s *Selection) AddSelection(sel *Selection) *Selection

AddSelection adds the specified Selection object's nodes to those in the current selection and returns a new Selection object.

func (*Selection) After added in v1.0.0

func (s *Selection) After(selector string) *Selection

After applies the selector from the root document and inserts the matched elements after the elements in the set of matched elements.

If one of the matched elements in the selection is not currently in the document, it's impossible to insert nodes after it, so it will be ignored.

This follows the same rules as Selection.Append.

func (*Selection) AfterHtml added in v1.0.0

func (s *Selection) AfterHtml(html string) *Selection

AfterHtml parses the html and inserts it after the set of matched elements.

This follows the same rules as Selection.Append.

func (*Selection) AfterMatcher added in v1.0.0

func (s *Selection) AfterMatcher(m Matcher) *Selection

AfterMatcher applies the matcher from the root document and inserts the matched elements after the elements in the set of matched elements.

If one of the matched elements in the selection is not currently in the document, it's impossible to insert nodes after it, so it will be ignored.

This follows the same rules as Selection.Append.

func (*Selection) AfterNodes added in v1.0.0

func (s *Selection) AfterNodes(ns ...*html.Node) *Selection

AfterNodes inserts the nodes after each element in the set of matched elements.

This follows the same rules as Selection.Append.

func (*Selection) AfterSelection added in v1.0.0

func (s *Selection) AfterSelection(sel *Selection) *Selection

AfterSelection inserts the elements in the selection after each element in the set of matched elements.

This follows the same rules as Selection.Append.

func (*Selection) AndSelf

func (s *Selection) AndSelf() *Selection

AndSelf adds the previous set of elements on the stack to the current set. It returns a new Selection object containing the current Selection combined with the previous one. Deprecated: This function has been deprecated and is now an alias for AddBack().

func (*Selection) Append added in v1.0.0

func (s *Selection) Append(selector string) *Selection

Append appends the elements specified by the selector to the end of each element in the set of matched elements, following those rules:

1) The selector is applied to the root document.

2) Elements that are part of the document will be moved to the new location.

3) If there are multiple locations to append to, cloned nodes will be appended to all target locations except the last one, which will be moved as noted in (2).

func (*Selection) AppendHtml added in v1.0.0

func (s *Selection) AppendHtml(html string) *Selection

AppendHtml parses the html and appends it to the set of matched elements.

func (*Selection) AppendMatcher added in v1.0.0

func (s *Selection) AppendMatcher(m Matcher) *Selection

AppendMatcher appends the elements specified by the matcher to the end of each element in the set of matched elements.

This follows the same rules as Selection.Append.

func (*Selection) AppendNodes added in v1.0.0

func (s *Selection) AppendNodes(ns ...*html.Node) *Selection

AppendNodes appends the specified nodes to each node in the set of matched elements.

This follows the same rules as Selection.Append.

func (*Selection) AppendSelection added in v1.0.0

func (s *Selection) AppendSelection(sel *Selection) *Selection

AppendSelection appends the elements in the selection to the end of each element in the set of matched elements.

This follows the same rules as Selection.Append.

func (*Selection) Attr

func (s *Selection) Attr(attrName string) (val string, exists bool)

Attr gets the specified attribute's value for the first element in the Selection. To get the value for each element individually, use a looping construct such as Each or Map method.

func (*Selection) AttrOr added in v1.0.0

func (s *Selection) AttrOr(attrName, defaultValue string) string

AttrOr works like Attr but returns default value if attribute is not present.

func (*Selection) Before added in v1.0.0

func (s *Selection) Before(selector string) *Selection

Before inserts the matched elements before each element in the set of matched elements.

This follows the same rules as Selection.Append.

func (*Selection) BeforeHtml added in v1.0.0

func (s *Selection) BeforeHtml(html string) *Selection

BeforeHtml parses the html and inserts it before the set of matched elements.

This follows the same rules as Selection.Append.

func (*Selection) BeforeMatcher added in v1.0.0

func (s *Selection) BeforeMatcher(m Matcher) *Selection

BeforeMatcher inserts the matched elements before each element in the set of matched elements.

This follows the same rules as Selection.Append.

func (*Selection) BeforeNodes added in v1.0.0

func (s *Selection) BeforeNodes(ns ...*html.Node) *Selection

BeforeNodes inserts the nodes before each element in the set of matched elements.

This follows the same rules as Selection.Append.

func (*Selection) BeforeSelection added in v1.0.0

func (s *Selection) BeforeSelection(sel *Selection) *Selection

BeforeSelection inserts the elements in the selection before each element in the set of matched elements.

This follows the same rules as Selection.Append.

func (*Selection) Children

func (s *Selection) Children() *Selection

Children gets the child elements of each element in the Selection. It returns a new Selection object containing these elements.

func (*Selection) ChildrenFiltered

func (s *Selection) ChildrenFiltered(selector string) *Selection

ChildrenFiltered gets the child elements of each element in the Selection, filtered by the specified selector. It returns a new Selection object containing these elements.

func (*Selection) ChildrenMatcher added in v1.0.0

func (s *Selection) ChildrenMatcher(m Matcher) *Selection

ChildrenMatcher gets the child elements of each element in the Selection, filtered by the specified matcher. It returns a new Selection object containing these elements.

func (*Selection) Clone added in v1.0.0

func (s *Selection) Clone() *Selection

Clone creates a deep copy of the set of matched nodes. The new nodes will not be attached to the document.

func (*Selection) Closest added in v0.2.0

func (s *Selection) Closest(selector string) *Selection

Closest gets the first element that matches the selector by testing the element itself and traversing up through its ancestors in the DOM tree.

func (*Selection) ClosestMatcher added in v1.0.0

func (s *Selection) ClosestMatcher(m Matcher) *Selection

ClosestMatcher gets the first element that matches the matcher by testing the element itself and traversing up through its ancestors in the DOM tree.

func (*Selection) ClosestNodes added in v0.2.0

func (s *Selection) ClosestNodes(nodes ...*html.Node) *Selection

ClosestNodes gets the first element that matches one of the nodes by testing the element itself and traversing up through its ancestors in the DOM tree.

func (*Selection) ClosestSelection added in v0.2.0

func (s *Selection) ClosestSelection(sel *Selection) *Selection

ClosestSelection gets the first element that matches one of the nodes in the Selection by testing the element itself and traversing up through its ancestors in the DOM tree.

func (*Selection) Contains

func (s *Selection) Contains(n *html.Node) bool

Contains returns true if the specified Node is within, at any depth, one of the nodes in the Selection object. It is NOT inclusive, to behave like jQuery's implementation, and unlike Javascript's .contains, so if the contained node is itself in the selection, it returns false.

func (*Selection) Contents

func (s *Selection) Contents() *Selection

Contents gets the children of each element in the Selection, including text and comment nodes. It returns a new Selection object containing these elements.

func (*Selection) ContentsFiltered

func (s *Selection) ContentsFiltered(selector string) *Selection

ContentsFiltered gets the children of each element in the Selection, filtered by the specified selector. It returns a new Selection object containing these elements. Since selectors only act on Element nodes, this function is an alias to ChildrenFiltered unless the selector is empty, in which case it is an alias to Contents.

func (*Selection) ContentsMatcher added in v1.0.0

func (s *Selection) ContentsMatcher(m Matcher) *Selection

ContentsMatcher gets the children of each element in the Selection, filtered by the specified matcher. It returns a new Selection object containing these elements. Since matchers only act on Element nodes, this function is an alias to ChildrenMatcher.

func (*Selection) Each

func (s *Selection) Each(f func(int, *Selection)) *Selection

Each iterates over a Selection object, executing a function for each matched element. It returns the current Selection object. The function f is called for each element in the selection with the index of the element in that selection starting at 0, and a *Selection that contains only that element.

func (*Selection) EachWithBreak added in v0.3.0

func (s *Selection) EachWithBreak(f func(int, *Selection) bool) *Selection

EachWithBreak iterates over a Selection object, executing a function for each matched element. It is identical to Each except that it is possible to break out of the loop by returning false in the callback function. It returns the current Selection object.

func (*Selection) Empty added in v1.0.0

func (s *Selection) Empty() *Selection

Empty removes all children nodes from the set of matched elements. It returns the children nodes in a new Selection.

func (*Selection) End

func (s *Selection) End() *Selection

End ends the most recent filtering operation in the current chain and returns the set of matched elements to its previous state.

func (*Selection) Eq

func (s *Selection) Eq(index int) *Selection

Eq reduces the set of matched elements to the one at the specified index. If a negative index is given, it counts backwards starting at the end of the set. It returns a new Selection object, and an empty Selection object if the index is invalid.

func (*Selection) Filter

func (s *Selection) Filter(selector string) *Selection

Filter reduces the set of matched elements to those that match the selector string. It returns a new Selection object for this subset of matching elements.

func (*Selection) FilterFunction

func (s *Selection) FilterFunction(f func(int, *Selection) bool) *Selection

FilterFunction reduces the set of matched elements to those that pass the function's test. It returns a new Selection object for this subset of elements.

func (*Selection) FilterMatcher added in v1.0.0

func (s *Selection) FilterMatcher(m Matcher) *Selection

FilterMatcher reduces the set of matched elements to those that match the given matcher. It returns a new Selection object for this subset of matching elements.

func (*Selection) FilterNodes

func (s *Selection) FilterNodes(nodes ...*html.Node) *Selection

FilterNodes reduces the set of matched elements to those that match the specified nodes. It returns a new Selection object for this subset of elements.

func (*Selection) FilterSelection

func (s *Selection) FilterSelection(sel *Selection) *Selection

FilterSelection reduces the set of matched elements to those that match a node in the specified Selection object. It returns a new Selection object for this subset of elements.

func (*Selection) Find

func (s *Selection) Find(selector string) *Selection

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.

func (*Selection) FindMatcher added in v1.0.0

func (s *Selection) FindMatcher(m Matcher) *Selection

FindMatcher gets the descendants of each element in the current set of matched elements, filtered by the matcher. It returns a new Selection object containing these matched elements.

func (*Selection) FindNodes

func (s *Selection) FindNodes(nodes ...*html.Node) *Selection

FindNodes gets the descendants of each element in the current Selection, filtered by some nodes. It returns a new Selection object containing these matched elements.

func (*Selection) FindSelection

func (s *Selection) FindSelection(sel *Selection) *Selection

FindSelection gets the descendants of each element in the current Selection, filtered by a Selection. It returns a new Selection object containing these matched elements.

func (*Selection) First

func (s *Selection) First() *Selection

First reduces the set of matched elements to the first in the set. It returns a new Selection object, and an empty Selection object if the the selection is empty.

func (*Selection) Get

func (s *Selection) Get(index int) *html.Node

Get retrieves the underlying node at the specified index. Get without parameter is not implemented, since the node array is available on the Selection object.

func (*Selection) Has

func (s *Selection) Has(selector string) *Selection

Has reduces the set of matched elements to those that have a descendant that matches the selector. It returns a new Selection object with the matching elements.

func (*Selection) HasClass

func (s *Selection) HasClass(class string) bool

HasClass determines whether any of the matched elements are assigned the given class.

func (*Selection) HasMatcher added in v1.0.0

func (s *Selection) HasMatcher(m Matcher) *Selection

HasMatcher reduces the set of matched elements to those that have a descendant that matches the matcher. It returns a new Selection object with the matching elements.

func (*Selection) HasNodes

func (s *Selection) HasNodes(nodes ...*html.Node) *Selection

HasNodes reduces the set of matched elements to those that have a descendant that matches one of the nodes. It returns a new Selection object with the matching elements.

func (*Selection) HasSelection

func (s *Selection) HasSelection(sel *Selection) *Selection

HasSelection reduces the set of matched elements to those that have a descendant that matches one of the nodes of the specified Selection object. It returns a new Selection object with the matching elements.

func (*Selection) Html

func (s *Selection) Html() (ret string, e error)

Html gets the HTML contents of the first element in the set of matched elements. It includes text and comment nodes.

func (*Selection) Index

func (s *Selection) Index() int

Index returns the position of the first element within the Selection object relative to its sibling elements.

func (*Selection) IndexMatcher added in v1.0.0

func (s *Selection) IndexMatcher(m Matcher) int

IndexMatcher returns the position of the first element within the Selection object relative to the elements matched by the matcher, or -1 if not found.

func (*Selection) IndexOfNode

func (s *Selection) IndexOfNode(node *html.Node) int

IndexOfNode returns the position of the specified node within the Selection object, or -1 if not found.

func (*Selection) IndexOfSelection

func (s *Selection) IndexOfSelection(sel *Selection) int

IndexOfSelection returns the position of the first node in the specified Selection object within this Selection object, or -1 if not found.

func (*Selection) IndexSelector

func (s *Selection) IndexSelector(selector string) int

IndexSelector returns the position of the first element within the Selection object relative to the elements matched by the selector, or -1 if not found.

func (*Selection) Intersection

func (s *Selection) Intersection(sel *Selection) *Selection

Intersection is an alias for FilterSelection.

func (*Selection) Is

func (s *Selection) Is(selector string) bool

Is checks the current matched set of elements against a selector and returns true if at least one of these elements matches.

func (*Selection) IsFunction

func (s *Selection) IsFunction(f func(int, *Selection) bool) bool

IsFunction checks the current matched set of elements against a predicate and returns true if at least one of these elements matches.

func (*Selection) IsMatcher added in v1.0.0

func (s *Selection) IsMatcher(m Matcher) bool

IsMatcher checks the current matched set of elements against a matcher and returns true if at least one of these elements matches.

func (*Selection) IsNodes

func (s *Selection) IsNodes(nodes ...*html.Node) bool

IsNodes checks the current matched set of elements against the specified nodes and returns true if at least one of these elements matches.

func (*Selection) IsSelection

func (s *Selection) IsSelection(sel *Selection) bool

IsSelection checks the current matched set of elements against a Selection object and returns true if at least one of these elements matches.

func (*Selection) Last

func (s *Selection) Last() *Selection

Last reduces the set of matched elements to the last in the set. It returns a new Selection object, and an empty Selection object if the selection is empty.

func (*Selection) Length

func (s *Selection) Length() int

Length returns the number of elements in the Selection object.

func (*Selection) Map

func (s *Selection) Map(f func(int, *Selection) string) (result []string)

Map passes each element in the current matched set through a function, producing a slice of string holding the returned values. The function f is called for each element in the selection with the index of the element in that selection starting at 0, and a *Selection that contains only that element.

func (*Selection) Next

func (s *Selection) Next() *Selection

Next gets the immediately following sibling of each element in the Selection. It returns a new Selection object containing the matched elements.

func (*Selection) NextAll

func (s *Selection) NextAll() *Selection

NextAll gets all the following siblings of each element in the Selection. It returns a new Selection object containing the matched elements.

func (*Selection) NextAllFiltered

func (s *Selection) NextAllFiltered(selector string) *Selection

NextAllFiltered gets all the following siblings of each element in the Selection filtered by a selector. It returns a new Selection object containing the matched elements.

func (*Selection) NextAllMatcher added in v1.0.0

func (s *Selection) NextAllMatcher(m Matcher) *Selection

NextAllMatcher gets all the following siblings of each element in the Selection filtered by a matcher. It returns a new Selection object containing the matched elements.

func (*Selection) NextFiltered

func (s *Selection) NextFiltered(selector string) *Selection

NextFiltered gets the immediately following sibling of each element in the Selection filtered by a selector. It returns a new Selection object containing the matched elements.

func (*Selection) NextFilteredUntil

func (s *Selection) NextFilteredUntil(filterSelector, untilSelector string) *Selection

NextFilteredUntil is like NextUntil, with the option to filter the results based on a selector string. It returns a new Selection object containing the matched elements.

func (*Selection) NextFilteredUntilMatcher added in v1.0.0

func (s *Selection) NextFilteredUntilMatcher(filter, until Matcher) *Selection

NextFilteredUntilMatcher is like NextUntilMatcher, with the option to filter the results based on a matcher. It returns a new Selection object containing the matched elements.

func (*Selection) NextFilteredUntilNodes

func (s *Selection) NextFilteredUntilNodes(filterSelector string, nodes ...*html.Node) *Selection

NextFilteredUntilNodes is like NextUntilNodes, with the option to filter the results based on a selector string. It returns a new Selection object containing the matched elements.

func (*Selection) NextFilteredUntilSelection

func (s *Selection) NextFilteredUntilSelection(filterSelector string, sel *Selection) *Selection

NextFilteredUntilSelection is like NextUntilSelection, with the option to filter the results based on a selector string. It returns a new Selection object containing the matched elements.

func (*Selection) NextMatcher added in v1.0.0

func (s *Selection) NextMatcher(m Matcher) *Selection

NextMatcher gets the immediately following sibling of each element in the Selection filtered by a matcher. It returns a new Selection object containing the matched elements.

func (*Selection) NextMatcherUntilNodes added in v1.0.0

func (s *Selection) NextMatcherUntilNodes(filter Matcher, nodes ...*html.Node) *Selection

NextMatcherUntilNodes is like NextUntilNodes, with the option to filter the results based on a matcher. It returns a new Selection object containing the matched elements.

func (*Selection) NextMatcherUntilSelection added in v1.0.0

func (s *Selection) NextMatcherUntilSelection(filter Matcher, sel *Selection) *Selection

NextMatcherUntilSelection is like NextUntilSelection, with the option to filter the results based on a matcher. It returns a new Selection object containing the matched elements.

func (*Selection) NextUntil

func (s *Selection) NextUntil(selector string) *Selection

NextUntil gets all following siblings of each element up to but not including the element matched by the selector. It returns a new Selection object containing the matched elements.

func (*Selection) NextUntilMatcher added in v1.0.0

func (s *Selection) NextUntilMatcher(m Matcher) *Selection

NextUntilMatcher gets all following siblings of each element up to but not including the element matched by the matcher. It returns a new Selection object containing the matched elements.

func (*Selection) NextUntilNodes

func (s *Selection) NextUntilNodes(nodes ...*html.Node) *Selection

NextUntilNodes gets all following siblings of each element up to but not including the element matched by the nodes. It returns a new Selection object containing the matched elements.

func (*Selection) NextUntilSelection

func (s *Selection) NextUntilSelection(sel *Selection) *Selection

NextUntilSelection gets all following siblings of each element up to but not including the element matched by the Selection. It returns a new Selection object containing the matched elements.

func (*Selection) Not

func (s *Selection) Not(selector string) *Selection

Not removes elements from the Selection that match the selector string. It returns a new Selection object with the matching elements removed.

func (*Selection) NotFunction

func (s *Selection) NotFunction(f func(int, *Selection) bool) *Selection

NotFunction removes elements from the Selection that pass the function's test. It returns a new Selection object with the matching elements removed.

func (*Selection) NotMatcher added in v1.0.0

func (s *Selection) NotMatcher(m Matcher) *Selection

NotMatcher removes elements from the Selection that match the given matcher. It returns a new Selection object with the matching elements removed.

func (*Selection) NotNodes

func (s *Selection) NotNodes(nodes ...*html.Node) *Selection

NotNodes removes elements from the Selection that match the specified nodes. It returns a new Selection object with the matching elements removed.

func (*Selection) NotSelection

func (s *Selection) NotSelection(sel *Selection) *Selection

NotSelection removes elements from the Selection that match a node in the specified Selection object. It returns a new Selection object with the matching elements removed.

func (*Selection) Parent

func (s *Selection) Parent() *Selection

Parent gets the parent of each element in the Selection. It returns a new Selection object containing the matched elements.

func (*Selection) ParentFiltered

func (s *Selection) ParentFiltered(selector string) *Selection

ParentFiltered gets the parent of each element in the Selection filtered by a selector. It returns a new Selection object containing the matched elements.

func (*Selection) ParentMatcher added in v1.0.0

func (s *Selection) ParentMatcher(m Matcher) *Selection

ParentMatcher gets the parent of each element in the Selection filtered by a matcher. It returns a new Selection object containing the matched elements.

func (*Selection) Parents

func (s *Selection) Parents() *Selection

Parents gets the ancestors of each element in the current Selection. It returns a new Selection object with the matched elements.

func (*Selection) ParentsFiltered

func (s *Selection) ParentsFiltered(selector string) *Selection

ParentsFiltered gets the ancestors of each element in the current Selection. It returns a new Selection object with the matched elements.

func (*Selection) ParentsFilteredUntil

func (s *Selection) ParentsFilteredUntil(filterSelector, untilSelector string) *Selection

ParentsFilteredUntil is like ParentsUntil, with the option to filter the results based on a selector string. It returns a new Selection object containing the matched elements.

func (*Selection) ParentsFilteredUntilMatcher added in v1.0.0

func (s *Selection) ParentsFilteredUntilMatcher(filter, until Matcher) *Selection

ParentsFilteredUntilMatcher is like ParentsUntilMatcher, with the option to filter the results based on a matcher. It returns a new Selection object containing the matched elements.

func (*Selection) ParentsFilteredUntilNodes

func (s *Selection) ParentsFilteredUntilNodes(filterSelector string, nodes ...*html.Node) *Selection

ParentsFilteredUntilNodes is like ParentsUntilNodes, with the option to filter the results based on a selector string. It returns a new Selection object containing the matched elements.

func (*Selection) ParentsFilteredUntilSelection

func (s *Selection) ParentsFilteredUntilSelection(filterSelector string, sel *Selection) *Selection

ParentsFilteredUntilSelection is like ParentsUntilSelection, with the option to filter the results based on a selector string. It returns a new Selection object containing the matched elements.

func (*Selection) ParentsMatcher added in v1.0.0

func (s *Selection) ParentsMatcher(m Matcher) *Selection

ParentsMatcher gets the ancestors of each element in the current Selection. It returns a new Selection object with the matched elements.

func (*Selection) ParentsMatcherUntilNodes added in v1.0.0

func (s *Selection) ParentsMatcherUntilNodes(filter Matcher, nodes ...*html.Node) *Selection

ParentsMatcherUntilNodes is like ParentsUntilNodes, with the option to filter the results based on a matcher. It returns a new Selection object containing the matched elements.

func (*Selection) ParentsMatcherUntilSelection added in v1.0.0

func (s *Selection) ParentsMatcherUntilSelection(filter Matcher, sel *Selection) *Selection

ParentsMatcherUntilSelection is like ParentsUntilSelection, with the option to filter the results based on a matcher. It returns a new Selection object containing the matched elements.

func (*Selection) ParentsUntil

func (s *Selection) ParentsUntil(selector string) *Selection

ParentsUntil gets the ancestors of each element in the Selection, up to but not including the element matched by the selector. It returns a new Selection object containing the matched elements.

func (*Selection) ParentsUntilMatcher added in v1.0.0

func (s *Selection) ParentsUntilMatcher(m Matcher) *Selection

ParentsUntilMatcher gets the ancestors of each element in the Selection, up to but not including the element matched by the matcher. It returns a new Selection object containing the matched elements.

func (*Selection) ParentsUntilNodes

func (s *Selection) ParentsUntilNodes(nodes ...*html.Node) *Selection

ParentsUntilNodes gets the ancestors of each element in the Selection, up to but not including the specified nodes. It returns a new Selection object containing the matched elements.

func (*Selection) ParentsUntilSelection

func (s *Selection) ParentsUntilSelection(sel *Selection) *Selection

ParentsUntilSelection gets the ancestors of each element in the Selection, up to but not including the elements in the specified Selection. It returns a new Selection object containing the matched elements.

func (*Selection) Prepend added in v1.0.0

func (s *Selection) Prepend(selector string) *Selection

Prepend prepends the elements specified by the selector to each element in the set of matched elements, following the same rules as Append.

func (*Selection) PrependHtml added in v1.0.0

func (s *Selection) PrependHtml(html string) *Selection

PrependHtml parses the html and prepends it to the set of matched elements.

func (*Selection) PrependMatcher added in v1.0.0

func (s *Selection) PrependMatcher(m Matcher) *Selection

PrependMatcher prepends the elements specified by the matcher to each element in the set of matched elements.

This follows the same rules as Selection.Append.

func (*Selection) PrependNodes added in v1.0.0

func (s *Selection) PrependNodes(ns ...*html.Node) *Selection

PrependNodes prepends the specified nodes to each node in the set of matched elements.

This follows the same rules as Selection.Append.

func (*Selection) PrependSelection added in v1.0.0

func (s *Selection) PrependSelection(sel *Selection) *Selection

PrependSelection prepends the elements in the selection to each element in the set of matched elements.

This follows the same rules as Selection.Append.

func (*Selection) Prev

func (s *Selection) Prev() *Selection

Prev gets the immediately preceding sibling of each element in the Selection. It returns a new Selection object containing the matched elements.

func (*Selection) PrevAll

func (s *Selection) PrevAll() *Selection

PrevAll gets all the preceding siblings of each element in the Selection. It returns a new Selection object containing the matched elements.

func (*Selection) PrevAllFiltered

func (s *Selection) PrevAllFiltered(selector string) *Selection

PrevAllFiltered gets all the preceding siblings of each element in the Selection filtered by a selector. It returns a new Selection object containing the matched elements.

func (*Selection) PrevAllMatcher added in v1.0.0

func (s *Selection) PrevAllMatcher(m Matcher) *Selection

PrevAllMatcher gets all the preceding siblings of each element in the Selection filtered by a matcher. It returns a new Selection object containing the matched elements.

func (*Selection) PrevFiltered

func (s *Selection) PrevFiltered(selector string) *Selection

PrevFiltered gets the immediately preceding sibling of each element in the Selection filtered by a selector. It returns a new Selection object containing the matched elements.

func (*Selection) PrevFilteredUntil

func (s *Selection) PrevFilteredUntil(filterSelector, untilSelector string) *Selection

PrevFilteredUntil is like PrevUntil, with the option to filter the results based on a selector string. It returns a new Selection object containing the matched elements.

func (*Selection) PrevFilteredUntilMatcher added in v1.0.0

func (s *Selection) PrevFilteredUntilMatcher(filter, until Matcher) *Selection

PrevFilteredUntilMatcher is like PrevUntilMatcher, with the option to filter the results based on a matcher. It returns a new Selection object containing the matched elements.

func (*Selection) PrevFilteredUntilNodes

func (s *Selection) PrevFilteredUntilNodes(filterSelector string, nodes ...*html.Node) *Selection

PrevFilteredUntilNodes is like PrevUntilNodes, with the option to filter the results based on a selector string. It returns a new Selection object containing the matched elements.

func (*Selection) PrevFilteredUntilSelection

func (s *Selection) PrevFilteredUntilSelection(filterSelector string, sel *Selection) *Selection

PrevFilteredUntilSelection is like PrevUntilSelection, with the option to filter the results based on a selector string. It returns a new Selection object containing the matched elements.

func (*Selection) PrevMatcher added in v1.0.0

func (s *Selection) PrevMatcher(m Matcher) *Selection

PrevMatcher gets the immediately preceding sibling of each element in the Selection filtered by a matcher. It returns a new Selection object containing the matched elements.

func (*Selection) PrevMatcherUntilNodes added in v1.0.0

func (s *Selection) PrevMatcherUntilNodes(filter Matcher, nodes ...*html.Node) *Selection

PrevMatcherUntilNodes is like PrevUntilNodes, with the option to filter the results based on a matcher. It returns a new Selection object containing the matched elements.

func (*Selection) PrevMatcherUntilSelection added in v1.0.0

func (s *Selection) PrevMatcherUntilSelection(filter Matcher, sel *Selection) *Selection

PrevMatcherUntilSelection is like PrevUntilSelection, with the option to filter the results based on a matcher. It returns a new Selection object containing the matched elements.

func (*Selection) PrevUntil

func (s *Selection) PrevUntil(selector string) *Selection

PrevUntil gets all preceding siblings of each element up to but not including the element matched by the selector. It returns a new Selection object containing the matched elements.

func (*Selection) PrevUntilMatcher added in v1.0.0

func (s *Selection) PrevUntilMatcher(m Matcher) *Selection

PrevUntilMatcher gets all preceding siblings of each element up to but not including the element matched by the matcher. It returns a new Selection object containing the matched elements.

func (*Selection) PrevUntilNodes

func (s *Selection) PrevUntilNodes(nodes ...*html.Node) *Selection

PrevUntilNodes gets all preceding siblings of each element up to but not including the element matched by the nodes. It returns a new Selection object containing the matched elements.

func (*Selection) PrevUntilSelection

func (s *Selection) PrevUntilSelection(sel *Selection) *Selection

PrevUntilSelection gets all preceding siblings of each element up to but not including the element matched by the Selection. It returns a new Selection object containing the matched elements.

func (*Selection) Remove added in v1.0.0

func (s *Selection) Remove() *Selection

Remove removes the set of matched elements from the document. It returns the same selection, now consisting of nodes not in the document.

func (*Selection) RemoveAttr added in v1.0.0

func (s *Selection) RemoveAttr(attrName string) *Selection

RemoveAttr removes the named attribute from each element in the set of matched elements.

func (*Selection) RemoveClass added in v1.0.0

func (s *Selection) RemoveClass(class ...string) *Selection

RemoveClass removes the given class(es) from each element in the set of matched elements. Multiple class names can be specified, separated by a space or via multiple arguments. If no class name is provided, all classes are removed.

func (*Selection) RemoveFiltered added in v1.0.0

func (s *Selection) RemoveFiltered(selector string) *Selection

RemoveFiltered removes the set of matched elements by selector. It returns the Selection of removed nodes.

func (*Selection) RemoveMatcher added in v1.0.0

func (s *Selection) RemoveMatcher(m Matcher) *Selection

RemoveMatcher removes the set of matched elements. It returns the Selection of removed nodes.

func (*Selection) ReplaceWith added in v1.0.0

func (s *Selection) ReplaceWith(selector string) *Selection

ReplaceWith replaces each element in the set of matched elements with the nodes matched by the given selector. It returns the removed elements.

This follows the same rules as Selection.Append.

func (*Selection) ReplaceWithHtml added in v1.0.0

func (s *Selection) ReplaceWithHtml(html string) *Selection

ReplaceWithHtml replaces each element in the set of matched elements with the parsed HTML. It returns the removed elements.

This follows the same rules as Selection.Append.

func (*Selection) ReplaceWithMatcher added in v1.0.0

func (s *Selection) ReplaceWithMatcher(m Matcher) *Selection

ReplaceWithMatcher replaces each element in the set of matched elements with the nodes matched by the given Matcher. It returns the removed elements.

This follows the same rules as Selection.Append.

func (*Selection) ReplaceWithNodes added in v1.0.0

func (s *Selection) ReplaceWithNodes(ns ...*html.Node) *Selection

ReplaceWithNodes replaces each element in the set of matched elements with the given nodes. It returns the removed elements.

This follows the same rules as Selection.Append.

func (*Selection) ReplaceWithSelection added in v1.0.0

func (s *Selection) ReplaceWithSelection(sel *Selection) *Selection

ReplaceWithSelection replaces each element in the set of matched elements with the nodes from the given Selection. It returns the removed elements.

This follows the same rules as Selection.Append.

func (*Selection) SetAttr added in v1.0.0

func (s *Selection) SetAttr(attrName, val string) *Selection

SetAttr sets the given attribute on each element in the set of matched elements.

func (*Selection) SetHtml added in v1.1.0

func (s *Selection) SetHtml(html string) *Selection

SetHtml sets the html content of each element in the selection to specified html string.

func (*Selection) SetText added in v1.1.0

func (s *Selection) SetText(text string) *Selection

SetText sets the content of each element in the selection to specified content. The provided text string is escaped.

func (*Selection) Siblings

func (s *Selection) Siblings() *Selection

Siblings gets the siblings of each element in the Selection. It returns a new Selection object containing the matched elements.

func (*Selection) SiblingsFiltered

func (s *Selection) SiblingsFiltered(selector string) *Selection

SiblingsFiltered gets the siblings of each element in the Selection filtered by a selector. It returns a new Selection object containing the matched elements.

func (*Selection) SiblingsMatcher added in v1.0.0

func (s *Selection) SiblingsMatcher(m Matcher) *Selection

SiblingsMatcher gets the siblings of each element in the Selection filtered by a matcher. It returns a new Selection object containing the matched elements.

func (*Selection) Size

func (s *Selection) Size() int

Size is an alias for Length.

func (*Selection) Slice

func (s *Selection) Slice(start, end int) *Selection

Slice reduces the set of matched elements to a subset specified by a range of indices. The start index is 0-based and indicates the index of the first element to select. The end index is 0-based and indicates the index at which the elements stop being selected (the end index is not selected).

The indices may be negative, in which case they represent an offset from the end of the selection.

The special value ToEnd may be specified as end index, in which case all elements until the end are selected. This works both for a positive and negative start index.

func (*Selection) Text

func (s *Selection) Text() string

Text gets the combined text contents of each element in the set of matched elements, including their descendants.

func (*Selection) ToggleClass added in v1.0.0

func (s *Selection) ToggleClass(class ...string) *Selection

ToggleClass adds or removes the given class(es) for each element in the set of matched elements. Multiple class names can be specified, separated by a space or via multiple arguments.

func (*Selection) Union

func (s *Selection) Union(sel *Selection) *Selection

Union is an alias for AddSelection.

func (*Selection) Unwrap added in v1.0.0

func (s *Selection) Unwrap() *Selection

Unwrap removes the parents of the set of matched elements, leaving the matched elements (and their siblings, if any) in their place. It returns the original selection.

func (*Selection) Wrap added in v1.0.0

func (s *Selection) Wrap(selector string) *Selection

Wrap wraps each element in the set of matched elements inside the first element matched by the given selector. The matched child is cloned before being inserted into the document.

It returns the original set of elements.

func (*Selection) WrapAll added in v1.0.0

func (s *Selection) WrapAll(selector string) *Selection

WrapAll wraps a single HTML structure, matched by the given selector, around all elements in the set of matched elements. The matched child is cloned before being inserted into the document.

It returns the original set of elements.

func (*Selection) WrapAllHtml added in v1.0.0

func (s *Selection) WrapAllHtml(html string) *Selection

WrapAllHtml wraps the given HTML structure around all elements in the set of matched elements. The matched child is cloned before being inserted into the document.

It returns the original set of elements.

func (*Selection) WrapAllMatcher added in v1.0.0

func (s *Selection) WrapAllMatcher(m Matcher) *Selection

WrapAllMatcher wraps a single HTML structure, matched by the given Matcher, around all elements in the set of matched elements. The matched child is cloned before being inserted into the document.

It returns the original set of elements.

func (*Selection) WrapAllNode added in v1.0.0

func (s *Selection) WrapAllNode(n *html.Node) *Selection

WrapAllNode wraps the given node around the first element in the Selection, making all other nodes in the Selection children of the given node. The node is cloned before being inserted into the document.

It returns the original set of elements.

func (*Selection) WrapAllSelection added in v1.0.0

func (s *Selection) WrapAllSelection(sel *Selection) *Selection

WrapAllSelection wraps a single HTML structure, the first node of the given Selection, around all elements in the set of matched elements. The matched child is cloned before being inserted into the document.

It returns the original set of elements.

func (*Selection) WrapHtml added in v1.0.0

func (s *Selection) WrapHtml(html string) *Selection

WrapHtml wraps each element in the set of matched elements inside the inner- most child of the given HTML.

It returns the original set of elements.

func (*Selection) WrapInner added in v1.0.0

func (s *Selection) WrapInner(selector string) *Selection

WrapInner wraps an HTML structure, matched by the given selector, around the content of element in the set of matched elements. The matched child is cloned before being inserted into the document.

It returns the original set of elements.

func (*Selection) WrapInnerHtml added in v1.0.0

func (s *Selection) WrapInnerHtml(html string) *Selection

WrapInnerHtml wraps an HTML structure, matched by the given selector, around the content of element in the set of matched elements. The matched child is cloned before being inserted into the document.

It returns the original set of elements.

func (*Selection) WrapInnerMatcher added in v1.0.0

func (s *Selection) WrapInnerMatcher(m Matcher) *Selection

WrapInnerMatcher wraps an HTML structure, matched by the given selector, around the content of element in the set of matched elements. The matched child is cloned before being inserted into the document.

It returns the original set of elements.

func (*Selection) WrapInnerNode added in v1.0.0

func (s *Selection) WrapInnerNode(n *html.Node) *Selection

WrapInnerNode wraps an HTML structure, matched by the given selector, around the content of element in the set of matched elements. The matched child is cloned before being inserted into the document.

It returns the original set of elements.

func (*Selection) WrapInnerSelection added in v1.0.0

func (s *Selection) WrapInnerSelection(sel *Selection) *Selection

WrapInnerSelection wraps an HTML structure, matched by the given selector, around the content of element in the set of matched elements. The matched child is cloned before being inserted into the document.

It returns the original set of elements.

func (*Selection) WrapMatcher added in v1.0.0

func (s *Selection) WrapMatcher(m Matcher) *Selection

WrapMatcher wraps each element in the set of matched elements inside the first element matched by the given matcher. The matched child is cloned before being inserted into the document.

It returns the original set of elements.

func (*Selection) WrapNode added in v1.0.0

func (s *Selection) WrapNode(n *html.Node) *Selection

WrapNode wraps each element in the set of matched elements inside the inner- most child of the given node. The given node is copied before being inserted into the document.

It returns the original set of elements.

func (*Selection) WrapSelection added in v1.0.0

func (s *Selection) WrapSelection(sel *Selection) *Selection

WrapSelection wraps each element in the set of matched elements inside the first element in the given Selection. The element is cloned before being inserted into the document.

It returns the original set of elements.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL