]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-sfc): support asset paths containing spaces (#8752)
authorJeff Tian <jeff.tian@outlook.com>
Fri, 20 Oct 2023 07:49:58 +0000 (15:49 +0800)
committerGitHub <noreply@github.com>
Fri, 20 Oct 2023 07:49:58 +0000 (15:49 +0800)
By decoding them before generating them as JavaScript import paths

fix https://github.com/vuejs/vitepress/issues/2596
fix https://github.com/vuejs/vitepress/issues/573

packages/compiler-sfc/__tests__/templateTransformAssetUrl.spec.ts
packages/compiler-sfc/src/template/transformAssetUrl.ts

index f267e73ede04acfa0ed32fe7a06cb9c15c680e30..44c13e47ea2108742cac0fa7107198b813d6344c 100644 (file)
@@ -166,4 +166,34 @@ describe('compiler sfc: transform asset url', () => {
     expect(code).toMatch(`_createStaticVNode`)
     expect(code).toMatchSnapshot()
   })
+
+  test('transform with stringify with space in absolute filename', () => {
+    const { code } = compileWithAssetUrls(
+      `<div><img src="/foo bar.png"/></div>`,
+      {
+        includeAbsolute: true
+      },
+      {
+        hoistStatic: true,
+        transformHoist: stringifyStatic
+      }
+    )
+    expect(code).toMatch(`_createElementVNode`)
+    expect(code).toContain(`import _imports_0 from '/foo bar.png'`)
+  })
+
+  test('transform with stringify with space in relative filename', () => {
+    const { code } = compileWithAssetUrls(
+      `<div><img src="./foo bar.png"/></div>`,
+      {
+        includeAbsolute: true
+      },
+      {
+        hoistStatic: true,
+        transformHoist: stringifyStatic
+      }
+    )
+    expect(code).toMatch(`_createElementVNode`)
+    expect(code).toContain(`import _imports_0 from './foo bar.png'`)
+  })
 })
index 32bf33bcea10bbb27537b47ef2e98157108aab03..4267d4ea3529719a4023e11e683aaf039ac6f482 100644 (file)
@@ -168,7 +168,13 @@ function getImportsExpressionExp(
         loc,
         ConstantTypes.CAN_STRINGIFY
       )
-      context.imports.push({ exp, path })
+
+      // We need to ensure the path is not encoded (to %2F),
+      // so we decode it back in case it is encoded
+      context.imports.push({
+        exp,
+        path: decodeURIComponent(path)
+      })
     }
 
     if (!hash) {