Skip to content

Commit 471d229

Browse files
committed
chore: support '
1 parent 81b4123 commit 471d229

File tree

1 file changed

+12
-6
lines changed
  • packages/vite/src/node/plugins

1 file changed

+12
-6
lines changed

packages/vite/src/node/plugins/html.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ export function getScriptInfo(node: DefaultTreeAdapterMap['element']): {
191191
return { src, sourceCodeLocation, isModule, isAsync }
192192
}
193193

194-
const attrValueWithQuotesStartRE = /=[\s\t\n\r]*"/
194+
const attrValueStartRE = /=[\s\t\n\r]*(["']|.)/
195195

196196
export function overwriteAttrValue(
197197
s: MagicString,
@@ -202,12 +202,18 @@ export function overwriteAttrValue(
202202
sourceCodeLocation!.startOffset,
203203
sourceCodeLocation!.endOffset
204204
)
205-
const valueWithQuotes = srcString.match(attrValueWithQuotesStartRE)
206-
const valueOffset =
207-
1 + (valueWithQuotes ? srcString.indexOf('"') : srcString.indexOf('='))
205+
const valueStart = srcString.match(attrValueStartRE)
206+
if (!valueStart) {
207+
// overwrite attr value can only be called for a well-defined value
208+
throw new Error(
209+
`[vite:html] internal error, failed to overwrite attribute value`
210+
)
211+
}
212+
const wrapOffset = valueStart[1] ? 1 : 0
213+
const valueOffset = valueStart.index! + valueStart[0].length - 1
208214
s.overwrite(
209-
sourceCodeLocation.startOffset + valueOffset,
210-
sourceCodeLocation.endOffset + (valueWithQuotes ? -1 : 0),
215+
sourceCodeLocation.startOffset + valueOffset + wrapOffset,
216+
sourceCodeLocation.endOffset - wrapOffset,
211217
newValue,
212218
{ contentOnly: true }
213219
)

0 commit comments

Comments
 (0)