Playground
gnomod.toml
module = "gno.land/p/jeronimoalbi/mdform/v0" gno = "0.9" [addpkg] creator = "g1edq4dugw0sgat4zxcw9xardvuydqf6cgleuc8p"
mdform.gno
package mdform
import (
"html"
"strings"
)
const (
InputTypeText = "text"
InputTypeNumber = "number"
InputTypeEmail = "email"
InputTypePhone = "tel"
InputTypePassword = "password"
InputTypeRadio = "radio"
InputTypeCheckbox = "checkbox"
)
var (
formAttributes = []string{"exec", "path"}
inputAttributes = []string{"checked", "description", "placeholder", "readonly", "required", "type", "value"}
textareaAttributes = []string{"placeholder", "readonly", "required", "rows", "value"}
selectAttributes = []string{"description", "readonly", "required", "selected"}
)
// New creates a new form.
func New(attributes ...string) *Form {
assertEvenAttributes(attributes)
form := &Form{}
for i := 0; i < len(attributes); i += 2 {
name, value := attributes[i], attributes[i+1]
assertIsValidAttribute(name, formAttributes)
form.attrs = append(form.attrs, formatAttribute(name, value))
}
return form
}
// Form is a form that can be rendered to Gno-Flavored Markdown.
type Form struct {
attrs []string
fields []string
}
// Input appends a new input to form fields.
// Use `Form.Radio()` or `Form.Checkbox()` to append those types of inputs to the form.
// Method panics when appending inputs of type radio or checkbox, or when attributes are not valid.
func (f *Form) Input(name string, attributes ...string) *Form {
name = strings.TrimSpace(name)
if name == "" {
panic("form input name is required")
}
assertEvenAttributes(attributes)
attrs := []string{formatAttribute("name", name)}
for i := 0; i < len(attributes); i += 2 {
name, value := attributes[i], attributes[i+1]
if name == "type" {
switch value {
case InputTypeRadio:
panic("use form.Radio() to create inputs of type radio")
case InputTypeCheckbox:
panic("use form.Checkbox() to create inputs of type checkbox")
}
}
assertIsValidAttribute(name, inputAttributes)
attrs = append(attrs, formatAttribute(name, value))
}
f.fields = append(f.fields, "<gno-input "+strings.Join(attrs, " ")+" />")
return f
}
// Radio appends a new input of type radio to form fields.
// Method panics when attributes are not valid.
func (f *Form) Radio(name, value string, attributes ...string) *Form {
return f.appendInputType(InputTypeRadio, name, value, attributes...)
}
// Checkbox appends a new input of type checkbox to form fields.
// Method panics when attributes are not valid.
func (f *Form) Checkbox(name, value string, attributes ...string) *Form {
return f.appendInputType(InputTypeCheckbox, name, value, attributes...)
}
// Textarea appends a new textarea to form fields.
// Method panics when attributes are not valid.
func (f *Form) Textarea(name string, attributes ...string) *Form {
name = strings.TrimSpace(name)
if name == "" {
panic("form textarea name is required")
}
assertEvenAttributes(attributes)
attrs := []string{formatAttribute("name", name)}
for i := 0; i < len(attributes); i += 2 {
name, value := attributes[i], attributes[i+1]
assertIsValidAttribute(name, textareaAttributes)
attrs = append(attrs, formatAttribute(name, value))
}
f.fields = append(f.fields, "<gno-textarea "+strings.Join(attrs, " ")+" />")
return f
}
// Select appends a new select to form fields.
// Method panics when attributes are not valid.
func (f *Form) Select(name, value string, attributes ...string) *Form {
name = strings.TrimSpace(name)
if name == "" {
panic("form select name is required")
}
assertEvenAttributes(attributes)
attrs := []string{
formatAttribute("name", name),
formatAttribute("value", value),
}
for i := 0; i < len(attributes); i += 2 {
name, value := attributes[i], attributes[i+1]
assertIsValidAttribute(name, selectAttributes)
attrs = append(attrs, formatAttribute(name, value))
}
f.fields = append(f.fields, "<gno-select "+strings.Join(attrs, " ")+" />")
return f
}
// String returns the form as Gno-Flavored Markdown.
func (f Form) String() string {
fields := strings.Join(f.fields, "\n")
attrs := strings.Join(f.attrs, " ")
if len(attrs) > 0 {
attrs = " " + attrs
}
return "<gno-form" + attrs + ">\n" + fields + "\n</gno-form>\n"
}
func (f *Form) appendInputType(typeName, name, value string, attributes ...string) *Form {
name = strings.TrimSpace(name)
if name == "" {
panic("form " + typeName + " input name is required")
}
assertEvenAttributes(attributes)
attrs := []string{
formatAttribute("type", typeName),
formatAttribute("name", name),
formatAttribute("value", value),
}
for i := 0; i < len(attributes); i += 2 {
name, value := attributes[i], attributes[i+1]
if name == "type" || name == "value" {
continue
}
assertIsValidAttribute(name, inputAttributes)
attrs = append(attrs, formatAttribute(name, value))
}
f.fields = append(f.fields, "<gno-input "+strings.Join(attrs, " ")+" />")
return f
}
func formatAttribute(name, value string) string {
return name + `="` + html.EscapeString(value) + `"`
}
func assertEvenAttributes(attrs []string) {
if len(attrs)%2 != 0 {
panic("expected an even number of attribute arguments")
}
}
func assertIsValidAttribute(attr string, attrs []string) {
for _, name := range attrs {
if name == attr {
return
}
}
panic("invalid attribute: " + attr)
}
mdform_test.gno
package mdform_test
import (
"strings"
"testing"
"gno.land/p/jeronimoalbi/mdform/v0"
"gno.land/p/nt/uassert/v0"
"gno.land/p/nt/urequire/v0"
)
// cur is a zero-value realm used as a placeholder when forwarding to
// uassert/urequire dispatch helpers that gained an `rlm realm` param.
// These tests pass `func()` callbacks (no crossing inside the callback),
// so rlm is ignored — a nil realm here is safe.
var cur realm
func TestNew(cur realm, t *testing.T) {
tests := []struct {
name string
attrs []string
markdown string
err string
}{
{
name: "ok",
attrs: []string{"exec", "FunctionName"},
markdown: `<gno-form exec="FunctionName"></gno-form>`,
},
{
name: "no attributes",
markdown: `<gno-form></gno-form>`,
},
{
name: "uneven attributes",
attrs: []string{"exec"},
err: "expected an even number of attribute arguments",
},
{
name: "invalid attribute",
attrs: []string{"foo", ""},
err: "invalid attribute: foo",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
var markdown string
fn := func() {
f := mdform.New(tc.attrs...)
markdown = strings.ReplaceAll(f.String(), "\n", "")
}
if tc.err != "" {
urequire.PanicsWithMessage(t, cur, tc.err, fn, "expect form to fail")
return
}
urequire.NotPanics(t, cur, fn, "expect form to be created")
uassert.Equal(t, tc.markdown, markdown, "expected markdown to match")
})
}
}
func TestFormInput(cur realm, t *testing.T) {
tests := []struct {
name string
inputName string
attrs []string
markdown string
err string
}{
{
name: "ok",
inputName: "test",
attrs: []string{"value", "foo"},
markdown: `<gno-form><gno-input name="test" value="foo" /></gno-form>`,
},
{
name: "no attributes",
inputName: "test",
markdown: `<gno-form><gno-input name="test" /></gno-form>`,
},
{
name: "empty name",
inputName: " ",
err: "form input name is required",
},
{
name: "radio type",
inputName: "test",
attrs: []string{"type", "radio"},
err: "use form.Radio() to create inputs of type radio",
},
{
name: "checkbox type",
inputName: "test",
attrs: []string{"type", "checkbox"},
err: "use form.Checkbox() to create inputs of type checkbox",
},
{
name: "uneven attributes",
inputName: "test",
attrs: []string{"value"},
err: "expected an even number of attribute arguments",
},
{
name: "invalid attribute",
inputName: "test",
attrs: []string{"foo", ""},
err: "invalid attribute: foo",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
var markdown string
fn := func() {
f := mdform.New()
f.Input(tc.inputName, tc.attrs...)
markdown = strings.ReplaceAll(f.String(), "\n", "")
}
if tc.err != "" {
urequire.PanicsWithMessage(t, cur, tc.err, fn, "expect form input to fail")
return
}
urequire.NotPanics(t, cur, fn, "expect form input to be created")
uassert.Equal(t, tc.markdown, markdown, "expected markdown to match")
})
}
}
func TestFormRadio(cur realm, t *testing.T) {
tests := []struct {
name string
inputName string
value string
attrs []string
markdown string
err string
}{
{
name: "ok",
inputName: "test",
value: "foo",
attrs: []string{"readonly", "true"},
markdown: `<gno-form><gno-input type="radio" name="test" value="foo" readonly="true" /></gno-form>`,
},
{
name: "no attributes",
inputName: "test",
value: "foo",
markdown: `<gno-form><gno-input type="radio" name="test" value="foo" /></gno-form>`,
},
{
name: "empty name",
inputName: " ",
err: "form radio input name is required",
},
{
name: "ignore type attribute",
inputName: "test",
attrs: []string{"type", "text"},
markdown: `<gno-form><gno-input type="radio" name="test" value="" /></gno-form>`,
},
{
name: "ignore value attribute",
inputName: "test",
attrs: []string{"value", "foo"},
markdown: `<gno-form><gno-input type="radio" name="test" value="" /></gno-form>`,
},
{
name: "uneven attributes",
inputName: "test",
attrs: []string{"readonly"},
err: "expected an even number of attribute arguments",
},
{
name: "invalid attribute",
inputName: "test",
attrs: []string{"foo", ""},
err: "invalid attribute: foo",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
var markdown string
fn := func() {
f := mdform.New()
f.Radio(tc.inputName, tc.value, tc.attrs...)
markdown = strings.ReplaceAll(f.String(), "\n", "")
}
if tc.err != "" {
urequire.PanicsWithMessage(t, cur, tc.err, fn, "expect form radio input to fail")
return
}
urequire.NotPanics(t, cur, fn, "expect form radio input to be created")
uassert.Equal(t, tc.markdown, markdown, "expected markdown to match")
})
}
}
func TestFormCheckbox(cur realm, t *testing.T) {
tests := []struct {
name string
inputName string
value string
attrs []string
markdown string
err string
}{
{
name: "ok",
inputName: "test",
value: "foo",
attrs: []string{"readonly", "true"},
markdown: `<gno-form><gno-input type="checkbox" name="test" value="foo" readonly="true" /></gno-form>`,
},
{
name: "no attributes",
inputName: "test",
value: "foo",
markdown: `<gno-form><gno-input type="checkbox" name="test" value="foo" /></gno-form>`,
},
{
name: "empty name",
inputName: " ",
err: "form checkbox input name is required",
},
{
name: "ignore type attribute",
inputName: "test",
attrs: []string{"type", "text"},
markdown: `<gno-form><gno-input type="checkbox" name="test" value="" /></gno-form>`,
},
{
name: "ignore value attribute",
inputName: "test",
attrs: []string{"value", "foo"},
markdown: `<gno-form><gno-input type="checkbox" name="test" value="" /></gno-form>`,
},
{
name: "uneven attributes",
inputName: "test",
attrs: []string{"readonly"},
err: "expected an even number of attribute arguments",
},
{
name: "invalid attribute",
inputName: "test",
attrs: []string{"foo", ""},
err: "invalid attribute: foo",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
var markdown string
fn := func() {
f := mdform.New()
f.Checkbox(tc.inputName, tc.value, tc.attrs...)
markdown = strings.ReplaceAll(f.String(), "\n", "")
}
if tc.err != "" {
urequire.PanicsWithMessage(t, cur, tc.err, fn, "expect form checkbox input to fail")
return
}
urequire.NotPanics(t, cur, fn, "expect form checkbox input to be created")
uassert.Equal(t, tc.markdown, markdown, "expected markdown to match")
})
}
}
func TestFormTextarea(cur realm, t *testing.T) {
tests := []struct {
name string
inputName string
attrs []string
markdown string
err string
}{
{
name: "ok",
inputName: "test",
attrs: []string{"value", "foo"},
markdown: `<gno-form><gno-textarea name="test" value="foo" /></gno-form>`,
},
{
name: "no attributes",
inputName: "test",
markdown: `<gno-form><gno-textarea name="test" /></gno-form>`,
},
{
name: "empty name",
inputName: " ",
err: "form textarea name is required",
},
{
name: "uneven attributes",
inputName: "test",
attrs: []string{"value"},
err: "expected an even number of attribute arguments",
},
{
name: "invalid attribute",
inputName: "test",
attrs: []string{"foo", ""},
err: "invalid attribute: foo",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
var markdown string
fn := func() {
f := mdform.New()
f.Textarea(tc.inputName, tc.attrs...)
markdown = strings.ReplaceAll(f.String(), "\n", "")
}
if tc.err != "" {
urequire.PanicsWithMessage(t, cur, tc.err, fn, "expect form textarea to fail")
return
}
urequire.NotPanics(t, cur, fn, "expect form textarea to be created")
uassert.Equal(t, tc.markdown, markdown, "expected markdown to match")
})
}
}
func TestFormSelect(cur realm, t *testing.T) {
tests := []struct {
name string
inputName string
value string
attrs []string
markdown string
err string
}{
{
name: "ok",
inputName: "test",
value: "foo",
attrs: []string{"readonly", "true"},
markdown: `<gno-form><gno-select name="test" value="foo" readonly="true" /></gno-form>`,
},
{
name: "no attributes",
inputName: "test",
value: "foo",
markdown: `<gno-form><gno-select name="test" value="foo" /></gno-form>`,
},
{
name: "empty name",
inputName: " ",
err: "form select name is required",
},
{
name: "uneven attributes",
inputName: "test",
attrs: []string{"value"},
err: "expected an even number of attribute arguments",
},
{
name: "invalid attribute",
inputName: "test",
attrs: []string{"foo", ""},
err: "invalid attribute: foo",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
var markdown string
fn := func() {
f := mdform.New()
f.Select(tc.inputName, tc.value, tc.attrs...)
markdown = strings.ReplaceAll(f.String(), "\n", "")
}
if tc.err != "" {
urequire.PanicsWithMessage(t, cur, tc.err, fn, "expect form textarea to fail")
return
}
urequire.NotPanics(t, cur, fn, "expect form textarea to be created")
uassert.Equal(t, tc.markdown, markdown, "expected markdown to match")
})
}
}