Skip to content

Commit

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

func (cntx *ContentMerge) writeStylesheets(w io.Writer) {
for _, href := range cntx.stylesheets {
stylesheets := stylesheetDeduplicationStrategy.Deduplicate(cntx.stylesheets)
for _, href := range stylesheets {
stylesheet := fmt.Sprintf("\n <link rel=\"stylesheet\" type=\"text/css\" href=\"%s\">", href)
io.WriteString(w, stylesheet)
}
Expand Down
6 changes: 5 additions & 1 deletion composition/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Fragment interface {

// MemorySize return the estimated size in bytes, for this object in memory
MemorySize() int

// Return the list of stylesheets used in this fragment
Stylesheets() []string
}
Expand Down Expand Up @@ -122,3 +122,7 @@ type Cache interface {
Invalidate()
PurgeEntries(keys []string)
}

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

import "strings"

// 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(hrefs []string) []string {
return hrefs
}

// 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(hrefs []string) []string {
var knownHrefs string
var result []string
const delimiter = "-|-"
for _, href := range hrefs {
if !strings.Contains(knownHrefs, href) {
result = append(result, href)
knownHrefs += delimiter + href
}
}
return result
}

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

import (
"testing"

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

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

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

type Strategy struct {
collecttion []string
}

func (strategy *Strategy) Deduplicate(hrefs []string) []string {
newhrefs := []string{}
for _, href := range hrefs {
newhrefs = append(newhrefs, href+"?abcdef")
strategy.collecttion = append(strategy.collecttion, href)
}
return newhrefs
}

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

a := assert.New(t)
stylesheets := []string{"a", "b"}
expected := []string{"a?abcdef", "b?abcdef"}
result := stylesheetDeduplicationStrategy.Deduplicate(stylesheets)
a.EqualValues(expected, result)
a.EqualValues(strategy.collecttion, stylesheets)
}
14 changes: 14 additions & 0 deletions composition_example/example_ui_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,17 @@ func sidebarHandler(w http.ResponseWriter, r *http.Request) {
teaserId := r.URL.Query().Get("teaser-id")
fmt.Fprintf(w, template, teaserId)
}


// Use an own deduplication strategy instead of the default one

type MyExampleDeduplicationStrategy struct {
}

func (strategy *MyExampleDeduplicationStrategy) Deduplicate(hrefs []string) []string {
return hrefs
}

func init() {
composition.SetStrategy(new(MyExampleDeduplicationStrategy))
}

0 comments on commit cd9d0a8

Please sign in to comment.