Skip to content

Commit

Permalink
Keep up backward compatibility.
Browse files Browse the repository at this point in the history
  • Loading branch information
cborgolte committed Aug 28, 2017
1 parent 1ca11d9 commit 4ea38de
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 17 deletions.
35 changes: 25 additions & 10 deletions composition/content_merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"io"
"strings"

"github.com/tarent/lib-compose/logging"

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

Expand All @@ -19,9 +21,10 @@ const (
// ContentMerge is a helper type for creation of a combined html document
// out of multiple Content pages.
type ContentMerge struct {
MetaJSON map[string]interface{}
Head []Fragment
BodyAttrs [][]html.Attribute
MetaJSON map[string]interface{}
Head []Fragment
BodyAttrs []Fragment
BodyAttrsArray [][]html.Attribute

// Aggregator for the Body Fragments of the results.
// Each fragment is insertes twice with full name and local name,
Expand Down Expand Up @@ -98,8 +101,9 @@ func generateExecutionFunction(cntx *ContentMerge, w io.Writer) (executeFragment

func collectBodyAttrs(bodyAttrs [][]html.Attribute) string {
var result map[string]string = make(map[string]string)
for _, attrs := range bodyAttrs {
for _, attr := range attrs {
for i := range bodyAttrs {
for j := range bodyAttrs[i] {
attr := &bodyAttrs[i][j]
val, exists := result[attr.Key]
if strings.ToLower(attr.Key) == "class" {
// aggregate all class attributes
Expand Down Expand Up @@ -145,8 +149,7 @@ func (cntx *ContentMerge) GetHtml() ([]byte, error) {
// open body tag
body := bytes.NewBuffer(make([]byte, 0, DefaultBufferSize))
io.WriteString(body, "\n <body")
io.WriteString(body, collectBodyAttrs(cntx.BodyAttrs))

io.WriteString(body, collectBodyAttrs(cntx.BodyAttrsArray))
io.WriteString(body, ">\n ")

startFragmentName := ""
Expand Down Expand Up @@ -199,7 +202,12 @@ func (cntx *ContentMerge) GetBodyFragmentByName(name string) (Fragment, bool) {

func (cntx *ContentMerge) AddContent(c Content, priority int) {
cntx.addHead(c.Head())
cntx.addBodyAttributes(c.BodyAttributes())
content2, ok := c.(Content2)
if ok {
cntx.addBodyAttributesArray(content2.BodyAttributesArray())
} else {
cntx.addBodyAttributes(c.BodyAttributes())
}
cntx.addBody(c)
cntx.addTail(c.Tail())
if priority > 0 {
Expand All @@ -213,9 +221,16 @@ func (cntx *ContentMerge) addHead(f Fragment) {
}
}

func (cntx *ContentMerge) addBodyAttributes(a []html.Attribute) {
func (cntx *ContentMerge) addBodyAttributes(f Fragment) {
logging.Logger.Warnf("Called deprecated function addBodyAttributes(). This content will not be rendered.")
if f != nil {
cntx.BodyAttrs = append(cntx.BodyAttrs, f)
}
}

func (cntx *ContentMerge) addBodyAttributesArray(a []html.Attribute) {
if a != nil {
cntx.BodyAttrs = append(cntx.BodyAttrs, a)
cntx.BodyAttrsArray = append(cntx.BodyAttrsArray, a)
}
}

Expand Down
5 changes: 4 additions & 1 deletion composition/html_content_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,10 @@ func Test_HtmlContentParser_parseBody(t *testing.T) {
`§[> local]§`, c.Body()["content"])
eqFragment(t, "<!-- tail -->§[> example.com/tail]§", c.Tail())

a.Equal(`some="attribute"`, joinAttrs(c.BodyAttributes()))
a.Equal(`some="attribute"`, joinAttrs(c.BodyAttributesArray()))

// check deprecated BodyAttributes() method
eqFragment(t, `some="attribute"`, c.BodyAttributes())

a.Equal(5, len(c.Dependencies()))
a.Equal(c.Dependencies()["example.com/xyz"], Params{"foo": "bar", "bazz": "buzz"})
Expand Down
14 changes: 12 additions & 2 deletions composition/interface_mocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,26 @@ func (_mr *_MockContentRecorder) Body() *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "Body")
}

func (_m *MockContent) BodyAttributes() []html.Attribute {
func (_m *MockContent) BodyAttributes() Fragment {
ret := _m.ctrl.Call(_m, "BodyAttributes")
ret0, _ := ret[0].([]html.Attribute)
ret0, _ := ret[0].(Fragment)
return ret0
}

func (_mr *_MockContentRecorder) BodyAttributes() *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "BodyAttributes")
}

func (_m *MockContent) BodyAttributesArray() []html.Attribute {
ret := _m.ctrl.Call(_m, "BodyAttributesArray")
ret0, _ := ret[0].([]html.Attribute)
return ret0
}

func (_mr *_MockContentRecorder) BodyAttributesArray() *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "BodyAttributesArray")
}

func (_m *MockContent) Dependencies() map[string]Params {
ret := _m.ctrl.Call(_m, "Dependencies")
ret0, _ := ret[0].(map[string]Params)
Expand Down
13 changes: 10 additions & 3 deletions composition/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type CacheStrategy interface {
// Params is a value type for a parameter map
type Params map[string]string

// Vontent is the abstration over includable data.
// Content is the abstraction over includable data.
// Content may be parsed of it may contain a stream represented by a non nil Reader(), not both.
type Content interface {

Expand Down Expand Up @@ -82,8 +82,8 @@ type Content interface {
// e.g. a script to load after rendering.
Tail() Fragment

// The attributes for the body element
BodyAttributes() []html.Attribute
// Deprecated: Return the attributes for the body element as Fragment.
BodyAttributes() Fragment

// Reader returns the stream with the content, of any.
// If Reader() == nil, no stream is available an it contains parsed data, only.
Expand All @@ -99,6 +99,13 @@ type Content interface {
MemorySize() int
}

type Content2 interface {
Content

// Return the attributes for the body element as array.
BodyAttributesArray() []html.Attribute
}

type ContentMerger interface {
// Add content to the merger
AddContent(c Content, priority int)
Expand Down
7 changes: 6 additions & 1 deletion composition/memory_content.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,12 @@ func (c *MemoryContent) Tail() Fragment {
return c.tail
}

func (c *MemoryContent) BodyAttributes() []html.Attribute {
// Deprecated: This method is deprecated
func (c *MemoryContent) BodyAttributes() Fragment {
return NewStringFragment(joinAttrs(c.bodyAttributes))
}

func (c *MemoryContent) BodyAttributesArray() []html.Attribute {
return c.bodyAttributes
}

Expand Down

0 comments on commit 4ea38de

Please sign in to comment.