Skip to content
Closed
Changes from 1 commit
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
30 changes: 17 additions & 13 deletions packages/react-native/Libraries/Blob/URL.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ function validateBaseUrl(url: string) {
export class URL {
_url: string;
_searchParamsInstance: ?URLSearchParams = null;
_urlObject: URL;

static createObjectURL(blob: Blob): string {
if (BLOB_URL_PREFIX === null) {
Expand All @@ -76,7 +77,6 @@ export class URL {
// Do nothing.
}

// $FlowFixMe[missing-local-annot]
constructor(url: string, base: string | URL) {
let baseUrl = null;
if (!base || validateBaseUrl(url)) {
Expand Down Expand Up @@ -104,51 +104,55 @@ export class URL {
}
this._url = `${baseUrl}${url}`;
}

// Create a URL object for easier parsing (browser-like behavior)
this._urlObject = new globalThis.URL(this._url);
}

get hash(): string {
throw new Error('URL.hash is not implemented');
return this._urlObject.hash || ''; // Extract the fragment part
}

get host(): string {
throw new Error('URL.host is not implemented');
return this._urlObject.host || ''; // Extracts the host and port (if present)
}

get hostname(): string {
throw new Error('URL.hostname is not implemented');
return this._urlObject.hostname || ''; // Extracts just the hostname (without port)
}

get href(): string {
return this.toString();
}

get origin(): string {
throw new Error('URL.origin is not implemented');
return this._urlObject.origin || ''; // Extracts the origin (protocol + hostname + port)
}

get password(): string {
throw new Error('URL.password is not implemented');
const match = this._urlObject.href.match(/^.*:\/\/(.*):(.*)@/);
return match && match[2] ? match[2] : ''; // Extract password from "username:password" part
}

get pathname(): string {
throw new Error('URL.pathname not implemented');
return this._urlObject.pathname || ''; // Extracts the pathname (e.g., "/path/to/resource")
}

get port(): string {
throw new Error('URL.port is not implemented');
return this._urlObject.port || ''; // Extracts the port part
}

get protocol(): string {
throw new Error('URL.protocol is not implemented');
return this._urlObject.protocol || ''; // Extracts the protocol (e.g., "http:" or "https:")
}

get search(): string {
throw new Error('URL.search is not implemented');
return this._urlObject.search || ''; // Extracts the query string (e.g., "?id=123")
}

get searchParams(): URLSearchParams {
if (this._searchParamsInstance == null) {
this._searchParamsInstance = new URLSearchParams();
this._searchParamsInstance = new URLSearchParams(this._urlObject.search);
}
return this._searchParamsInstance;
}
Expand All @@ -161,13 +165,13 @@ export class URL {
if (this._searchParamsInstance === null) {
return this._url;
}
// $FlowFixMe[incompatible-use]
const instanceString = this._searchParamsInstance.toString();
const separator = this._url.indexOf('?') > -1 ? '&' : '?';
return this._url + separator + instanceString;
}

get username(): string {
throw new Error('URL.username is not implemented');
const match = this._urlObject.href.match(/^.*:\/\/(.*?)(?::(.*))?@/);
return match && match[1] ? match[1] : ''; // Extract username from "username:password" part
}
}
Loading