- From: Tab Atkins Jr. <notifications@github.com>
- Date: Mon, 28 Aug 2023 11:07:05 -0700
- To: whatwg/url <url@noreply.github.com>
- Cc: Subscribed <subscribed@noreply.github.com>
- Message-ID: <whatwg/url/issues/782@github.com>
Currently URLs can only be created from a string, and then modified after that. If you have an object of URL parts you have to make a dummy URL and then modify each part in turn. However, the various bits of URLs have some special rules preventing arbitrary modification; for instance when setting `protocol` it runs the URL parser in a special way, and that restricts the protocol from being altered in certain ways. (See the ["scheme state" state, step 2.1](https://url.spec.whatwg.org/#scheme-state).) I'm happy to assume that those restrictions are necessary to ensure a consistent data model for some reason, but it does mean that if you're wanting to construct a URL that you have entirely in parts, then your choice of dummy URL to initialize the URL object with affects whether or not you'll be able to create your desired final URL, even if a URL made of said parts would be perfectly valid and correctly parsable if originally presented in string form. It would be helpful to have a way to construct a URL directly from parts, to avoid issues like this. Suggestion: a static `URL.from(object)` method, that takes a dictionary matching the modifiable bits of the URL class and returns a fresh URL set up appropriately. This probably isn't quite trivial, since right now the *only* way to create a URL is by invoking the parser and then invoking the parser more for each bit, so there's a degree of statefulness in this, but hiding this complexity from authors seems worthwhile. /cc @bakkot @ljharb, who asked about this in the WHATWG chat room -- Reply to this email directly or view it on GitHub: https://github.com/whatwg/url/issues/782 You are receiving this because you are subscribed to this thread. Message ID: <whatwg/url/issues/782@github.com>
Received on Monday, 28 August 2023 18:07:11 UTC