Skip to content

Commit 9c107ad

Browse files
committed
Adding cdp.Node.WriteTo method
1 parent 47f0cf1 commit 9c107ad

File tree

4 files changed

+302
-179
lines changed

4 files changed

+302
-179
lines changed

gen/gogen.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,17 @@ func (fb fileBuffers) get(s string, pkgName string, d *pdl.Domain, domains []*pd
186186
"github.com/mailru/easyjson/jwriter": "",
187187
"github.com/chromedp/sysutil": "",
188188
}
189+
// add io only for cdp package
190+
if pkgName == "cdp" {
191+
importMap["io"] = ""
192+
}
189193
for _, d := range domains {
190-
importMap[basePkg+"/"+genutil.PackageName(d)] = ""
194+
pn := genutil.PackageName(d)
195+
// skip adding cdproto/io package to cdp package
196+
if pkgName == "cdp" && pn == "io" {
197+
continue
198+
}
199+
importMap[basePkg+"/"+pn] = ""
191200
}
192201
gotpl.StreamFileImportTemplate(w, importMap)
193202

gen/gotpl/extra.qtpl

+77-20
Original file line numberDiff line numberDiff line change
@@ -182,64 +182,121 @@ func (n *Node) FullXPath() string {
182182
return n.xpath(false, false)
183183
}
184184

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

191191
n.RLock()
192192
defer n.RUnlock()
193193

194-
s := n.LocalName
195-
if s == "" {
196-
s = n.NodeName
194+
var err error
195+
var nn, c int
196+
197+
// prefix
198+
if c, err = w.Write([]byte(prefix)); err != nil {
199+
return nn + c, err
197200
}
201+
nn += c
198202

203+
// node name
204+
if n.LocalName != "" {
205+
if c, err = w.Write([]byte(n.LocalName)); err != nil {
206+
return nn + c, err
207+
}
208+
nn += c
209+
} else {
210+
if c, err = w.Write([]byte(n.NodeName)); err != nil {
211+
return nn + c, err
212+
}
213+
nn += c
214+
}
215+
216+
// add #id
217+
var hasID int
199218
for i := 0; i < len(n.Attributes); i += 2 {
200219
if strings.ToLower(n.Attributes[i]) == "id" {
201-
s += "#" + n.Attributes[i+1]
220+
if c, err = w.Write([]byte("#" + n.Attributes[i+1])); err != nil {
221+
return nn + c, err
222+
}
223+
nn += c
224+
hasID = 2
202225
break
203226
}
204227
}
205228

229+
// node type
206230
if n.NodeType != NodeTypeElement && n.NodeType != NodeTypeText {
207-
s += fmt.Sprintf(" <%s>", n.NodeType)
231+
if c, err = fmt.Fprintf(w, " <%s>", n.NodeType); err != nil {
232+
return nn + c, err
233+
}
234+
nn += c
208235
}
209236

237+
// node value
210238
if n.NodeType == NodeTypeText {
211239
v := n.NodeValue
212240
if len(v) > 15 {
213241
v = v[:15] + "..."
214242
}
215-
s += fmt.Sprintf(" %q", v)
243+
if c, err = fmt.Fprintf(w, " %q", v); err != nil {
244+
return nn + c, err
245+
}
246+
nn += c
216247
}
217248

218-
if n.NodeType == NodeTypeElement && len(n.Attributes) > 0 {
219-
attrs := ""
220-
for i := 0; i < len(n.Attributes); i += 2 {
249+
// attributes
250+
if n.NodeType == NodeTypeElement && len(n.Attributes) > hasID {
251+
if c, err = w.Write([]byte(" [")); err != nil {
252+
return nn + c, err
253+
}
254+
nn += c
255+
for i, space := 0, ""; i < len(n.Attributes); i += 2 {
221256
if strings.ToLower(n.Attributes[i]) == "id" {
222257
continue
223258
}
224-
if attrs != "" {
225-
attrs += " "
259+
if c, err = fmt.Fprintf(w, "%s%s=%q", space, n.Attributes[i], n.Attributes[i+1]); err != nil {
260+
return nn + c, err
261+
}
262+
nn += c
263+
if space == "" {
264+
space = " "
226265
}
227-
attrs += fmt.Sprintf("%s=%q", n.Attributes[i], n.Attributes[i+1])
228266
}
229-
if attrs != "" {
230-
s += " [" + attrs + "]"
267+
if c, err = w.Write([]byte{']'}); err != nil {
268+
return nn + c, err
231269
}
270+
nn += c
232271
}
233272

273+
// node id
234274
if nodeIDs {
235-
s += fmt.Sprintf(" (%d)", n.NodeID)
275+
if c, err = fmt.Fprintf(w, " (%d)", n.NodeID); err != nil {
276+
return nn + c, err
277+
}
278+
nn += c
236279
}
237280

281+
// children
238282
for i := 0; i < len(n.Children); i++ {
239-
s += "\n" + n.Children[i].Dump(prefix+indent, indent, nodeIDs)
283+
if c, err = fmt.Fprintln(w); err != nil {
284+
return nn + c, err
285+
}
286+
nn += c
287+
if c, err = n.Children[i].WriteTo(w, prefix+indent, indent, nodeIDs); err != nil {
288+
return nn + c, err
289+
}
290+
nn += c
240291
}
292+
return nn, nil
293+
}
241294

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

245302
// NodeState is the state of a DOM node.

0 commit comments

Comments
 (0)