HTMLHyperlinkElementUtils.cpp 17 KB

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