-
-
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.
adding stylesheet and javascript tags
- Loading branch information
1 parent
172cc7e
commit 3e2d60f
Showing
2 changed files
with
60 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,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) | ||
} |
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,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()) | ||
} | ||
} |