-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinliners.go
48 lines (38 loc) · 975 Bytes
/
inliners.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package premailer
import (
"bytes"
"strings"
douceur "github.com/aymerick/douceur/inliner"
"golang.org/x/net/html"
)
type Inliner func(string) (string, error)
func Douceur(raw string) (*html.Node, error) {
inlined, err := douceur.Inline(raw)
if err != nil {
return nil, err
}
doc, err := html.Parse(bytes.NewBuffer([]byte(inlined)))
if err != nil {
return nil, err
}
// Apply some corrections to match github.com/premailer/premailer behaviour
eachElement(doc, func(n *html.Node) bool {
var attrs []html.Attribute
for _, attr := range n.Attr {
if attr.Key == "style" {
// skip style elements on children of the `head` tag (and the head tag itself)
if hasParent(n, "head") {
continue
}
// remove trailing semicolons from style tags
if attr.Val[len(attr.Val)-1] == ';' {
attr.Val = strings.TrimRight(attr.Val, ";")
}
}
attrs = append(attrs, attr)
}
n.Attr = attrs
return true
})
return doc, err
}