Skip to content

Commit

Permalink
Merge / Overwrite body attributes.
Browse files Browse the repository at this point in the history
When merging attributes of the <body> Element,
all existing class attributes will be merged into one.
All other existing attributes will be ov overwritten by new ones.
  • Loading branch information
cborgolte committed Aug 3, 2017
1 parent 6fc4386 commit 1ca11d9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
33 changes: 29 additions & 4 deletions composition/content_merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,34 @@ func generateExecutionFunction(cntx *ContentMerge, w io.Writer) (executeFragment
return executeFragment
}

func collectBodyAttrs(bodyAttrs [][]html.Attribute) string {
var result map[string]string = make(map[string]string)
for _, attrs := range bodyAttrs {
for _, attr := range attrs {
val, exists := result[attr.Key]
if strings.ToLower(attr.Key) == "class" {
// aggregate all class attributes
var newVal string
if exists {
newVal = val + " "
}
newVal = newVal + attr.Val
result[attr.Key] = newVal
} else {
// but overwrite others
result[attr.Key] = attr.Val
}
}
}

var sResult string
for k, v := range result {
sResult = sResult + fmt.Sprintf(` %s="%s"`, k, v)
}

return sResult
}

func (cntx *ContentMerge) GetHtml() ([]byte, error) {

if len(cntx.priorities) > 0 {
Expand All @@ -117,10 +145,7 @@ func (cntx *ContentMerge) GetHtml() ([]byte, error) {
// open body tag
body := bytes.NewBuffer(make([]byte, 0, DefaultBufferSize))
io.WriteString(body, "\n <body")
for _, a := range cntx.BodyAttrs {
io.WriteString(body, " ")
io.WriteString(body, joinAttrs(a))
}
io.WriteString(body, collectBodyAttrs(cntx.BodyAttrs))

io.WriteString(body, ">\n ")

Expand Down
22 changes: 17 additions & 5 deletions composition/content_merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package composition

import (
"errors"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -24,15 +25,15 @@ func Test_ContentMerge_MergeBodyAttributes(t *testing.T) {
cm.AddContent(&MemoryContent{
name: LayoutFragmentName,
head: NewStringFragment("<page1-head/>\n"),
bodyAttributes: htmlAttributes(map[string]string{"a": "b", "class": "class1 class2"}),
bodyAttributes: htmlAttributes(map[string]string{"a": "b", "foo": "bar0", "class": "class1 class2"}),
tail: NewStringFragment(" <page1-tail/>\n"),
body: map[string]Fragment{"": layout},
}, 0)

cm.AddContent(&MemoryContent{
name: "example.com",
head: NewStringFragment(" <page2-head/>\n"),
bodyAttributes: htmlAttributes(map[string]string{"foo": "bar", "class": "class3"}),
bodyAttributes: htmlAttributes(map[string]string{"foo": "bar1", "class": "class3"}),
tail: NewStringFragment(" <page2-tail/>"),
body: map[string]Fragment{
"page2-a": NewStringFragment("<page2-body-a/>"),
Expand All @@ -42,15 +43,26 @@ func Test_ContentMerge_MergeBodyAttributes(t *testing.T) {
cm.AddContent(&MemoryContent{
name: "page3",
head: NewStringFragment(" <page3-head/>"),
bodyAttributes: htmlAttributes(map[string]string{"foo": "bar", "class": "class4"}),
bodyAttributes: htmlAttributes(map[string]string{"foo": "bar2", "class": "class4"}),
body: map[string]Fragment{
"": NewStringFragment("<page3-body-a/>"),
}}, MAX_PRIORITY) // just to trigger the priority-parsing and see that it doesn't crash..

html, err := cm.GetHtml()
a.Contains(string(html), `<body`)
bodyElement := strings.SplitN(string(html), "<body", 2)[1]
bodyElement = strings.SplitN(bodyElement, ">", 2)[0]
a.NoError(err)
expectedBody := `<body a="b" class="class1 class2" foo="bar" class="class3" foo="bar" class="class4">`
a.Contains(string(html), expectedBody)
// expect class attributes to be aggregated, all others to be overwritten (here: foo)
a.Contains(bodyElement, `a="b"`)
a.Contains(bodyElement, `foo="bar2"`)
a.NotContains(bodyElement, `foo="bar0"`)
a.NotContains(bodyElement, `foo="bar1"`)
a.Contains(bodyElement, `class="class1 class2 class3 class4"`)
// assure, there are no additional class attributes
a.NotContains(bodyElement, `class="class1 class2"`)
a.NotContains(bodyElement, `class="class3 class4"`)
a.NotContains(bodyElement, `class="class4"`)
}

func Test_ContentMerge_PositiveCase(t *testing.T) {
Expand Down

0 comments on commit 1ca11d9

Please sign in to comment.