Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(html): handle attrs with prefix (fixes #10337) #10381

Merged
merged 1 commit into from Oct 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 16 additions & 5 deletions packages/vite/src/node/plugins/html.ts
Expand Up @@ -182,6 +182,7 @@ export function getScriptInfo(node: DefaultTreeAdapterMap['element']): {
let isModule = false
let isAsync = false
for (const p of node.attrs) {
if (p.prefix !== undefined) continue
if (p.name === 'src') {
if (!src) {
src = p
Expand Down Expand Up @@ -412,9 +413,10 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
const assetAttrs = assetAttrsConfig[node.nodeName]
if (assetAttrs) {
for (const p of node.attrs) {
if (p.value && assetAttrs.includes(p.name)) {
const attrKey = getAttrKey(p)
if (p.value && assetAttrs.includes(attrKey)) {
const attrSourceCodeLocation =
node.sourceCodeLocation!.attrs![p.name]
node.sourceCodeLocation!.attrs![attrKey]
// assetsUrl may be encodeURI
const url = decodeURI(p.value)
if (!isExcludedUrl(url)) {
Expand All @@ -423,7 +425,9 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
isCSSRequest(url) &&
// should not be converted if following attributes are present (#6748)
!node.attrs.some(
(p) => p.name === 'media' || p.name === 'disabled'
(p) =>
p.prefix === undefined &&
(p.name === 'media' || p.name === 'disabled')
)
) {
// CSS references, convert to import
Expand Down Expand Up @@ -453,7 +457,10 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
// <tag style="... url(...) ..."></tag>
// extract inline styles as virtual css and add class attribute to tag for selecting
const inlineStyle = node.attrs.find(
(prop) => prop.name === 'style' && prop.value.includes('url(') // only url(...) in css need to emit file
(prop) =>
prop.prefix === undefined &&
prop.name === 'style' &&
prop.value.includes('url(') // only url(...) in css need to emit file
)
if (inlineStyle) {
inlineModuleIndex++
Expand Down Expand Up @@ -527,7 +534,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
) {
try {
const url =
attr.name === 'srcset'
attr.prefix === undefined && attr.name === 'srcset'
? await processSrcSet(content, ({ url }) =>
urlToBuiltUrl(url, id, config, this)
)
Expand Down Expand Up @@ -1133,3 +1140,7 @@ function serializeAttrs(attrs: HtmlTagDescriptor['attrs']): string {
function incrementIndent(indent: string = '') {
return `${indent}${indent[0] === '\t' ? '\t' : ' '}`
}

export function getAttrKey(attr: Token.Attribute): string {
return attr.prefix === undefined ? attr.name : `${attr.prefix}:${attr.name}`
}
8 changes: 5 additions & 3 deletions packages/vite/src/node/server/middlewares/indexHtml.ts
Expand Up @@ -9,6 +9,7 @@ import {
addToHTMLProxyCache,
applyHtmlTransforms,
assetAttrsConfig,
getAttrKey,
getScriptInfo,
nodeIsElement,
overwriteAttrValue,
Expand Down Expand Up @@ -112,7 +113,7 @@ const processNodeUrl = (
// rewrite after `../index.js` -> `localhost:5173/index.js`.

const processedUrl =
attr.name === 'srcset'
attr.name === 'srcset' && attr.prefix === undefined
? processSrcSetSync(url, ({ url }) => replacer(url))
: replacer(url)
overwriteAttrValue(s, sourceCodeLocation, processedUrl)
Expand Down Expand Up @@ -228,10 +229,11 @@ const devHtmlHook: IndexHtmlTransformHook = async (
const assetAttrs = assetAttrsConfig[node.nodeName]
if (assetAttrs) {
for (const p of node.attrs) {
if (p.value && assetAttrs.includes(p.name)) {
const attrKey = getAttrKey(p)
if (p.value && assetAttrs.includes(attrKey)) {
processNodeUrl(
p,
node.sourceCodeLocation!.attrs![p.name],
node.sourceCodeLocation!.attrs![attrKey],
s,
config,
htmlPath,
Expand Down
14 changes: 14 additions & 0 deletions playground/html/public/sprite.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 6 additions & 1 deletion playground/html/valid.html
Expand Up @@ -7,4 +7,9 @@
</datalist>

<div id="no-quotes-on-attr">No quotes on Attr</div>
<script type="module" src=/valid.js></script>
<script type="module" src=/valid.js></script>

<svg>
<!-- attr with prefix -->
<use xlink:href="/sprite.svg#svg"></use>
</svg>