HTMLHyperlinkElementUtils.cpp 17 KB

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