mirror of
https://github.com/PuerkitoBio/goquery.git
synced 2024-04-21 12:31:36 +00:00
Merge pull request #399 from anthonygedeon/issue/377-save-manipulated-html
Fixes #377, renders the html that can be manipulated with writer
This commit is contained in:
+12
-6
@@ -2,6 +2,7 @@ package goquery
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"io"
|
||||||
|
|
||||||
"golang.org/x/net/html"
|
"golang.org/x/net/html"
|
||||||
)
|
)
|
||||||
@@ -57,6 +58,16 @@ func nodeName(node *html.Node) string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Render renders the html of the first element from selector 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
|
// 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 +77,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
|
||||||
|
|||||||
@@ -80,7 +80,6 @@ func TestNodeNameMultiSel(t *testing.T) {
|
|||||||
t.Errorf("want %v, got %v", in, out)
|
t.Errorf("want %v, got %v", in, out)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
||||||
|
|||||||
Reference in New Issue
Block a user