-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlayout.go
More file actions
247 lines (210 loc) · 5.9 KB
/
layout.go
File metadata and controls
247 lines (210 loc) · 5.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package html
// Note: this file is WASM-linked. Per RFC §7 the WASM build must stay under the
// 3.5 MB raw / 1 MB gzip size budget, so we deliberately avoid importing
// dappco.re/go/core here — it transitively pulls in fmt/os/log (~500 KB+).
// The stdlib strconv primitive is safe for WASM.
import "strconv"
// Compile-time interface check.
var _ Node = (*Layout)(nil)
// ErrInvalidLayoutVariant is retained for compatibility.
//
// Layout variant strings now silently skip unknown characters instead of
// surfacing validation errors, so this sentinel is never returned by the
// current implementation.
var ErrInvalidLayoutVariant error = layoutInvalidVariantSentinel{}
type layoutInvalidVariantSentinel struct{}
func (layoutInvalidVariantSentinel) Error() string {
return "html: invalid layout variant"
}
// slotMeta holds the semantic HTML mapping for each HLCRF slot.
type slotMeta struct {
tag string
role string
}
// slotRegistry maps slot letters to their semantic HTML elements and ARIA roles.
var slotRegistry = map[byte]slotMeta{
'H': {tag: "header", role: "banner"},
'L': {tag: "nav", role: "navigation"},
'C': {tag: "main", role: "main"},
'R': {tag: "aside", role: "complementary"},
'F': {tag: "footer", role: "contentinfo"},
}
// Layout is an HLCRF compositor. Arranges nodes into semantic HTML regions
// with deterministic path-based IDs.
// Usage example: page := NewLayout("HCF").H(Text("title")).C(Text("body"))
type Layout struct {
variant string // "HLCRF", "HCF", "C", etc.
path string // "" for root, "C.0" for nested
slots map[byte][]Node // H, L, C, R, F → children
variantErr error
}
func renderWithLayoutPath(node Node, ctx *Context, path string) string {
if node == nil {
return ""
}
if renderer, ok := node.(layoutPathRenderer); ok {
return renderer.renderWithLayoutPath(ctx, path)
}
return node.Render(ctx)
}
// NewLayout creates a new Layout with the given variant string.
// Usage example: page := NewLayout("HLCRF")
// The variant determines which slots are rendered (e.g., "HLCRF", "HCF", "C").
func NewLayout(variant string) *Layout {
l := &Layout{
variant: variant,
slots: make(map[byte][]Node),
}
return l
}
// ValidateLayoutVariant is retained for compatibility.
//
// Variant strings are permissive now: unknown characters are ignored during
// rendering, so this helper always returns nil.
func ValidateLayoutVariant(variant string) error {
_ = variant
return nil
}
func (l *Layout) slotsForSlot(slot byte) []Node {
if l == nil {
return nil
}
if l.slots == nil {
l.slots = make(map[byte][]Node)
}
return l.slots[slot]
}
// H appends nodes to the Header slot.
// Usage example: NewLayout("HCF").H(Text("title"))
func (l *Layout) H(nodes ...Node) *Layout {
if l == nil {
return nil
}
l.slots['H'] = append(l.slotsForSlot('H'), nodes...)
return l
}
// L appends nodes to the Left navigation slot.
// Usage example: NewLayout("LC").L(Text("nav"))
func (l *Layout) L(nodes ...Node) *Layout {
if l == nil {
return nil
}
l.slots['L'] = append(l.slotsForSlot('L'), nodes...)
return l
}
// C appends nodes to the Content (main) slot.
// Usage example: NewLayout("C").C(Text("body"))
func (l *Layout) C(nodes ...Node) *Layout {
if l == nil {
return nil
}
l.slots['C'] = append(l.slotsForSlot('C'), nodes...)
return l
}
// R appends nodes to the Right aside slot.
// Usage example: NewLayout("CR").R(Text("ads"))
func (l *Layout) R(nodes ...Node) *Layout {
if l == nil {
return nil
}
l.slots['R'] = append(l.slotsForSlot('R'), nodes...)
return l
}
// F appends nodes to the Footer slot.
// Usage example: NewLayout("CF").F(Text("footer"))
func (l *Layout) F(nodes ...Node) *Layout {
if l == nil {
return nil
}
l.slots['F'] = append(l.slotsForSlot('F'), nodes...)
return l
}
// blockID returns the deterministic data-block coordinate for a rendered slot.
func (l *Layout) blockID(slot byte, rendered int) string {
if l.path == "" {
if rendered == 0 {
return string(slot)
}
return string(slot) + "." + strconv.Itoa(rendered)
}
if rendered == 0 {
return l.path
}
return l.path + "." + strconv.Itoa(rendered)
}
// VariantError is retained for compatibility.
//
// Layouts no longer record variant validation errors, so this always returns
// nil. Unknown characters are ignored at render time.
func (l *Layout) VariantError() error {
if l == nil {
return nil
}
return nil
}
// Render produces the semantic HTML for this layout.
// Usage example: html := NewLayout("C").C(Text("body")).Render(NewContext())
// Only slots present in the variant string are rendered.
func (l *Layout) Render(ctx *Context) string {
if l == nil {
return ""
}
if ctx == nil {
ctx = NewContext()
}
b := newTextBuilder()
slotCounts := make(map[byte]int)
slotOrdinal := 0
for i := range len(l.variant) {
slot := l.variant[i]
meta, ok := slotRegistry[slot]
if !ok {
continue
}
count := slotOrdinal
slotOrdinal++
children := l.slots[slot]
if len(children) == 0 {
continue
}
if l.path == "" {
count = slotCounts[slot]
slotCounts[slot] = count + 1
}
bid := l.blockID(slot, count)
b.WriteByte('<')
b.WriteString(escapeHTML(meta.tag))
b.WriteString(` role="`)
b.WriteString(escapeAttr(meta.role))
b.WriteString(`" data-block="`)
b.WriteString(escapeAttr(bid))
b.WriteString(`">`)
for i, child := range children {
if child == nil {
continue
}
b.WriteString(renderWithLayoutPath(child, ctx, bid+"."+strconv.Itoa(i)))
}
b.WriteString("</")
b.WriteString(meta.tag)
b.WriteByte('>')
}
return b.String()
}
type layoutVariantError struct {
variant string
}
func (e *layoutVariantError) Error() string {
return "html: invalid layout variant " + e.variant
}
func (e *layoutVariantError) Unwrap() error {
return ErrInvalidLayoutVariant
}
func (l *Layout) renderWithLayoutPath(ctx *Context, path string) string {
if l == nil {
return ""
}
clone := *l
clone.path = path
return clone.Render(ctx)
}