diff --git a/fragment.go b/fragment.go index ebcbcaa..726749b 100644 --- a/fragment.go +++ b/fragment.go @@ -17,6 +17,8 @@ func NewDocumentFragment(nodes []*html.Node) *DocumentFragment { return &DocumentFragment{nodes: nodes} } +func (d *DocumentFragment) String() string { return outerHTML(d.nodes...) } + func (d *DocumentFragment) NodeType() spec.NodeType { return spec.NodeTypeDocumentFragment } func (d *DocumentFragment) CloneNode(deep bool) spec.Node { diff --git a/fragment_test.go b/fragment_test.go index 29cfcc7..1061911 100644 --- a/fragment_test.go +++ b/fragment_test.go @@ -367,3 +367,16 @@ func TestDocumentFragment_QuerySelectorAll(t *testing.T) { require.Nil(t, fragment.QuerySelectorAll("#not-found")) }) } + +func TestDocumentFragment_String(t *testing.T) { + t.Run("found two", func(t *testing.T) { + rawHTML := ` +
+
+
` + + fragment := parseDocumentFragment(t, rawHTML) + + assert.Equal(t, rawHTML, fragment.String()) + }) +} diff --git a/go.mod b/go.mod index 133e502..1912da4 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,8 @@ module github.com/crhntr/dom -go 1.21 +go 1.22 + +toolchain go1.22.4 require ( github.com/andybalholm/cascadia v1.3.2 @@ -10,6 +12,7 @@ require ( ) require ( + github.com/crhntr/hx v0.0.1-dev.2 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/kr/pretty v0.3.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect diff --git a/go.sum b/go.sum index 3b792fb..cddb146 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,8 @@ github.com/andybalholm/cascadia v1.3.2 h1:3Xi6Dw5lHF15JtdcmAHD3i1+T8plmv7BQ/nsViSLyss= github.com/andybalholm/cascadia v1.3.2/go.mod h1:7gtRlve5FxPPgIgX36uWBX58OdBsSS6lUvCFb+h7KvU= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +github.com/crhntr/hx v0.0.1-dev.2 h1:aw1f1XnbVhLwpvm+dGpVRpBDlkaHwTSUHiHRjSinTzk= +github.com/crhntr/hx v0.0.1-dev.2/go.mod h1:wKtm+v8CXtOnWHIh9TYizRfDYaiN/j55AlJGt6fz180= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= diff --git a/node.go b/node.go index 9391d31..d191e44 100644 --- a/node.go +++ b/node.go @@ -12,11 +12,12 @@ import ( "github.com/crhntr/dom/spec" ) -func outerHTML(node *html.Node) string { +func outerHTML(nodes ...*html.Node) string { var buf bytes.Buffer - err := html.Render(&buf, node) - if err != nil { - return "" + for _, node := range nodes { + if err := html.Render(&buf, node); err != nil { + return "" + } } return buf.String() }