Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/core/router/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,21 @@ export function getSidebarNavigationTarget(linkElm) {
};
}

/**
* Decode a query value, keeping the raw text when it is not valid
* percent-encoding (e.g. a literal "%") instead of throwing a URIError.
*
* @param {string} str
* @returns {string}
*/
function safeDecode(str) {
try {
return decode(str);
} catch {
return str;
}
}

/**
* @param {string} query
* @return {Record<string, string>}
Expand All @@ -124,7 +139,7 @@ export function parseQuery(query) {
query.split('&').forEach(param => {
const parts = param.replace(/\+/g, ' ').split('=');

res[parts[0]] = parts[1] && decode(parts[1]);
res[parts[0]] = parts[1] && safeDecode(parts[1]);
});

return res;
Expand Down
21 changes: 21 additions & 0 deletions test/unit/router-util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,32 @@ import {
getClickedLink,
getSidebarNavigationTarget,
isCurrentContextNavigation,
parseQuery,
} from '../../src/core/router/util.js';

// Suite
// -----------------------------------------------------------------------------
describe('router/util', () => {
describe('parseQuery()', () => {
test('does not throw on malformed percent-encoding', () => {
expect(parseQuery('?a=100%&b=%E0%A4%A&c=b%zz')).toEqual({
a: '100%',
b: '%E0%A4%A',
c: 'b%zz',
});
});

test('still decodes valid percent-encoding', () => {
expect(parseQuery('?a=50%25&b=x%20y&c=m+n&d=&e')).toEqual({
a: '50%',
b: 'x y',
c: 'm n',
d: '',
e: undefined,
});
});
});

describe('navigation click helpers', () => {
test('finds a link from a nested click target', () => {
document.body.innerHTML = '<a href="/guide"><span>Guide</span></a>';
Expand Down