DOMURL.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  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 = MUST(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. auto& vm = realm().vm();
  153. // 1. Let parsedURL be the result of running the basic URL parser on the given value.
  154. URL::URL parsed_url = href;
  155. // 2. If parsedURL is failure, then throw a TypeError.
  156. if (!parsed_url.is_valid())
  157. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Invalid URL"sv };
  158. // 3. Set this’s URL to parsedURL.
  159. m_url = move(parsed_url);
  160. // 4. Empty this’s query object’s list.
  161. m_query->m_list.clear();
  162. // 5. Let query be this’s URL’s query.
  163. auto query = m_url.query();
  164. // 6. If query is non-null, then set this’s query object’s list to the result of parsing query.
  165. if (query.has_value())
  166. m_query->m_list = TRY_OR_THROW_OOM(vm, url_decode(*query));
  167. return {};
  168. }
  169. // https://url.spec.whatwg.org/#dom-url-origin
  170. WebIDL::ExceptionOr<String> DOMURL::origin() const
  171. {
  172. auto& vm = realm().vm();
  173. // The origin getter steps are to return the serialization of this’s URL’s origin. [HTML]
  174. return TRY_OR_THROW_OOM(vm, String::from_byte_string(m_url.serialize_origin()));
  175. }
  176. // https://url.spec.whatwg.org/#dom-url-protocol
  177. WebIDL::ExceptionOr<String> DOMURL::protocol() const
  178. {
  179. auto& vm = realm().vm();
  180. // The protocol getter steps are to return this’s URL’s scheme, followed by U+003A (:).
  181. return TRY_OR_THROW_OOM(vm, String::formatted("{}:", m_url.scheme()));
  182. }
  183. // https://url.spec.whatwg.org/#ref-for-dom-url-protocol%E2%91%A0
  184. WebIDL::ExceptionOr<void> DOMURL::set_protocol(String const& protocol)
  185. {
  186. auto& vm = realm().vm();
  187. // The protocol setter steps are to basic URL parse the given value, followed by U+003A (:), with this’s URL as
  188. // url and scheme start state as state override.
  189. auto result_url = URL::Parser::basic_parse(TRY_OR_THROW_OOM(vm, String::formatted("{}:", protocol)), {}, m_url, URL::Parser::State::SchemeStart);
  190. if (result_url.is_valid())
  191. m_url = move(result_url);
  192. return {};
  193. }
  194. // https://url.spec.whatwg.org/#dom-url-username
  195. String const& DOMURL::username() const
  196. {
  197. // The username getter steps are to return this’s URL’s username.
  198. return m_url.username();
  199. }
  200. // https://url.spec.whatwg.org/#ref-for-dom-url-username%E2%91%A0
  201. void DOMURL::set_username(String const& username)
  202. {
  203. // 1. If this’s URL cannot have a username/password/port, then return.
  204. if (m_url.cannot_have_a_username_or_password_or_port())
  205. return;
  206. // 2. Set the username given this’s URL and the given value.
  207. MUST(m_url.set_username(username));
  208. }
  209. // https://url.spec.whatwg.org/#dom-url-password
  210. String const& DOMURL::password() const
  211. {
  212. // The password getter steps are to return this’s URL’s password.
  213. return m_url.password();
  214. }
  215. // https://url.spec.whatwg.org/#ref-for-dom-url-password%E2%91%A0
  216. void DOMURL::set_password(String const& password)
  217. {
  218. // 1. If this’s URL cannot have a username/password/port, then return.
  219. if (m_url.cannot_have_a_username_or_password_or_port())
  220. return;
  221. // 2. Set the password given this’s URL and the given value.
  222. MUST(m_url.set_password(password));
  223. }
  224. // https://url.spec.whatwg.org/#dom-url-host
  225. WebIDL::ExceptionOr<String> DOMURL::host() const
  226. {
  227. auto& vm = realm().vm();
  228. // 1. Let url be this’s URL.
  229. auto& url = m_url;
  230. // 2. If url’s host is null, then return the empty string.
  231. if (url.host().has<Empty>())
  232. return String {};
  233. // 3. If url’s port is null, return url’s host, serialized.
  234. if (!url.port().has_value())
  235. return TRY_OR_THROW_OOM(vm, url.serialized_host());
  236. // 4. Return url’s host, serialized, followed by U+003A (:) and url’s port, serialized.
  237. return TRY_OR_THROW_OOM(vm, String::formatted("{}:{}", TRY_OR_THROW_OOM(vm, url.serialized_host()), *url.port()));
  238. }
  239. // https://url.spec.whatwg.org/#dom-url-hostref-for-dom-url-host%E2%91%A0
  240. void DOMURL::set_host(String const& host)
  241. {
  242. // 1. If this’s URL’s cannot-be-a-base-URL is true, then return.
  243. if (m_url.cannot_be_a_base_url())
  244. return;
  245. // 2. Basic URL parse the given value with this’s URL as url and host state as state override.
  246. auto result_url = URL::Parser::basic_parse(host, {}, m_url, URL::Parser::State::Host);
  247. if (result_url.is_valid())
  248. m_url = move(result_url);
  249. }
  250. // https://url.spec.whatwg.org/#dom-url-hostname
  251. WebIDL::ExceptionOr<String> DOMURL::hostname() const
  252. {
  253. auto& vm = realm().vm();
  254. // 1. If this’s URL’s host is null, then return the empty string.
  255. if (m_url.host().has<Empty>())
  256. return String {};
  257. // 2. Return this’s URL’s host, serialized.
  258. return TRY_OR_THROW_OOM(vm, m_url.serialized_host());
  259. }
  260. // https://url.spec.whatwg.org/#ref-for-dom-url-hostname①
  261. void DOMURL::set_hostname(String const& hostname)
  262. {
  263. // 1. If this’s URL’s cannot-be-a-base-URL is true, then return.
  264. if (m_url.cannot_be_a_base_url())
  265. return;
  266. // 2. Basic URL parse the given value with this’s URL as url and hostname state as state override.
  267. auto result_url = URL::Parser::basic_parse(hostname, {}, m_url, URL::Parser::State::Hostname);
  268. if (result_url.is_valid())
  269. m_url = move(result_url);
  270. }
  271. // https://url.spec.whatwg.org/#dom-url-port
  272. WebIDL::ExceptionOr<String> DOMURL::port() const
  273. {
  274. auto& vm = realm().vm();
  275. // 1. If this’s URL’s port is null, then return the empty string.
  276. if (!m_url.port().has_value())
  277. return String {};
  278. // 2. Return this’s URL’s port, serialized.
  279. return TRY_OR_THROW_OOM(vm, String::formatted("{}", *m_url.port()));
  280. }
  281. // https://url.spec.whatwg.org/#ref-for-dom-url-port%E2%91%A0
  282. void DOMURL::set_port(String const& port)
  283. {
  284. // 1. If this’s URL cannot have a username/password/port, then return.
  285. if (m_url.cannot_have_a_username_or_password_or_port())
  286. return;
  287. // 2. If the given value is the empty string, then set this’s URL’s port to null.
  288. if (port.is_empty()) {
  289. m_url.set_port({});
  290. }
  291. // 3. Otherwise, basic URL parse the given value with this’s URL as url and port state as state override.
  292. else {
  293. auto result_url = URL::Parser::basic_parse(port, {}, m_url, URL::Parser::State::Port);
  294. if (result_url.is_valid())
  295. m_url = move(result_url);
  296. }
  297. }
  298. // https://url.spec.whatwg.org/#dom-url-pathname
  299. String DOMURL::pathname() const
  300. {
  301. // The pathname getter steps are to return the result of URL path serializing this’s URL.
  302. return m_url.serialize_path();
  303. }
  304. // https://url.spec.whatwg.org/#ref-for-dom-url-pathname%E2%91%A0
  305. void DOMURL::set_pathname(String const& pathname)
  306. {
  307. // FIXME: These steps no longer match the speci.
  308. // 1. If this’s URL’s cannot-be-a-base-URL is true, then return.
  309. if (m_url.cannot_be_a_base_url())
  310. return;
  311. // 2. Empty this’s URL’s path.
  312. auto url = m_url; // We copy the URL here to follow other browser's behavior of reverting the path change if the parse failed.
  313. url.set_paths({});
  314. // 3. Basic URL parse the given value with this’s URL as url and path start state as state override.
  315. auto result_url = URL::Parser::basic_parse(pathname, {}, move(url), URL::Parser::State::PathStart);
  316. if (result_url.is_valid())
  317. m_url = move(result_url);
  318. }
  319. // https://url.spec.whatwg.org/#dom-url-search
  320. WebIDL::ExceptionOr<String> DOMURL::search() const
  321. {
  322. auto& vm = realm().vm();
  323. // 1. If this’s URL’s query is either null or the empty string, then return the empty string.
  324. if (!m_url.query().has_value() || m_url.query()->is_empty())
  325. return String {};
  326. // 2. Return U+003F (?), followed by this’s URL’s query.
  327. return TRY_OR_THROW_OOM(vm, String::formatted("?{}", *m_url.query()));
  328. }
  329. // https://url.spec.whatwg.org/#ref-for-dom-url-search%E2%91%A0
  330. WebIDL::ExceptionOr<void> DOMURL::set_search(String const& search)
  331. {
  332. auto& vm = realm().vm();
  333. // 1. Let url be this’s URL.
  334. auto& url = m_url;
  335. // 2. If the given value is the empty string:
  336. if (search.is_empty()) {
  337. // 1. Set url’s query to null.
  338. url.set_query({});
  339. // 2. Empty this’s query object’s list.
  340. m_query->m_list.clear();
  341. // 3. Potentially strip trailing spaces from an opaque path with this.
  342. strip_trailing_spaces_from_an_opaque_path(*this);
  343. // 4. Return.
  344. return {};
  345. }
  346. // 3. Let input be the given value with a single leading U+003F (?) removed, if any.
  347. auto search_as_string_view = search.bytes_as_string_view();
  348. auto input = search_as_string_view.substring_view(search_as_string_view.starts_with('?'));
  349. // 4. Set url’s query to the empty string.
  350. auto url_copy = url; // We copy the URL here to follow other browser's behavior of reverting the search change if the parse failed.
  351. url_copy.set_query(String {});
  352. // 5. Basic URL parse input with url as url and query state as state override.
  353. auto result_url = URL::Parser::basic_parse(input, {}, move(url_copy), URL::Parser::State::Query);
  354. if (result_url.is_valid()) {
  355. m_url = move(result_url);
  356. // 6. Set this’s query object’s list to the result of parsing input.
  357. m_query->m_list = TRY_OR_THROW_OOM(vm, url_decode(input));
  358. }
  359. return {};
  360. }
  361. // https://url.spec.whatwg.org/#dom-url-searchparams
  362. JS::NonnullGCPtr<URLSearchParams const> DOMURL::search_params() const
  363. {
  364. // The searchParams getter steps are to return this’s query object.
  365. return m_query;
  366. }
  367. // https://url.spec.whatwg.org/#dom-url-hash
  368. WebIDL::ExceptionOr<String> DOMURL::hash() const
  369. {
  370. auto& vm = realm().vm();
  371. // 1. If this’s URL’s fragment is either null or the empty string, then return the empty string.
  372. if (!m_url.fragment().has_value() || m_url.fragment()->is_empty())
  373. return String {};
  374. // 2. Return U+0023 (#), followed by this’s URL’s fragment.
  375. return TRY_OR_THROW_OOM(vm, String::formatted("#{}", m_url.fragment()));
  376. }
  377. // https://url.spec.whatwg.org/#ref-for-dom-url-hash%E2%91%A0
  378. void DOMURL::set_hash(String const& hash)
  379. {
  380. // 1. If the given value is the empty string:
  381. if (hash.is_empty()) {
  382. // 1. Set this’s URL’s fragment to null.
  383. m_url.set_fragment({});
  384. // 2. Potentially strip trailing spaces from an opaque path with this.
  385. strip_trailing_spaces_from_an_opaque_path(*this);
  386. // 3. Return.
  387. return;
  388. }
  389. // 2. Let input be the given value with a single leading U+0023 (#) removed, if any.
  390. auto hash_as_string_view = hash.bytes_as_string_view();
  391. auto input = hash_as_string_view.substring_view(hash_as_string_view.starts_with('#'));
  392. // 3. Set this’s URL’s fragment to the empty string.
  393. auto url = m_url; // We copy the URL here to follow other browser's behavior of reverting the hash change if the parse failed.
  394. url.set_fragment(String {});
  395. // 4. Basic URL parse input with this’s URL as url and fragment state as state override.
  396. auto result_url = URL::Parser::basic_parse(input, {}, move(url), URL::Parser::State::Fragment);
  397. if (result_url.is_valid())
  398. m_url = move(result_url);
  399. }
  400. // https://url.spec.whatwg.org/#concept-url-origin
  401. HTML::Origin url_origin(URL::URL const& url)
  402. {
  403. // FIXME: We should probably have an extended version of URL::URL for LibWeb instead of standalone functions like this.
  404. // The origin of a URL url is the origin returned by running these steps, switching on url’s scheme:
  405. // -> "blob"
  406. if (url.scheme() == "blob"sv) {
  407. auto url_string = url.to_string().release_value_but_fixme_should_propagate_errors();
  408. // 1. If url’s blob URL entry is non-null, then return url’s blob URL entry’s environment’s origin.
  409. if (auto blob_url_entry = FileAPI::blob_url_store().get(url_string); blob_url_entry.has_value())
  410. return blob_url_entry->environment->origin();
  411. // 2. Let pathURL be the result of parsing the result of URL path serializing url.
  412. auto path_url = parse(url.serialize_path());
  413. // 3. If pathURL is failure, then return a new opaque origin.
  414. if (!path_url.is_valid())
  415. return HTML::Origin {};
  416. // 4. If pathURL’s scheme is "http", "https", or "file", then return pathURL’s origin.
  417. if (path_url.scheme().is_one_of("http"sv, "https"sv, "file"sv))
  418. return url_origin(path_url);
  419. // 5. Return a new opaque origin.
  420. return HTML::Origin {};
  421. }
  422. // -> "ftp"
  423. // -> "http"
  424. // -> "https"
  425. // -> "ws"
  426. // -> "wss"
  427. if (url.scheme().is_one_of("ftp"sv, "http"sv, "https"sv, "ws"sv, "wss"sv)) {
  428. // Return the tuple origin (url’s scheme, url’s host, url’s port, null).
  429. return HTML::Origin(url.scheme().to_byte_string(), url.host(), url.port().value_or(0));
  430. }
  431. // -> "file"
  432. // AD-HOC: Our resource:// is basically an alias to file://
  433. if (url.scheme() == "file"sv || url.scheme() == "resource"sv) {
  434. // Unfortunate as it is, this is left as an exercise to the reader. When in doubt, return a new opaque origin.
  435. // Note: We must return an origin with the `file://' protocol for `file://' iframes to work from `file://' pages.
  436. return HTML::Origin(url.scheme().to_byte_string(), String {}, 0);
  437. }
  438. // -> Otherwise
  439. // Return a new opaque origin.
  440. return HTML::Origin {};
  441. }
  442. // https://url.spec.whatwg.org/#concept-domain
  443. bool host_is_domain(URL::Host const& host)
  444. {
  445. // A domain is a non-empty ASCII string that identifies a realm within a network.
  446. return host.has<String>() && host.get<String>() != String {};
  447. }
  448. // https://url.spec.whatwg.org/#potentially-strip-trailing-spaces-from-an-opaque-path
  449. void strip_trailing_spaces_from_an_opaque_path(DOMURL& url)
  450. {
  451. // 1. If url’s URL does not have an opaque path, then return.
  452. // FIXME: Reimplement this step once we modernize the URL implementation to meet the spec.
  453. if (!url.cannot_be_a_base_url())
  454. return;
  455. // 2. If url’s URL’s fragment is non-null, then return.
  456. if (url.fragment().has_value())
  457. return;
  458. // 3. If url’s URL’s query is non-null, then return.
  459. if (url.query().has_value())
  460. return;
  461. // 4. Remove all trailing U+0020 SPACE code points from url’s URL’s path.
  462. // NOTE: At index 0 since the first step tells us that the URL only has one path segment.
  463. auto opaque_path = url.path_segment_at_index(0);
  464. auto trimmed_path = opaque_path.trim(" "sv, TrimMode::Right);
  465. url.set_paths({ trimmed_path });
  466. }
  467. // https://url.spec.whatwg.org/#concept-url-parser
  468. URL::URL parse(StringView input, Optional<URL::URL> const& base_url)
  469. {
  470. // FIXME: We should probably have an extended version of URL::URL for LibWeb instead of standalone functions like this.
  471. // 1. Let url be the result of running the basic URL parser on input with base and encoding.
  472. auto url = URL::Parser::basic_parse(input, base_url);
  473. // 2. If url is failure, return failure.
  474. if (!url.is_valid())
  475. return {};
  476. // 3. If url’s scheme is not "blob", return url.
  477. if (url.scheme() != "blob")
  478. return url;
  479. // 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.
  480. auto blob_url_entry = FileAPI::resolve_a_blob_url(url);
  481. if (blob_url_entry.has_value()) {
  482. url.set_blob_url_entry(URL::BlobURLEntry {
  483. .type = blob_url_entry->object->type(),
  484. .byte_buffer = MUST(ByteBuffer::copy(blob_url_entry->object->raw_bytes())),
  485. });
  486. }
  487. // 5. Return url
  488. return url;
  489. }
  490. }