diff --git a/packages/vite/src/node/plugins/html.ts b/packages/vite/src/node/plugins/html.ts index a3df68ca539602..f09c2a6fcd89b0 100644 --- a/packages/vite/src/node/plugins/html.ts +++ b/packages/vite/src/node/plugins/html.ts @@ -196,7 +196,7 @@ export function getScriptInfo(node: DefaultTreeAdapterMap['element']): { return { src, sourceCodeLocation, isModule, isAsync } } -const attrValueStartRE = /=[\s\t\n\r]*(["']|.)/ +const attrValueStartRE = /=[\s\t\n\r]*(.)/ export function overwriteAttrValue( s: MagicString, @@ -214,7 +214,7 @@ export function overwriteAttrValue( `[vite:html] internal error, failed to overwrite attribute value` ) } - const wrapOffset = valueStart[1] ? 1 : 0 + const wrapOffset = valueStart[1] === '"' || valueStart[1] === "'" ? 1 : 0 const valueOffset = valueStart.index! + valueStart[0].length - 1 s.overwrite( sourceCodeLocation.startOffset + valueOffset + wrapOffset, diff --git a/playground/html/__tests__/html.spec.ts b/playground/html/__tests__/html.spec.ts index 388496bb49900f..c4563a5f632b81 100644 --- a/playground/html/__tests__/html.spec.ts +++ b/playground/html/__tests__/html.spec.ts @@ -256,3 +256,14 @@ test('importmap', () => { 'An import map is added after module script load was triggered.' ) }) + +describe('Valid HTML', () => { + test('valid HTML is parsed', async () => { + await page.goto(viteTestUrl + '/valid.html') + expect(await page.textContent('#no-quotes-on-attr')).toBe( + 'No quotes on Attr working' + ) + + expect(await getColor('#duplicated-attrs')).toBe('green') + }) +}) diff --git a/playground/html/valid.html b/playground/html/valid.html index 9ff48bbeafba6b..a2bd28f802184e 100644 --- a/playground/html/valid.html +++ b/playground/html/valid.html @@ -5,3 +5,6 @@ + +