Skip to content

Commit

Permalink
fix: no quote on attrs (#10117)
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev authored Sep 14, 2022
1 parent 5d56e42 commit f541239
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/vite/src/node/plugins/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
11 changes: 11 additions & 0 deletions playground/html/__tests__/html.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
})
3 changes: 3 additions & 0 deletions playground/html/valid.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@
<datalist>
<option value="option-without-closing-tag">
</datalist>

<div id="no-quotes-on-attr">No quotes on Attr</div>
<script type="module" src=/valid.js></script>
3 changes: 3 additions & 0 deletions playground/html/valid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
document.getElementById(
`no-quotes-on-attr`
).innerHTML = `No quotes on Attr working`

0 comments on commit f541239

Please sign in to comment.