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)
| 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. |
| 37 | func 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. |
| 71 | func rootNode(n *html.Node) *html.Node { |
no test coverage detected