Skip to content

Commit af58f13

Browse files
committed
Provide option to disable inlining attributes
Provide option to disable inlining attributes as many of these are obsolete and the values they accept are not always the same as CSS.
1 parent b8ddf16 commit af58f13

5 files changed

Lines changed: 49 additions & 32 deletions

File tree

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
default: douceur
2+
3+
.PHONY: douceur
4+
douceur:
5+
CGO_ENABLED=0 GOOS=linux go build -a -ldflags '-extldflags "-static"' .

douceur.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,19 @@ import (
1212

1313
const (
1414
// Version is package version
15-
Version = "0.3.1"
15+
Version = "0.3.2"
1616
)
1717

1818
var (
19-
flagVersion bool
20-
cssPath string
19+
flagVersion bool
20+
noAttributes bool
21+
cssPath string
2122
)
2223

2324
func init() {
2425
flag.BoolVar(&flagVersion, "version", false, "Display version")
25-
flag.StringVar(&cssPath, "es", "", "Include external stylesheet when inlining")
26+
flag.BoolVar(&noAttributes, "n", false, "Don't inline obsolete attributes like bgcolor & valign")
27+
flag.StringVar(&cssPath, "c", "", "Include external stylesheet when inlining")
2628
}
2729

2830
func main() {
@@ -54,6 +56,7 @@ func main() {
5456
}
5557

5658
func usage() {
59+
fmt.Printf("Help: %s -h", os.Args[0])
5760
fmt.Printf("Usage: %s %s %s\n", os.Args[0], "(parse|inline)", "/path/to/file")
5861
fmt.Printf("Usage: %s %s < %s\n", os.Args[0], "(parse|inline)", "/path/to/file")
5962
}
@@ -74,13 +77,19 @@ func parseCSS(filePath string) {
7477
// inlines CSS into HTML and display result
7578
func inlineCSS(filePath string, cssPath string) {
7679
htmlInput := string(read(filePath))
77-
cssInput := ""
80+
instance := inliner.NewInliner(htmlInput)
7881

7982
if cssPath != "" {
80-
cssInput = string(readFile(cssPath))
83+
cssInput := string(readFile(cssPath))
84+
err := instance.ParseStylesheet(cssInput)
85+
if err != nil {
86+
fmt.Println("Inlining error: ", err)
87+
os.Exit(1)
88+
}
8189
}
8290

83-
output, err := inliner.InlineWithExternalCSS(htmlInput, cssInput)
91+
instance.InlineAttributes(!noAttributes)
92+
output, err := instance.Inline()
8493
if err != nil {
8594
fmt.Println("Inlining error: ", err)
8695
os.Exit(1)

inliner/element.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func (element *Element) addStyleRule(styleRule *StyleRule) {
6868
}
6969

7070
// Inline styles on element
71-
func (element *Element) inline() error {
71+
func (element *Element) inline(inlineAttributes bool) error {
7272
// compute declarations
7373
declarations, err := element.computeDeclarations()
7474
if err != nil {
@@ -82,7 +82,9 @@ func (element *Element) inline() error {
8282
}
8383

8484
// set additionnal attributes
85-
element.setAttributesFromStyle(declarations)
85+
if inlineAttributes {
86+
element.setAttributesFromStyle(declarations)
87+
}
8688

8789
return nil
8890
}

inliner/inliner.go

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ type Inliner struct {
2525
// Raw HTML
2626
html string
2727

28+
// Add obsolete attributes for older clients
29+
inlineAttributes bool
30+
2831
// Parsed HTML document
2932
doc *goquery.Document
3033

@@ -44,30 +47,16 @@ type Inliner struct {
4447
// NewInliner instanciates a new Inliner
4548
func NewInliner(html string) *Inliner {
4649
return &Inliner{
47-
html: html,
48-
elements: make(map[string]*Element),
50+
html: html,
51+
elements: make(map[string]*Element),
52+
inlineAttributes: true,
4953
}
5054
}
5155

5256
// Inline inlines css into html document
5357
func Inline(html string) (string, error) {
54-
return InlineWithExternalCSS(html, "")
55-
}
56-
57-
// InlineWithExternalCSS Includes external css
58-
func InlineWithExternalCSS(html string, css string) (string, error) {
5958
inliner := NewInliner(html)
60-
err := inliner.parseStylesheet(css)
61-
if err != nil {
62-
return "", err
63-
}
64-
65-
result, err := inliner.Inline()
66-
if err != nil {
67-
return "", err
68-
}
69-
70-
return result, nil
59+
return inliner.Inline()
7160
}
7261

7362
// Inline inlines CSS and returns HTML
@@ -97,6 +86,11 @@ func (inliner *Inliner) Inline() (string, error) {
9786
return inliner.genHTML()
9887
}
9988

89+
// InlineAttributes sets if the inliner should inline attributes
90+
func (inliner *Inliner) InlineAttributes(val bool) {
91+
inliner.inlineAttributes = val
92+
}
93+
10094
// Parses raw html
10195
func (inliner *Inliner) parseHTML() error {
10296
doc, err := goquery.NewDocumentFromReader(strings.NewReader(inliner.html))
@@ -114,7 +108,7 @@ func (inliner *Inliner) parseStylesheets() error {
114108
var result error
115109

116110
inliner.doc.Find("style").EachWithBreak(func(i int, s *goquery.Selection) bool {
117-
result = inliner.parseStylesheet(s.Text())
111+
result = inliner.ParseStylesheet(s.Text())
118112
if result != nil {
119113
return false
120114
}
@@ -128,7 +122,8 @@ func (inliner *Inliner) parseStylesheets() error {
128122
return result
129123
}
130124

131-
func (inliner *Inliner) parseStylesheet(s string) error {
125+
// ParseStylesheet parses a stylesheet for inlining.
126+
func (inliner *Inliner) ParseStylesheet(s string) error {
132127
stylesheet, err := parser.Parse(s)
133128
if err != nil {
134129
return err
@@ -188,7 +183,7 @@ func (inliner *Inliner) inlineStyleRules() error {
188183
element.elt.RemoveAttr(eltMarkerAttr)
189184

190185
// inline element
191-
err := element.inline()
186+
err := element.inline(inliner.inlineAttributes)
192187
if err != nil {
193188
return err
194189
}

inliner/inliner_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func TestInliner(t *testing.T) {
4747
}
4848

4949
// Simple rule inlining with two declarations
50-
func TestInlineWithExternalCSS(t *testing.T) {
50+
func TestParseStylesheet(t *testing.T) {
5151
input := `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
5252
<html xmlns="http://www.w3.org/1999/xhtml">
5353
<head>
@@ -74,7 +74,13 @@ func TestInlineWithExternalCSS(t *testing.T) {
7474
7575
</body></html>`
7676

77-
output, err := InlineWithExternalCSS(input, cssInput)
77+
instance := NewInliner(input)
78+
err := instance.ParseStylesheet(cssInput)
79+
if err != nil {
80+
t.Fatal("Failed to inline html:", err)
81+
}
82+
83+
output, err := instance.Inline()
7884
if err != nil {
7985
t.Fatal("Failed to inline html:", err)
8086
}

0 commit comments

Comments
 (0)