2020-12-10 04:00:23 -08:00
|
|
|
package html_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
g "github.com/maragudk/gomponents"
|
|
|
|
. "github.com/maragudk/gomponents/html"
|
2021-10-06 11:49:43 -07:00
|
|
|
"github.com/maragudk/gomponents/internal/assert"
|
2020-12-10 04:00:23 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBooleanAttributes(t *testing.T) {
|
|
|
|
cases := map[string]func() g.Node{
|
2021-06-18 00:39:47 -07:00
|
|
|
"async": Async,
|
|
|
|
"autofocus": AutoFocus,
|
|
|
|
"autoplay": AutoPlay,
|
2023-05-11 02:22:01 -07:00
|
|
|
"checked": Checked,
|
2021-06-18 00:39:47 -07:00
|
|
|
"controls": Controls,
|
|
|
|
"defer": Defer,
|
|
|
|
"disabled": Disabled,
|
|
|
|
"loop": Loop,
|
|
|
|
"multiple": Multiple,
|
|
|
|
"muted": Muted,
|
|
|
|
"playsinline": PlaysInline,
|
|
|
|
"readonly": ReadOnly,
|
|
|
|
"required": Required,
|
|
|
|
"selected": Selected,
|
2020-12-10 04:00:23 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
for name, fn := range cases {
|
|
|
|
t.Run(fmt.Sprintf("should output %v", name), func(t *testing.T) {
|
|
|
|
n := g.El("div", fn())
|
|
|
|
assert.Equal(t, fmt.Sprintf(`<div %v></div>`, name), n)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSimpleAttributes(t *testing.T) {
|
|
|
|
cases := map[string]func(string) g.Node{
|
|
|
|
"accept": Accept,
|
Simplify available elements (#55)
`a`, `form`, `img`, `input`, `label`, `option`, `progress`, `select`, and `textarea` are now just regular elements (without helper parameters), because:
- Sometimes the use case doesn't fit (`a` as anchor without href, for example)
- There's no reason these are special among the others, so streamlining them makes sense
Also added new attributes `action`, `alt`, `for`, `method` that I had somehow missed.
2020-12-10 05:20:33 -08:00
|
|
|
"action": Action,
|
|
|
|
"alt": Alt,
|
2021-06-08 07:52:57 -07:00
|
|
|
"as": As,
|
2020-12-10 04:00:23 -08:00
|
|
|
"autocomplete": AutoComplete,
|
|
|
|
"charset": Charset,
|
|
|
|
"class": Class,
|
|
|
|
"cols": Cols,
|
2023-01-17 04:00:16 -08:00
|
|
|
"colspan": ColSpan,
|
2020-12-10 04:00:23 -08:00
|
|
|
"content": Content,
|
2022-09-29 03:46:36 -07:00
|
|
|
"enctype": EncType,
|
Simplify available elements (#55)
`a`, `form`, `img`, `input`, `label`, `option`, `progress`, `select`, and `textarea` are now just regular elements (without helper parameters), because:
- Sometimes the use case doesn't fit (`a` as anchor without href, for example)
- There's no reason these are special among the others, so streamlining them makes sense
Also added new attributes `action`, `alt`, `for`, `method` that I had somehow missed.
2020-12-10 05:20:33 -08:00
|
|
|
"for": For,
|
2020-12-10 04:00:23 -08:00
|
|
|
"form": FormAttr,
|
|
|
|
"height": Height,
|
|
|
|
"href": Href,
|
|
|
|
"id": ID,
|
|
|
|
"lang": Lang,
|
2021-09-07 03:09:57 -07:00
|
|
|
"loading": Loading,
|
2020-12-10 04:00:23 -08:00
|
|
|
"max": Max,
|
|
|
|
"maxlength": MaxLength,
|
Simplify available elements (#55)
`a`, `form`, `img`, `input`, `label`, `option`, `progress`, `select`, and `textarea` are now just regular elements (without helper parameters), because:
- Sometimes the use case doesn't fit (`a` as anchor without href, for example)
- There's no reason these are special among the others, so streamlining them makes sense
Also added new attributes `action`, `alt`, `for`, `method` that I had somehow missed.
2020-12-10 05:20:33 -08:00
|
|
|
"method": Method,
|
2020-12-10 04:00:23 -08:00
|
|
|
"min": Min,
|
|
|
|
"minlength": MinLength,
|
|
|
|
"name": Name,
|
|
|
|
"pattern": Pattern,
|
|
|
|
"placeholder": Placeholder,
|
2021-06-18 00:39:47 -07:00
|
|
|
"poster": Poster,
|
|
|
|
"preload": Preload,
|
2020-12-10 04:00:23 -08:00
|
|
|
"rel": Rel,
|
2020-12-22 02:07:33 -08:00
|
|
|
"role": Role,
|
2020-12-10 04:00:23 -08:00
|
|
|
"rows": Rows,
|
2023-01-17 04:00:16 -08:00
|
|
|
"rowspan": RowSpan,
|
2020-12-10 04:00:23 -08:00
|
|
|
"src": Src,
|
2021-09-07 03:09:57 -07:00
|
|
|
"srcset": SrcSet,
|
2023-01-17 04:05:47 -08:00
|
|
|
"step": Step,
|
2020-12-10 04:00:23 -08:00
|
|
|
"style": StyleAttr,
|
|
|
|
"tabindex": TabIndex,
|
|
|
|
"target": Target,
|
|
|
|
"title": TitleAttr,
|
|
|
|
"type": Type,
|
|
|
|
"value": Value,
|
|
|
|
"width": Width,
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, fn := range cases {
|
|
|
|
t.Run(fmt.Sprintf(`should output %v="hat"`, name), func(t *testing.T) {
|
|
|
|
n := g.El("div", fn("hat"))
|
|
|
|
assert.Equal(t, fmt.Sprintf(`<div %v="hat"></div>`, name), n)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-12-22 02:07:33 -08:00
|
|
|
|
|
|
|
func TestAria(t *testing.T) {
|
|
|
|
t.Run("returns an attribute which name is prefixed with aria-", func(t *testing.T) {
|
|
|
|
n := Aria("selected", "true")
|
|
|
|
assert.Equal(t, ` aria-selected="true"`, n)
|
|
|
|
})
|
|
|
|
}
|
2020-12-22 02:46:49 -08:00
|
|
|
|
|
|
|
func TestDataAttr(t *testing.T) {
|
|
|
|
t.Run("returns an attribute which name is prefixed with data-", func(t *testing.T) {
|
|
|
|
n := DataAttr("id", "partyhat")
|
|
|
|
assert.Equal(t, ` data-id="partyhat"`, n)
|
|
|
|
})
|
|
|
|
}
|