diff options
Diffstat (limited to 'attr')
-rw-r--r-- | attr/attributes.go | 38 | ||||
-rw-r--r-- | attr/attributes_test.go | 32 | ||||
-rw-r--r-- | attr/boolean.go | 45 | ||||
-rw-r--r-- | attr/boolean_test.go | 32 | ||||
-rw-r--r-- | attr/simple.go | 121 | ||||
-rw-r--r-- | attr/simple_test.go | 51 |
6 files changed, 0 insertions, 319 deletions
diff --git a/attr/attributes.go b/attr/attributes.go deleted file mode 100644 index 938d762..0000000 --- a/attr/attributes.go +++ /dev/null @@ -1,38 +0,0 @@ -// Package attr provides shortcuts and helpers to common HTML attributes. -// See https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes for a list of attributes. -package attr - -import ( - "io" - "sort" - "strings" - - g "github.com/maragudk/gomponents" -) - -// Classes is a map of strings to booleans, which Renders to an attribute with name "class". -// The attribute value is a sorted, space-separated string of all the map keys, -// for which the corresponding map value is true. -type Classes map[string]bool - -func (c Classes) Render(w io.Writer) error { - var included []string - for c, include := range c { - if include { - included = append(included, c) - } - } - sort.Strings(included) - return g.Attr("class", strings.Join(included, " ")).Render(w) -} - -func (c Classes) Type() g.NodeType { - return g.AttributeType -} - -// String satisfies fmt.Stringer. -func (c Classes) String() string { - var b strings.Builder - _ = c.Render(&b) - return b.String() -} diff --git a/attr/attributes_test.go b/attr/attributes_test.go deleted file mode 100644 index da04a20..0000000 --- a/attr/attributes_test.go +++ /dev/null @@ -1,32 +0,0 @@ -package attr_test - -import ( - "testing" - - g "github.com/maragudk/gomponents" - "github.com/maragudk/gomponents/assert" - "github.com/maragudk/gomponents/attr" -) - -func TestClasses(t *testing.T) { - t.Run("given a map, returns sorted keys from the map with value true", func(t *testing.T) { - assert.Equal(t, ` class="boheme-hat hat partyhat"`, attr.Classes{ - "boheme-hat": true, - "hat": true, - "partyhat": true, - "turtlehat": false, - }) - }) - - t.Run("renders as attribute in an element", func(t *testing.T) { - e := g.El("div", attr.Classes{"hat": true}) - assert.Equal(t, `<div class="hat"></div>`, e) - }) - - t.Run("also works with fmt", func(t *testing.T) { - a := attr.Classes{"hat": true} - if a.String() != ` class="hat"` { - t.FailNow() - } - }) -} diff --git a/attr/boolean.go b/attr/boolean.go deleted file mode 100644 index 2a70c9d..0000000 --- a/attr/boolean.go +++ /dev/null @@ -1,45 +0,0 @@ -package attr - -import ( - g "github.com/maragudk/gomponents" -) - -func Async() g.Node { - return g.Attr("async") -} - -func AutoFocus() g.Node { - return g.Attr("autofocus") -} - -func AutoPlay() g.Node { - return g.Attr("autoplay") -} - -func Controls() g.Node { - return g.Attr("controls") -} - -func Defer() g.Node { - return g.Attr("defer") -} - -func Disabled() g.Node { - return g.Attr("disabled") -} - -func Multiple() g.Node { - return g.Attr("multiple") -} - -func ReadOnly() g.Node { - return g.Attr("readonly") -} - -func Required() g.Node { - return g.Attr("required") -} - -func Selected() g.Node { - return g.Attr("selected") -} diff --git a/attr/boolean_test.go b/attr/boolean_test.go deleted file mode 100644 index 309b025..0000000 --- a/attr/boolean_test.go +++ /dev/null @@ -1,32 +0,0 @@ -package attr_test - -import ( - "fmt" - "testing" - - g "github.com/maragudk/gomponents" - "github.com/maragudk/gomponents/assert" - "github.com/maragudk/gomponents/attr" -) - -func TestBooleanAttributes(t *testing.T) { - cases := map[string]func() g.Node{ - "async": attr.Async, - "autofocus": attr.AutoFocus, - "autoplay": attr.AutoPlay, - "controls": attr.Controls, - "defer": attr.Defer, - "disabled": attr.Disabled, - "multiple": attr.Multiple, - "readonly": attr.ReadOnly, - "required": attr.Required, - "selected": attr.Selected, - } - - 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) - }) - } -} diff --git a/attr/simple.go b/attr/simple.go deleted file mode 100644 index a96c139..0000000 --- a/attr/simple.go +++ /dev/null @@ -1,121 +0,0 @@ -package attr - -import ( - g "github.com/maragudk/gomponents" -) - -func Accept(v string) g.Node { - return g.Attr("accept", v) -} - -func AutoComplete(v string) g.Node { - return g.Attr("autocomplete", v) -} - -func Charset(v string) g.Node { - return g.Attr("charset", v) -} - -func Class(v string) g.Node { - return g.Attr("class", v) -} - -func Cols(v string) g.Node { - return g.Attr("cols", v) -} - -func Content(v string) g.Node { - return g.Attr("content", v) -} - -func Form(v string) g.Node { - return g.Attr("form", v) -} - -func Height(v string) g.Node { - return g.Attr("height", v) -} - -func Href(v string) g.Node { - return g.Attr("href", v) -} - -func ID(v string) g.Node { - return g.Attr("id", v) -} - -func Lang(v string) g.Node { - return g.Attr("lang", v) -} - -func Max(v string) g.Node { - return g.Attr("max", v) -} - -func MaxLength(v string) g.Node { - return g.Attr("maxlength", v) -} - -func Min(v string) g.Node { - return g.Attr("min", v) -} - -func MinLength(v string) g.Node { - return g.Attr("minlength", v) -} - -func Name(v string) g.Node { - return g.Attr("name", v) -} - -func Pattern(v string) g.Node { - return g.Attr("pattern", v) -} - -func Preload(v string) g.Node { - return g.Attr("preload", v) -} - -func Placeholder(v string) g.Node { - return g.Attr("placeholder", v) -} - -func Rel(v string) g.Node { - return g.Attr("rel", v) -} - -func Rows(v string) g.Node { - return g.Attr("rows", v) -} - -func Src(v string) g.Node { - return g.Attr("src", v) -} - -func Style(v string) g.Node { - return g.Attr("style", v) -} - -func TabIndex(v string) g.Node { - return g.Attr("tabindex", v) -} - -func Target(v string) g.Node { - return g.Attr("target", v) -} - -func Title(v string) g.Node { - return g.Attr("title", v) -} - -func Type(v string) g.Node { - return g.Attr("type", v) -} - -func Value(v string) g.Node { - return g.Attr("value", v) -} - -func Width(v string) g.Node { - return g.Attr("width", v) -} diff --git a/attr/simple_test.go b/attr/simple_test.go deleted file mode 100644 index d006b76..0000000 --- a/attr/simple_test.go +++ /dev/null @@ -1,51 +0,0 @@ -package attr_test - -import ( - "fmt" - "testing" - - g "github.com/maragudk/gomponents" - "github.com/maragudk/gomponents/assert" - "github.com/maragudk/gomponents/attr" -) - -func TestSimpleAttributes(t *testing.T) { - cases := map[string]func(string) g.Node{ - "accept": attr.Accept, - "autocomplete": attr.AutoComplete, - "charset": attr.Charset, - "class": attr.Class, - "cols": attr.Cols, - "content": attr.Content, - "form": attr.Form, - "height": attr.Height, - "href": attr.Href, - "id": attr.ID, - "lang": attr.Lang, - "max": attr.Max, - "maxlength": attr.MaxLength, - "min": attr.Min, - "minlength": attr.MinLength, - "name": attr.Name, - "pattern": attr.Pattern, - "preload": attr.Preload, - "placeholder": attr.Placeholder, - "rel": attr.Rel, - "rows": attr.Rows, - "src": attr.Src, - "style": attr.Style, - "tabindex": attr.TabIndex, - "target": attr.Target, - "title": attr.Title, - "type": attr.Type, - "value": attr.Value, - "width": attr.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) - }) - } -} |