@@ -23,7 +23,14 @@ export function processNodes(filepath: string, content: string, ast: AST.Root):
2323 */
2424 const addDataAttribute = ( node : AST . RegularElement ) : void => {
2525 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 ;
2734
2835 const classAttr = node . attributes . find (
2936 ( 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):
3946 const encodedBase64Data = Buffer . from ( dataAttrValue , "utf8" ) . toString ( "base64" ) ;
4047 const dataAttribute = `data-svelte-trace="${ encodedBase64Data } "` ;
4148
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 } ` ) ;
4564 } ;
4665
4766 /**
@@ -52,6 +71,7 @@ export function processNodes(filepath: string, content: string, ast: AST.Root):
5271 addDataAttribute ( node ) ;
5372 }
5473
74+ // Handle different node structures
5575 if ( Array . isArray ( node . children ) ) {
5676 node . children . forEach ( walk ) ;
5777 }
0 commit comments