2020-12-10 04:00:23 -08:00
|
|
|
package html_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
|
|
|
type erroringWriter struct{}
|
|
|
|
|
|
|
|
func (w *erroringWriter) Write(p []byte) (n int, err error) {
|
|
|
|
return 0, errors.New("don't want to write")
|
|
|
|
}
|
|
|
|
|
2020-12-10 04:13:10 -08:00
|
|
|
func TestDoctype(t *testing.T) {
|
2020-12-10 04:00:23 -08:00
|
|
|
t.Run("returns doctype and children", func(t *testing.T) {
|
2020-12-10 04:13:10 -08:00
|
|
|
assert.Equal(t, `<!doctype html><html></html>`, Doctype(g.El("html")))
|
2020-12-10 04:00:23 -08:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("errors on write error in Render", func(t *testing.T) {
|
2020-12-10 04:13:10 -08:00
|
|
|
err := Doctype(g.El("html")).Render(&erroringWriter{})
|
2020-12-10 04:00:23 -08:00
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSimpleElements(t *testing.T) {
|
2021-01-07 01:20:03 -08:00
|
|
|
cases := map[string]func(...g.Node) g.Node{
|
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
|
|
|
"a": A,
|
2021-05-05 00:03:16 -07:00
|
|
|
"abbr": Abbr,
|
2020-12-10 04:00:23 -08:00
|
|
|
"address": Address,
|
|
|
|
"article": Article,
|
|
|
|
"aside": Aside,
|
|
|
|
"audio": Audio,
|
2021-05-05 00:03:16 -07:00
|
|
|
"b": B,
|
2020-12-10 04:00:23 -08:00
|
|
|
"blockquote": BlockQuote,
|
|
|
|
"body": Body,
|
|
|
|
"button": Button,
|
|
|
|
"canvas": Canvas,
|
2021-05-05 00:03:16 -07:00
|
|
|
"caption": Caption,
|
2020-12-10 04:00:23 -08:00
|
|
|
"cite": Cite,
|
|
|
|
"code": Code,
|
|
|
|
"colgroup": ColGroup,
|
2020-12-22 02:46:49 -08:00
|
|
|
"data": DataEl,
|
2020-12-10 04:00:23 -08:00
|
|
|
"datalist": DataList,
|
2021-05-05 00:03:16 -07:00
|
|
|
"dd": Dd,
|
|
|
|
"del": Del,
|
2020-12-10 04:00:23 -08:00
|
|
|
"details": Details,
|
2021-05-05 00:03:16 -07:00
|
|
|
"dfn": Dfn,
|
2020-12-10 04:00:23 -08:00
|
|
|
"dialog": Dialog,
|
|
|
|
"div": Div,
|
|
|
|
"dl": Dl,
|
2021-05-05 00:03:16 -07:00
|
|
|
"dt": Dt,
|
|
|
|
"em": Em,
|
2020-12-10 04:00:23 -08:00
|
|
|
"fieldset": FieldSet,
|
2021-05-05 00:03:16 -07:00
|
|
|
"figcaption": FigCaption,
|
2020-12-10 04:00:23 -08:00
|
|
|
"figure": Figure,
|
|
|
|
"footer": Footer,
|
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
|
|
|
"form": FormEl,
|
2021-05-05 00:03:16 -07:00
|
|
|
"h1": H1,
|
|
|
|
"h2": H2,
|
|
|
|
"h3": H3,
|
|
|
|
"h4": H4,
|
|
|
|
"h5": H5,
|
|
|
|
"h6": H6,
|
2020-12-10 04:00:23 -08:00
|
|
|
"head": Head,
|
|
|
|
"header": Header,
|
|
|
|
"hgroup": HGroup,
|
|
|
|
"html": HTML,
|
2021-05-05 00:03:16 -07:00
|
|
|
"i": I,
|
2020-12-10 04:00:23 -08:00
|
|
|
"iframe": IFrame,
|
2021-05-05 00:03:16 -07:00
|
|
|
"ins": Ins,
|
|
|
|
"kbd": Kbd,
|
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
|
|
|
"label": Label,
|
2020-12-10 04:00:23 -08:00
|
|
|
"legend": Legend,
|
|
|
|
"li": Li,
|
|
|
|
"main": Main,
|
2021-05-05 00:03:16 -07:00
|
|
|
"mark": Mark,
|
2020-12-10 04:00:23 -08:00
|
|
|
"menu": Menu,
|
|
|
|
"meter": Meter,
|
|
|
|
"nav": Nav,
|
|
|
|
"noscript": NoScript,
|
|
|
|
"object": Object,
|
|
|
|
"ol": Ol,
|
|
|
|
"optgroup": OptGroup,
|
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
|
|
|
"option": Option,
|
2020-12-10 04:00:23 -08:00
|
|
|
"p": P,
|
|
|
|
"picture": Picture,
|
|
|
|
"pre": Pre,
|
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
|
|
|
"progress": Progress,
|
2021-05-05 00:03:16 -07:00
|
|
|
"q": Q,
|
|
|
|
"s": S,
|
|
|
|
"samp": Samp,
|
2020-12-10 04:00:23 -08:00
|
|
|
"script": Script,
|
|
|
|
"section": Section,
|
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
|
|
|
"select": Select,
|
2021-05-05 00:03:16 -07:00
|
|
|
"small": Small,
|
2020-12-10 04:00:23 -08:00
|
|
|
"span": Span,
|
2021-05-05 00:03:16 -07:00
|
|
|
"strong": Strong,
|
2020-12-10 04:00:23 -08:00
|
|
|
"style": StyleEl,
|
2021-05-05 00:03:16 -07:00
|
|
|
"sub": Sub,
|
2020-12-10 04:00:23 -08:00
|
|
|
"summary": Summary,
|
2021-05-05 00:03:16 -07:00
|
|
|
"sup": Sup,
|
2020-12-10 04:00:23 -08:00
|
|
|
"svg": SVG,
|
|
|
|
"table": Table,
|
|
|
|
"tbody": TBody,
|
|
|
|
"td": Td,
|
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
|
|
|
"textarea": Textarea,
|
2020-12-10 04:00:23 -08:00
|
|
|
"tfoot": TFoot,
|
|
|
|
"th": Th,
|
|
|
|
"thead": THead,
|
2021-05-05 00:03:16 -07:00
|
|
|
"time": Time,
|
|
|
|
"title": TitleEl,
|
2020-12-10 04:00:23 -08:00
|
|
|
"tr": Tr,
|
2021-05-05 00:03:16 -07:00
|
|
|
"u": U,
|
2020-12-10 04:00:23 -08:00
|
|
|
"ul": Ul,
|
2021-05-05 00:03:16 -07:00
|
|
|
"var": Var,
|
2021-06-18 00:39:47 -07:00
|
|
|
"video": Video,
|
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 := fn(g.Attr("id", "hat"))
|
|
|
|
assert.Equal(t, fmt.Sprintf(`<%v id="hat"></%v>`, name, name), n)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSimpleVoidKindElements(t *testing.T) {
|
2021-01-07 01:20:03 -08:00
|
|
|
cases := map[string]func(...g.Node) g.Node{
|
2020-12-10 04:00:23 -08:00
|
|
|
"area": Area,
|
|
|
|
"base": Base,
|
|
|
|
"br": Br,
|
|
|
|
"col": Col,
|
|
|
|
"embed": Embed,
|
|
|
|
"hr": Hr,
|
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
|
|
|
"img": Img,
|
|
|
|
"input": Input,
|
2020-12-10 04:00:23 -08:00
|
|
|
"link": Link,
|
|
|
|
"meta": Meta,
|
|
|
|
"param": Param,
|
|
|
|
"source": Source,
|
|
|
|
"wbr": Wbr,
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, fn := range cases {
|
|
|
|
t.Run(fmt.Sprintf("should output %v", name), func(t *testing.T) {
|
|
|
|
n := fn(g.Attr("id", "hat"))
|
|
|
|
assert.Equal(t, fmt.Sprintf(`<%v id="hat">`, name), n)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|