Skip to content

Commit

Permalink
Merge pull request #10 from gochore/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfogre authored Jun 22, 2020
2 parents 1d10c80 + f0fd61d commit bf4c8a7
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 12 deletions.
16 changes: 14 additions & 2 deletions htmlt/html.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package htmlt

import (
"encoding/base64"
"fmt"
"io"
"net/http"
"strings"

"github.com/gochore/emailt/internal/rend"
Expand Down Expand Up @@ -97,8 +99,18 @@ func Strong(format Html, a ...interface{}) Html {
}

// Img return a html element <img>
func Img(src, alt string, format Html, a ...interface{}) Html {
return Sprintf(fmt.Sprintf(`<a src="%s" alt="%s">%s</a>`, src, alt, format), a...)
func Img(src, alt string) Html {
return Sprintf(`<img src="%s" alt="%s"/>`, src, alt)
}

// Img return a html element <img> with embedded data
func ImgEmbedded(image []byte, alt string) Html {
src := &strings.Builder{}
src.WriteString(fmt.Sprintf("data:%s;base64,", http.DetectContentType(image)))
encoder := base64.NewEncoder(base64.StdEncoding, src)
_, _ = encoder.Write(image)
_ = encoder.Close()
return Img(src.String(), alt)
}

// Del return a html element <del>
Expand Down
48 changes: 38 additions & 10 deletions htmlt/html_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package htmlt

import (
"bytes"
"encoding/base64"
"fmt"
"io"
"testing"
Expand Down Expand Up @@ -426,10 +427,8 @@ func TestI(t *testing.T) {

func TestImg(t *testing.T) {
type args struct {
src string
alt string
format Html
a []interface{}
src string
alt string
}
tests := []struct {
name string
Expand All @@ -439,17 +438,15 @@ func TestImg(t *testing.T) {
{
name: "regular",
args: args{
src: "http://example.com",
alt: "example",
format: "abc%d",
a: []interface{}{1},
src: "http://example.com",
alt: "example",
},
want: `<a src="http://example.com" alt="example">abc1</a>`,
want: `<img src="http://example.com" alt="example"/>`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Img(tt.args.src, tt.args.alt, tt.args.format, tt.args.a...); got != tt.want {
if got := Img(tt.args.src, tt.args.alt); got != tt.want {
t.Errorf("Img() = %v, want %v", got, tt.want)
}
})
Expand Down Expand Up @@ -575,3 +572,34 @@ func TestHtml_Str(t *testing.T) {
t.Errorf("Sprintf() = %v, want %v", got, want)
}
}

func TestImgEmbedded(t *testing.T) {
type args struct {
image []byte
alt string
}
tests := []struct {
name string
args args
want Html
}{
{
name: "regular",
args: args{
image: func() []byte {
ret, _ := base64.StdEncoding.DecodeString("iVBORw0KGgoAAAANSUhEUgAAAWgAAAAKCAIAAAAWzFNnAAABHElEQVR4nOzUL0sDcRzHcRH/DIwqYpAViyiiYHdomMFD2LhgENMegDBBWBgLA8GBD2BJDIM7diBn8IIyzYIiisUyDCJqFKYWH8HnB1/GtfervvnC/Tj4DAVB4Pv+gBCG4fPLsapzs3vu214+UjWTFLofFVWzk/XS0pWqzfu1p1pd1flqpeEdqVqO95dLf6reNYerXlHVWtx2vzf/vqJqMnXrvu2nbpfHVW01vjq/q6rmRq6/D05UHTvczW1eqto5X8/uPKjaPV3s60WZCVVbvU/3bfTzqmphdCa9v+Cu01ttVd/Oiu7bi4VY1Y1HL71vjpIbVQdVAACF4QBgxnAAMGM4AJgxHADMGA4AZgwHADOGA4AZwwHA7D8AAP///GZtPo9PSgsAAAAASUVORK5CYII=")
return ret
}(),
alt: "test",
},
want: `<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAWgAAAAKCAIAAAAWzFNnAAABHElEQVR4nOzUL0sDcRzHcRH/DIwqYpAViyiiYHdomMFD2LhgENMegDBBWBgLA8GBD2BJDIM7diBn8IIyzYIiisUyDCJqFKYWH8HnB1/GtfervvnC/Tj4DAVB4Pv+gBCG4fPLsapzs3vu214+UjWTFLofFVWzk/XS0pWqzfu1p1pd1flqpeEdqVqO95dLf6reNYerXlHVWtx2vzf/vqJqMnXrvu2nbpfHVW01vjq/q6rmRq6/D05UHTvczW1eqto5X8/uPKjaPV3s60WZCVVbvU/3bfTzqmphdCa9v+Cu01ttVd/Oiu7bi4VY1Y1HL71vjpIbVQdVAACF4QBgxnAAMGM4AJgxHADMGA4AZgwHADOGA4AZwwHA7D8AAP///GZtPo9PSgsAAAAASUVORK5CYII=" alt="test"/>`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ImgEmbedded(tt.args.image, tt.args.alt); got != tt.want {
t.Errorf("ImgEmbedded() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit bf4c8a7

Please sign in to comment.