Skip to content

Commit

Permalink
Adding cdp.Node.WriteTo method
Browse files Browse the repository at this point in the history
  • Loading branch information
kenshaw committed Feb 2, 2024
1 parent 47f0cf1 commit 9c107ad
Show file tree
Hide file tree
Showing 4 changed files with 302 additions and 179 deletions.
11 changes: 10 additions & 1 deletion gen/gogen.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,17 @@ func (fb fileBuffers) get(s string, pkgName string, d *pdl.Domain, domains []*pd
"github.com/mailru/easyjson/jwriter": "",
"github.com/chromedp/sysutil": "",
}
// add io only for cdp package
if pkgName == "cdp" {
importMap["io"] = ""
}
for _, d := range domains {
importMap[basePkg+"/"+genutil.PackageName(d)] = ""
pn := genutil.PackageName(d)
// skip adding cdproto/io package to cdp package
if pkgName == "cdp" && pn == "io" {
continue
}
importMap[basePkg+"/"+pn] = ""
}
gotpl.StreamFileImportTemplate(w, importMap)

Expand Down
97 changes: 77 additions & 20 deletions gen/gotpl/extra.qtpl
Original file line number Diff line number Diff line change
Expand Up @@ -182,64 +182,121 @@ func (n *Node) FullXPath() string {
return n.xpath(false, false)
}

// Dump builds a printable string representation of the node and its children.
func (n *Node) Dump(prefix, indent string, nodeIDs bool) string {
// WriteTo writes a readable representation of the node and all its children to w.
func (n *Node) WriteTo(w io.Writer, prefix, indent string, nodeIDs bool) (int, error) {
if n == nil {
return prefix + "<nil>"
return w.Write([]byte(prefix + "<nil>"))
}

n.RLock()
defer n.RUnlock()

s := n.LocalName
if s == "" {
s = n.NodeName
var err error
var nn, c int

// prefix
if c, err = w.Write([]byte(prefix)); err != nil {
return nn + c, err
}
nn += c

// node name
if n.LocalName != "" {
if c, err = w.Write([]byte(n.LocalName)); err != nil {
return nn + c, err
}
nn += c
} else {
if c, err = w.Write([]byte(n.NodeName)); err != nil {
return nn + c, err
}
nn += c
}

// add #id
var hasID int
for i := 0; i < len(n.Attributes); i += 2 {
if strings.ToLower(n.Attributes[i]) == "id" {
s += "#" + n.Attributes[i+1]
if c, err = w.Write([]byte("#" + n.Attributes[i+1])); err != nil {
return nn + c, err
}
nn += c
hasID = 2
break
}
}

// node type
if n.NodeType != NodeTypeElement && n.NodeType != NodeTypeText {
s += fmt.Sprintf(" <%s>", n.NodeType)
if c, err = fmt.Fprintf(w, " <%s>", n.NodeType); err != nil {
return nn + c, err
}
nn += c
}

// node value
if n.NodeType == NodeTypeText {
v := n.NodeValue
if len(v) > 15 {
v = v[:15] + "..."
}
s += fmt.Sprintf(" %q", v)
if c, err = fmt.Fprintf(w, " %q", v); err != nil {
return nn + c, err
}
nn += c
}

if n.NodeType == NodeTypeElement && len(n.Attributes) > 0 {
attrs := ""
for i := 0; i < len(n.Attributes); i += 2 {
// attributes
if n.NodeType == NodeTypeElement && len(n.Attributes) > hasID {
if c, err = w.Write([]byte(" [")); err != nil {
return nn + c, err
}
nn += c
for i, space := 0, ""; i < len(n.Attributes); i += 2 {
if strings.ToLower(n.Attributes[i]) == "id" {
continue
}
if attrs != "" {
attrs += " "
if c, err = fmt.Fprintf(w, "%s%s=%q", space, n.Attributes[i], n.Attributes[i+1]); err != nil {
return nn + c, err
}
nn += c
if space == "" {
space = " "
}
attrs += fmt.Sprintf("%s=%q", n.Attributes[i], n.Attributes[i+1])
}
if attrs != "" {
s += " [" + attrs + "]"
if c, err = w.Write([]byte{']'}); err != nil {
return nn + c, err
}
nn += c
}

// node id
if nodeIDs {
s += fmt.Sprintf(" (%d)", n.NodeID)
if c, err = fmt.Fprintf(w, " (%d)", n.NodeID); err != nil {
return nn + c, err
}
nn += c
}

// children
for i := 0; i < len(n.Children); i++ {
s += "\n" + n.Children[i].Dump(prefix+indent, indent, nodeIDs)
if c, err = fmt.Fprintln(w); err != nil {
return nn + c, err
}
nn += c
if c, err = n.Children[i].WriteTo(w, prefix+indent, indent, nodeIDs); err != nil {
return nn + c, err
}
nn += c
}
return nn, nil
}

return prefix + s
// Dump builds a printable string representation of the node and its children.
func (n *Node) Dump(prefix, indent string, nodeIDs bool) string {
var buf bytes.Buffer
_, _ = n.WriteTo(&buf, prefix, indent, nodeIDs)
return buf.String()
}

// NodeState is the state of a DOM node.
Expand Down
Loading

0 comments on commit 9c107ad

Please sign in to comment.