-
-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 5226d92
Showing
8 changed files
with
260 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
*.log | ||
.DS_Store | ||
doc | ||
tmp | ||
pkg | ||
*.gem | ||
*.pid | ||
coverage | ||
coverage.data | ||
build/* | ||
*.pbxuser | ||
*.mode1v3 | ||
.svn | ||
profile | ||
.console_history | ||
.sass-cache/* | ||
.rake_tasks~ | ||
*.log.lck | ||
solr/ | ||
.jhw-cache/ | ||
jhw.* | ||
*.sublime* | ||
node_modules/ | ||
dist/ | ||
generated/ | ||
.vendor/ | ||
bin/* | ||
gin-bin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package tags | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
) | ||
|
||
type BlockTag struct { | ||
*Tag | ||
Body interface{} | ||
} | ||
|
||
func (b BlockTag) String() string { | ||
bb := &bytes.Buffer{} | ||
bb.WriteString(b.Tag.String()) | ||
bb.WriteString(fmt.Sprintf("%s", b.Body)) | ||
bb.WriteString("</") | ||
bb.WriteString(b.Name) | ||
bb.WriteString(">") | ||
return bb.String() | ||
} | ||
|
||
func NewBlockTag(name string, opts Options) *BlockTag { | ||
tag := &BlockTag{ | ||
Tag: New(name, opts), | ||
Body: opts["body"], | ||
} | ||
delete(tag.Options, "body") | ||
if tag.Body == nil { | ||
tag.Body = opts["value"] | ||
delete(tag.Options, "value") | ||
} | ||
|
||
return tag | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package tags_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/markbates/tags" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_NewBlockTag_WithBody(t *testing.T) { | ||
r := require.New(t) | ||
|
||
tag := tags.NewBlockTag("div", tags.Options{ | ||
"body": "hi there!", | ||
}) | ||
r.Equal(tag.Body, "hi there!") | ||
r.Nil(tag.Options["body"]) | ||
} | ||
|
||
func Test_NewBlockTag_WithValue(t *testing.T) { | ||
r := require.New(t) | ||
|
||
tag := tags.NewBlockTag("div", tags.Options{ | ||
"value": "hi there!", | ||
}) | ||
r.Equal(tag.Body, "hi there!") | ||
r.Nil(tag.Options["value"]) | ||
} | ||
|
||
func Test_BlockTag_String(t *testing.T) { | ||
r := require.New(t) | ||
|
||
tag := tags.NewBlockTag("div", tags.Options{ | ||
"body": "hi there!", | ||
}) | ||
r.Equal(`<div>hi there!</div>`, tag.String()) | ||
} | ||
|
||
func Test_BlockTag_String_WithOpts(t *testing.T) { | ||
r := require.New(t) | ||
|
||
tag := tags.NewBlockTag("div", tags.Options{ | ||
"body": "hi there!", | ||
"class": "foo bar baz", | ||
}) | ||
r.Equal(`<div class="foo bar baz">hi there!</div>`, tag.String()) | ||
} | ||
|
||
func Test_BlockTag_String_SubTag(t *testing.T) { | ||
r := require.New(t) | ||
|
||
tag := tags.NewBlockTag("div", tags.Options{ | ||
"body": tags.NewBlockTag("p", tags.Options{ | ||
"body": "hi!", | ||
}), | ||
}) | ||
r.Equal(`<div><p>hi!</p></div>`, tag.String()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package tags | ||
|
||
import ( | ||
"fmt" | ||
"html/template" | ||
"strconv" | ||
|
||
"github.com/markbates/pop/nulls" | ||
) | ||
|
||
func esc(s interface{}) string { | ||
var st string | ||
switch t := s.(type) { | ||
case fmt.Stringer: | ||
st = t.String() | ||
case nulls.String: | ||
st = t.String | ||
case nulls.Int: | ||
st = strconv.Itoa(t.Int) | ||
case bool: | ||
st = fmt.Sprintf("%t", t) | ||
case string: | ||
st = t | ||
case int: | ||
st = strconv.Itoa(t) | ||
case nil: | ||
st = "" | ||
default: | ||
st = fmt.Sprintf("%s", s) | ||
} | ||
return template.HTMLEscapeString(st) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package tags | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type Options map[string]interface{} | ||
|
||
func (o Options) String() string { | ||
var out = make([]string, 0, len(o)) | ||
var tmp = make([]string, 2) | ||
for k, v := range o { | ||
tmp[0] = esc(k) | ||
tmp[1] = fmt.Sprintf("\"%s\"", esc(v)) | ||
out = append(out, strings.Join(tmp, "=")) | ||
} | ||
return strings.Join(out, " ") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package tags_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/markbates/tags" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_Options_String(t *testing.T) { | ||
r := require.New(t) | ||
o := tags.Options{ | ||
"value": "Mark", | ||
"id": "foo-bar", | ||
"class": "foo bar baz", | ||
} | ||
s := o.String() | ||
r.Contains(s, `value="Mark"`) | ||
r.Contains(s, `id="foo-bar"`) | ||
r.Contains(s, `class="foo bar baz"`) | ||
} | ||
|
||
func Test_Options_String_Escaped(t *testing.T) { | ||
r := require.New(t) | ||
o := tags.Options{ | ||
"<b>": "<p>", | ||
} | ||
s := o.String() | ||
r.Equal(`<b>="<p>"`, s) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package tags | ||
|
||
import "bytes" | ||
|
||
type Tag struct { | ||
Name string | ||
Options Options | ||
} | ||
|
||
func (t Tag) String() string { | ||
bb := &bytes.Buffer{} | ||
bb.WriteString("<") | ||
bb.WriteString(t.Name) | ||
if len(t.Options) > 0 { | ||
bb.WriteString(" ") | ||
bb.WriteString(t.Options.String()) | ||
} | ||
bb.WriteString(">") | ||
return bb.String() | ||
} | ||
|
||
func New(name string, opts Options) *Tag { | ||
tag := &Tag{ | ||
Name: name, | ||
Options: opts, | ||
} | ||
|
||
return tag | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package tags_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/markbates/tags" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_Tag(t *testing.T) { | ||
r := require.New(t) | ||
tag := tags.New("input", tags.Options{}) | ||
r.Equal("input", tag.Name) | ||
} | ||
|
||
func Test_Tag_WithName(t *testing.T) { | ||
r := require.New(t) | ||
tag := tags.New("div", tags.Options{}) | ||
r.Equal("div", tag.Name) | ||
r.Equal(`<div>`, tag.String()) | ||
} | ||
|
||
func Test_Tag_WithValue(t *testing.T) { | ||
r := require.New(t) | ||
tag := tags.New("input", tags.Options{ | ||
"value": "Mark", | ||
}) | ||
r.Equal(`<input value="Mark">`, tag.String()) | ||
} |