From dcf5a644eb6a2eef65fb940f3407c12828a679bc Mon Sep 17 00:00:00 2001 From: Matej Simek Date: Sun, 20 Dec 2020 05:49:36 +0100 Subject: [PATCH] fix: sidebar active class and expand don't work as expect when use "space" in markdown filename (#1454) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: decode href in sidebar (#1032) * Create sidebar.test.js Co-authored-by: 沈唁 <52o@qq52o.cn> --- src/core/event/sidebar.js | 2 +- test/e2e/sidebar.test.js | 70 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 test/e2e/sidebar.test.js diff --git a/src/core/event/sidebar.js b/src/core/event/sidebar.js index 0e1f028c1..c142529ef 100644 --- a/src/core/event/sidebar.js +++ b/src/core/event/sidebar.js @@ -83,7 +83,7 @@ export function getAndActive(router, el, isParent, autoTitle) { links .sort((a, b) => b.href.length - a.href.length) .forEach(a => { - const href = a.getAttribute('href'); + const href = decodeURI(a.getAttribute('href')); const node = isParent ? a.parentNode : a; a.title = a.title || a.innerText; diff --git a/test/e2e/sidebar.test.js b/test/e2e/sidebar.test.js new file mode 100644 index 000000000..d289d1cbc --- /dev/null +++ b/test/e2e/sidebar.test.js @@ -0,0 +1,70 @@ +const docsifyInit = require('../helpers/docsify-init'); + +// Suite +// ----------------------------------------------------------------------------- +describe('Sidebar Tests', function() { + // Tests + // --------------------------------------------------------------------------- + test('Active Test', async () => { + const docsifyInitConfig = { + markdown: { + sidebar: ` + - [Test Space](test%20space) + - [Test _](test_foo) + - [Test -](test-foo) + - [Test .](test.foo) + - [Test >](test>foo) + - [Test](test) + `, + }, + routes: { + '/test space.md': ` + # Test Space + `, + '/test_foo.md': ` + # Test _ + `, + '/test-foo.md': ` + # Test - + `, + '/test.foo.md': ` + # Test . + `, + '/test>foo.md': ` + # Test > + `, + '/test.md': ` + # Test page + `, + }, + }; + + await docsifyInit(docsifyInitConfig); + await page.click('a[href="#/test%20space"]'); + await expect(page).toEqualText( + '.sidebar-nav li[class=active]', + 'Test Space' + ); + expect(page.url()).toMatch(/\/test%20space$/); + + await page.click('a[href="#/test_foo"]'); + await expect(page).toEqualText('.sidebar-nav li[class=active]', 'Test _'); + expect(page.url()).toMatch(/\/test_foo$/); + + await page.click('a[href="#/test-foo"]'); + await expect(page).toEqualText('.sidebar-nav li[class=active]', 'Test -'); + expect(page.url()).toMatch(/\/test-foo$/); + + await page.click('a[href="#/test.foo"]'); + await expect(page).toEqualText('.sidebar-nav li[class=active]', 'Test .'); + expect(page.url()).toMatch(/\/test.foo$/); + + await page.click('a[href="#/test>foo"]'); + await expect(page).toEqualText('.sidebar-nav li[class=active]', 'Test >'); + expect(page.url()).toMatch(/\/test%3Efoo$/); + + await page.click('a[href="#/test"]'); + await expect(page).toEqualText('.sidebar-nav li[class=active]', 'Test'); + expect(page.url()).toMatch(/\/test$/); + }); +});