Skip to content

Commit 6f58a02

Browse files
committed
add new func
1 parent daf34a1 commit 6f58a02

File tree

2 files changed

+74
-57
lines changed

2 files changed

+74
-57
lines changed

element.go

Lines changed: 62 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -49,84 +49,89 @@ var (
4949
NA = Attrs{}
5050
)
5151

52+
// New creates an element with the specified tag.
53+
func New(tag string, attrs Attrs, children ...templ.Component) *Element {
54+
return &Element{Tag: tag, Attrs: attrs, Children: children}
55+
}
56+
5257
// A creates an anchor element <a></a>.
5358
func A(attrs Attrs, children ...templ.Component) *Element {
54-
return &Element{Tag: "a", Attrs: attrs, Children: children}
59+
return New("a", attrs, children...)
5560
}
5661

5762
// Address creates an address element <address></address>.
5863
func Address(attrs Attrs, children ...templ.Component) *Element {
59-
return &Element{Tag: "address", Attrs: attrs, Children: children}
64+
return New("address", attrs, children...)
6065
}
6166

6267
// Article creates an article element <article></article>.
6368
func Article(attrs Attrs, children ...templ.Component) *Element {
64-
return &Element{Tag: "article", Attrs: attrs, Children: children}
69+
return New("article", attrs, children...)
6570
}
6671

6772
// Aside creates an aside element <aside></aside>.
6873
func Aside(attrs Attrs, children ...templ.Component) *Element {
69-
return &Element{Tag: "aside", Attrs: attrs, Children: children}
74+
return New("aside", attrs, children...)
7075
}
7176

7277
// B creates a bold element <b></b>.
7378
func B(attrs Attrs, children ...templ.Component) *Element {
74-
return &Element{Tag: "b", Attrs: attrs, Children: children}
79+
return New("b", attrs, children...)
7580
}
7681

7782
// Blockquote creates a blockquote element <blockquote></blockquote>.
7883
func Blockquote(attrs Attrs, children ...templ.Component) *Element {
79-
return &Element{Tag: "blockquote", Attrs: attrs, Children: children}
84+
return New("blockquote", attrs, children...)
8085
}
8186

8287
// Body creates a body element <body></body>.
8388
func Body(attrs Attrs, children ...templ.Component) *Element {
84-
return &Element{Tag: "body", Attrs: attrs, Children: children}
89+
return New("body", attrs, children...)
8590
}
8691

8792
// Button creates a button element <button></button>.
8893
func Button(attrs Attrs, children ...templ.Component) *Element {
89-
return &Element{Tag: "button", Attrs: attrs, Children: children}
94+
return New("button", attrs, children...)
9095
}
9196

9297
// BR creates a line break element <br />.
9398
func BR(attrs Attrs) *Element {
94-
return &Element{Tag: "br", Attrs: attrs, Children: []templ.Component{}}
99+
return New("br", attrs)
95100
}
96101

97102
// Caption creates a caption element <caption></caption>.
98103
func Caption(attrs Attrs, children ...templ.Component) *Element {
99-
return &Element{Tag: "caption", Attrs: attrs, Children: children}
104+
return New("caption", attrs, children...)
100105
}
101106

102107
// DD creates a definition description element <dd></dd>.
103108
func DD(attrs Attrs, children ...templ.Component) *Element {
104-
return &Element{Tag: "dd", Attrs: attrs, Children: children}
109+
return New("dd", attrs, children...)
105110
}
106111

107112
// Details creates a details element <details></details>.
108113
func Details(attrs Attrs, children ...templ.Component) *Element {
109-
return &Element{Tag: "details", Attrs: attrs, Children: children}
114+
return New("details", attrs, children...)
110115
}
111116

112117
// Dialog creates a dialog element <dialog></dialog>.
113118
func Dialog(attrs Attrs, children ...templ.Component) *Element {
114-
return &Element{Tag: "dialog", Attrs: attrs, Children: children}
119+
return New("dialog", attrs, children...)
115120
}
116121

117122
// Div creates a div element <div></div>.
118123
func Div(attrs Attrs, children ...templ.Component) *Element {
119-
return &Element{Tag: "div", Attrs: attrs, Children: children}
124+
return New("div", attrs, children...)
120125
}
121126

