Skip to content

Commit

Permalink
MWPW-140172 Fix anchor scroll (#1651)
Browse files Browse the repository at this point in the history
* MWPW-140172 Fix anchor scroll

* unit test
  • Loading branch information
meganthecoder authored Dec 15, 2023
1 parent 62a8fd5 commit f831bd9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion libs/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -901,7 +901,7 @@ async function loadPostLCP(config) {

export function scrollToHashedElement(hash) {
if (!hash) return;
const elementId = hash.slice(1);
const elementId = decodeURIComponent(hash).slice(1);
let targetElement;
try {
targetElement = document.querySelector(`#${elementId}:not(.dialog-modal)`);
Expand Down
1 change: 1 addition & 0 deletions test/utils/mocks/body.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<div>
<!-- Block Decorating -->
<div id="not-block"></div>
<div id="tools-für-das-verhalten"></div>
<!-- Picture Decorating -->
<p><em><picture><img src="http://localhost:2000/test/utils/mocks/media_.png"/></picture></em></p>
<p><strong><picture><img src="http://localhost:2000/test/utils/mocks/media_.png"/></picture></strong></p>
Expand Down
11 changes: 11 additions & 0 deletions test/utils/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,17 @@ describe('Utils', () => {
utils.scrollToHashedElement('');
expect(scrollToCalled).to.be.false;
});

it('should scroll to the hashed element with special character', () => {
let scrollToCalled = false;
window.scrollTo = () => {
scrollToCalled = true;
};

utils.scrollToHashedElement('#tools-f%C3%BCr-das-verhalten');
expect(scrollToCalled).to.be.true;
expect(document.getElementById('tools-für-das-verhalten')).to.exist;
});
});

describe('useDotHtml', async () => {
Expand Down

0 comments on commit f831bd9

Please sign in to comment.