-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cockpit location ts #21276
Draft
jelly
wants to merge
2
commits into
cockpit-project:main
Choose a base branch
from
jelly:cockpit-location-ts
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Cockpit location ts #21276
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
/* | ||
* This file is part of Cockpit. | ||
* | ||
* Copyright (C) 2024 Red Hat, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
import { url_root, calculate_application } from './_internal/location-utils'; | ||
|
||
/* HACK: Mozilla will unescape 'window.location.hash' before returning | ||
* it, which is broken. | ||
* | ||
* https://bugzilla.mozilla.org/show_bug.cgi?id=135309 | ||
*/ | ||
export function get_window_location_hash() { | ||
return (window.location.href.split('#')[1] || ''); | ||
} | ||
|
||
export class Location { | ||
path: string[]; | ||
href: string; | ||
url_root: string; | ||
options: any; | ||
|
||
constructor() { | ||
const application = calculate_application(); | ||
this.url_root = url_root || ""; | ||
|
||
if (window.mock?.url_root) | ||
this.url_root = window.mock.url_root; | ||
|
||
if (application.indexOf("cockpit+=") === 0) { | ||
if (this.url_root) | ||
this.url_root += '/'; | ||
this.url_root = this.url_root + application.replace("cockpit+", ''); | ||
} | ||
|
||
this.href = get_window_location_hash(); | ||
this.options = {}; // untyped | ||
this.path = this.decode(this.href, this.options); | ||
} | ||
|
||
#resolve_path_dots(parts) { | ||
const out = []; | ||
const length = parts.length; | ||
for (let i = 0; i < length; i++) { | ||
const part = parts[i]; | ||
if (part === "" || part == ".") { | ||
continue; | ||
} else if (part == "..") { | ||
if (out.length === 0) | ||
return null; | ||
out.pop(); | ||
} else { | ||
out.push(part); | ||
} | ||
} | ||
return out; | ||
} | ||
|
||
href_for_go_or_replace(/* ... */) { | ||
console.log("thisthing", this); | ||
let href; | ||
if (arguments.length == 1 && arguments[0] instanceof Location) { | ||
href = String(arguments[0]); | ||
} else if (typeof arguments[0] == "string") { | ||
const options = arguments[1] || { }; | ||
href = this.encode(this.decode(arguments[0], options), options); | ||
} else { | ||
href = this.encode.apply(this, arguments); | ||
} | ||
return href; | ||
} | ||
|
||
decode_path(input) { | ||
const parts = input.split('/').map(decodeURIComponent); | ||
let result, i; | ||
let pre_parts = []; | ||
|
||
if (this.url_root) | ||
pre_parts = this.url_root.split('/').map(decodeURIComponent); | ||
|
||
if (input && input[0] !== "/") { | ||
result = [].concat(this.path); | ||
result.pop(); | ||
result = result.concat(parts); | ||
} else { | ||
result = parts; | ||
} | ||
|
||
result = this.#resolve_path_dots(result); | ||
for (i = 0; i < pre_parts.length; i++) { | ||
if (pre_parts[i] !== result[i]) | ||
break; | ||
} | ||
if (i == pre_parts.length) | ||
result.splice(0, pre_parts.length); | ||
|
||
return result; | ||
} | ||
|
||
encode(path: string, options, with_root: boolean = false) { | ||
if (typeof path == "string") | ||
path = this.decode_path(path); | ||
|
||
let href = "/" + path.map(encodeURIComponent).join("/"); | ||
if (with_root && this.url_root && href.indexOf("/" + this.url_root + "/") !== 0) | ||
href = "/" + this.url_root + href; | ||
|
||
/* Undo unnecessary encoding of these */ | ||
href = href.replaceAll("%40", "@"); | ||
href = href.replaceAll("%3D", "="); | ||
href = href.replaceAll("%2B", "+"); | ||
href = href.replaceAll("%23", "#"); | ||
|
||
const query: string[] = []; | ||
if (options) { | ||
for (const opt in options) { | ||
let value = options[opt]; | ||
if (!Array.isArray(value)) | ||
value = [value]; | ||
value.forEach(function(v: string) { | ||
query.push(encodeURIComponent(opt) + "=" + encodeURIComponent(v)); | ||
}); | ||
} | ||
if (query.length > 0) | ||
href += "?" + query.join("&"); | ||
} | ||
return href; | ||
} | ||
|
||
decode(href: string, options) { | ||
if (href[0] == '#') | ||
href = href.substr(1); | ||
|
||
const pos = href.indexOf('?'); | ||
const first = (pos === -1) ? href : href.substr(0, pos); | ||
const path = this.decode_path(first); | ||
if (pos !== -1 && options) { | ||
href.substring(pos + 1).split("&") | ||
.forEach(function(opt) { | ||
const parts = opt.split('='); | ||
const name = decodeURIComponent(parts[0]); | ||
const value = decodeURIComponent(parts[1]); | ||
if (options[name]) { | ||
let last = options[name]; | ||
if (!Array.isArray(value)) | ||
last = options[name] = [last]; | ||
last.push(value); | ||
} else { | ||
options[name] = value; | ||
Check failure Code scanning / CodeQL Remote property injection High
A property name to write to depends on a
user-provided value Error loading related location Loading |
||
} | ||
}); | ||
} | ||
|
||
return path; | ||
} | ||
|
||
replace(/* ... */) { | ||
const href = this.href_for_go_or_replace.apply(this, arguments); | ||
window.location.replace(window.location.pathname + '#' + href); | ||
} | ||
|
||
go(/* ... */) { | ||
const href = this.href_for_go_or_replace.apply(this, arguments); | ||
window.location.hash = '#' + href; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check failure
Code scanning / CodeQL
Remote property injection High