122127
// DL creates a definition list element <dl></dl>.
123128
func DL(attrs Attrs, children ...templ.Component) *Element {
124-
return &Element{Tag: "dl", Attrs: attrs, Children: children}
129+
return New("dl", attrs, children...)
125130
}
126131

127132
// DT creates a definition term element <dt></dt>.
128133
func DT(attrs Attrs, children ...templ.Component) *Element {
129-
return &Element{Tag: "dt", Attrs: attrs, Children: children}
134+
return New("dt", attrs, children...)
130135
}
131136

132137
// Empty creates an empty element.
@@ -136,12 +141,12 @@ func Empty() *Element {
136141

137142
// Fieldset creates a fieldset element <fieldset></fieldset>.
138143
func Fieldset(attrs Attrs, children ...templ.Component) *Element {
139-
return &Element{Tag: "fieldset", Attrs: attrs, Children: children}
144+
return New("fieldset", attrs, children...)
140145
}
141146

142147
// Form creates a form element <form></form>.
143148
func Form(attrs Attrs, children ...templ.Component) *Element {
144-
return &Element{Tag: "form", Attrs: attrs, Children: children}
149+
return New("form", attrs, children...)
145150
}
146151

147152
// Fragment allows you to create a fragment of elements without a parent element.
@@ -151,117 +156,117 @@ func Fragment(children ...templ.Component) *Element {
151156

152157
// H1 creates a heading level 1 element <h1></h1>.
153158
func H1(attrs Attrs, children ...templ.Component) *Element {
154-
return &Element{Tag: "h1", Attrs: attrs, Children: children}
159+
return New("h1", attrs, children...)
155160
}
156161

157162
// H2 creates a heading level 2 element <h2></h2>.
158163
func H2(attrs Attrs, children ...templ.Component) *Element {
159-
return &Element{Tag: "h2", Attrs: attrs, Children: children}
164+
return New("h2", attrs, children...)
160165
}
161166

162167
// H3 creates a heading level 3 element <h3></h3>.
163168
func H3(attrs Attrs, children ...templ.Component) *Element {
164-
return &Element{Tag: "h3", Attrs: attrs, Children: children}
169+
return New("h3", attrs, children...)
165170
}
166171

167172
// H4 creates a heading level 4 element <h4></h4>.
168173
func H4(attrs Attrs, children ...templ.Component) *Element {
169-
return &Element{Tag: "h4", Attrs: attrs, Children: children}
174+
return New("h4", attrs, children...)
170175
}
171176

172177
// H5 creates a heading level 5 element <h5></h5>.
173178
func H5(attrs Attrs, children ...templ.Component) *Element {
174-
return &Element{Tag: "h5", Attrs: attrs, Children: children}
179+
return New("h5", attrs, children...)
175180
}
176181

177182
// Head creates a head element <head></head>.
178183
func Head(attrs Attrs, children ...templ.Component) *Element {
179-
return &Element{Tag: "head", Attrs: attrs, Children: children}
184+
return New("head", attrs, children...)
180185
}
181186

182187
// Header creates a header element <header></header>.
183188
func Header(attrs Attrs, children ...templ.Component) *Element {
184-
return &Element{Tag: "header", Attrs: attrs, Children: children}
189+
return New("header", attrs, children...)
185190
}
186191

187192
// HR creates a horizontal rule element <hr />.
188193
func HR(attrs Attrs) *Element {
189-
return &Element{Tag: "hr", Attrs: attrs, Children: []templ.Component{}}
194+
return New("hr", attrs)
190195
}
191196

192197
// Html creates an html element <html></html>.
193198
func Html(attrs Attrs, children ...templ.Component) *Element {
194-
return &Element{Tag: "html", Attrs: attrs, Children: children}
199+
return New("html", attrs, children...)
195200
}
196201

197202
// I creates an italic element <i></i>.
198203
func I(attrs Attrs, children ...templ.Component) *Element {
199-
return &Element{Tag: "i", Attrs: attrs, Children: children}
204+
return New("i", attrs, children...)
200205
}
201206

202207
// Img creates an image element <img />.
203208
func Img(attrs Attrs) *Element {
204-
return &Element{Tag: "img", Attrs: attrs, Children: []templ.Component{}}
209+
return New("img", attrs)
205210
}
206211

207212
// Input creates an input element <input />.
208213
func Input(attrs Attrs) *Element {
209-
return &Element{Tag: "input", Attrs: attrs, Children: []templ.Component{}}
214+
return New("input", attrs)
210215
}
211216

212217
// Label creates a label element <label></label>.
213218
func Label(attrs Attrs, children ...templ.Component) *Element {
214-
return &Element{Tag: "label", Attrs: attrs, Children: children}
219+
return New("label", attrs, children...)
215220
}
216221

217222
// Legend creates a legend element <legend></legend>.
218223
func Legend(attrs Attrs, children ...templ.Component) *Element {
219-
return &Element{Tag: "legend", Attrs: attrs, Children: children}
224+
return New("legend", attrs, children...)
220225
}
221226

222227
// LI creates a list item element <li></li>.
223228
func LI(attrs Attrs, children ...templ.Component) *Element {
224-
return &Element{Tag: "li", Attrs: attrs, Children: children}
229+
return New("li", attrs, children...)
225230
}
226231

227232
// Link creates a link element <link />.
228233
func Link(attrs Attrs) *Element {
229-
return &Element{Tag: "link", Attrs: attrs, Children: []templ.Component{}}
234+
return New("link", attrs)
230235
}
231236

232237
// Main creates a main element <main></main>.
233238
func Main(attrs Attrs, children ...templ.Component) *Element {
234-
return &Element{Tag: "main", Attrs: attrs, Children: children}
239+
return New("main", attrs, children...)
235240
}
236241

237242
// Meta creates a meta element <meta />.
238243
func Meta(attrs Attrs) *Element {
239-
return &Element{Tag: "meta", Attrs: attrs, Children: []templ.Component{}}
244+
return New("meta", attrs)
240245
}
241246

242247
// Nav creates a nav element <nav></nav>.
243248
func Nav(attrs Attrs, children ...templ.Component) *Element {
244-
return &Element{Tag: "nav", Attrs: attrs, Children: children}
249+
return New("nav", attrs, children...)
245250
}
246251

247252
// OL creates an ordered list element <ol></ol>.
248253
func OL(attrs Attrs, children ...templ.Component) *Element {
249-
return &Element{Tag: "ol", Attrs: attrs, Children: children}
254+
return New("ol", attrs, children...)
250255
}
251256

252257
// Optgroup creates an option group element <optgroup></optgroup>.
253258
func Optgroup(attrs Attrs, children ...templ.Component) *Element {
254-
return &Element{Tag: "optgroup", Attrs: attrs, Children: children}
259+
return New("optgroup", attrs, children...)
255260
}
256261

257262
// Option creates an option element <option></option>.
258263
func Option(attrs Attrs, children ...templ.Component) *Element {
259-
return &Element{Tag: "option", Attrs: attrs, Children: children}
264+
return New("option", attrs, children...)
260265
}
261266

262267
// P creates a paragraph element <p></p>.
263268
func P(attrs Attrs, children ...templ.Component) *Element {
264-
return &Element{Tag: "p", Attrs: attrs, Children: children}
269+
return New("p", attrs, children...)
265270
}
266271

267272
// Raw creates an element with raw HTML content.
@@ -272,47 +277,47 @@ func Raw(html string) *Element {
272277

273278
// Section creates a section element <section></section>.
274279
func Section(attrs Attrs, children ...templ.Component) *Element {
275-
return &Element{Tag: "section", Attrs: attrs, Children: children}
280+
return New("section", attrs, children...)
276281
}
277282

278283
// Select creates a select element <select></select>.
279284
func Select(attrs Attrs, children ...templ.Component) *Element {
280-
return &Element{Tag: "select", Attrs: attrs, Children: children}
285+
return New("select", attrs, children...)
281286
}
282287

283288
// Small creates a small element <small></small>.
284289
func Small(attrs Attrs, children ...templ.Component) *Element {
285-
return &Element{Tag: "small", Attrs: attrs, Children: children}
290+
return New("small", attrs, children...)
286291
}
287292

288293
// Span creates a span element <span></span>.
289294
func Span(attrs Attrs, children ...templ.Component) *Element {
290-
return &Element{Tag: "span", Attrs: attrs, Children: children}
295+
return New("span", attrs, children...)
291296
}
292297

293298
// Strong creates a strong element <strong></strong>.
294299
func Strong(attrs Attrs, children ...templ.Component) *Element {
295-
return &Element{Tag: "strong", Attrs: attrs, Children: children}
300+
return New("strong", attrs, children...)
296301
}
297302

298303
// Summary creates a summary element <summary></summary>.
299304
func Summary(attrs Attrs, children ...templ.Component) *Element {
300-
return &Element{Tag: "summary", Attrs: attrs, Children: children}
305+
return New("summary", attrs, children...)
301306
}
302307

303308
// Table creates a table element <table></table>.
304309
func Table(attrs Attrs, children ...templ.Component) *Element {
305-
return &Element{Tag: "table", Attrs: attrs, Children: children}
310+
return New("table", attrs, children...)
306311
}
307312

308313
// TBody creates a table body element <tbody></tbody>.
309314
func TBody(attrs Attrs, children ...templ.Component) *Element {
310-
return &Element{Tag: "tbody", Attrs: attrs, Children: children}
315+
return New("tbody", attrs, children...)
311316
}
312317

313318
// TD creates a table data element <td></td>.
314319
func TD(attrs Attrs, children ...templ.Component) *Element {
315-
return &Element{Tag: "td", Attrs: attrs, Children: children}
320+
return New("td", attrs, children...)
316321
}
317322

318323
// Text creates an element with text content.
@@ -323,37 +328,37 @@ func Text(text string) *Element {
323328

324329
// Textarea creates a textarea element <textarea></textarea>.
325330
func Textarea(attrs Attrs, children ...templ.Component) *Element {
326-
return &Element{Tag: "textarea", Attrs: attrs, Children: children}
331+
return New("textarea", attrs, children...)
327332
}
328333

329334
// TFoot creates a table footer element <tfoot></tfoot>.
330335
func TFoot(attrs Attrs, children ...templ.Component) *Element {
331-
return &Element{Tag: "tfoot", Attrs: attrs, Children: children}
336+
return New("tfoot", attrs, children...)
332337
}
333338

334339
// TH creates a table header element <th></th>.
335340
func TH(attrs Attrs, children ...templ.Component) *Element {
336-
return &Element{Tag: "th", Attrs: attrs, Children: children}
341+
return New("th", attrs, children...)
337342
}
338343

339344
// THead creates a table header element <thead></thead>.
340345
func THead(attrs Attrs, children ...templ.Component) *Element {
341-
return &Element{Tag: "thead", Attrs: attrs, Children: children}
346+
return New("thead", attrs, children...)
342347
}
343348

344349
// Title creates a title element <title></title>.
345350
func Title(attrs Attrs, children ...templ.Component) *Element {
346-
return &Element{Tag: "title", Attrs: attrs, Children: children}
351+
return New("title", attrs, children...)
347352
}
348353

349354
// TR creates a table row element <tr></tr>.
350355
func TR(attrs Attrs, children ...templ.Component) *Element {
351-
return &Element{Tag: "tr", Attrs: attrs, Children: children}
356+
return New("tr", attrs, children...)
352357
}
353358

354359
// UL creates an unordered list element <ul></ul>.
355360
func UL(attrs Attrs, children ...templ.Component) *Element {
356-
return &Element{Tag: "ul", Attrs: attrs, Children: children}
361+
return New("ul", attrs, children...)
357362
}
358363

359364
// AddChildren adds children *Element's to this element.

element_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ func TestElementTags(t *testing.T) {
2424
nodeFunc func() *Element
2525
expected string
2626
}{
27+
{"New", func() *Element { return New("custom", NA) }, "custom"},
2728
{"A", func() *Element { return A(NA) }, "a"},
2829
{"Address", func() *Element { return Address(NA) }, "address"},
2930
{"Article", func() *Element { return Article(NA) }, "article"},
@@ -236,6 +237,17 @@ func TestRenderElementAddChildren(t *testing.T) {
236237
}
237238
}
238239

240+
// Test rendering a custom Element with a tag and text content.
241+
func TestRenderNewElement(t *testing.T) {
242+
node := New("custom", Attrs{"class": "example"}, Text("Hello, World!"))
243+
expected := `<custom class="example">Hello, World!</custom>`
244+
245+
output := renderElement(t, node)
246+
if output != expected {
247+
t.Errorf("Expected %q, got %q", expected, output)
248+
}
249+
}
250+
239251
// BenchmarkRender benchmarks the Render method of the Element struct.
240252
func BenchmarkRender(b *testing.B) {
241253
node := Div(Attrs{"class": "example"}, Text("Hello, World!"))

0 commit comments

Comments
 (0)