DOMURL.cpp 21 KB

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