-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patheval_attributes.go
More file actions
422 lines (360 loc) · 10.4 KB
/
Copy patheval_attributes.go
File metadata and controls
422 lines (360 loc) · 10.4 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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
package vuego
import (
"fmt"
"strconv"
"strings"
"golang.org/x/net/html"
"github.com/titpetric/vuego/internal/helpers"
)
func (v *Vue) evalAttributes(ctx VueContext, n *html.Node) (map[string]any, error) {
if n.Type != html.ElementNode {
return nil, nil
}
results := map[string]any{}
var newAttrs []html.Attribute
// First pass: collect static attributes and evaluate bound ones
for _, a := range n.Attr {
key := a.Key
val := strings.TrimSpace(a.Val)
// Skip internal attributes that hold already-evaluated content
// These should not be re-interpolated
if key == "data-v-text-content" || key == "data-v-html-content" {
newAttrs = append(newAttrs, html.Attribute{Key: key, Val: val})
continue
}
boundValue := val
boundName := key
// literal bindings
if strings.HasPrefix(key, ":") {
boundName = boundName[1:]
}
if strings.HasPrefix(key, "v-bind:") {
boundName = boundName[7:]
}
if strings.HasPrefix(key, "[") && strings.HasSuffix(key, "]") {
key = boundName
}
switch {
case boundName != key:
boundValue, err := v.evalBoundAttribute(ctx, boundName, val)
if err != nil {
return nil, fmt.Errorf("error evaluating attr %s: %w", boundName, err)
}
if !helpers.IsTruthy(boundValue) {
continue
}
results[boundName] = boundValue
default:
var err error
if containsInterpolation(val) {
boundValue, err = v.interpolate(ctx, val)
if err != nil {
return nil, fmt.Errorf("error evaluating attr %s: %w", boundName, err)
}
}
newAttrs = append(newAttrs, html.Attribute{
Key: boundName,
Val: boundValue,
})
}
}
// Second pass: merge bound attributes with static ones
for attrName, boundValue := range results {
// Check if there's a static attribute with the same name
staticIdx := -1
for i, a := range newAttrs {
if a.Key == attrName {
staticIdx = i
break
}
}
if staticIdx >= 0 {
// Merge with static attribute (special handling for class and style)
if attrName == "class" {
newAttrs[staticIdx].Val = fmt.Sprintf("%s %s", newAttrs[staticIdx].Val, boundValue)
continue
}
if attrName == "style" {
// Merge styles, with bound value taking precedence
staticStyle := newAttrs[staticIdx].Val
mergedStyle := v.mergeStyles(staticStyle, boundValue.(string))
newAttrs[staticIdx].Val = mergedStyle
continue
}
// For other attributes, bound value replaces static
newAttrs[staticIdx].Val = fmt.Sprint(boundValue)
} else {
if helpers.IsTruthy(boundValue) {
newAttrs = append(newAttrs, html.Attribute{
Key: attrName,
Val: fmt.Sprint(boundValue),
})
continue
}
}
}
n.Attr = newAttrs
// Don't overwrite results with stringified attributes
// The results map already contains the correctly typed bound values from line 39
// Only add static attributes to results (they are naturally strings from interpolation)
for _, v := range newAttrs {
// Only add if not already in results (bound attributes take precedence and keep their type)
if _, exists := results[v.Key]; !exists {
results[v.Key] = v.Val
}
}
return results, nil
}
// evalBoundAttribute evaluates a bound attribute, handling objects for class/style and function calls.
func (v *Vue) evalBoundAttribute(ctx VueContext, attrName, expr string) (any, error) {
expr = strings.TrimSpace(expr)
// Evaluate interpolation
if containsInterpolation(expr) {
interpolated, err := v.interpolate(ctx, expr)
if err != nil {
return "", err
}
return interpolated, nil
}
// Check if the expression is an object literal
if strings.HasPrefix(expr, "{") && strings.HasSuffix(expr, "}") {
return v.evalObjectBinding(ctx, attrName, expr), nil
}
// Check if it's a function call or pipe expression
if strings.Contains(expr, "|") || helpers.IsFunctionCall(expr) || helpers.IsComplexExpr(expr) {
pipe := parsePipeExpr(expr)
val, err := v.evalPipe(ctx, pipe)
if err != nil {
return "", err
}
return val, nil
}
// Regular variable binding
valResolved, ok := ctx.stack.Resolve(expr)
if ok {
return valResolved, nil
}
return "", nil
}
// evalObjectBinding evaluates object literals like {display: "none"} or {active: true, error: false}
// For :class, treats values as booleans and includes keys where value is truthy.
// For :style, treats values as strings and builds CSS property:value pairs.
func (v *Vue) evalObjectBinding(ctx VueContext, attrName, expr string) string {
expr = strings.TrimSpace(expr)
if !strings.HasPrefix(expr, "{") || !strings.HasSuffix(expr, "}") {
return ""
}
content := expr[1 : len(expr)-1] // Remove { }
pairs := v.parseObjectPairs(ctx, content)
switch attrName {
case "class":
return v.buildClassString(pairs)
case "style":
return v.buildStyleString(pairs)
}
// For other attributes, just concatenate all values
var values []string
for _, v := range pairs {
if v != "" {
values = append(values, v)
}
}
return strings.Join(values, " ")
}
// parseObjectPairs parses key:value pairs from an object literal.
// Returns a slice of resolved values in order.
func (v *Vue) parseObjectPairs(ctx VueContext, content string) []string {
var pairs []string
// Split by comma, but respect quoted strings
items := v.splitObjectItems(content)
for _, item := range items {
item = strings.TrimSpace(item)
if item == "" {
continue
}
// Split by colon
colonIdx := strings.Index(item, ":")
if colonIdx == -1 {
continue
}
key := strings.TrimSpace(item[:colonIdx])
key = strings.Trim(key, "'")
valueExpr := strings.TrimSpace(item[colonIdx+1:])
// Try to resolve as expression first (handles literals and expressions)
val, err := v.exprEval.Eval(valueExpr, ctx.ExprEnv())
if err != nil {
// Fall back to stack resolution for variable references
var ok bool
val, ok = ctx.stack.Resolve(valueExpr)
if !ok {
pairs = append(pairs, "")
continue
}
}
// Store both key and resolved value
pairs = append(pairs, fmt.Sprintf("%s:%v", key, val))
}
return pairs
}
// splitObjectItems splits comma-separated items in an object, respecting quoted strings.
func (v *Vue) splitObjectItems(content string) []string {
var items []string
var current strings.Builder
inQuotes := false
quoteChar := rune(0)
inBrackets := 0
for _, ch := range content {
switch {
case (ch == '"' || ch == '\'') && !inQuotes:
inQuotes = true
quoteChar = ch
current.WriteRune(ch)
case ch == quoteChar && inQuotes:
inQuotes = false
current.WriteRune(ch)
case ch == '{' && !inQuotes:
inBrackets++
current.WriteRune(ch)
case ch == '}' && !inQuotes:
inBrackets--
current.WriteRune(ch)
case ch == ',' && !inQuotes && inBrackets == 0:
items = append(items, current.String())
current.Reset()
default:
current.WriteRune(ch)
}
}
if current.Len() > 0 {
items = append(items, current.String())
}
return items
}
// buildClassString builds a space-separated class string from key:value pairs.
// Includes key only if the boolean value is truthy.
func (v *Vue) buildClassString(pairs []string) string {
var classes []string
for _, pair := range pairs {
pair = strings.TrimSpace(pair)
if pair == "" {
continue
}
colonIdx := strings.Index(pair, ":")
if colonIdx == -1 {
continue
}
key := strings.TrimSpace(pair[:colonIdx])
valueStr := strings.TrimSpace(pair[colonIdx+1:])
// Check if value is truthy using the actual type
val := parseValue(valueStr)
if helpers.IsTruthy(val) {
classes = append(classes, key)
}
}
return strings.Join(classes, " ")
}
// buildStyleString builds a CSS style string from key:value pairs.
// Each pair becomes a property:value; entry.
// camelCase keys are automatically converted to kebab-case (e.g., fontSize -> font-size).
func (v *Vue) buildStyleString(pairs []string) string {
var styles []string
for _, pair := range pairs {
pair = strings.TrimSpace(pair)
if pair == "" {
continue
}
colonIdx := strings.Index(pair, ":")
if colonIdx == -1 {
continue
}
key := strings.TrimSpace(pair[:colonIdx])
value := strings.TrimSpace(pair[colonIdx+1:])
// Remove quotes if present
value = strings.Trim(value, "\"'")
if value != "" {
// Convert camelCase to kebab-case if the key doesn't contain hyphens
if !strings.Contains(key, "-") {
key = camelToKebab(key)
}
styles = append(styles, key+":"+value+";")
}
}
return strings.Join(styles, "")
}
// camelToKebab converts camelCase strings to kebab-case.
// e.g., "fontSize" -> "font-size", "backgroundColor" -> "background-color"
func camelToKebab(s string) string {
var result strings.Builder
for i, r := range s {
if i > 0 && r >= 'A' && r <= 'Z' {
result.WriteRune('-')
result.WriteRune(r + 32) // Convert to lowercase
} else {
result.WriteRune(r)
}
}
return result.String()
}
// parseValue converts a string representation to a Go value for truthiness check.
func parseValue(s string) interface{} {
s = strings.TrimSpace(s)
// Handle boolean strings
switch s {
case "true":
return true
case "false":
return false
}
// Handle quoted strings
if (strings.HasPrefix(s, "\"") && strings.HasSuffix(s, "\"")) ||
(strings.HasPrefix(s, "'") && strings.HasSuffix(s, "'")) {
return strings.Trim(s, "\"'")
}
// Handle numbers - convert to int for proper truthiness checking
if i, err := strconv.ParseInt(s, 10, 64); err == nil {
return int(i)
}
if f, err := strconv.ParseFloat(s, 64); err == nil {
return f
}
return s
}
// mergeStyles merges static and bound CSS styles, with bound values taking precedence.
func (v *Vue) mergeStyles(staticStyle, boundStyle string) string {
// Parse both styles into maps
staticMap := parseStyleMap(staticStyle)
boundMap := parseStyleMap(boundStyle)
// Merge: bound values override static ones
for k, v := range boundMap {
staticMap[k] = v
}
// Rebuild style string
var styles []string
for k, v := range staticMap {
styles = append(styles, k+":"+v+";")
}
return strings.Join(styles, "")
}
// parseStyleMap parses a CSS style string into a map of properties to values.
func parseStyleMap(style string) map[string]string {
result := make(map[string]string)
if style == "" {
return result
}
// Split by semicolon to get individual properties
parts := strings.Split(style, ";")
for _, part := range parts {
part = strings.TrimSpace(part)
if part == "" {
continue
}
// Split by colon to get key-value pair
kv := strings.SplitN(part, ":", 2)
if len(kv) == 2 {
key := strings.TrimSpace(kv[0])
val := strings.TrimSpace(kv[1])
result[key] = val
}
}
return result
}