DOMURL.cpp 22 KB

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