-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support creating Premailer from bytes
- Loading branch information
Showing
2 changed files
with
70 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 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 | ||
} |
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,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>") | ||
} |