DOMURL.cpp 22 KB

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