Skip to content

Commit

Permalink
feat(dom): add String method to DocumentFragment
Browse files Browse the repository at this point in the history
  • Loading branch information
crhntr committed Jul 9, 2024
1 parent bbb4471 commit dd7be25
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 5 deletions.
2 changes: 2 additions & 0 deletions fragment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 13 additions & 0 deletions fragment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 := `
<section><div class="a" id="i1"></div></section>
<section></section>
<section><div class="a" id="i2"></div><div class="a" id="i3"></div></section>`

fragment := parseDocumentFragment(t, rawHTML)

assert.Equal(t, rawHTML, fragment.String())
})
}
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
9 changes: 5 additions & 4 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down

0 comments on commit dd7be25

Please sign in to comment.