1
0

Compare commits

..

10 Commits

Author SHA1 Message Date
258100b833 Add XMLEl function 2023-12-26 20:39:00 -08:00
Markus Wüstenberg
d81de8319f
Test Go 1.21 in CI (#147) 2023-09-08 11:03:47 +02:00
Markus Wüstenberg
214138645b Test Go 1.21 in CI 2023-09-08 11:01:06 +02:00
Markus Wüstenberg
a9890f5337
Add "checked" bool attribute (#136)
Fixes #127.
2023-05-11 11:23:18 +02:00
Markus Wüstenberg
4248e85def Add "checked" bool attribute
Fixes #127.
2023-05-11 11:22:01 +02:00
Markus Wüstenberg
4f9709afcc
Document Text/Textf helpers better (#135)
Both in the readme and package doc.

Fixes #133.
2023-05-11 10:08:27 +02:00
Markus Wüstenberg
c129ae8da1 Document Text/Textf helpers better
Both in the readme and package doc.

Fixes #133.
2023-05-11 10:06:23 +02:00
Markus Wüstenberg
b638b8b078
Test with Go 1.20 in CI (#134)
Fixes #129.
2023-05-04 09:49:50 +02:00
Markus Wüstenberg
f26f56cc56 Make version numbers strings in yaml 2023-05-04 09:48:27 +02:00
Markus Wüstenberg
b1f7754ccd Test with Go 1.20 in CI 2023-05-04 09:46:20 +02:00
5 changed files with 41 additions and 8 deletions

View File

@ -20,10 +20,12 @@ jobs:
strategy: strategy:
matrix: matrix:
go: go:
- 1.16 - "1.16"
- 1.17 - "1.17"
- 1.18 - "1.18"
- 1.19 - "1.19"
- "1.20"
- "1.21"
steps: steps:
- name: Checkout - name: Checkout

View File

@ -25,14 +25,16 @@ Made in 🇩🇰 by [maragu](https://www.maragu.dk), maker of [online Go courses
- Auto-completion - Auto-completion
- Nice formatting with `gofmt` - Nice formatting with `gofmt`
- Simple API that's easy to learn and use (you know most already if you know HTML) - Simple API that's easy to learn and use (you know most already if you know HTML)
- Useful helpers like `Text` and `Textf` that insert HTML-escaped text, `Map` for mapping data to components,
and `If` for conditional rendering.
- No external dependencies - No external dependencies
## Usage ## Usage
Get the library using `go get`: Get the library using `go get`:
```shell script ```shell
go get -u github.com/maragudk/gomponents go get github.com/maragudk/gomponents
``` ```
The preferred way to use gomponents is with so-called dot-imports (note the dot before the `gomponents/html` import), The preferred way to use gomponents is with so-called dot-imports (note the dot before the `gomponents/html` import),

View File

@ -4,8 +4,8 @@
// to the given writer as a string. // to the given writer as a string.
// //
// All DOM elements and attributes can be created by using the El and Attr functions. // All DOM elements and attributes can be created by using the El and Attr functions.
// The functions Text, Textf, Raw, and Rawf can be used to create text nodes. // The functions Text, Textf, Raw, and Rawf can be used to create text nodes, either HTML-escaped or unescaped.
// See also helper functions Group, Map, and If. // See also helper functions Group, Map, and If for mapping data to Nodes and inserting them conditionally.
// //
// For basic HTML elements and attributes, see the package html. // For basic HTML elements and attributes, see the package html.
// For higher-level HTML components, see the package components. // For higher-level HTML components, see the package components.
@ -92,6 +92,30 @@ func El(name string, children ...Node) Node {
}) })
} }
// XMLEl behaves identically to El with the exception that checking for void
// elements is disabled. This allows use of elements such as "link" which need
// to have children in RSS feeds, for example.
func XMLEl(name string, children ...Node) Node {
return NodeFunc(func(w2 io.Writer) error {
w := &statefulWriter{w: w2}
w.Write([]byte("<" + name))
for _, c := range children {
renderChild(w, c, AttributeType)
}
w.Write([]byte(">"))
for _, c := range children {
renderChild(w, c, ElementType)
}
w.Write([]byte("</" + name + ">"))
return w.err
})
}
// renderChild c to the given writer w if the node type is t. // renderChild c to the given writer w if the node type is t.
func renderChild(w *statefulWriter, c Node, t NodeType) { func renderChild(w *statefulWriter, c Node, t NodeType) {
if w.err != nil || c == nil { if w.err != nil || c == nil {

View File

@ -16,6 +16,10 @@ func AutoPlay() g.Node {
return g.Attr("autoplay") return g.Attr("autoplay")
} }
func Checked() g.Node {
return g.Attr("checked")
}
func Controls() g.Node { func Controls() g.Node {
return g.Attr("controls") return g.Attr("controls")
} }

View File

@ -14,6 +14,7 @@ func TestBooleanAttributes(t *testing.T) {
"async": Async, "async": Async,
"autofocus": AutoFocus, "autofocus": AutoFocus,
"autoplay": AutoPlay, "autoplay": AutoPlay,
"checked": Checked,
"controls": Controls, "controls": Controls,
"defer": Defer, "defer": Defer,
"disabled": Disabled, "disabled": Disabled,