mirror of
https://github.com/PuerkitoBio/goquery
synced 2025-10-05 08:46:53 +08:00
Fixes PuerkitoBio#377, renders the html that can be manipulated with an writer as input
This commit is contained in:
21
utilities.go
21
utilities.go
@@ -2,6 +2,7 @@ package goquery
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"io"
|
||||||
|
|
||||||
"golang.org/x/net/html"
|
"golang.org/x/net/html"
|
||||||
)
|
)
|
||||||
@@ -57,6 +58,19 @@ func nodeName(node *html.Node) string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Render renders the html of the first element from selector and writes it to the writer.
|
||||||
|
// It behaves similar to OuterHtml but takes io.Writer as input.
|
||||||
|
func Render(w io.Writer, s *Selection) error {
|
||||||
|
if s.Length() == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
n := s.Get(0)
|
||||||
|
if err := html.Render(w, n); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// OuterHtml returns the outer HTML rendering of the first item in
|
// OuterHtml returns the outer HTML rendering of the first item in
|
||||||
// the selection - that is, the HTML including the first element's
|
// the selection - that is, the HTML including the first element's
|
||||||
// tag and attributes.
|
// tag and attributes.
|
||||||
@@ -66,12 +80,7 @@ func nodeName(node *html.Node) string {
|
|||||||
// a property provided by the DOM).
|
// a property provided by the DOM).
|
||||||
func OuterHtml(s *Selection) (string, error) {
|
func OuterHtml(s *Selection) (string, error) {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
|
if err := Render(&buf, s); err != nil {
|
||||||
if s.Length() == 0 {
|
|
||||||
return "", nil
|
|
||||||
}
|
|
||||||
n := s.Get(0)
|
|
||||||
if err := html.Render(&buf, n); err != nil {
|
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
return buf.String(), nil
|
return buf.String(), nil
|
||||||
|
@@ -24,6 +24,49 @@ var allNodes = `<!doctype html>
|
|||||||
</body>
|
</body>
|
||||||
</html>`
|
</html>`
|
||||||
|
|
||||||
|
func assertMatchNodes(t testing.TB, doc *Document) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
n0 := doc.Nodes[0]
|
||||||
|
nDT := n0.FirstChild
|
||||||
|
sMeta := doc.Find("meta")
|
||||||
|
sP := doc.Find("p")
|
||||||
|
nP := sP.Get(0)
|
||||||
|
nComment := nP.FirstChild
|
||||||
|
nText := nComment.NextSibling
|
||||||
|
sHeaders := doc.Find(".header")
|
||||||
|
|
||||||
|
cases := []struct {
|
||||||
|
node *html.Node
|
||||||
|
sel *Selection
|
||||||
|
want string
|
||||||
|
}{
|
||||||
|
{nDT, nil, "<!DOCTYPE html>"}, // render makes DOCTYPE all caps
|
||||||
|
{nil, sMeta, `<meta a="b"/>`}, // and auto-closes the meta
|
||||||
|
{nil, sP, `<p><!-- this is a comment -->
|
||||||
|
This is some text.
|
||||||
|
</p>`},
|
||||||
|
{nComment, nil, "<!-- this is a comment -->"},
|
||||||
|
{nText, nil, `
|
||||||
|
This is some text.
|
||||||
|
`},
|
||||||
|
{nil, sHeaders, `<h1 class="header"></h1>`},
|
||||||
|
}
|
||||||
|
for i, c := range cases {
|
||||||
|
if c.sel == nil {
|
||||||
|
c.sel = newSingleSelection(c.node, doc)
|
||||||
|
}
|
||||||
|
got, err := OuterHtml(c.sel)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if got != c.want {
|
||||||
|
t.Errorf("%d: want %q, got %q", i, c.want, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestNodeName(t *testing.T) {
|
func TestNodeName(t *testing.T) {
|
||||||
doc, err := NewDocumentFromReader(strings.NewReader(allNodes))
|
doc, err := NewDocumentFromReader(strings.NewReader(allNodes))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -81,48 +124,18 @@ func TestNodeNameMultiSel(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRender(t *testing.T) {
|
||||||
|
doc, err := NewDocumentFromReader(strings.NewReader(allNodes))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
assertMatchNodes(t, doc)
|
||||||
|
}
|
||||||
|
|
||||||
func TestOuterHtml(t *testing.T) {
|
func TestOuterHtml(t *testing.T) {
|
||||||
doc, err := NewDocumentFromReader(strings.NewReader(allNodes))
|
doc, err := NewDocumentFromReader(strings.NewReader(allNodes))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
assertMatchNodes(t, doc)
|
||||||
n0 := doc.Nodes[0]
|
|
||||||
nDT := n0.FirstChild
|
|
||||||
sMeta := doc.Find("meta")
|
|
||||||
sP := doc.Find("p")
|
|
||||||
nP := sP.Get(0)
|
|
||||||
nComment := nP.FirstChild
|
|
||||||
nText := nComment.NextSibling
|
|
||||||
sHeaders := doc.Find(".header")
|
|
||||||
|
|
||||||
cases := []struct {
|
|
||||||
node *html.Node
|
|
||||||
sel *Selection
|
|
||||||
want string
|
|
||||||
}{
|
|
||||||
{nDT, nil, "<!DOCTYPE html>"}, // render makes DOCTYPE all caps
|
|
||||||
{nil, sMeta, `<meta a="b"/>`}, // and auto-closes the meta
|
|
||||||
{nil, sP, `<p><!-- this is a comment -->
|
|
||||||
This is some text.
|
|
||||||
</p>`},
|
|
||||||
{nComment, nil, "<!-- this is a comment -->"},
|
|
||||||
{nText, nil, `
|
|
||||||
This is some text.
|
|
||||||
`},
|
|
||||||
{nil, sHeaders, `<h1 class="header"></h1>`},
|
|
||||||
}
|
|
||||||
for i, c := range cases {
|
|
||||||
if c.sel == nil {
|
|
||||||
c.sel = newSingleSelection(c.node, doc)
|
|
||||||
}
|
|
||||||
got, err := OuterHtml(c.sel)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if got != c.want {
|
|
||||||
t.Errorf("%d: want %q, got %q", i, c.want, got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user