MCPcopy
hub / github.com/vitejs/vite / overwriteAttrValue

Function overwriteAttrValue

packages/vite/src/node/plugins/html.ts:257–281  ·  view source on GitHub ↗
(
  s: MagicString,
  sourceCodeLocation: Token.Location,
  newValue: string,
)

Source from the content-addressed store, hash-verified

255const attrValueStartRE = /=\s*(.)/
256
257export function overwriteAttrValue(
258 s: MagicString,
259 sourceCodeLocation: Token.Location,
260 newValue: string,
261): MagicString {
262 const srcString = s.slice(
263 sourceCodeLocation.startOffset,
264 sourceCodeLocation.endOffset,
265 )
266 const valueStart = attrValueStartRE.exec(srcString)
267 if (!valueStart) {
268 // overwrite attr value can only be called for a well-defined value
269 throw new Error(
270 `[vite:html] internal error, failed to overwrite attribute value`,
271 )
272 }
273 const wrapOffset = valueStart[1] === '"' || valueStart[1] === "'" ? 1 : 0
274 const valueOffset = valueStart.index! + valueStart[0].length - 1
275 s.update(
276 sourceCodeLocation.startOffset + valueOffset + wrapOffset,
277 sourceCodeLocation.endOffset - wrapOffset,
278 newValue,
279 )
280 return s
281}
282
283export function removeViteIgnoreAttr(
284 s: MagicString,

Callers 2

devHtmlHookFunction · 0.90
handlerFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected