Skip to content

Commit f541239

Browse files
authored
fix: no quote on attrs (#10117)
1 parent 5d56e42 commit f541239

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

packages/vite/src/node/plugins/html.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ export function getScriptInfo(node: DefaultTreeAdapterMap['element']): {
196196
return { src, sourceCodeLocation, isModule, isAsync }
197197
}
198198

199-
const attrValueStartRE = /=[\s\t\n\r]*(["']|.)/
199+
const attrValueStartRE = /=[\s\t\n\r]*(.)/
200200

201201
export function overwriteAttrValue(
202202
s: MagicString,
@@ -214,7 +214,7 @@ export function overwriteAttrValue(
214214
`[vite:html] internal error, failed to overwrite attribute value`
215215
)
216216
}
217-
const wrapOffset = valueStart[1] ? 1 : 0
217+
const wrapOffset = valueStart[1] === '"' || valueStart[1] === "'" ? 1 : 0
218218
const valueOffset = valueStart.index! + valueStart[0].length - 1
219219
s.overwrite(
220220
sourceCodeLocation.startOffset + valueOffset + wrapOffset,

playground/html/__tests__/html.spec.ts

+11
Original file line numberDiff line numberDiff line change
@@ -256,3 +256,14 @@ test('importmap', () => {
256256
'An import map is added after module script load was triggered.'
257257
)
258258
})
259+
260+
describe('Valid HTML', () => {
261+
test('valid HTML is parsed', async () => {
262+
await page.goto(viteTestUrl + '/valid.html')
263+
expect(await page.textContent('#no-quotes-on-attr')).toBe(
264+
'No quotes on Attr working'
265+
)
266+
267+
expect(await getColor('#duplicated-attrs')).toBe('green')
268+
})
269+
})

playground/html/valid.html

+3
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@
55
<datalist>
66
<option value="option-without-closing-tag">
77
</datalist>
8+
9+
<div id="no-quotes-on-attr">No quotes on Attr</div>
10+
<script type="module" src=/valid.js></script>

playground/html/valid.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
document.getElementById(
2+
`no-quotes-on-attr`
3+
).innerHTML = `No quotes on Attr working`

0 commit comments

Comments
 (0)