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

Function handleElement

htmlcore/handler.go:54–258  ·  view source on GitHub ↗

handleElement calls the handler in [ElementHandlers] associated with the current node using the given context. If there is no handler associated with it, it uses default hardcoded configuration code.

(ctx *Context)

Source from the content-addressed store, hash-verified

52// using the given context. If there is no handler associated with it, it uses default
53// hardcoded configuration code.
54func handleElement(ctx *Context) {
55 tag := ctx.Node.Data
56 h, ok := ElementHandlers[tag]
57 if ok {
58 if h(ctx) {
59 return
60 }
61 }
62
63 if slices.Contains(textTags, tag) {
64 handleTextTag(ctx)
65 return
66 }
67
68 switch tag {
69 case "script", "title", "meta":
70 // we don't render anything
71 case "link":
72 rel := getAttr(ctx.Node, "rel")
73 // TODO(kai/htmlcore): maybe handle preload
74 if rel == "preload" {
75 return
76 }
77 // TODO(kai/htmlcore): support links other than stylesheets
78 if rel != "stylesheet" {
79 return
80 }
81 resp, err := Get(ctx, getAttr(ctx.Node, "href"))
82 if errors.Log(err) != nil {
83 return
84 }
85 defer resp.Body.Close()
86 b, err := io.ReadAll(resp.Body)
87 if errors.Log(err) != nil {
88 return
89 }
90 ctx.addStyle(string(b))
91 case "style":
92 ctx.addStyle(ExtractText(ctx))
93 case "body", "main", "div", "section", "nav", "footer", "header", "ol", "ul":
94 w := New[core.Frame](ctx)
95 ctx.NewParent = w
96 if tag == "body" {
97 w.Styler(func(s *styles.Style) {
98 s.Grow.Set(1, 1)
99 })
100 }
101 if tag == "ol" || tag == "ul" {
102 w.Styler(func(s *styles.Style) {
103 s.Grow.Set(1, 0)
104 })
105 }
106 case "button":
107 New[core.Button](ctx).SetText(ExtractText(ctx))
108 case "h1":
109 handleText(ctx).SetType(core.TextDisplaySmall)
110 case "h2":
111 handleText(ctx).SetType(core.TextHeadlineMedium)

Callers 1

readHTMLNodeFunction · 0.85

Calls 15

SetTextMethod · 0.95
LogFunction · 0.92
NewFrameFunction · 0.92
AsWidgetFunction · 0.92
ReadFunction · 0.92
Log1Function · 0.92
BindFunction · 0.92
NewBufferFunction · 0.92
handleTextTagFunction · 0.85
getAttrFunction · 0.85
ExtractTextFunction · 0.85
handleTextFunction · 0.85

Tested by

no test coverage detected