MCPcopy Create free account
hub / github.com/cogentcore/core / readHTMLNode

Function readHTMLNode

htmlcore/html.go:37–68  ·  view source on GitHub ↗

readHTMLNode reads HTML from the given [*html.Node] and adds corresponding Cogent Core widgets to the given [core.Widget], using the given context.

(ctx *Context, parent core.Widget, n *html.Node)

Source from the content-addressed store, hash-verified

35// readHTMLNode reads HTML from the given [*html.Node] and adds corresponding
36// Cogent Core widgets to the given [core.Widget], using the given context.
37func readHTMLNode(ctx *Context, parent core.Widget, n *html.Node) error {
38 // nil parent means we are root, so we add user agent styles here
39 if n.Parent == nil {
40 ctx.Node = n
41 ctx.addStyle(userAgentStyles)
42 }
43
44 switch n.Type {
45 case html.TextNode:
46 str := strings.TrimSpace(n.Data)
47 if str != "" {
48 New[core.Text](ctx).SetText(str)
49 }
50 case html.ElementNode:
51 ctx.Node = n
52 ctx.BlockParent = parent
53 ctx.NewParent = nil
54
55 handleElement(ctx)
56 default:
57 ctx.NewParent = parent
58 }
59
60 if ctx.NewParent != nil && n.FirstChild != nil {
61 readHTMLNode(ctx, ctx.NewParent, n.FirstChild)
62 }
63
64 if n.NextSibling != nil {
65 readHTMLNode(ctx, parent, n.NextSibling)
66 }
67 return nil
68}
69
70// rootNode returns the root node of the given node.
71func rootNode(n *html.Node) *html.Node {

Callers 2

ReadHTMLFunction · 0.85
extractTextFunction · 0.85

Calls 3

handleElementFunction · 0.85
addStyleMethod · 0.80
SetTextMethod · 0.45

Tested by

no test coverage detected