-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.go
More file actions
298 lines (264 loc) · 5.92 KB
/
node.go
File metadata and controls
298 lines (264 loc) · 5.92 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package restache
import (
"slices"
"golang.org/x/net/html/atom"
)
type NodeType uint32
const (
ErrorNode NodeType = iota
TextNode
ComponentNode
ElementNode
CommentNode
VariableNode
RangeNode
WhenNode
UnlessNode
)
type Attribute struct {
Key string
KeyAtom atom.Atom
Val string
IsExpr bool
}
type PathComponent struct {
Key string
IsRange bool
}
type Node struct {
Parent, FirstChild, LastChild, PrevSibling, NextSibling *Node
Type NodeType
DataAtom atom.Atom
Data string
Attr []Attribute
Path []PathComponent
}
func (n *Node) TagName() string {
if n.DataAtom != 0 {
return n.DataAtom.String()
}
return n.Data
}
// InsertBefore inserts newChild as a child of n, immediately before oldChild
// in the sequence of n's children. oldChild may be nil, in which case newChild
// is appended to the end of n's children.
//
// It will panic if newChild already has a parent or siblings.
func (n *Node) InsertBefore(newChild, oldChild *Node) {
if newChild.Parent != nil || newChild.PrevSibling != nil || newChild.NextSibling != nil {
panic("restache: InsertBefore called for an attached child Node")
}
var prev, next *Node
if oldChild != nil {
prev, next = oldChild.PrevSibling, oldChild
} else {
prev = n.LastChild
}
if prev != nil {
prev.NextSibling = newChild
} else {
n.FirstChild = newChild
}
if next != nil {
next.PrevSibling = newChild
} else {
n.LastChild = newChild
}
newChild.Parent = n
newChild.PrevSibling = prev
newChild.NextSibling = next
}
func (n *Node) Render(w writer) (int, error) {
return Render(w, n)
}
// AppendChild adds a node c as a child of n.
//
// It will panic if c already has a parent or siblings.
func (n *Node) AppendChild(c *Node) {
if c.Parent != nil || c.PrevSibling != nil || c.NextSibling != nil {
panic("restache: AppendChild called for an attached child Node")
}
last := n.LastChild
if last != nil {
last.NextSibling = c
} else {
n.FirstChild = c
}
n.LastChild = c
c.Parent = n
c.PrevSibling = last
}
// RemoveChild removes a node c that is a child of n. Afterwards, c will have
// no parent and no siblings.
//
// It will panic if c's parent is not n.
func (n *Node) RemoveChild(c *Node) {
if c.Parent != n {
panic("restache: RemoveChild called for a non-child Node")
}
if n.FirstChild == c {
n.FirstChild = c.NextSibling
}
if c.NextSibling != nil {
c.NextSibling.PrevSibling = c.PrevSibling
}
if n.LastChild == c {
n.LastChild = c.PrevSibling
}
if c.PrevSibling != nil {
c.PrevSibling.NextSibling = c.NextSibling
}
c.Parent = nil
c.PrevSibling = nil
c.NextSibling = nil
}
func (n *Node) wrapChildrenInFragment() {
first := n.FirstChild
if first == nil {
// range with empty body; <></>
n.AppendChild(&Node{Type: ElementNode})
return
}
// range with exactly one element child; prepend key attr
if n.Type == RangeNode &&
first.NextSibling == nil &&
first.Type == ElementNode {
first.Attr = append([]Attribute{{
Key: "key",
Val: "key",
IsExpr: true,
}}, first.Attr...)
return
}
if first.NextSibling == nil && !(first.Type == TextNode || first.Type == CommentNode) {
return // already a single non-text node; no fragment needed
}
// for other scenarios, add fragment
frag := &Node{
Type: ElementNode,
Path: slices.Clone(n.Path),
}
if n.Type == RangeNode {
frag.Data = "React.Fragment"
frag.Attr = []Attribute{{
Key: "key",
Val: "key",
IsExpr: true,
}}
}
for c := n.FirstChild; c != nil; {
next := c.NextSibling
n.RemoveChild(c)
frag.AppendChild(c)
c = next
}
n.AppendChild(frag)
}
func (n *Node) nameEquals(name []byte) bool {
if len(n.Data) != len(name) {
return false
}
for i := range n.Data {
if n.Data[i] != name[i] {
return false
}
}
return true
}
// extractUnknownElementTags returns the .Data of every ElementNode whose DataAtom == 0,
// without duplicates, in depth-first (pre-order) order.
func (n *Node) extractUnknownElementTags() []string {
if n == nil {
return nil
}
seen := make(map[string]struct{}, 16) // seen .Data values
out := make([]string, 0, 8)
stack := []*Node{n.FirstChild}
for len(stack) > 0 {
i := len(stack) - 1
c := stack[i]
stack = stack[:i]
for c != nil {
if c.Type == ElementNode && c.DataAtom == 0 {
if _, ok := seen[c.Data]; !ok {
seen[c.Data] = struct{}{}
out = append(out, c.Data)
}
}
if nx := c.NextSibling; nx != nil {
stack = append(stack, nx)
}
c = c.FirstChild
}
}
return out
}
func (n *Node) renameUnknownElementTags(rewrites map[string]string) {
if n == nil {
return
}
var stack nodeStack
if n.FirstChild != nil {
stack = append(stack, n.FirstChild)
}
for len(stack) > 0 {
c := stack.pop()
for c != nil {
if c.Type == ElementNode && c.DataAtom == 0 {
if newVal, ok := rewrites[c.Data]; ok {
c.Data = newVal
}
}
if next := c.NextSibling; next != nil {
stack = append(stack, next)
}
c = c.FirstChild
}
}
}
// nodeStack is a stack of nodes.
type nodeStack []*Node
// pop pops the stack. It will panic if s is empty.
func (s *nodeStack) pop() *Node {
i := len(*s)
n := (*s)[i-1]
*s = (*s)[:i-1]
return n
}
// top returns the most recently pushed node, or nil if s is empty.
func (s *nodeStack) top() *Node {
if i := len(*s); i > 0 {
return (*s)[i-1]
}
return nil
}
func (s *nodeStack) popUntilAtom(a atom.Atom) bool {
for i := len(*s) - 1; i >= 0; i-- {
n := (*s)[i]
if n.Type == ElementNode && n.DataAtom == a {
*s = (*s)[:i]
return true
}
}
return false
}
func (s *nodeStack) popUntilName(name []byte) bool {
for i := len(*s) - 1; i >= 0; i-- {
n := (*s)[i]
if n.Type == ElementNode && n.nameEquals(name) {
*s = (*s)[:i]
return true
}
}
return false
}
func (s *nodeStack) popControl(name []byte) (*Node, bool) {
for len(*s) > 1 {
n := s.pop()
if (n.Type == RangeNode || n.Type == WhenNode || n.Type == UnlessNode) &&
n.nameEquals(name) {
return n, true
}
}
return nil, false
}