Skip to content

Commit

Permalink
Merge e9cd2c6 into b0d2519
Browse files Browse the repository at this point in the history
  • Loading branch information
cborgolte committed May 31, 2017
2 parents b0d2519 + e9cd2c6 commit 82bf2e6
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 10 deletions.
3 changes: 2 additions & 1 deletion composition/content_merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ func (cntx *ContentMerge) collectStylesheets(f Fragment) {
}

func (cntx *ContentMerge) writeStylesheets(w io.Writer) {
for _, attrs := range cntx.stylesheets {
stylesheets := stylesheetDeduplicationStrategy.Deduplicate(cntx.stylesheets)
for _, attrs := range stylesheets {
joinedAttr := joinAttrs(attrs)
stylesheet := fmt.Sprintf("\n <link %s>", joinedAttr)
io.WriteString(w, stylesheet)
Expand Down
5 changes: 0 additions & 5 deletions composition/content_merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ import (
"golang.org/x/net/html"
)

func stylesheetAttrs(href string) []html.Attribute {
commonAttr := []html.Attribute{{Key: "rel", Val: "stylesheet"}, {Key: "type", Val: "text/css"}}
return append(commonAttr, html.Attribute{Key: "href", Val: href})
}

func Test_ContentMerge_PositiveCase(t *testing.T) {
a := assert.New(t)

Expand Down
4 changes: 4 additions & 0 deletions composition/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,7 @@ type Cache interface {
Invalidate()
PurgeEntries(keys []string)
}

type StylesheetDeduplicationStrategy interface {
Deduplicate(stylesheetAttrs [][]html.Attribute) [][]html.Attribute
}
53 changes: 53 additions & 0 deletions composition/stylesheet_deduplication.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package composition

import (
"strings"

"golang.org/x/net/html"
)

// Initialization: Sets the default deduplication strategy to be used.
func init() {
SetStrategy(new(IdentityDeduplicationStrategy))
}

// Set another deduplication strategy.
func SetStrategy(strategy StylesheetDeduplicationStrategy) {
stylesheetDeduplicationStrategy = strategy
}

// NOOP strategy.
// This stragegy will insert all found stylesheets w/o any filtering.
type IdentityDeduplicationStrategy struct {
}

func (strategy *IdentityDeduplicationStrategy) Deduplicate(stylesheets [][]html.Attribute) [][]html.Attribute {
return stylesheets
}

// Simple strategy
// Implements a very simple deduplication stragegy. That is, it filters out
// stylesheets with duplicate href value.
type SimpleDeduplicationStrategy struct {
}

// Remove duplicate entries from hrefs.
func (strategy *SimpleDeduplicationStrategy) Deduplicate(stylesheets [][]html.Attribute) (result [][]html.Attribute) {
var knownHrefs string
const delimiter = "-|-"
for _, stylesheetAttrs := range stylesheets {
hrefAttr, attrExists := getAttr(stylesheetAttrs, "href")
if !attrExists {
continue
}
href := hrefAttr.Val
if !strings.Contains(knownHrefs, href) {
result = append(result, stylesheetAttrs)
knownHrefs += delimiter + href
}
}
return result
}

// Variable to hold the active strategy
var stylesheetDeduplicationStrategy StylesheetDeduplicationStrategy
75 changes: 75 additions & 0 deletions composition/stylesheet_deduplication_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package composition

import (
"testing"

"golang.org/x/net/html"

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

func stylesheetAttrs(href string) []html.Attribute {
commonAttr := []html.Attribute{{Key: "rel", Val: "stylesheet"}, {Key: "type", Val: "text/css"}}
return append(commonAttr, html.Attribute{Key: "href", Val: href})
}

func Test_DefaultDeduplicationStrategy(t *testing.T) {
a := assert.New(t)
stylesheets := [][]html.Attribute{stylesheetAttrs("/a"), stylesheetAttrs("/b")}
result := stylesheetDeduplicationStrategy.Deduplicate(stylesheets)
a.EqualValues(stylesheets, result)
}

func Test_SimpleDeduplicationStrategy(t *testing.T) {
a := assert.New(t)
stylesheets := [][]html.Attribute{
stylesheetAttrs("/a"),
stylesheetAttrs("/b"),
stylesheetAttrs("/a"),
stylesheetAttrs("/b"),
stylesheetAttrs("/c"),
stylesheetAttrs("/a"),
}
expected := [][]html.Attribute{
stylesheetAttrs("/a"),
stylesheetAttrs("/b"),
stylesheetAttrs("/c"),
}
deduper := new(SimpleDeduplicationStrategy)
result := deduper.Deduplicate(stylesheets)
a.EqualValues(expected, result)
}

// Tests for setting an own deduplication strategy
type Strategy struct {
}

func (strategy *Strategy) Deduplicate(stylesheets [][]html.Attribute) (result [][]html.Attribute) {
for i, stylesheetAttrs := range stylesheets {
if i%2 == 0 {
result = append(result, stylesheetAttrs)
}
}
return result
}

func Test_OwnDeduplicationStrategy(t *testing.T) {
strategy := new(Strategy)
SetStrategy(strategy)

a := assert.New(t)
stylesheets := [][]html.Attribute{
stylesheetAttrs("/a"),
stylesheetAttrs("/b"),
stylesheetAttrs("/c"),
stylesheetAttrs("/d"),
stylesheetAttrs("/e"),
}
expected := [][]html.Attribute{
stylesheetAttrs("/a"),
stylesheetAttrs("/c"),
stylesheetAttrs("/e"),
}
result := stylesheetDeduplicationStrategy.Deduplicate(stylesheets)
a.EqualValues(expected, result)
}
4 changes: 0 additions & 4 deletions composition_example/example_ui_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ func Test_integration_test(t *testing.T) {
expected, err := ioutil.ReadFile("./expected_test_result.html")
expectedS := strings.Replace(string(expected), "http://127.0.0.1:8080", s.URL, -1)

// debug - don't commit!
ioutil.WriteFile("/tmp/expected", []byte(expectedS), 0644)
ioutil.WriteFile("/tmp/result", body, 0644)

a.NoError(err)
htmlEqual(t, expectedS, string(body))
}
Expand Down

0 comments on commit 82bf2e6

Please sign in to comment.