Skip to content

Commit

Permalink
adding stylesheet and javascript tags
Browse files Browse the repository at this point in the history
  • Loading branch information
paganotoni committed Jan 26, 2018
1 parent 172cc7e commit 3e2d60f
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
19 changes: 19 additions & 0 deletions misc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package tags

//StylesheetTag builds the <link> tag to load styles on the page.
func StylesheetTag(opts Options) *Tag {
if opts["rel"] == nil {
opts["rel"] = "stylesheet"
}

return New("link", opts)
}

//JavascriptTag builds JS tags based in passed options
func JavascriptTag(opts Options) *Tag {
if opts["src"] != nil {
delete(opts, "body")
}

return New("script", opts)
}
41 changes: 41 additions & 0 deletions misc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package tags

import (
"testing"

"github.com/stretchr/testify/require"
)

func Test_StylesheetTag(t *testing.T) {
r := require.New(t)
tcases := []struct {
Options Options
Expected string
}{
{Options: Options{"href": "styles.css"}, Expected: `<link href="styles.css" rel="stylesheet" />`},
{Options: Options{}, Expected: `<link rel="stylesheet" />`},
{Options: Options{"style": "text/scss"}, Expected: `<link rel="stylesheet" style="text/scss" />`},
}

for _, tcase := range tcases {
tag := StylesheetTag(tcase.Options)
r.Equal(tcase.Expected, tag.String())
}
}

func Test_JavascriptTag(t *testing.T) {
r := require.New(t)
tcases := []struct {
Options Options
Expected string
}{
{Options: Options{"src": "script.js"}, Expected: `<script src="script.js"></script>`},
{Options: Options{"src": "script.js", "body": `alert("hello")`}, Expected: `<script src="script.js"></script>`},
{Options: Options{"body": `alert("hello")`}, Expected: `<script>alert("hello")</script>`},
}

for _, tcase := range tcases {
tag := JavascriptTag(tcase.Options)
r.Equal(tcase.Expected, tag.String())
}
}

0 comments on commit 3e2d60f

Please sign in to comment.