DOMURL.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. /*
  2. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  3. * Copyright (c) 2021, the SerenityOS developers.
  4. * Copyright (c) 2023, networkException <networkexception@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <AK/IPv4Address.h>
  9. #include <AK/IPv6Address.h>
  10. #include <LibURL/Parser.h>
  11. #include <LibWeb/Bindings/DOMURLPrototype.h>
  12. #include <LibWeb/Bindings/Intrinsics.h>
  13. #include <LibWeb/DOMURL/DOMURL.h>
  14. #include <LibWeb/FileAPI/Blob.h>
  15. #include <LibWeb/FileAPI/BlobURLStore.h>
  16. namespace Web::DOMURL {
  17. JS_DEFINE_ALLOCATOR(DOMURL);
  18. JS::NonnullGCPtr<DOMURL> DOMURL::create(JS::Realm& realm, URL::URL url, JS::NonnullGCPtr<URLSearchParams> query)
  19. {
  20. return realm.heap().allocate<DOMURL>(realm, realm, move(url), move(query));
  21. }
  22. // https://url.spec.whatwg.org/#api-url-parser
  23. static Optional<URL::URL> parse_api_url(String const& url, Optional<String> const& base)
  24. {
  25. // FIXME: We somewhat awkwardly have two failure states encapsulated in the return type (and convert between them in the steps),
  26. // ideally we'd get rid of URL's valid flag
  27. // 1. Let parsedBase be null.
  28. Optional<URL::URL> parsed_base;
  29. // 2. If base is non-null:
  30. if (base.has_value()) {
  31. // 1. Set parsedBase to the result of running the basic URL parser on base.
  32. auto parsed_base_url = URL::Parser::basic_parse(*base);
  33. // 2. If parsedBase is failure, then return failure.
  34. if (!parsed_base_url.is_valid())
  35. return {};
  36. parsed_base = parsed_base_url;
  37. }
  38. // 3. Return the result of running the basic URL parser on url with parsedBase.
  39. auto parsed = URL::Parser::basic_parse(url, parsed_base);
  40. return parsed.is_valid() ? parsed : Optional<URL::URL> {};
  41. }
  42. // https://url.spec.whatwg.org/#dom-url-url
  43. WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMURL>> DOMURL::construct_impl(JS::Realm& realm, String const& url, Optional<String> const& base)
  44. {
  45. // 1. Let parsedURL be the result of running the API URL parser on url with base, if given.
  46. auto parsed_url = parse_api_url(url, base);
  47. // 2. If parsedURL is failure, then throw a TypeError.
  48. if (!parsed_url.has_value())
  49. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Invalid URL"sv };
  50. // 3. Let query be parsedURL’s query, if that is non-null, and the empty string otherwise.
  51. auto query = parsed_url->query().value_or(String {});
  52. // 4. Set this’s URL to parsedURL.
  53. // 5. Set this’s query object to a new URLSearchParams object.
  54. auto query_object = MUST(URLSearchParams::construct_impl(realm, query));
  55. // 6. Initialize this’s query object with query.
  56. auto result_url = DOMURL::create(realm, parsed_url.release_value(), move(query_object));
  57. // 7. Set this’s query object’s URL object to this.
  58. result_url->m_query->m_url = result_url;
  59. return result_url;
  60. }
  61. DOMURL::DOMURL(JS::Realm& realm, URL::URL url, JS::NonnullGCPtr<URLSearchParams> query)
  62. : PlatformObject(realm)
  63. , m_url(move(url))
  64. , m_query(move(query))
  65. {
  66. }
  67. DOMURL::~DOMURL() = default;
  68. void DOMURL::initialize(JS::Realm& realm)
  69. {
  70. Base::initialize(realm);
  71. WEB_SET_PROTOTYPE_FOR_INTERFACE_WITH_CUSTOM_NAME(DOMURL, URL);
  72. }
  73. void DOMURL::visit_edges(Cell::Visitor& visitor)
  74. {
  75. Base::visit_edges(visitor);
  76. visitor.visit(m_query);
  77. }
  78. // https://w3c.github.io/FileAPI/#dfn-createObjectURL
  79. WebIDL::ExceptionOr<String> DOMURL::create_object_url(JS::VM& vm, JS::NonnullGCPtr<FileAPI::Blob> object)
  80. {
  81. // The createObjectURL(obj) static method must return the result of adding an entry to the blob URL store for obj.
  82. return TRY_OR_THROW_OOM(vm, FileAPI::add_entry_to_blob_url_store(object));
  83. }
  84. // https://w3c.github.io/FileAPI/#dfn-revokeObjectURL
  85. WebIDL::ExceptionOr<void> DOMURL::revoke_object_url(JS::VM& vm, StringView url)
  86. {
  87. // 1. Let url record be the result of parsing url.
  88. auto url_record = parse(url);
  89. // 2. If url record’s scheme is not "blob", return.
  90. if (url_record.scheme() != "blob"sv)
  91. return {};
  92. // 3. Let origin be the origin of url record.
  93. auto origin = url_origin(url_record);
  94. // 4. Let settings be the current settings object.
  95. auto& settings = HTML::current_settings_object();
  96. // 5. If origin is not same origin with settings’s origin, return.
  97. if (!origin.is_same_origin(settings.origin()))
  98. return {};
  99. // 6. Remove an entry from the Blob URL Store for url.
  100. TRY_OR_THROW_OOM(vm, FileAPI::remove_entry_from_blob_url_store(url));
  101. return {};
  102. }
  103. // https://url.spec.whatwg.org/#dom-url-canparse
  104. bool DOMURL::can_parse(JS::VM&, String const& url, Optional<String> const& base)
  105. {
  106. // 1. Let parsedURL be the result of running the API URL parser on url with base, if given.
  107. auto parsed_url = parse_api_url(url, base);
  108. // 2. If parsedURL is failure, then return false.
  109. if (!parsed_url.has_value())
  110. return false;
  111. // 3. Return true.
  112. return true;
  113. }
  114. // https://url.spec.whatwg.org/#dom-url-href
  115. WebIDL::ExceptionOr<String> DOMURL::href() const
  116. {
  117. auto& vm = realm().vm();
  118. // The href getter steps and the toJSON() method steps are to return the serialization of this’s URL.
  119. return TRY_OR_THROW_OOM(vm, String::from_byte_string(m_url.serialize()));
  120. }
  121. // https://url.spec.whatwg.org/#dom-url-tojson
  122. WebIDL::ExceptionOr<String> DOMURL::to_json() const
  123. {
  124. auto& vm = realm().vm();
  125. // The href getter steps and the toJSON() method steps are to return the serialization of this’s URL.
  126. return TRY_OR_THROW_OOM(vm, String::from_byte_string(m_url.serialize()));
  127. }
  128. // https://url.spec.whatwg.org/#ref-for-dom-url-href②
  129. WebIDL::ExceptionOr<void> DOMURL::set_href(String const& href)
  130. {
  131. auto& vm = realm().vm();
  132. // 1. Let parsedURL be the result of running the basic URL parser on the given value.
  133. URL::URL parsed_url = href;
  134. // 2. If parsedURL is failure, then throw a TypeError.
  135. if (!parsed_url.is_valid())
  136. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Invalid URL"sv };
  137. // 3. Set this’s URL to parsedURL.
  138. m_url = move(parsed_url);
  139. // 4. Empty this’s query object’s list.
  140. m_query->m_list.clear();
  141. // 5. Let query be this’s URL’s query.
  142. auto query = m_url.query();
  143. // 6. If query is non-null, then set this’s query object’s list to the result of parsing query.
  144. if (query.has_value())
  145. m_query->m_list = TRY_OR_THROW_OOM(vm, url_decode(*query));
  146. return {};
  147. }
  148. // https://url.spec.whatwg.org/#dom-url-origin
  149. WebIDL::ExceptionOr<String> DOMURL::origin() const
  150. {
  151. auto& vm = realm().vm();
  152. // The origin getter steps are to return the serialization of this’s URL’s origin. [HTML]
  153. return TRY_OR_THROW_OOM(vm, String::from_byte_string(m_url.serialize_origin()));
  154. }
  155. // https://url.spec.whatwg.org/#dom-url-protocol
  156. WebIDL::ExceptionOr<String> DOMURL::protocol() const
  157. {
  158. auto& vm = realm().vm();
  159. // The protocol getter steps are to return this’s URL’s scheme, followed by U+003A (:).
  160. return TRY_OR_THROW_OOM(vm, String::formatted("{}:", m_url.scheme()));
  161. }
  162. // https://url.spec.whatwg.org/#ref-for-dom-url-protocol%E2%91%A0
  163. WebIDL::ExceptionOr<void> DOMURL::set_protocol(String const& protocol)
  164. {
  165. auto& vm = realm().vm();
  166. // The protocol setter steps are to basic URL parse the given value, followed by U+003A (:), with this’s URL as
  167. // url and scheme start state as state override.
  168. auto result_url = URL::Parser::basic_parse(TRY_OR_THROW_OOM(vm, String::formatted("{}:", protocol)), {}, m_url, URL::Parser::State::SchemeStart);
  169. if (result_url.is_valid())
  170. m_url = move(result_url);
  171. return {};
  172. }
  173. // https://url.spec.whatwg.org/#dom-url-username
  174. WebIDL::ExceptionOr<String> DOMURL::username() const
  175. {
  176. auto& vm = realm().vm();
  177. // The username getter steps are to return this’s URL’s username.
  178. return TRY_OR_THROW_OOM(vm, m_url.username());
  179. }
  180. // https://url.spec.whatwg.org/#ref-for-dom-url-username%E2%91%A0
  181. void DOMURL::set_username(String const& username)
  182. {
  183. // 1. If this’s URL cannot have a username/password/port, then return.
  184. if (m_url.cannot_have_a_username_or_password_or_port())
  185. return;
  186. // 2. Set the username given this’s URL and the given value.
  187. MUST(m_url.set_username(username));
  188. }
  189. // https://url.spec.whatwg.org/#dom-url-password
  190. WebIDL::ExceptionOr<String> DOMURL::password() const
  191. {
  192. auto& vm = realm().vm();
  193. // The password getter steps are to return this’s URL’s password.
  194. return TRY_OR_THROW_OOM(vm, m_url.password());
  195. }
  196. // https://url.spec.whatwg.org/#ref-for-dom-url-password%E2%91%A0
  197. void DOMURL::set_password(String const& password)
  198. {
  199. // 1. If this’s URL cannot have a username/password/port, then return.
  200. if (m_url.cannot_have_a_username_or_password_or_port())
  201. return;
  202. // 2. Set the password given this’s URL and the given value.
  203. MUST(m_url.set_password(password));
  204. }
  205. // https://url.spec.whatwg.org/#dom-url-host
  206. WebIDL::ExceptionOr<String> DOMURL::host() const
  207. {
  208. auto& vm = realm().vm();
  209. // 1. Let url be this’s URL.
  210. auto& url = m_url;
  211. // 2. If url’s host is null, then return the empty string.
  212. if (url.host().has<Empty>())
  213. return String {};
  214. // 3. If url’s port is null, return url’s host, serialized.
  215. if (!url.port().has_value())
  216. return TRY_OR_THROW_OOM(vm, url.serialized_host());
  217. // 4. Return url’s host, serialized, followed by U+003A (:) and url’s port, serialized.
  218. return TRY_OR_THROW_OOM(vm, String::formatted("{}:{}", TRY_OR_THROW_OOM(vm, url.serialized_host()), *url.port()));
  219. }
  220. // https://url.spec.whatwg.org/#dom-url-hostref-for-dom-url-host%E2%91%A0
  221. void DOMURL::set_host(String const& host)
  222. {
  223. // 1. If this’s URL’s cannot-be-a-base-URL is true, then return.
  224. if (m_url.cannot_be_a_base_url())
  225. return;
  226. // 2. Basic URL parse the given value with this’s URL as url and host state as state override.
  227. auto result_url = URL::Parser::basic_parse(host, {}, m_url, URL::Parser::State::Host);
  228. if (result_url.is_valid())
  229. m_url = move(result_url);
  230. }
  231. // https://url.spec.whatwg.org/#dom-url-hostname
  232. WebIDL::ExceptionOr<String> DOMURL::hostname() const
  233. {
  234. auto& vm = realm().vm();
  235. // 1. If this’s URL’s host is null, then return the empty string.
  236. if (m_url.host().has<Empty>())
  237. return String {};
  238. // 2. Return this’s URL’s host, serialized.
  239. return TRY_OR_THROW_OOM(vm, m_url.serialized_host());
  240. }
  241. // https://url.spec.whatwg.org/#ref-for-dom-url-hostname①
  242. void DOMURL::set_hostname(String const& hostname)
  243. {
  244. // 1. If this’s URL’s cannot-be-a-base-URL is true, then return.
  245. if (m_url.cannot_be_a_base_url())
  246. return;
  247. // 2. Basic URL parse the given value with this’s URL as url and hostname state as state override.
  248. auto result_url = URL::Parser::basic_parse(hostname, {}, m_url, URL::Parser::State::Hostname);
  249. if (result_url.is_valid())
  250. m_url = move(result_url);
  251. }
  252. // https://url.spec.whatwg.org/#dom-url-port
  253. WebIDL::ExceptionOr<String> DOMURL::port() const
  254. {
  255. auto& vm = realm().vm();
  256. // 1. If this’s URL’s port is null, then return the empty string.
  257. if (!m_url.port().has_value())
  258. return String {};
  259. // 2. Return this’s URL’s port, serialized.
  260. return TRY_OR_THROW_OOM(vm, String::formatted("{}", *m_url.port()));
  261. }
  262. // https://url.spec.whatwg.org/#ref-for-dom-url-port%E2%91%A0
  263. void DOMURL::set_port(String const& port)
  264. {
  265. // 1. If this’s URL cannot have a username/password/port, then return.
  266. if (m_url.cannot_have_a_username_or_password_or_port())
  267. return;
  268. // 2. If the given value is the empty string, then set this’s URL’s port to null.
  269. if (port.is_empty()) {
  270. m_url.set_port({});
  271. }
  272. // 3. Otherwise, basic URL parse the given value with this’s URL as url and port state as state override.
  273. else {
  274. auto result_url = URL::Parser::basic_parse(port, {}, m_url, URL::Parser::State::Port);
  275. if (result_url.is_valid())
  276. m_url = move(result_url);
  277. }
  278. }
  279. // https://url.spec.whatwg.org/#dom-url-pathname
  280. WebIDL::ExceptionOr<String> DOMURL::pathname() const
  281. {
  282. auto& vm = realm().vm();
  283. // The pathname getter steps are to return the result of URL path serializing this’s URL.
  284. return TRY_OR_THROW_OOM(vm, String::from_byte_string(m_url.serialize_path(URL::ApplyPercentDecoding::No)));
  285. }
  286. // https://url.spec.whatwg.org/#ref-for-dom-url-pathname%E2%91%A0
  287. void DOMURL::set_pathname(String const& pathname)
  288. {
  289. // FIXME: These steps no longer match the speci.
  290. // 1. If this’s URL’s cannot-be-a-base-URL is true, then return.
  291. if (m_url.cannot_be_a_base_url())
  292. return;
  293. // 2. Empty this’s URL’s path.
  294. auto url = m_url; // We copy the URL here to follow other browser's behavior of reverting the path change if the parse failed.
  295. url.set_paths({});
  296. // 3. Basic URL parse the given value with this’s URL as url and path start state as state override.
  297. auto result_url = URL::Parser::basic_parse(pathname, {}, move(url), URL::Parser::State::PathStart);
  298. if (result_url.is_valid())
  299. m_url = move(result_url);
  300. }
  301. // https://url.spec.whatwg.org/#dom-url-search
  302. WebIDL::ExceptionOr<String> DOMURL::search() const
  303. {
  304. auto& vm = realm().vm();
  305. // 1. If this’s URL’s query is either null or the empty string, then return the empty string.
  306. if (!m_url.query().has_value() || m_url.query()->is_empty())
  307. return String {};
  308. // 2. Return U+003F (?), followed by this’s URL’s query.
  309. return TRY_OR_THROW_OOM(vm, String::formatted("?{}", *m_url.query()));
  310. }
  311. // https://url.spec.whatwg.org/#ref-for-dom-url-search%E2%91%A0
  312. WebIDL::ExceptionOr<void> DOMURL::set_search(String const& search)
  313. {
  314. auto& vm = realm().vm();
  315. // 1. Let url be this’s URL.
  316. auto& url = m_url;
  317. // 2. If the given value is the empty string:
  318. if (search.is_empty()) {
  319. // 1. Set url’s query to null.
  320. url.set_query({});
  321. // 2. Empty this’s query object’s list.
  322. m_query->m_list.clear();
  323. // 3. Potentially strip trailing spaces from an opaque path with this.
  324. strip_trailing_spaces_from_an_opaque_path(*this);
  325. // 4. Return.
  326. return {};
  327. }
  328. // 3. Let input be the given value with a single leading U+003F (?) removed, if any.
  329. auto search_as_string_view = search.bytes_as_string_view();
  330. auto input = search_as_string_view.substring_view(search_as_string_view.starts_with('?'));
  331. // 4. Set url’s query to the empty string.
  332. auto url_copy = url; // We copy the URL here to follow other browser's behavior of reverting the search change if the parse failed.
  333. url_copy.set_query(String {});
  334. // 5. Basic URL parse input with url as url and query state as state override.
  335. auto result_url = URL::Parser::basic_parse(input, {}, move(url_copy), URL::Parser::State::Query);
  336. if (result_url.is_valid()) {
  337. m_url = move(result_url);
  338. // 6. Set this’s query object’s list to the result of parsing input.
  339. m_query->m_list = TRY_OR_THROW_OOM(vm, url_decode(input));
  340. }
  341. return {};
  342. }
  343. // https://url.spec.whatwg.org/#dom-url-searchparams
  344. JS::NonnullGCPtr<URLSearchParams const> DOMURL::search_params() const
  345. {
  346. // The searchParams getter steps are to return this’s query object.
  347. return m_query;
  348. }
  349. // https://url.spec.whatwg.org/#dom-url-hash
  350. WebIDL::ExceptionOr<String> DOMURL::hash() const
  351. {
  352. auto& vm = realm().vm();
  353. // 1. If this’s URL’s fragment is either null or the empty string, then return the empty string.
  354. if (!m_url.fragment().has_value() || m_url.fragment()->is_empty())
  355. return String {};
  356. // 2. Return U+0023 (#), followed by this’s URL’s fragment.
  357. return TRY_OR_THROW_OOM(vm, String::formatted("#{}", m_url.fragment()));
  358. }
  359. // https://url.spec.whatwg.org/#ref-for-dom-url-hash%E2%91%A0
  360. void DOMURL::set_hash(String const& hash)
  361. {
  362. // 1. If the given value is the empty string:
  363. if (hash.is_empty()) {
  364. // 1. Set this’s URL’s fragment to null.
  365. m_url.set_fragment({});
  366. // 2. Potentially strip trailing spaces from an opaque path with this.
  367. strip_trailing_spaces_from_an_opaque_path(*this);
  368. // 3. Return.
  369. return;
  370. }
  371. // 2. Let input be the given value with a single leading U+0023 (#) removed, if any.
  372. auto hash_as_string_view = hash.bytes_as_string_view();
  373. auto input = hash_as_string_view.substring_view(hash_as_string_view.starts_with('#'));
  374. // 3. Set this’s URL’s fragment to the empty string.
  375. auto url = m_url; // We copy the URL here to follow other browser's behavior of reverting the hash change if the parse failed.
  376. url.set_fragment(String {});
  377. // 4. Basic URL parse input with this’s URL as url and fragment state as state override.
  378. auto result_url = URL::Parser::basic_parse(input, {}, move(url), URL::Parser::State::Fragment);
  379. if (result_url.is_valid())
  380. m_url = move(result_url);
  381. }
  382. // https://url.spec.whatwg.org/#concept-url-origin
  383. HTML::Origin url_origin(URL::URL const& url)
  384. {
  385. // FIXME: We should probably have an extended version of URL::URL for LibWeb instead of standalone functions like this.
  386. // The origin of a URL url is the origin returned by running these steps, switching on url’s scheme:
  387. // -> "blob"
  388. if (url.scheme() == "blob"sv) {
  389. auto url_string = url.to_string().release_value_but_fixme_should_propagate_errors();
  390. // 1. If url’s blob URL entry is non-null, then return url’s blob URL entry’s environment’s origin.
  391. if (auto blob_url_entry = FileAPI::blob_url_store().get(url_string); blob_url_entry.has_value())
  392. return blob_url_entry->environment->origin();
  393. // 2. Let pathURL be the result of parsing the result of URL path serializing url.
  394. auto path_url = parse(url.serialize_path());
  395. // 3. If pathURL is failure, then return a new opaque origin.
  396. if (!path_url.is_valid())
  397. return HTML::Origin {};
  398. // 4. If pathURL’s scheme is "http", "https", or "file", then return pathURL’s origin.
  399. if (path_url.scheme().is_one_of("http"sv, "https"sv, "file"sv))
  400. return url_origin(path_url);
  401. // 5. Return a new opaque origin.
  402. return HTML::Origin {};
  403. }
  404. // -> "ftp"
  405. // -> "http"
  406. // -> "https"
  407. // -> "ws"
  408. // -> "wss"
  409. if (url.scheme().is_one_of("ftp"sv, "http"sv, "https"sv, "ws"sv, "wss"sv)) {
  410. // Return the tuple origin (url’s scheme, url’s host, url’s port, null).
  411. return HTML::Origin(url.scheme().to_byte_string(), url.host(), url.port().value_or(0));
  412. }
  413. // -> "file"
  414. if (url.scheme() == "file"sv) {
  415. // Unfortunate as it is, this is left as an exercise to the reader. When in doubt, return a new opaque origin.
  416. // Note: We must return an origin with the `file://' protocol for `file://' iframes to work from `file://' pages.
  417. return HTML::Origin(url.scheme().to_byte_string(), String {}, 0);
  418. }
  419. // -> Otherwise
  420. // Return a new opaque origin.
  421. return HTML::Origin {};
  422. }
  423. // https://url.spec.whatwg.org/#concept-domain
  424. bool host_is_domain(URL::Host const& host)
  425. {
  426. // A domain is a non-empty ASCII string that identifies a realm within a network.
  427. return host.has<String>() && host.get<String>() != String {};
  428. }
  429. // https://url.spec.whatwg.org/#potentially-strip-trailing-spaces-from-an-opaque-path
  430. void strip_trailing_spaces_from_an_opaque_path(DOMURL& url)
  431. {
  432. // 1. If url’s URL does not have an opaque path, then return.
  433. // FIXME: Reimplement this step once we modernize the URL implementation to meet the spec.
  434. if (!url.cannot_be_a_base_url())
  435. return;
  436. // 2. If url’s URL’s fragment is non-null, then return.
  437. if (url.fragment().has_value())
  438. return;
  439. // 3. If url’s URL’s query is non-null, then return.
  440. if (url.query().has_value())
  441. return;
  442. // 4. Remove all trailing U+0020 SPACE code points from url’s URL’s path.
  443. // NOTE: At index 0 since the first step tells us that the URL only has one path segment.
  444. auto opaque_path = url.path_segment_at_index(0);
  445. auto trimmed_path = opaque_path.trim(" "sv, TrimMode::Right);
  446. url.set_paths({ trimmed_path });
  447. }
  448. // https://url.spec.whatwg.org/#concept-url-parser
  449. URL::URL parse(StringView input, Optional<URL::URL> const& base_url)
  450. {
  451. // FIXME: We should probably have an extended version of URL::URL for LibWeb instead of standalone functions like this.
  452. // 1. Let url be the result of running the basic URL parser on input with base and encoding.
  453. auto url = URL::Parser::basic_parse(input, base_url);
  454. // 2. If url is failure, return failure.
  455. if (!url.is_valid())
  456. return {};
  457. // 3. If url’s scheme is not "blob",
  458. if (url.scheme() != "blob")
  459. return url;
  460. // FIXME: 4. Set url’s blob URL entry to the result of resolving the blob URL url,
  461. // FIXME: 5. if that did not return failure, and null otherwise.
  462. // 6. Return url
  463. return url;
  464. }
  465. }