@@ -23,7 +23,14 @@ export function processNodes(filepath: string, content: string, ast: AST.Root):
23
23
*/
24
24
const addDataAttribute = ( node : AST . RegularElement ) : void => {
25
25
if ( node . type !== "RegularElement" ) return ;
26
- if ( node . name === "script" || node . name === "style" ) return ;
26
+ if (
27
+ node . name === "script" ||
28
+ node . name === "style" ||
29
+ node . name === "meta" ||
30
+ node . name === "link" ||
31
+ node . name === "title"
32
+ )
33
+ return ;
27
34
28
35
const classAttr = node . attributes . find (
29
36
( attr ) : attr is AST . Attribute => attr . type === "Attribute" && attr . name === "class"
@@ -39,9 +46,21 @@ export function processNodes(filepath: string, content: string, ast: AST.Root):
39
46
const encodedBase64Data = Buffer . from ( dataAttrValue , "utf8" ) . toString ( "base64" ) ;
40
47
const dataAttribute = `data-svelte-trace="${ encodedBase64Data } "` ;
41
48
42
- // Always insert before the closing '>' to make it the last attribute
43
- const tagEnd = content . indexOf ( ">" , node . start ) ;
44
- magicString . appendLeft ( tagEnd , ` ${ dataAttribute } ` ) ;
49
+ // Find the correct insertion point based on whether it's self-closing or not
50
+ let insertionPoint : number ;
51
+
52
+ // If there are attributes, insert after the last attribute
53
+ if ( node . attributes . length > 0 ) {
54
+ const lastAttr = node . attributes [ node . attributes . length - 1 ] ;
55
+ insertionPoint = lastAttr . end ;
56
+ }
57
+ // If no attributes, insert after the tag name. Included +1 for '<'
58
+ else {
59
+ const tagNameEnd = node . start + 1 + node . name . length ;
60
+ insertionPoint = tagNameEnd ;
61
+ }
62
+
63
+ magicString . appendLeft ( insertionPoint , ` ${ dataAttribute } ` ) ;
45
64
} ;
46
65
47
66
/**
@@ -52,6 +71,7 @@ export function processNodes(filepath: string, content: string, ast: AST.Root):
52
71
addDataAttribute ( node ) ;
53
72
}
54
73
74
+ // Handle different node structures
55
75
if ( Array . isArray ( node . children ) ) {
56
76
node . children . forEach ( walk ) ;
57
77
}
0 commit comments