Skip to content

Commit

Permalink
Handle encoded non-decoded hrefs for active elms
Browse files Browse the repository at this point in the history
  • Loading branch information
jhildenbiddle committed Apr 16, 2024
1 parent 876c587 commit 7480a05
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 17 deletions.
44 changes: 32 additions & 12 deletions src/core/event/index.js
Expand Up @@ -389,7 +389,7 @@ export function Events(Base) {
* @returns Element|undefined
*/
#markAppNavActiveElm() {
const href = this.router.toURL(this.route.path);
const href = decodeURIComponent(this.router.toURL(this.route.path));
const navElm = dom.find('nav.app-nav');

if (!navElm) {
Expand All @@ -399,11 +399,17 @@ export function Events(Base) {
const newActive = dom
.findAll(navElm, 'a')
.sort((a, b) => b.href.length - a.href.length)
.find(a => href.includes(decodeURI(a.getAttribute('href'))));
.find(
a =>
href.includes(a.getAttribute('href')) ||
href.includes(decodeURI(a.getAttribute('href')))
);
const oldActive = dom.find(navElm, 'li.active');

oldActive?.classList.remove('active');
newActive?.classList.add('active');
if (newActive && newActive !== oldActive) {
oldActive?.classList.remove('active');
newActive.classList.add('active');
}

return newActive;
}
Expand All @@ -425,10 +431,17 @@ export function Events(Base) {
}

const oldActive = dom.find(sidebar, 'li.active');
const newActive = dom.find(sidebar, `a[href='${href}']`)?.closest('li');

oldActive?.classList.remove('active');
newActive?.classList.add('active');
const newActive = dom
.find(
sidebar,
`a[href="${href}"], a[href="${decodeURIComponent(href)}"]`
)
?.closest('li');

if (newActive && newActive !== oldActive) {
oldActive?.classList.remove('active');
newActive.classList.add('active');
}

return newActive;
}
Expand All @@ -451,10 +464,17 @@ export function Events(Base) {

const path = href?.split('?')[0];
const oldPage = dom.find(sidebar, 'li[aria-current]');
const newPage = dom.find(sidebar, `a[href='${path}']`)?.closest('li');

oldPage?.removeAttribute('aria-current');
newPage?.setAttribute('aria-current', 'page');
const newPage = dom
.find(
sidebar,
`a[href="${path}"], a[href="${decodeURIComponent(path)}"]`
)
?.closest('li');

if (newPage && newPage !== oldPage) {
oldPage?.removeAttribute('aria-current');
newPage.setAttribute('aria-current', 'page');
}

return newPage;
}
Expand Down
10 changes: 5 additions & 5 deletions test/e2e/sidebar.test.js
Expand Up @@ -44,6 +44,10 @@ test.describe('Sidebar Tests', () => {

await docsifyInit(docsifyInitConfig);

await page.click('a[href="#/test"]');
await expect(activeLinkElm).toHaveText('Test');
expect(page.url()).toMatch(/\/test$/);

await page.click('a[href="#/test%20space"]');
await expect(activeLinkElm).toHaveText('Test Space');
expect(page.url()).toMatch(/\/test%20space$/);
Expand All @@ -57,15 +61,11 @@ test.describe('Sidebar Tests', () => {
expect(page.url()).toMatch(/\/test-foo$/);

await page.click('a[href="#/test.foo"]');
expect(page.url()).toMatch(/\/test.foo$/);
await expect(activeLinkElm).toHaveText('Test .');
expect(page.url()).toMatch(/\/test.foo$/);

await page.click('a[href="#/test>foo"]');
await expect(activeLinkElm).toHaveText('Test >');
expect(page.url()).toMatch(/\/test%3Efoo$/);

await page.click('a[href="#/test"]');
await expect(activeLinkElm).toHaveText('Test');
expect(page.url()).toMatch(/\/test$/);
});
});

0 comments on commit 7480a05

Please sign in to comment.