Skip to content

Commit c24d3e0

Browse files
authored
Merge pull request #2 from spekary/main
Badges
2 parents 5c5b7d1 + b9e7b04 commit c24d3e0

File tree

6 files changed

+53
-46
lines changed

6 files changed

+53
-46
lines changed

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1+
[![Go Reference](https://pkg.go.dev/badge/github.com/goradd/html5tag.svg)](https://pkg.go.dev/github.com/goradd/html5tag)
2+
![Build Status](https://img.shields.io/github/workflow/status/goradd/got/Go)
3+
[![Go Report Card](https://goreportcard.com/badge/github.com/goradd/html5tag)](https://goreportcard.com/report/github.com/goradd/html5tag)
4+
[![codecov](https://codecov.io/gh/goradd/html5tag/branch/main/graph/badge.svg?token=L8KC75KWWR)](https://codecov.io/gh/goradd/html5tag)
5+
16
# html5tag
27

38
The html5tag package contains utilities to generate html 5 tags.
49
Choose between string versions of the
5-
functions for easy tag creation, or io.Writer versions for speed. It
6-
also has a tag builder for convenience and can perform math operations
10+
functions for easy tag creation, or io.Writer versions for speed.
11+
12+
html5tag also has a tag builder for convenience and can perform math operations
713
on numeric style values.
814

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

2026
main() {
2127

22-
// Render an input tag, inside a div tag, inside a body tag
28+
// Render an input tag, inside a div tag, inside a body tag using different tag building mechanisms
2329

2430
a := NewAttributes().
2531
SetID("myText").
@@ -41,4 +47,4 @@ main() {
4147
}
4248
```
4349

44-
For complete documentation, start at the documentation for Tag and drill down from there.
50+
For complete documentation, start at the documentation for `RenderTag()` and `WriteTag()` and drill down from there.

attributes.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,7 @@ func (a Attributes) set(k string, v string) bool {
123123
return !existed || oldVal != v
124124
}
125125

126-
// Set sets a particular attribute and returns Attributes so that it can be chained. Set will accept a nil
127-
// Attributes value.
126+
// Set sets a particular attribute and returns Attributes so that it can be chained.
128127
//
129128
// It looks for special attributes like "class", "style" and "data" to do some error checking
130129
// on them. Use SetData to set data attributes.
@@ -490,10 +489,10 @@ func (a Attributes) AddValuesChanged(attrKey string, values string) bool {
490489
return true
491490
}
492491
return false
493-
} else {
494-
a.set(attrKey, values)
495-
return true
496492
}
493+
494+
a.set(attrKey, values)
495+
return true
497496
}
498497

499498
// AddValues adds space separated values to the end of an attribute value.
@@ -751,9 +750,8 @@ func ValueString(i interface{}) string {
751750
return v
752751
case int:
753752
return strconv.Itoa(v)
754-
default:
755-
return fmt.Sprint(i)
756753
}
754+
return fmt.Sprint(i)
757755
}
758756

759757
// getAttributesFromTemplate returns Attributes extracted from a string in the form

attributes_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,6 @@ func ExampleAttributes_Merge() {
329329
// Output: class="that" style="width:6px"
330330
}
331331

332-
333-
334332
func ExampleAttributes_AddClass() {
335333
a := NewAttributes()
336334
a.AddClass("this")

style.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,11 @@ func NewStyle() Style {
3939
return make(map[string]string)
4040
}
4141

42-
// NewStyleFromMap creates a new style from a string map.
43-
func NewStyleFromMap(m map[string]string) Style {
44-
s := NewStyle()
45-
for k, v := range m {
46-
s[k] = v
47-
}
48-
return s
42+
// Copy copies the given style. It also turns a map[string]string into a Style.
43+
func (s Style) Copy() Style {
44+
s2 := NewStyle()
45+
s2.Merge(s)
46+
return s2
4947
}
5048

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

274-
// StyleCreator is a helper struct to create a style from a string map.
275-
type StyleCreator map[string]string
276-
277-
// Create creates a style from a StyleCreator.
278-
func (c StyleCreator) Create() Style {
279-
return NewStyleFromMap(c)
280-
}
281-
282272
// MergeStyleStrings merges the styles found in the two style strings.
283273
// s2 wins conflicts.
284274
func MergeStyleStrings(s1, s2 string) string {

style_test.go

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,23 @@ import (
55
"testing"
66
)
77

8-
func Example_newStyleFromMap() {
9-
s := NewStyleFromMap(map[string]string{"color": "green", "size": "9"})
10-
fmt.Print(s)
8+
func ExampleStyle_Copy() {
9+
s := Style{"color": "green", "size": "9"}
10+
s2 := s.Copy()
11+
12+
fmt.Print(s2)
1113
//Output: color:green;size:9
1214
}
1315

1416
func ExampleStyle_Len() {
15-
s := NewStyleFromMap(map[string]string{"color": "green", "size": "9"})
17+
s := Style{"color": "green", "size": "9"}
1618
fmt.Print(s.Len())
1719
//Output: 2
1820
}
1921

20-
func ExampleStyle_SetTo() {
22+
func ExampleStyle_SetString() {
2123
s := NewStyle()
22-
s.SetString("height: 9em; width: 100%; position:absolute")
24+
_, _ = s.SetString("height: 9em; width: 100%; position:absolute")
2325
fmt.Print(s)
2426
//Output: height:9em;position:absolute;width:100%
2527
}
@@ -33,38 +35,38 @@ func ExampleStyle_Set_a() {
3335

3436
func ExampleStyle_Set_b() {
3537
s := NewStyle()
36-
s.SetString("height:9px")
38+
_, _ = s.SetString("height:9px")
3739
s.Set("height", "+ 10")
3840
fmt.Print(s)
3941
//Output: height:19px
4042
}
4143

4244
func ExampleStyle_Get() {
4345
s := NewStyle()
44-
s.SetString("height: 9em; width: 100%; position:absolute")
46+
_, _ = s.SetString("height: 9em; width: 100%; position:absolute")
4547
fmt.Print(s.Get("width"))
4648
//Output: 100%
4749
}
4850

4951
func ExampleStyle_Remove() {
5052
s := NewStyle()
51-
s.SetString("height: 9em; width: 100%; position:absolute")
53+
_, _ = s.SetString("height: 9em; width: 100%; position:absolute")
5254
s.Remove("position")
5355
fmt.Print(s)
5456
//Output: height:9em;width:100%
5557
}
5658

5759
func ExampleStyle_RemoveAll() {
5860
s := NewStyle()
59-
s.SetString("height: 9em; width: 100%; position:absolute")
61+
_, _ = s.SetString("height: 9em; width: 100%; position:absolute")
6062
s.RemoveAll()
6163
fmt.Print(s)
6264
//Output:
6365
}
6466

6567
func ExampleStyle_Has() {
6668
s := NewStyle()
67-
s.SetString("height: 9em; width: 100%; position:absolute")
69+
_, _ = s.SetString("height: 9em; width: 100%; position:absolute")
6870
fmt.Print(s.Has("width"), s.Has("display"))
6971
//Output:true false
7072
}
@@ -148,12 +150,26 @@ func TestStyleLengths(t *testing.T) {
148150
}
149151

150152
changed, err = s.SetChanged("width", "1")
153+
if !changed {
154+
t.Error("Expected change")
155+
}
156+
if err != nil {
157+
t.Error(err)
158+
}
159+
151160
if w := s.Get("width"); w != "1px" {
152161
t.Error("Expected a 1px")
153162
}
154163

155164
// test a non-length numeric
156165
changed, err = s.SetChanged("volume", "4")
166+
if !changed {
167+
t.Error("Expected change")
168+
}
169+
if err != nil {
170+
t.Error(err)
171+
}
172+
157173
if w := s.Get("volume"); w != "4" {
158174
t.Error("Expected a 4")
159175
}
@@ -210,7 +226,7 @@ func TestNilStyle(t *testing.T) {
210226
}
211227

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

215231
type args struct {
216232
attribute string
@@ -225,10 +241,10 @@ func TestStyle_mathOp(t *testing.T) {
225241
wantErr bool
226242
wantString string
227243
}{
228-
{"Test empty", c.Create(), args{"margin", "+", "1"}, true, false, "height:10;margin:1;width:20en"},
229-
{"Test float error", c.Create(), args{"margin", "+", "1a"}, false, true, "height:10;margin:;width:20en"},
230-
{"Test mul no unit", c.Create(), args{"height", "*", "2"}, true, false, "height:20;margin:;width:20en"},
231-
{"Test div w/ unit", c.Create(), args{"width", "/", "2"}, true, false, "height:10;margin:;width:10en"},
244+
{"Test empty", c.Copy(), args{"margin", "+", "1"}, true, false, "height:10;margin:1;width:20en"},
245+
{"Test float error", c.Copy(), args{"margin", "+", "1a"}, false, true, "height:10;margin:;width:20en"},
246+
{"Test mul no unit", c.Copy(), args{"height", "*", "2"}, true, false, "height:20;margin:;width:20en"},
247+
{"Test div w/ unit", c.Copy(), args{"width", "/", "2"}, true, false, "height:10;margin:;width:10en"},
232248
}
233249
for _, tt := range tests {
234250
t.Run(tt.name, func(t *testing.T) {

tagbuilder.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ func (b *TagBuilder) String() string {
108108
}
109109
if b.isVoid {
110110
return RenderVoidTag(b.tag, b.attributes)
111-
} else {
112-
return RenderTag(b.tag, b.attributes, b.innerHtml)
113111
}
112+
return RenderTag(b.tag, b.attributes, b.innerHtml)
114113
}

0 commit comments

Comments
 (0)