From f5412397524db100d07c68dad19f13eabe365b9a Mon Sep 17 00:00:00 2001 From: patak Date: Wed, 14 Sep 2022 12:37:36 +0200 Subject: [PATCH] fix: no quote on attrs (#10117) --- packages/vite/src/node/plugins/html.ts | 4 ++-- playground/html/__tests__/html.spec.ts | 11 +++++++++++ playground/html/valid.html | 3 +++ playground/html/valid.js | 3 +++ 4 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 playground/html/valid.js 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 @@ + +
No quotes on Attr
+ \ No newline at end of file diff --git a/playground/html/valid.js b/playground/html/valid.js new file mode 100644 index 00000000000000..f7f62b132fbc94 --- /dev/null +++ b/playground/html/valid.js @@ -0,0 +1,3 @@ +document.getElementById( + `no-quotes-on-attr` +).innerHTML = `No quotes on Attr working`