-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathutils.go
97 lines (83 loc) · 1.94 KB
/
utils.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
package transform
import (
astro "github.com/withastro/compiler/internal"
"golang.org/x/net/html/atom"
)
func hasTruthyAttr(n *astro.Node, key string) bool {
for _, attr := range n.Attr {
if attr.Key == key &&
(attr.Type == astro.EmptyAttribute) ||
(attr.Type == astro.ExpressionAttribute && attr.Val == "true") ||
(attr.Type == astro.QuotedAttribute && (attr.Val == "" || attr.Val == "true")) {
return true
}
}
return false
}
func HasSetDirective(n *astro.Node) bool {
return HasAttr(n, "set:html") || HasAttr(n, "set:text")
}
func HasInlineDirective(n *astro.Node) bool {
return HasAttr(n, "is:inline")
}
func AttrIndex(n *astro.Node, key string) int {
for i, attr := range n.Attr {
if attr.Key == key {
return i
}
}
return -1
}
func HasAttr(n *astro.Node, key string) bool {
return AttrIndex(n, key) != -1
}
func GetAttr(n *astro.Node, key string) *astro.Attribute {
for _, attr := range n.Attr {
if attr.Key == key {
return &attr
}
}
return nil
}
func IsHoistable(n *astro.Node, renderScriptEnabled bool) bool {
parent := n.Closest(func(p *astro.Node) bool {
return p.DataAtom == atom.Svg || p.DataAtom == atom.Noscript || p.DataAtom == atom.Template
})
if renderScriptEnabled && parent != nil && parent.Expression {
return true
}
return parent == nil
}
func IsImplicitNode(n *astro.Node) bool {
return HasAttr(n, astro.ImplicitNodeMarker)
}
func IsImplicitNodeMarker(attr astro.Attribute) bool {
return attr.Key == astro.ImplicitNodeMarker
}
func IsTopLevel(n *astro.Node) bool {
if IsImplicitNode(n) || n.Data == "" {
return false
}
p := n.Parent
if p == nil {
return true
}
if IsImplicitNode(p) || p.Data == "" {
return true
}
if p.Component {
return IsTopLevel(p)
}
return false
}
func GetQuotedAttr(n *astro.Node, key string) string {
for _, attr := range n.Attr {
if attr.Key == key {
if attr.Type == astro.QuotedAttribute {
return attr.Val
}
return ""
}
}
return ""
}