@@ -137,19 +137,31 @@ function extractAttributes(
137
137
ctx : Context ,
138
138
) {
139
139
const script = element . type === "SvelteScriptElement"
140
- const code =
141
- " " . repeat ( element . range [ 0 ] ) +
142
- ctx . sourceCode . template
143
- . slice ( ...element . range )
144
- . replace (
145
- script
146
- ? / < s c r i p t ( \s [ \s \S ] * ?) ? > ( [ \s \S ] * ?) < \/ s c r i p t > / giu
147
- : / < s t y l e ( \s [ \s \S ] * ?) ? > ( [ \s \S ] * ?) < \/ s t y l e > / giu,
148
- ( _tag , attributes : string | undefined , context : string ) =>
149
- `${ script ? "<div " : "<div " } ${
150
- attributes || ""
151
- } >${ " " . repeat ( context . length ) } </div>`,
152
- )
140
+
141
+ let code = " " . repeat ( element . range [ 0 ] )
142
+
143
+ const elementCode = ctx . sourceCode . template . slice ( ...element . range )
144
+ const startRegex = script
145
+ ? / < s c r i p t ( \s [ \s \S ] * ?) ? > / giu
146
+ : / < s t y l e ( \s [ \s \S ] * ?) ? > / giu
147
+ const endTag = script ? "</script>" : "</style>"
148
+ let re
149
+ let index = 0
150
+ while ( ( re = startRegex . exec ( elementCode ) ) ) {
151
+ const [ , attributes ] = re
152
+
153
+ const endTagIndex = elementCode . indexOf ( endTag , startRegex . lastIndex )
154
+ if ( endTagIndex >= 0 ) {
155
+ const contextLength = endTagIndex - startRegex . lastIndex
156
+ code += elementCode . slice ( index , re . index )
157
+ code += `${ script ? "<div " : "<div " } ${ attributes || "" } >`
158
+ code += `${ " " . repeat ( contextLength ) } </div>`
159
+ startRegex . lastIndex = index = endTagIndex + endTag . length
160
+ } else {
161
+ break
162
+ }
163
+ }
164
+ code += elementCode . slice ( index )
153
165
const svelteAst = parse ( code ) as SvAST . Ast
154
166
155
167
const fakeElement = svelteAst . html . children . find (
0 commit comments