Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: migrate from vue/compiler-dom to parse5 #9678

Merged
merged 10 commits into from
Aug 24, 2022
Prev Previous commit
Next Next commit
chore: support '
  • Loading branch information
patak-dev committed Aug 21, 2022
commit 471d2299ef5137c8d625c43e57f50fbf2dbd3262
18 changes: 12 additions & 6 deletions packages/vite/src/node/plugins/html.ts
Original file line number Diff line number Diff line change
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