Skip to content

Commit

Permalink
Fix hash router for urls ending with a hash (#4730)
Browse files Browse the repository at this point in the history
  • Loading branch information
birkskyum authored Sep 21, 2024
1 parent be7e209 commit 57d7d83
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

### 🐞 Bug fixes
- Fix circle won't render on mesa 24.1 with AMD GPU ([#4062](https://github.com/maplibre/maplibre-gl-js/issues/4062))
- Fix hash router for urls ending with a hashtag ([#4730](https://github.com/maplibre/maplibre-gl-js/pull/4730))
- _...Add new stuff here..._

## 4.7.0
Expand Down
65 changes: 65 additions & 0 deletions src/ui/hash.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,71 @@ describe('hash', () => {
expect(window.location.hash).toBe('#baz&foo=bar');
});

test('initialize http://localhost/#', () => {
window.location.href = 'http://localhost/#';
createHash().addTo(map);
map.setZoom(3);
expect(window.location.hash).toBe('#3/0/0');
expect(window.location.href).toBe('http://localhost/#3/0/0');
map.setCenter([2.0, 1.0]);
expect(window.location.hash).toBe('#3/1/2');
expect(window.location.href).toBe('http://localhost/#3/1/2');
});

test('initialize http://localhost/##', () => {
window.location.href = 'http://localhost/##';
createHash().addTo(map);
map.setZoom(3);
expect(window.location.hash).toBe('#3/0/0');
expect(window.location.href).toBe('http://localhost/#3/0/0');
map.setCenter([2.0, 1.0]);
expect(window.location.hash).toBe('#3/1/2');
expect(window.location.href).toBe('http://localhost/#3/1/2');
});

test('initialize http://localhost#', () => {
window.location.href = 'http://localhost#';
createHash().addTo(map);
map.setZoom(4);
expect(window.location.hash).toBe('#4/0/0');
expect(window.location.href).toBe('http://localhost/#4/0/0');
map.setCenter([2.0, 1.0]);
expect(window.location.hash).toBe('#4/1/2');
expect(window.location.href).toBe('http://localhost/#4/1/2');
});

test('initialize http://localhost/', () => {
window.location.href = 'http://localhost/';
createHash().addTo(map);
map.setZoom(5);
expect(window.location.hash).toBe('#5/0/0');
expect(window.location.href).toBe('http://localhost/#5/0/0');
map.setCenter([2.0, 1.0]);
expect(window.location.hash).toBe('#5/1/2');
expect(window.location.href).toBe('http://localhost/#5/1/2');
});

test('initialize default value for window.location.href', () => {
createHash().addTo(map);
map.setZoom(5);
expect(window.location.hash).toBe('#5/0/0');
expect(window.location.href).toBe('http://localhost/#5/0/0');
map.setCenter([2.0, 1.0]);
expect(window.location.hash).toBe('#5/1/2');
expect(window.location.href).toBe('http://localhost/#5/1/2');
});

test('initialize http://localhost', () => {
window.location.href = 'http://localhost';
createHash().addTo(map);
map.setZoom(4);
expect(window.location.hash).toBe('#4/0/0');
expect(window.location.href).toBe('http://localhost/#4/0/0');
map.setCenter([2.0, 1.0]);
expect(window.location.hash).toBe('#4/1/2');
expect(window.location.href).toBe('http://localhost/#4/1/2');
});

test('map#remove', () => {
const container = window.document.createElement('div');
Object.defineProperty(container, 'clientWidth', {value: 512});
Expand Down
2 changes: 1 addition & 1 deletion src/ui/hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export class Hash {

_updateHashUnthrottled = () => {
// Replace if already present, else append the updated hash string
const location = window.location.href.replace(/(#.+)?$/, this.getHashString());
const location = window.location.href.replace(/(#.*)?$/, this.getHashString());
window.history.replaceState(window.history.state, null, location);
};

Expand Down

0 comments on commit 57d7d83

Please sign in to comment.