Skip to content

Commit

Permalink
support creating Premailer from bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
vanng822 committed Apr 5, 2019
1 parent da22702 commit 4ae54dc
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
19 changes: 19 additions & 0 deletions premailer/premailer_from_bytes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package premailer

import (
"bytes"

"github.com/PuerkitoBio/goquery"
)

// NewPremailerFromBytes take in a document in byte
// and create a goquery.Document
// and then create and Premailer instance.
func NewPremailerFromBytes(doc []byte, options *Options) (Premailer, error) {
read := bytes.NewReader(doc)
d, err := goquery.NewDocumentFromReader(read)
if err != nil {
return nil, err
}
return NewPremailer(d, options), nil
}
51 changes: 51 additions & 0 deletions premailer/premailer_from_bytes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package premailer

import (
"testing"

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

func TestPremailerBasicHTMLBytes(t *testing.T) {
html := []byte(`<html>
<head>
<title>Title</title>
<style type="text/css">
h1 {
width: 50px;
color:red;
}
h2 {
vertical-align: top;
}
h3 {
text-align: right;
}
strong {
text-decoration:none
}
div {
background-color: green
}
</style>
</head>
<body>
<h1>Hi!</h1>
<h2>There</h2>
<h3>Hello</h3>
<p><strong>Yes!</strong></p>
<div>Green color</div>
</body>
</html>`)

p, err := NewPremailerFromBytes(html, nil)
assert.Nil(t, err)
result_html, err := p.Transform()
assert.Nil(t, err)

assert.Contains(t, result_html, "<h1 style=\"color:red;width:50px\" width=\"50\">Hi!</h1>")
assert.Contains(t, result_html, "<h2 style=\"vertical-align:top\" valign=\"top\">There</h2>")
assert.Contains(t, result_html, "<h3 style=\"text-align:right\" align=\"right\">Hello</h3>")
assert.Contains(t, result_html, "<p><strong style=\"text-decoration:none\">Yes!</strong></p>")
assert.Contains(t, result_html, "<div style=\"background-color:green\" bgcolor=\"green\">Green color</div>")
}

0 comments on commit 4ae54dc

Please sign in to comment.