Skip to content

Commit 9cd9d1a

Browse files
committed
fix(html): handle attrs with prefix (fixes vitejs#10337)
1 parent 2401253 commit 9cd9d1a

File tree

4 files changed

+41
-9
lines changed

4 files changed

+41
-9
lines changed

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

+16-5
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ export function getScriptInfo(node: DefaultTreeAdapterMap['element']): {
182182
let isModule = false
183183
let isAsync = false
184184
for (const p of node.attrs) {
185+
if (p.prefix !== undefined) continue
185186
if (p.name === 'src') {
186187
if (!src) {
187188
src = p
@@ -412,9 +413,10 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
412413
const assetAttrs = assetAttrsConfig[node.nodeName]
413414
if (assetAttrs) {
414415
for (const p of node.attrs) {
415-
if (p.value && assetAttrs.includes(p.name)) {
416+
const attrKey = getAttrKey(p)
417+
if (p.value && assetAttrs.includes(attrKey)) {
416418
const attrSourceCodeLocation =
417-
node.sourceCodeLocation!.attrs![p.name]
419+
node.sourceCodeLocation!.attrs![attrKey]
418420
// assetsUrl may be encodeURI
419421
const url = decodeURI(p.value)
420422
if (!isExcludedUrl(url)) {
@@ -423,7 +425,9 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
423425
isCSSRequest(url) &&
424426
// should not be converted if following attributes are present (#6748)
425427
!node.attrs.some(
426-
(p) => p.name === 'media' || p.name === 'disabled'
428+
(p) =>
429+
p.prefix === undefined &&
430+
(p.name === 'media' || p.name === 'disabled')
427431
)
428432
) {
429433
// CSS references, convert to import
@@ -453,7 +457,10 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
453457
// <tag style="... url(...) ..."></tag>
454458
// extract inline styles as virtual css and add class attribute to tag for selecting
455459
const inlineStyle = node.attrs.find(
456-
(prop) => prop.name === 'style' && prop.value.includes('url(') // only url(...) in css need to emit file
460+
(prop) =>
461+
prop.prefix === undefined &&
462+
prop.name === 'style' &&
463+
prop.value.includes('url(') // only url(...) in css need to emit file
457464
)
458465
if (inlineStyle) {
459466
inlineModuleIndex++
@@ -527,7 +534,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
527534
) {
528535
try {
529536
const url =
530-
attr.name === 'srcset'
537+
attr.prefix === undefined && attr.name === 'srcset'
531538
? await processSrcSet(content, ({ url }) =>
532539
urlToBuiltUrl(url, id, config, this)
533540
)
@@ -1133,3 +1140,7 @@ function serializeAttrs(attrs: HtmlTagDescriptor['attrs']): string {
11331140
function incrementIndent(indent: string = '') {
11341141
return `${indent}${indent[0] === '\t' ? '\t' : ' '}`
11351142
}
1143+
1144+
export function getAttrKey(attr: Token.Attribute): string {
1145+
return attr.prefix === undefined ? attr.name : `${attr.prefix}:${attr.name}`
1146+
}

packages/vite/src/node/server/middlewares/indexHtml.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
addToHTMLProxyCache,
1010
applyHtmlTransforms,
1111
assetAttrsConfig,
12+
getAttrKey,
1213
getScriptInfo,
1314
nodeIsElement,
1415
overwriteAttrValue,
@@ -112,7 +113,7 @@ const processNodeUrl = (
112113
// rewrite after `../index.js` -> `localhost:5173/index.js`.
113114

114115
const processedUrl =
115-
attr.name === 'srcset'
116+
attr.name === 'srcset' && attr.prefix === undefined
116117
? processSrcSetSync(url, ({ url }) => replacer(url))
117118
: replacer(url)
118119
overwriteAttrValue(s, sourceCodeLocation, processedUrl)
@@ -228,10 +229,11 @@ const devHtmlHook: IndexHtmlTransformHook = async (
228229
const assetAttrs = assetAttrsConfig[node.nodeName]
229230
if (assetAttrs) {
230231
for (const p of node.attrs) {
231-
if (p.value && assetAttrs.includes(p.name)) {
232+
const attrKey = getAttrKey(p)
233+
if (p.value && assetAttrs.includes(attrKey)) {
232234
processNodeUrl(
233235
p,
234-
node.sourceCodeLocation!.attrs![p.name],
236+
node.sourceCodeLocation!.attrs![attrKey],
235237
s,
236238
config,
237239
htmlPath,

playground/html/public/sprite.svg

+14
Loading

playground/html/valid.html

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,9 @@
77
</datalist>
88

99
<div id="no-quotes-on-attr">No quotes on Attr</div>
10-
<script type="module" src=/valid.js></script>
10+
<script type="module" src=/valid.js></script>
11+
12+
<svg>
13+
<!-- attr with prefix -->
14+
<use xlink:href="/sprite.svg#svg"></use>
15+
</svg>

0 commit comments

Comments
 (0)