Skip to content

Commit 3369786

Browse files
committed
feat: Allow extend/include nodes to apply attribute overwrites
1 parent e3272c2 commit 3369786

11 files changed

Lines changed: 62 additions & 3 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION := v0.1.1
1+
VERSION := v0.1.2
22
BINARY_NAME := mend
33

44
run:

example/header.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<header>
2-
<mend:include src="icon.svg" :color="{{ .color }}" />
2+
<mend:include src="icon.svg" @fill="{{ .color }}" />
33
This is a header!
44
</header>

example/icon.svg

Lines changed: 1 addition & 1 deletion
Loading

mend/attrs/attributes.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,28 @@ func (attrs Attributes) ParamKeys() map[string]string {
4040
return out
4141
}
4242

43+
func (attrs Attributes) InheritAttributes() Attributes {
44+
out := New([]html.Attribute{})
45+
for _, key := range attrs.order {
46+
if strings.HasPrefix(key, "@") {
47+
out.order = append(out.order, key[1:])
48+
out.values[key[1:]] = attrs.values[key]
49+
}
50+
}
51+
return out
52+
}
53+
54+
func (attrs Attributes) Merge(overwrite Attributes) Attributes {
55+
for _, key := range overwrite.order {
56+
_, ok := attrs.values[key]
57+
if !ok {
58+
attrs.order = append(attrs.order, key)
59+
}
60+
attrs.values[key] = overwrite.values[key]
61+
}
62+
return attrs
63+
}
64+
4365
func (attrs Attributes) ParseExpressions(
4466
source string,
4567
fn func(string, string) (string, error),

mend/tags/node.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type Node interface {
1515
ParseExpressions(string, expressionFunc) error
1616
ReplaceText(text string, with string)
1717
Clone() Node
18+
MergeAttributes(attrs attrs.Attributes) (ok bool)
1819
}
1920

2021
type NodeWithChildren interface {
@@ -114,3 +115,12 @@ func (node *pairedNode) Clone() *pairedNode {
114115
clone.Children = children
115116
return &clone
116117
}
118+
119+
func (node *pairedNode) MergeAttributes(attrs attrs.Attributes) bool {
120+
for _, child := range node.Children {
121+
if child.MergeAttributes(attrs) {
122+
return true
123+
}
124+
}
125+
return false
126+
}

mend/tags/node_comment.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"strings"
66

7+
"github.com/bbfh-dev/mend/mend/attrs"
78
"github.com/bbfh-dev/mend/mend/settings"
89
)
910

@@ -40,3 +41,7 @@ func (node *CommentNode) Clone() Node {
4041
clone := *node
4142
return &clone
4243
}
44+
45+
func (node *CommentNode) MergeAttributes(attrs attrs.Attributes) bool {
46+
return false
47+
}

mend/tags/node_doctype.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package tags
33
import (
44
"fmt"
55

6+
"github.com/bbfh-dev/mend/mend/attrs"
67
"github.com/bbfh-dev/mend/mend/settings"
78
)
89

@@ -36,3 +37,7 @@ func (node *DoctypeNode) Clone() Node {
3637
clone := *node
3738
return &clone
3839
}
40+
41+
func (node *DoctypeNode) MergeAttributes(attrs attrs.Attributes) bool {
42+
return false
43+
}

mend/tags/node_tag.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,8 @@ func (node *TagNode) Clone() Node {
5656
clone.pairedNode = clone.pairedNode.Clone()
5757
return &clone
5858
}
59+
60+
func (node *TagNode) MergeAttributes(attrs attrs.Attributes) bool {
61+
node.Attributes = node.Attributes.Merge(attrs)
62+
return true
63+
}

mend/tags/node_text.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package tags
33
import (
44
"strings"
55

6+
"github.com/bbfh-dev/mend/mend/attrs"
67
"github.com/bbfh-dev/mend/mend/settings"
78
)
89

@@ -39,3 +40,7 @@ func (node *TextNode) Clone() Node {
3940
clone := *node
4041
return &clone
4142
}
43+
44+
func (node *TextNode) MergeAttributes(attrs attrs.Attributes) bool {
45+
return false
46+
}

mend/tags/node_void.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,8 @@ func (node *VoidNode) Clone() Node {
4141
clone := *node
4242
return &clone
4343
}
44+
45+
func (node *VoidNode) MergeAttributes(attrs attrs.Attributes) bool {
46+
node.Attributes = node.Attributes.Merge(attrs)
47+
return true
48+
}

0 commit comments

Comments
 (0)