-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwriteattr.go
207 lines (175 loc) · 4.84 KB
/
writeattr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package fir
import (
"bytes"
"fmt"
"strings"
"slices"
"github.com/valyala/bytebufferpool"
"golang.org/x/net/html"
)
func htmlNodeToBytes(n *html.Node) []byte {
return []byte(htmlNodetoString(n))
}
func htmlNodetoString(n *html.Node) string {
buf := bytebufferpool.Get()
defer bytebufferpool.Put(buf)
err := html.Render(buf, n)
if err != nil {
panic(fmt.Sprintf("failed to render HTML: %v", err))
}
return html.UnescapeString(buf.String())
}
// Recursive function to set the "fir-key" attribute to all nested children
func setKeyToChildren(node *html.Node, key string) {
if node == nil || node.Type != html.ElementNode {
return
}
if key == "" {
for _, attr := range node.Attr {
if attr.Key == "fir-key" {
key = attr.Val
break
}
}
} else {
for _, attr := range node.Attr {
if attr.Key == "fir-key" {
if key != attr.Val {
setKeyToChildren(node, attr.Val)
}
break
}
}
}
if key == "" {
return
}
for child := node.FirstChild; child != nil; child = child.NextSibling {
setKeyToChildren(child, key)
if child.Type == html.ElementNode {
hasPrefixAttribute := false
for _, attr := range child.Attr {
if strings.HasPrefix(attr.Key, "@") || strings.HasPrefix(attr.Key, "x-on") {
hasPrefixAttribute = true
break
}
}
if !hasPrefixAttribute {
continue
}
hasKeyAttribute := false
for _, attr := range child.Attr {
if attr.Key == "fir-key" {
hasKeyAttribute = true
break
}
}
if !hasKeyAttribute {
child.Attr = append(child.Attr, html.Attribute{Key: "fir-key", Val: key})
}
}
}
}
func removeAttr(n *html.Node, attr string) {
for i, a := range n.Attr {
if a.Key == attr {
n.Attr = append(n.Attr[:i], n.Attr[i+1:]...)
break
}
}
}
func hasAttr(n *html.Node, attr string) bool {
for _, a := range n.Attr {
if a.Key == attr {
return true
}
}
return false
}
func setAttr(n *html.Node, key, val string) {
n.Attr = append(n.Attr, html.Attribute{Key: key, Val: val})
}
func getAttr(n *html.Node, key string) string {
for _, a := range n.Attr {
if a.Key == key {
return a.Val
}
}
return ""
}
func addAttributes(content []byte) []byte {
doc, err := html.Parse(bytes.NewReader(content))
if err != nil {
panic(err)
}
writeAttributes(doc)
return htmlNodeToBytes(doc)
}
func writeAttributes(node *html.Node) {
if node.Type == html.ElementNode {
setKeyToChildren(node, "")
attrMap := make(map[string]string)
for _, attr := range node.Attr {
attrMap[attr.Key] = attr.Val
}
for attrKey, attrVal := range attrMap {
if !strings.HasPrefix(attrKey, "@fir:") && !strings.HasPrefix(attrKey, "x-on:fir:") {
continue
}
eventns := strings.TrimPrefix(attrKey, "@fir:")
eventns = strings.TrimPrefix(eventns, "x-on:fir:")
// eventns might have modifiers like .prevent, .stop, .self, .once, .window, .document etc. remove them
eventnsParts := strings.SplitN(eventns, ".", -1)
var modifiers string
if len(eventnsParts) > 0 {
eventns = eventnsParts[0]
}
if len(eventnsParts) > 1 {
modifierParts := eventnsParts[1:]
modifierParts = slices.DeleteFunc(modifierParts, func(s string) bool {
return s == "nohtml"
})
modifiers = strings.Join(modifierParts, ".")
}
// eventns might have a filter:[e1:ok,e2:ok] containing multiple event:state separated by comma
eventnsList, filterExists := getEventNsList(eventns)
// if filter exists remove the current attribute from the node
if filterExists {
removeAttr(node, attrKey)
}
for _, eventns := range eventnsList {
if strings.Contains(eventns, ":pending") || strings.Contains(eventns, ":done") {
// remove template from the eventns if it exists
parts := strings.Split(eventns, "::")
if len(parts) == 2 {
eventns = parts[0]
}
}
eventns = strings.TrimSpace(eventns)
// set @fir|x-on:fir:eventns attribute to the node
eventnsWithModifiers := fmt.Sprintf("%s.%s", eventns, modifiers)
if len(modifiers) == 0 {
eventnsWithModifiers = eventns
}
atFirOk := hasAttr(node, fmt.Sprintf("@fir:%s", eventnsWithModifiers))
xOnFirOk := hasAttr(node, fmt.Sprintf("x-on:fir:%s", eventnsWithModifiers))
// if the node already has @fir:x attribute, then skip
if !atFirOk && !xOnFirOk {
setAttr(node, fmt.Sprintf("@fir:%s", eventnsWithModifiers), attrVal)
}
// set class fir-myevent-ok--myblock
key := getAttr(node, "fir-key")
targetClass := fmt.Sprintf("fir-%s", getClassNameWithKey(eventns, &key))
classes := strings.Fields(getAttr(node, "class"))
if !slices.Contains(classes, targetClass) {
classes = append(classes, targetClass)
removeAttr(node, "class")
node.Attr = append(node.Attr, html.Attribute{Key: "class", Val: strings.Join(classes, " ")})
}
}
}
}
for c := node.FirstChild; c != nil; c = c.NextSibling {
writeAttributes(c)
}
}