HTMLHyperlinkElementUtils.cpp 17 KB

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