Skip to content

Commit

Permalink
feat: add String method to Document
Browse files Browse the repository at this point in the history
this is really helpful for tests and debugging
  • Loading branch information
crhntr committed May 18, 2024
1 parent 14d666f commit 140e804
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
1 change: 1 addition & 0 deletions document.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Document struct {
node *html.Node
}

func (d *Document) String() string { return outerHTML(d.node) }
func (d *Document) NodeType() spec.NodeType { return nodeType(d.node.Type) }
func (d *Document) CloneNode(deep bool) spec.Node { return NewNode(cloneNode(d.node, deep)) }
func (d *Document) IsSameNode(other spec.Node) bool { return isSameNode(d.node, other) }
Expand Down
9 changes: 1 addition & 8 deletions element.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,7 @@ func (e *Element) SetOuterHTML(s string) {
e.node.Parent.RemoveChild(e.node)
}

func (e *Element) OuterHTML() string {
var buf bytes.Buffer
err := html.Render(&buf, e.node)
if err != nil {
panic(err)
}
return buf.String()
}
func (e *Element) OuterHTML() string { return outerHTML(e.node) }

type siblingElements struct {
firstChild *html.Node
Expand Down
9 changes: 9 additions & 0 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ import (
"github.com/crhntr/dom/spec"
)

func outerHTML(node *html.Node) string {
var buf bytes.Buffer
err := html.Render(&buf, node)
if err != nil {
return ""
}
return buf.String()
}

func nodeType(nodeType html.NodeType) spec.NodeType {
switch nodeType {
case html.TextNode:
Expand Down

0 comments on commit 140e804

Please sign in to comment.