-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
134 additions
and
10 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
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
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
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,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 |
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,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) | ||
} |
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