Skip to content

Commit c4de247

Browse files
committed
bug #2829 [LiveComponent] Fix query string decoding with no = (smnandre)
This PR was squashed before being merged into the 2.x branch. Discussion ---------- [LiveComponent] Fix query string decoding with no `=` | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | Docs? | no | Issues | Fix #2826 (when released and a website update) | License | MIT The root cause of #2826 is in fact a bug in LiveComponent ... that i'm very surprised no one already experienced oO In url_utils: fromQueryString missed a nullcheck. Triggering a TypeError. ` ?foo&bar= ` Commits ------- e53fc8b [LiveComponent] Fix query string decoding with no `=`
2 parents aad5516 + e53fc8b commit c4de247

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

src/LiveComponent/assets/dist/live_controller.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2798,7 +2798,7 @@ function fromQueryString(search) {
27982798
const entries = search.split('&').map((i) => i.split('='));
27992799
const data = {};
28002800
entries.forEach(([key, value]) => {
2801-
value = decodeURIComponent(value.replace(/\+/g, '%20'));
2801+
value = decodeURIComponent(String(value || '').replace(/\+/g, '%20'));
28022802
if (!key.includes('[')) {
28032803
data[key] = value;
28042804
}

src/LiveComponent/assets/src/url_utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ function fromQueryString(search: string) {
104104
const data: any = {};
105105

106106
entries.forEach(([key, value]) => {
107-
value = decodeURIComponent(value.replace(/\+/g, '%20'));
107+
value = decodeURIComponent(String(value || '').replace(/\+/g, '%20'));
108108

109109
if (!key.includes('[')) {
110110
data[key] = value;

src/LiveComponent/assets/test/url_utils.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,32 @@ describe('url_utils', () => {
115115
expect(urlUtils.search).toEqual('');
116116
});
117117
});
118+
119+
describe('fromQueryString', () => {
120+
const urlUtils: UrlUtils = new UrlUtils(window.location.href);
121+
122+
beforeEach(() => {
123+
// Reset search before each test
124+
urlUtils.search = '';
125+
});
126+
127+
it('parses a query string with value', () => {
128+
urlUtils.search = '?param1=value1';
129+
expect(urlUtils.get('param1')).toEqual('value1');
130+
});
131+
132+
it('parses a query string with empty value', () => {
133+
urlUtils.search = '?param1=&param2=value2';
134+
expect(urlUtils.get('param1')).toEqual('');
135+
expect(urlUtils.get('param2')).toEqual('value2');
136+
});
137+
138+
it('parses a query string without equal sign', () => {
139+
urlUtils.search = '?param1&param2=value2';
140+
expect(urlUtils.get('param1')).toEqual('');
141+
expect(urlUtils.get('param2')).toEqual('value2');
142+
});
143+
});
118144
});
119145

120146
describe('HistoryStrategy', () => {

0 commit comments

Comments
 (0)