Fixes PuerkitoBio#377, renders the html that can be manipulated with an writer as input

This commit is contained in:
Anthony Gedeon
2021-10-24 21:08:06 -05:00
parent 6a7f1c4a50
commit 98eb88c22b
2 changed files with 67 additions and 45 deletions

View File

@@ -2,6 +2,7 @@ package goquery
import (
"bytes"
"io"
"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
// the selection - that is, the HTML including the first element's
// tag and attributes.
@@ -66,12 +80,7 @@ func nodeName(node *html.Node) string {
// 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