Skip to content

Commit

Permalink
chore: support '
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev committed Aug 21, 2022
1 parent 81b4123 commit 471d229
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions packages/vite/src/node/plugins/html.ts
Expand Up @@ -191,7 +191,7 @@ export function getScriptInfo(node: DefaultTreeAdapterMap['element']): {
return { src, sourceCodeLocation, isModule, isAsync }
}

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

export function overwriteAttrValue(
s: MagicString,
Expand All @@ -202,12 +202,18 @@ export function overwriteAttrValue(
sourceCodeLocation!.startOffset,
sourceCodeLocation!.endOffset
)
const valueWithQuotes = srcString.match(attrValueWithQuotesStartRE)
const valueOffset =
1 + (valueWithQuotes ? srcString.indexOf('"') : srcString.indexOf('='))
const valueStart = srcString.match(attrValueStartRE)
if (!valueStart) {
// overwrite attr value can only be called for a well-defined value
throw new Error(
`[vite:html] internal error, failed to overwrite attribute value`
)
}
const wrapOffset = valueStart[1] ? 1 : 0
const valueOffset = valueStart.index! + valueStart[0].length - 1
s.overwrite(
sourceCodeLocation.startOffset + valueOffset,
sourceCodeLocation.endOffset + (valueWithQuotes ? -1 : 0),
sourceCodeLocation.startOffset + valueOffset + wrapOffset,
sourceCodeLocation.endOffset - wrapOffset,
newValue,
{ contentOnly: true }
)
Expand Down

0 comments on commit 471d229

Please sign in to comment.