Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
[![Go Reference](https://pkg.go.dev/badge/github.com/goradd/html5tag.svg)](https://pkg.go.dev/github.com/goradd/html5tag)
![Build Status](https://img.shields.io/github/workflow/status/goradd/got/Go)
[![Go Report Card](https://goreportcard.com/badge/github.com/goradd/html5tag)](https://goreportcard.com/report/github.com/goradd/html5tag)
[![codecov](https://codecov.io/gh/goradd/html5tag/branch/main/graph/badge.svg?token=L8KC75KWWR)](https://codecov.io/gh/goradd/html5tag)

# html5tag

The html5tag package contains utilities to generate html 5 tags.
Choose between string versions of the
functions for easy tag creation, or io.Writer versions for speed. It
also has a tag builder for convenience and can perform math operations
functions for easy tag creation, or io.Writer versions for speed.

html5tag also has a tag builder for convenience and can perform math operations
on numeric style values.

html5tag does some checks to make sure tags are well-formed. For example,
Expand All @@ -19,7 +25,7 @@ import . "github.com/goradd/html5tag"

main() {

// Render an input tag, inside a div tag, inside a body tag
// Render an input tag, inside a div tag, inside a body tag using different tag building mechanisms

a := NewAttributes().
SetID("myText").
Expand All @@ -41,4 +47,4 @@ main() {
}
```

For complete documentation, start at the documentation for Tag and drill down from there.
For complete documentation, start at the documentation for `RenderTag()` and `WriteTag()` and drill down from there.
12 changes: 5 additions & 7 deletions attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ func (a Attributes) set(k string, v string) bool {
return !existed || oldVal != v
}

// Set sets a particular attribute and returns Attributes so that it can be chained. Set will accept a nil
// Attributes value.
// Set sets a particular attribute and returns Attributes so that it can be chained.
//
// It looks for special attributes like "class", "style" and "data" to do some error checking
// on them. Use SetData to set data attributes.
Expand Down Expand Up @@ -490,10 +489,10 @@ func (a Attributes) AddValuesChanged(attrKey string, values string) bool {
return true
}
return false
} else {
a.set(attrKey, values)
return true
}

a.set(attrKey, values)
return true
}

// AddValues adds space separated values to the end of an attribute value.
Expand Down Expand Up @@ -751,9 +750,8 @@ func ValueString(i interface{}) string {
return v
case int:
return strconv.Itoa(v)
default:
return fmt.Sprint(i)
}
return fmt.Sprint(i)
}

// getAttributesFromTemplate returns Attributes extracted from a string in the form
Expand Down
2 changes: 0 additions & 2 deletions attributes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,6 @@ func ExampleAttributes_Merge() {
// Output: class="that" style="width:6px"
}



func ExampleAttributes_AddClass() {
a := NewAttributes()
a.AddClass("this")
Expand Down
20 changes: 5 additions & 15 deletions style.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,11 @@ func NewStyle() Style {
return make(map[string]string)
}

// NewStyleFromMap creates a new style from a string map.
func NewStyleFromMap(m map[string]string) Style {
s := NewStyle()
for k, v := range m {
s[k] = v
}
return s
// Copy copies the given style. It also turns a map[string]string into a Style.
func (s Style) Copy() Style {
s2 := NewStyle()
s2.Merge(s)
return s2
}

// Merge merges the styles from one style to another. Conflicts will overwrite the current style.
Expand Down Expand Up @@ -271,14 +269,6 @@ func StyleString(i interface{}) string {
return sValue
}

// StyleCreator is a helper struct to create a style from a string map.
type StyleCreator map[string]string

// Create creates a style from a StyleCreator.
func (c StyleCreator) Create() Style {
return NewStyleFromMap(c)
}

// MergeStyleStrings merges the styles found in the two style strings.
// s2 wins conflicts.
func MergeStyleStrings(s1, s2 string) string {
Expand Down
48 changes: 32 additions & 16 deletions style_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,23 @@ import (
"testing"
)

func Example_newStyleFromMap() {
s := NewStyleFromMap(map[string]string{"color": "green", "size": "9"})
fmt.Print(s)
func ExampleStyle_Copy() {
s := Style{"color": "green", "size": "9"}
s2 := s.Copy()

fmt.Print(s2)
//Output: color:green;size:9
}

func ExampleStyle_Len() {
s := NewStyleFromMap(map[string]string{"color": "green", "size": "9"})
s := Style{"color": "green", "size": "9"}
fmt.Print(s.Len())
//Output: 2
}

func ExampleStyle_SetTo() {
func ExampleStyle_SetString() {
s := NewStyle()
s.SetString("height: 9em; width: 100%; position:absolute")
_, _ = s.SetString("height: 9em; width: 100%; position:absolute")
fmt.Print(s)
//Output: height:9em;position:absolute;width:100%
}
Expand All @@ -33,38 +35,38 @@ func ExampleStyle_Set_a() {

func ExampleStyle_Set_b() {
s := NewStyle()
s.SetString("height:9px")
_, _ = s.SetString("height:9px")
s.Set("height", "+ 10")
fmt.Print(s)
//Output: height:19px
}

func ExampleStyle_Get() {
s := NewStyle()
s.SetString("height: 9em; width: 100%; position:absolute")
_, _ = s.SetString("height: 9em; width: 100%; position:absolute")
fmt.Print(s.Get("width"))
//Output: 100%
}

func ExampleStyle_Remove() {
s := NewStyle()
s.SetString("height: 9em; width: 100%; position:absolute")
_, _ = s.SetString("height: 9em; width: 100%; position:absolute")
s.Remove("position")
fmt.Print(s)
//Output: height:9em;width:100%
}

func ExampleStyle_RemoveAll() {
s := NewStyle()
s.SetString("height: 9em; width: 100%; position:absolute")
_, _ = s.SetString("height: 9em; width: 100%; position:absolute")
s.RemoveAll()
fmt.Print(s)
//Output:
}

func ExampleStyle_Has() {
s := NewStyle()
s.SetString("height: 9em; width: 100%; position:absolute")
_, _ = s.SetString("height: 9em; width: 100%; position:absolute")
fmt.Print(s.Has("width"), s.Has("display"))
//Output:true false
}
Expand Down Expand Up @@ -148,12 +150,26 @@ func TestStyleLengths(t *testing.T) {
}

changed, err = s.SetChanged("width", "1")
if !changed {
t.Error("Expected change")
}
if err != nil {
t.Error(err)
}

if w := s.Get("width"); w != "1px" {
t.Error("Expected a 1px")
}

// test a non-length numeric
changed, err = s.SetChanged("volume", "4")
if !changed {
t.Error("Expected change")
}
if err != nil {
t.Error(err)
}

if w := s.Get("volume"); w != "4" {
t.Error("Expected a 4")
}
Expand Down Expand Up @@ -210,7 +226,7 @@ func TestNilStyle(t *testing.T) {
}

func TestStyle_mathOp(t *testing.T) {
c := StyleCreator{"height": "10", "margin": "", "width": "20en"}
c := Style{"height": "10", "margin": "", "width": "20en"}

type args struct {
attribute string
Expand All @@ -225,10 +241,10 @@ func TestStyle_mathOp(t *testing.T) {
wantErr bool
wantString string
}{
{"Test empty", c.Create(), args{"margin", "+", "1"}, true, false, "height:10;margin:1;width:20en"},
{"Test float error", c.Create(), args{"margin", "+", "1a"}, false, true, "height:10;margin:;width:20en"},
{"Test mul no unit", c.Create(), args{"height", "*", "2"}, true, false, "height:20;margin:;width:20en"},
{"Test div w/ unit", c.Create(), args{"width", "/", "2"}, true, false, "height:10;margin:;width:10en"},
{"Test empty", c.Copy(), args{"margin", "+", "1"}, true, false, "height:10;margin:1;width:20en"},
{"Test float error", c.Copy(), args{"margin", "+", "1a"}, false, true, "height:10;margin:;width:20en"},
{"Test mul no unit", c.Copy(), args{"height", "*", "2"}, true, false, "height:20;margin:;width:20en"},
{"Test div w/ unit", c.Copy(), args{"width", "/", "2"}, true, false, "height:10;margin:;width:10en"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
3 changes: 1 addition & 2 deletions tagbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ func (b *TagBuilder) String() string {
}
if b.isVoid {
return RenderVoidTag(b.tag, b.attributes)
} else {
return RenderTag(b.tag, b.attributes, b.innerHtml)
}
return RenderTag(b.tag, b.attributes, b.innerHtml)
}