HTMLHyperlinkElementUtils.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/URLParser.h>
  7. #include <LibWeb/DOM/Document.h>
  8. #include <LibWeb/HTML/HTMLHyperlinkElementUtils.h>
  9. #include <LibWeb/HTML/Navigable.h>
  10. #include <LibWeb/Loader/FrameLoader.h>
  11. namespace Web::HTML {
  12. HTMLHyperlinkElementUtils::~HTMLHyperlinkElementUtils() = default;
  13. // https://html.spec.whatwg.org/multipage/links.html#reinitialise-url
  14. void HTMLHyperlinkElementUtils::reinitialize_url() const
  15. {
  16. // 1. If element's url is non-null, its scheme is "blob", and its cannot-be-a-basFe-URL is true, terminate these steps.
  17. if (m_url.has_value() && m_url->scheme() == "blob"sv && m_url->cannot_be_a_base_url())
  18. return;
  19. // 2. Set the url.
  20. const_cast<HTMLHyperlinkElementUtils*>(this)->set_the_url();
  21. }
  22. // https://html.spec.whatwg.org/multipage/links.html#concept-hyperlink-url-set
  23. void HTMLHyperlinkElementUtils::set_the_url()
  24. {
  25. // 1. If this element's href content attribute is absent, set this element's url to null.
  26. auto href_content_attribute = hyperlink_element_utils_href();
  27. if (href_content_attribute.is_null()) {
  28. m_url = {};
  29. return;
  30. }
  31. // 2. Otherwise, parse this element's href content attribute value relative to this element's node document.
  32. // If parsing is successful, set this element's url to the result; otherwise, set this element's url to null.
  33. m_url = hyperlink_element_utils_document().parse_url(href_content_attribute);
  34. }
  35. // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-origin
  36. DeprecatedString HTMLHyperlinkElementUtils::origin() const
  37. {
  38. // 1. Reinitialize url.
  39. reinitialize_url();
  40. // 2. If this element's url is null, return the empty string.
  41. if (!m_url.has_value())
  42. return DeprecatedString::empty();
  43. // 3. Return the serialization of this element's url's origin.
  44. return m_url->serialize_origin();
  45. }
  46. // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-protocol
  47. DeprecatedString HTMLHyperlinkElementUtils::protocol() const
  48. {
  49. // 1. Reinitialize url.
  50. reinitialize_url();
  51. // 2. If this element's url is null, return ":".
  52. if (!m_url.has_value())
  53. return ":"sv;
  54. // 3. Return this element's url's scheme, followed by ":".
  55. return DeprecatedString::formatted("{}:", m_url->scheme());
  56. }
  57. // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-protocol
  58. void HTMLHyperlinkElementUtils::set_protocol(StringView protocol)
  59. {
  60. // 1. Reinitialize url.
  61. reinitialize_url();
  62. // 2. If this element's url is null, terminate these steps.
  63. if (!m_url.has_value())
  64. return;
  65. // 3. Basic URL parse the given value, followed by ":", with this element's url as url and scheme start state as state override.
  66. auto result_url = URLParser::basic_parse(DeprecatedString::formatted("{}:", protocol), {}, m_url, URLParser::State::SchemeStart);
  67. if (result_url.is_valid())
  68. m_url = move(result_url);
  69. // 4. Update href.
  70. update_href();
  71. }
  72. // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-username
  73. DeprecatedString HTMLHyperlinkElementUtils::username() const
  74. {
  75. // 1. Reinitialize url.
  76. reinitialize_url();
  77. // 2. If this element's url is null, return the empty string.
  78. if (!m_url.has_value())
  79. return DeprecatedString::empty();
  80. // 3. Return this element's url's username.
  81. return m_url->username().release_value().to_deprecated_string();
  82. }
  83. // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-username
  84. void HTMLHyperlinkElementUtils::set_username(StringView username)
  85. {
  86. // 1. Reinitialize url.
  87. reinitialize_url();
  88. // 2. Let url be this element's url.
  89. auto& url = m_url;
  90. // 3. If url is null or url cannot have a username/password/port, then return.
  91. if (!url.has_value() || url->cannot_have_a_username_or_password_or_port())
  92. return;
  93. // 4. Set the username given this’s URL and the given value.
  94. MUST(url->set_username(username));
  95. // 5. Update href.
  96. update_href();
  97. }
  98. // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-password
  99. DeprecatedString HTMLHyperlinkElementUtils::password() const
  100. {
  101. // 1. Reinitialize url.
  102. reinitialize_url();
  103. // 2. Let url be this element's url.
  104. auto& url = m_url;
  105. // 3. If url is null, then return the empty string.
  106. if (!url.has_value())
  107. return DeprecatedString::empty();
  108. // 4. Return url's password.
  109. return url->password().release_value().to_deprecated_string();
  110. }
  111. // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-password
  112. void HTMLHyperlinkElementUtils::set_password(StringView password)
  113. {
  114. // 1. Reinitialize url.
  115. reinitialize_url();
  116. // 2. Let url be this element's url.
  117. auto& url = m_url;
  118. // 3. If url is null or url cannot have a username/password/port, then return.
  119. if (!url.has_value() || url->cannot_have_a_username_or_password_or_port())
  120. return;
  121. // 4. Set the password, given url and the given value.
  122. MUST(url->set_password(password));
  123. // 5. Update href.
  124. update_href();
  125. }
  126. // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-host
  127. DeprecatedString HTMLHyperlinkElementUtils::host() const
  128. {
  129. // 1. Reinitialize url.
  130. reinitialize_url();
  131. // 2. Let url be this element's url.
  132. auto& url = m_url;
  133. // 3. If url or url's host is null, return the empty string.
  134. if (!url.has_value() || url->host().has<Empty>())
  135. return DeprecatedString::empty();
  136. // 4. If url's port is null, return url's host, serialized.
  137. if (!url->port().has_value())
  138. return url->serialized_host().release_value_but_fixme_should_propagate_errors().to_deprecated_string();
  139. // 5. Return url's host, serialized, followed by ":" and url's port, serialized.
  140. return DeprecatedString::formatted("{}:{}", url->serialized_host().release_value_but_fixme_should_propagate_errors(), url->port().value());
  141. }
  142. // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-host
  143. void HTMLHyperlinkElementUtils::set_host(StringView host)
  144. {
  145. // 1. Reinitialize url.
  146. reinitialize_url();
  147. // 2. Let url be this element's url.
  148. auto& url = m_url;
  149. // 3. If url is null or url's cannot-be-a-base-URL is true, then return.
  150. if (!url.has_value() || url->cannot_be_a_base_url())
  151. return;
  152. // 4. Basic URL parse the given value, with url as url and host state as state override.
  153. auto result_url = URLParser::basic_parse(host, {}, url, URLParser::State::Host);
  154. if (result_url.is_valid())
  155. m_url = move(result_url);
  156. // 5. Update href.
  157. update_href();
  158. }
  159. DeprecatedString HTMLHyperlinkElementUtils::hostname() const
  160. {
  161. // 1. Reinitialize url.
  162. //
  163. // 2. Let url be this element's url.
  164. AK::URL url(href());
  165. // 3. If url or url's host is null, return the empty string.
  166. if (url.host().has<Empty>())
  167. return DeprecatedString::empty();
  168. // 4. Return url's host, serialized.
  169. return url.serialized_host().release_value_but_fixme_should_propagate_errors().to_deprecated_string();
  170. }
  171. void HTMLHyperlinkElementUtils::set_hostname(StringView hostname)
  172. {
  173. // 1. Reinitialize url.
  174. reinitialize_url();
  175. // 2. Let url be this element's url.
  176. auto& url = m_url;
  177. // 3. If url is null or url's cannot-be-a-base-URL is true, then return.
  178. if (!url.has_value() || url->cannot_be_a_base_url())
  179. return;
  180. // 4. Basic URL parse the given value, with url as url and hostname state as state override.
  181. auto result_url = URLParser::basic_parse(hostname, {}, m_url, URLParser::State::Hostname);
  182. if (result_url.is_valid())
  183. m_url = move(result_url);
  184. // 5. Update href.
  185. update_href();
  186. }
  187. // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-port
  188. DeprecatedString HTMLHyperlinkElementUtils::port() const
  189. {
  190. // 1. Reinitialize url.
  191. reinitialize_url();
  192. // 2. Let url be this element's url.
  193. auto& url = m_url;
  194. // 3. If url or url's port is null, return the empty string.
  195. if (!url.has_value() || !url->port().has_value())
  196. return DeprecatedString::empty();
  197. // 4. Return url's port, serialized.
  198. return DeprecatedString::number(url->port().value());
  199. }
  200. // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-port
  201. void HTMLHyperlinkElementUtils::set_port(StringView port)
  202. {
  203. // 1. Reinitialize url.
  204. reinitialize_url();
  205. // 2. Let url be this element's url.
  206. // 3. If url is null or url cannot have a username/password/port, then return.
  207. if (!m_url.has_value() || m_url->cannot_have_a_username_or_password_or_port())
  208. return;
  209. // 4. If the given value is the empty string, then set url's port to null.
  210. if (port.is_empty()) {
  211. m_url->set_port({});
  212. } else {
  213. // 5. Otherwise, basic URL parse the given value, with url as url and port state as state override.
  214. auto result_url = URLParser::basic_parse(port, {}, m_url, URLParser::State::Port);
  215. if (result_url.is_valid())
  216. m_url = move(result_url);
  217. }
  218. // 6. Update href.
  219. update_href();
  220. }
  221. // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-pathname
  222. DeprecatedString HTMLHyperlinkElementUtils::pathname() const
  223. {
  224. // 1. Reinitialize url.
  225. reinitialize_url();
  226. // 2. Let url be this element's url.
  227. // 3. If url is null, return the empty string.
  228. if (!m_url.has_value())
  229. return DeprecatedString::empty();
  230. // 4. If url's cannot-be-a-base-URL is true, then return url's path[0].
  231. // 5. If url's path is empty, then return the empty string.
  232. // 6. Return "/", followed by the strings in url's path (including empty strings), separated from each other by "/".
  233. return m_url->serialize_path();
  234. }
  235. // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-pathname
  236. void HTMLHyperlinkElementUtils::set_pathname(StringView pathname)
  237. {
  238. // 1. Reinitialize url.
  239. reinitialize_url();
  240. // 2. Let url be this element's url.
  241. // 3. If url is null or url's cannot-be-a-base-URL is true, then return.
  242. if (!m_url.has_value() || m_url->cannot_be_a_base_url())
  243. return;
  244. // 4. Set url's path to the empty list.
  245. auto url = m_url; // We copy the URL here to follow other browser's behavior of reverting the path change if the parse failed.
  246. url->set_paths({});
  247. // 5. Basic URL parse the given value, with url as url and path start state as state override.
  248. auto result_url = URLParser::basic_parse(pathname, {}, move(url), URLParser::State::PathStart);
  249. if (result_url.is_valid())
  250. m_url = move(result_url);
  251. // 6. Update href.
  252. update_href();
  253. }
  254. DeprecatedString HTMLHyperlinkElementUtils::search() const
  255. {
  256. // 1. Reinitialize url.
  257. reinitialize_url();
  258. // 2. Let url be this element's url.
  259. // 3. If url is null, or url's query is either null or the empty string, return the empty string.
  260. if (!m_url.has_value() || !m_url->query().has_value() || m_url->query()->is_empty())
  261. return DeprecatedString::empty();
  262. // 4. Return "?", followed by url's query.
  263. return DeprecatedString::formatted("?{}", m_url->query());
  264. }
  265. void HTMLHyperlinkElementUtils::set_search(StringView search)
  266. {
  267. // 1. Reinitialize url.
  268. reinitialize_url();
  269. // 2. Let url be this element's url.
  270. // 3. If url is null, terminate these steps.
  271. if (!m_url.has_value())
  272. return;
  273. // 4. If the given value is the empty string, set url's query to null.
  274. if (search.is_empty()) {
  275. m_url->set_query({});
  276. } else {
  277. // 5. Otherwise:
  278. // 1. Let input be the given value with a single leading "?" removed, if any.
  279. auto input = search.substring_view(search.starts_with('?'));
  280. // 2. Set url's query to the empty string.
  281. auto url_copy = m_url; // We copy the URL here to follow other browser's behavior of reverting the search change if the parse failed.
  282. url_copy->set_query(String {});
  283. // 3. Basic URL parse input, with null, this element's node document's document's character encoding, url as url, and query state as state override.
  284. auto result_url = URLParser::basic_parse(input, {}, move(url_copy), URLParser::State::Query);
  285. if (result_url.is_valid())
  286. m_url = move(result_url);
  287. }
  288. // 6. Update href.
  289. update_href();
  290. }
  291. DeprecatedString HTMLHyperlinkElementUtils::hash() const
  292. {
  293. // 1. Reinitialize url.
  294. reinitialize_url();
  295. // 2. Let url be this element's url.
  296. // 3. If url is null, or url's fragment is either null or the empty string, return the empty string.
  297. if (!m_url.has_value() || !m_url->fragment().has_value() || m_url->fragment()->is_empty())
  298. return DeprecatedString::empty();
  299. // 4. Return "#", followed by url's fragment.
  300. return DeprecatedString::formatted("#{}", *m_url->fragment());
  301. }
  302. void HTMLHyperlinkElementUtils::set_hash(StringView hash)
  303. {
  304. // 1. Reinitialize url.
  305. reinitialize_url();
  306. // 2. Let url be this element's url.
  307. // 3. If url is null, then return.
  308. if (!m_url.has_value())
  309. return;
  310. // 4. If the given value is the empty string, set url's fragment to null.
  311. if (hash.is_empty()) {
  312. m_url->set_fragment({});
  313. } else {
  314. // 5. Otherwise:
  315. // 1. Let input be the given value with a single leading "#" removed, if any.
  316. auto input = hash.substring_view(hash.starts_with('#'));
  317. // 2. Set url's fragment to the empty string.
  318. auto url_copy = m_url; // We copy the URL here to follow other browser's behavior of reverting the hash change if the parse failed.
  319. url_copy->set_fragment(String {});
  320. // 3. Basic URL parse input, with url as url and fragment state as state override.
  321. auto result_url = URLParser::basic_parse(input, {}, move(url_copy), URLParser::State::Fragment);
  322. if (result_url.is_valid())
  323. m_url = move(result_url);
  324. }
  325. // 6. Update href.
  326. update_href();
  327. }
  328. // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-href
  329. DeprecatedString HTMLHyperlinkElementUtils::href() const
  330. {
  331. // 1. Reinitialize url.
  332. reinitialize_url();
  333. // 2. Let url be this element's url.
  334. auto& url = m_url;
  335. // 3. If url is null and this element has no href content attribute, return the empty string.
  336. auto href_content_attribute = hyperlink_element_utils_href();
  337. if (!url.has_value() && href_content_attribute.is_null())
  338. return DeprecatedString::empty();
  339. // 4. Otherwise, if url is null, return this element's href content attribute's value.
  340. if (!url->is_valid())
  341. return href_content_attribute;
  342. // 5. Return url, serialized.
  343. return url->serialize();
  344. }
  345. // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-href
  346. WebIDL::ExceptionOr<void> HTMLHyperlinkElementUtils::set_href(StringView href)
  347. {
  348. // The href attribute's setter must set this element's href content attribute's value to the given value.
  349. return set_hyperlink_element_utils_href(href);
  350. }
  351. // https://html.spec.whatwg.org/multipage/links.html#update-href
  352. void HTMLHyperlinkElementUtils::update_href()
  353. {
  354. // To update href, set the element's href content attribute's value to the element's url, serialized.
  355. }
  356. bool HTMLHyperlinkElementUtils::cannot_navigate() const
  357. {
  358. // An element element cannot navigate if one of the following is true:
  359. // 1. element's node document is not fully active
  360. auto const& document = const_cast<HTMLHyperlinkElementUtils*>(this)->hyperlink_element_utils_document();
  361. if (!document.is_fully_active())
  362. return true;
  363. // 2. element is not an a element and is not connected.
  364. if (!hyperlink_element_utils_is_html_anchor_element() && !hyperlink_element_utils_is_connected())
  365. return true;
  366. return false;
  367. }
  368. // https://html.spec.whatwg.org/multipage/links.html#following-hyperlinks-2
  369. void HTMLHyperlinkElementUtils::follow_the_hyperlink(Optional<DeprecatedString> hyperlink_suffix)
  370. {
  371. // 1. If subject cannot navigate, then return.
  372. if (cannot_navigate())
  373. return;
  374. // 2. Let replace be false.
  375. [[maybe_unused]] auto replace = false;
  376. // 3. Let targetAttributeValue be the empty string.
  377. DeprecatedString target_attribute_value;
  378. // 4. If subject is an a or area element, then set targetAttributeValue to the result of getting an element's target given subject.
  379. target_attribute_value = hyperlink_element_utils_get_an_elements_target();
  380. // 5. Let noopener be the result of getting an element's noopener with subject and targetAttributeValue.
  381. auto noopener = hyperlink_element_utils_get_an_elements_noopener(target_attribute_value);
  382. // 6. Let targetNavigable be the first return value of applying the rules for choosing a navigable given
  383. // targetAttributeValue, subject's node navigable, and noopener.
  384. auto target_navigable = hyperlink_element_utils_document().navigable()->choose_a_navigable(target_attribute_value, noopener).navigable;
  385. // 7. If targetNavigable is null, then return.
  386. if (!target_navigable)
  387. return;
  388. // 8. Parse a URL given subject's href attribute, relative to subject's node document.
  389. auto url = hyperlink_element_utils_document().parse_url(href());
  390. // 9. If that is successful, let url be the resulting URL string.
  391. // Otherwise, if parsing the URL failed, then return.
  392. if (!url.is_valid())
  393. return;
  394. // 10. If that is successful, let URL be the resulting URL string.
  395. auto url_string = url.to_deprecated_string();
  396. // 12. If hyperlinkSuffix is non-null, then append it to URL.
  397. if (hyperlink_suffix.has_value()) {
  398. StringBuilder url_builder;
  399. url_builder.append(url_string);
  400. url_builder.append(*hyperlink_suffix);
  401. url_string = url_builder.to_deprecated_string();
  402. }
  403. // FIXME: 12. If subject's link types includes the noreferrer keyword, then set referrerPolicy to "no-referrer".
  404. // 13. Navigate targetNavigable to url using subject's node document, with referrerPolicy set to referrerPolicy.
  405. MUST(target_navigable->navigate(url, hyperlink_element_utils_document()));
  406. }
  407. }