From 934cebb750704a1d2f0a3701df6b67ffa8caaa81 Mon Sep 17 00:00:00 2001 From: sy-records <52o@qq52o.cn> Date: Fri, 12 Aug 2022 09:33:57 +0800 Subject: [PATCH 1/7] test: add test for history.getFile --- test/unit/router-history-base.test.js | 31 +++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/test/unit/router-history-base.test.js b/test/unit/router-history-base.test.js index a486ed2db..7ce3ca92c 100644 --- a/test/unit/router-history-base.test.js +++ b/test/unit/router-history-base.test.js @@ -66,4 +66,35 @@ describe('router/history/base', () => { expect(url).toBe('/README'); }); }); + + // alias: uri + // --------------------------------------------------------------------------- + describe('getFile', () => { + // Tests + // ------------------------------------------------------------------------- + test('path is url', () => { + const file = history.getFile('https://some/raw/url/README.md'); + + expect(file).toBe('https://some/raw/url/README.md'); + }); + test('path is url, but ext is .html', () => { + const file = history.getFile('https://foo.com/index.html'); + + expect(file).toBe('https://foo.com/index.html'); + }); + test('path is url, bug with parameters', () => { + const file = history.getFile( + 'https://some/raw/url/README.md?token=Mytoken' + ); + + expect(file).toBe('https://some/raw/url/README.md?token=Mytoken'); + }); + test('path is url, but ext is different', () => { + history = new MockHistory({ ext: '.ext' }); + + const file = history.getFile('https://some/raw/url/README.md'); + + expect(file).toBe('https://some/raw/url/README.md.ext'); + }); + }); }); From 80a0dfe608a94d082cf801e1a9f2422a42ab0806 Mon Sep 17 00:00:00 2001 From: sy-records <52o@qq52o.cn> Date: Fri, 12 Aug 2022 15:08:39 +0800 Subject: [PATCH 2/7] fix: fix typo --- test/unit/router-history-base.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/unit/router-history-base.test.js b/test/unit/router-history-base.test.js index 7ce3ca92c..889ce447d 100644 --- a/test/unit/router-history-base.test.js +++ b/test/unit/router-history-base.test.js @@ -67,7 +67,7 @@ describe('router/history/base', () => { }); }); - // alias: uri + // getFile test // --------------------------------------------------------------------------- describe('getFile', () => { // Tests @@ -82,7 +82,7 @@ describe('router/history/base', () => { expect(file).toBe('https://foo.com/index.html'); }); - test('path is url, bug with parameters', () => { + test('path is url, but with parameters', () => { const file = history.getFile( 'https://some/raw/url/README.md?token=Mytoken' ); From 041f1c8ceeb4b02628e9ec23e2ce9894fd8bec36 Mon Sep 17 00:00:00 2001 From: Luffy <52o@qq52o.cn> Date: Tue, 14 Jan 2025 14:05:07 +0800 Subject: [PATCH 3/7] ci: fix lint --- test/unit/router-history-base.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/router-history-base.test.js b/test/unit/router-history-base.test.js index 889ce447d..58ff08246 100644 --- a/test/unit/router-history-base.test.js +++ b/test/unit/router-history-base.test.js @@ -84,7 +84,7 @@ describe('router/history/base', () => { }); test('path is url, but with parameters', () => { const file = history.getFile( - 'https://some/raw/url/README.md?token=Mytoken' + 'https://some/raw/url/README.md?token=Mytoken', ); expect(file).toBe('https://some/raw/url/README.md?token=Mytoken'); From d196777907aa393a4e6baec4d1407bbcc6c589dc Mon Sep 17 00:00:00 2001 From: Luffy <52o@qq52o.cn> Date: Tue, 14 Jan 2025 14:16:33 +0800 Subject: [PATCH 4/7] fix: handle query parameters in getFileName method --- src/core/router/history/base.js | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/core/router/history/base.js b/src/core/router/history/base.js index 268cbb1f4..c52fe5f8a 100644 --- a/src/core/router/history/base.js +++ b/src/core/router/history/base.js @@ -1,10 +1,10 @@ import { + cleanPath, getPath, isAbsolutePath, - stringifyQuery, - cleanPath, replaceSlug, resolvePath, + stringifyQuery, } from '../util.js'; import { noop } from '../../util/core.js'; @@ -32,11 +32,17 @@ export class History { } #getFileName(path, ext) { - return new RegExp(`\\.(${ext.replace(/^\./, '')}|html)$`, 'g').test(path) - ? path - : /\/$/g.test(path) - ? `${path}README${ext}` - : `${path}${ext}`; + const [basePath, query] = path.split("?"); + + const hasValidExt = new RegExp(`\\.(${ext.replace(/^\./, '')}|html)$`, 'g').test(basePath); + + const updatedPath = hasValidExt + ? basePath + : /\/$/g.test(basePath) + ? `${basePath}README${ext}` + : `${basePath}${ext}`; + + return query ? `${updatedPath}?${query}` : updatedPath; } getBasePath() { From 404d28ceb539c34b4b4bdd19949e61f43c689eb6 Mon Sep 17 00:00:00 2001 From: Luffy <52o@qq52o.cn> Date: Tue, 14 Jan 2025 14:18:14 +0800 Subject: [PATCH 5/7] ci: fix lint --- src/core/router/history/base.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/core/router/history/base.js b/src/core/router/history/base.js index c52fe5f8a..fb60b8d19 100644 --- a/src/core/router/history/base.js +++ b/src/core/router/history/base.js @@ -32,9 +32,12 @@ export class History { } #getFileName(path, ext) { - const [basePath, query] = path.split("?"); + const [basePath, query] = path.split('?'); - const hasValidExt = new RegExp(`\\.(${ext.replace(/^\./, '')}|html)$`, 'g').test(basePath); + const hasValidExt = new RegExp( + `\\.(${ext.replace(/^\./, '')}|html)$`, + 'g', + ).test(basePath); const updatedPath = hasValidExt ? basePath From 04a9eb30809dd97d6db6f8ffeac47e87eb2e0fd3 Mon Sep 17 00:00:00 2001 From: Luffy Date: Wed, 9 Apr 2025 18:53:47 +0800 Subject: [PATCH 6/7] Update src/core/router/history/base.js Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/core/router/history/base.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/core/router/history/base.js b/src/core/router/history/base.js index fb60b8d19..168aefa95 100644 --- a/src/core/router/history/base.js +++ b/src/core/router/history/base.js @@ -35,8 +35,7 @@ export class History { const [basePath, query] = path.split('?'); const hasValidExt = new RegExp( - `\\.(${ext.replace(/^\./, '')}|html)$`, - 'g', + `\\.(${ext.replace(/^\./, '')}|html)$` ).test(basePath); const updatedPath = hasValidExt From cc21f4d996862eaf25a3e70e66a731c9941412dc Mon Sep 17 00:00:00 2001 From: Luffy <52o@qq52o.cn> Date: Wed, 9 Apr 2025 18:58:12 +0800 Subject: [PATCH 7/7] fix: improve regex formatting in getFileName method --- src/core/router/history/base.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/core/router/history/base.js b/src/core/router/history/base.js index 168aefa95..352aa0f03 100644 --- a/src/core/router/history/base.js +++ b/src/core/router/history/base.js @@ -34,9 +34,9 @@ export class History { #getFileName(path, ext) { const [basePath, query] = path.split('?'); - const hasValidExt = new RegExp( - `\\.(${ext.replace(/^\./, '')}|html)$` - ).test(basePath); + const hasValidExt = new RegExp(`\\.(${ext.replace(/^\./, '')}|html)$`).test( + basePath, + ); const updatedPath = hasValidExt ? basePath