HTMLHyperlinkElementUtils.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. namespace Web::HTML {
  10. HTMLHyperlinkElementUtils::~HTMLHyperlinkElementUtils() = default;
  11. // https://html.spec.whatwg.org/multipage/links.html#reinitialise-url
  12. void HTMLHyperlinkElementUtils::reinitialize_url() const
  13. {
  14. // 1. If element's url is non-null, its scheme is "blob", and its cannot-be-a-basFe-URL is true, terminate these steps.
  15. if (m_url.has_value() && m_url->scheme() == "blob"sv && m_url->cannot_be_a_base_url())
  16. return;
  17. // 2. Set the url.
  18. const_cast<HTMLHyperlinkElementUtils*>(this)->set_the_url();
  19. }
  20. // https://html.spec.whatwg.org/multipage/links.html#concept-hyperlink-url-set
  21. void HTMLHyperlinkElementUtils::set_the_url()
  22. {
  23. // 1. If this element's href content attribute is absent, set this element's url to null.
  24. auto href_content_attribute = hyperlink_element_utils_href();
  25. if (href_content_attribute.is_null()) {
  26. m_url = {};
  27. return;
  28. }
  29. // 2. Otherwise, parse this element's href content attribute value relative to this element's node document.
  30. // If parsing is successful, set this element's url to the result; otherwise, set this element's url to null.
  31. m_url = hyperlink_element_utils_document().parse_url(href_content_attribute);
  32. }
  33. // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-origin
  34. String HTMLHyperlinkElementUtils::origin() const
  35. {
  36. // 1. Reinitialize url.
  37. reinitialize_url();
  38. // 2. If this element's url is null, return the empty string.
  39. if (!m_url.has_value())
  40. return String::empty();
  41. // 3. Return the serialization of this element's url's origin.
  42. return m_url->serialize_origin();
  43. }
  44. // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-protocol
  45. String HTMLHyperlinkElementUtils::protocol() const
  46. {
  47. // 1. Reinitialize url.
  48. reinitialize_url();
  49. // 2. If this element's url is null, return ":".
  50. if (!m_url.has_value())
  51. return ":"sv;
  52. // 3. Return this element's url's scheme, followed by ":".
  53. return String::formatted("{}:", m_url->scheme());
  54. }
  55. // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-protocol
  56. void HTMLHyperlinkElementUtils::set_protocol(String protocol)
  57. {
  58. // 1. Reinitialize url.
  59. reinitialize_url();
  60. // 2. If this element's url is null, terminate these steps.
  61. if (!m_url.has_value())
  62. return;
  63. // 3. Basic URL parse the given value, followed by ":", with this element's url as url and scheme start state as state override.
  64. auto result_url = URLParser::parse(String::formatted("{}:", protocol), nullptr, m_url, URLParser::State::SchemeStart);
  65. if (result_url.is_valid())
  66. m_url = move(result_url);
  67. // 4. Update href.
  68. update_href();
  69. }
  70. // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-username
  71. String HTMLHyperlinkElementUtils::username() const
  72. {
  73. // 1. Reinitialize url.
  74. reinitialize_url();
  75. // 2. If this element's url is null, return the empty string.
  76. if (!m_url.has_value())
  77. return String::empty();
  78. // 3. Return this element's url's username.
  79. return m_url->username();
  80. }
  81. // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-username
  82. void HTMLHyperlinkElementUtils::set_username(String username)
  83. {
  84. // 1. Reinitialize url.
  85. reinitialize_url();
  86. // 2. Let url be this element's url.
  87. auto& url = m_url;
  88. // 3. If url is null or url cannot have a username/password/port, then return.
  89. if (!url.has_value() || url->cannot_have_a_username_or_password_or_port())
  90. return;
  91. // 4. Set the username given this’s URL and the given value.
  92. url->set_username(AK::URL::percent_encode(username, AK::URL::PercentEncodeSet::Userinfo));
  93. // 5. Update href.
  94. update_href();
  95. }
  96. // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-password
  97. String HTMLHyperlinkElementUtils::password() const
  98. {
  99. // 1. Reinitialize url.
  100. reinitialize_url();
  101. // 2. Let url be this element's url.
  102. auto& url = m_url;
  103. // 3. If url is null, then return the empty string.
  104. if (!url.has_value())
  105. return String::empty();
  106. // 4. Return url's password.
  107. return url->password();
  108. }
  109. // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-password
  110. void HTMLHyperlinkElementUtils::set_password(String password)
  111. {
  112. // 1. Reinitialize url.
  113. reinitialize_url();
  114. // 2. Let url be this element's url.
  115. auto& url = m_url;
  116. // 3. If url is null or url cannot have a username/password/port, then return.
  117. if (!url.has_value() || url->cannot_have_a_username_or_password_or_port())
  118. return;
  119. // 4. Set the password, given url and the given value.
  120. url->set_password(move(password));
  121. // 5. Update href.
  122. update_href();
  123. }
  124. // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-host
  125. String HTMLHyperlinkElementUtils::host() const
  126. {
  127. // 1. Reinitialize url.
  128. reinitialize_url();
  129. // 2. Let url be this element's url.
  130. auto& url = m_url;
  131. // 3. If url or url's host is null, return the empty string.
  132. if (!url.has_value() || url->host().is_null())
  133. return String::empty();
  134. // 4. If url's port is null, return url's host, serialized.
  135. if (!url->port().has_value())
  136. return url->host();
  137. // 5. Return url's host, serialized, followed by ":" and url's port, serialized.
  138. return String::formatted("{}:{}", url->host(), url->port().value());
  139. }
  140. // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-host
  141. void HTMLHyperlinkElementUtils::set_host(String host)
  142. {
  143. // 1. Reinitialize url.
  144. reinitialize_url();
  145. // 2. Let url be this element's url.
  146. auto& url = m_url;
  147. // 3. If url is null or url's cannot-be-a-base-URL is true, then return.
  148. if (!url.has_value() || url->cannot_be_a_base_url())
  149. return;
  150. // 4. Basic URL parse the given value, with url as url and host state as state override.
  151. auto result_url = URLParser::parse(host, nullptr, url, URLParser::State::Host);
  152. if (result_url.is_valid())
  153. m_url = move(result_url);
  154. // 5. Update href.
  155. update_href();
  156. }
  157. String HTMLHyperlinkElementUtils::hostname() const
  158. {
  159. // 1. Reinitialize url.
  160. //
  161. // 2. Let url be this element's url.
  162. //
  163. // 3. If url or url's host is null, return the empty string.
  164. //
  165. // 4. Return url's host, serialized.
  166. return AK::URL(href()).host();
  167. }
  168. void HTMLHyperlinkElementUtils::set_hostname(String hostname)
  169. {
  170. // 1. Reinitialize url.
  171. reinitialize_url();
  172. // 2. Let url be this element's url.
  173. auto& url = m_url;
  174. // 3. If url is null or url's cannot-be-a-base-URL is true, then return.
  175. if (!url.has_value() || url->cannot_be_a_base_url())
  176. return;
  177. // 4. Basic URL parse the given value, with url as url and hostname state as state override.
  178. auto result_url = URLParser::parse(hostname, nullptr, m_url, URLParser::State::Hostname);
  179. if (result_url.is_valid())
  180. m_url = move(result_url);
  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::empty();
  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(String 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. } else {
  210. // 5. Otherwise, basic URL parse the given value, with url as url and port state as state override.
  211. auto result_url = URLParser::parse(port, nullptr, m_url, URLParser::State::Port);
  212. if (result_url.is_valid())
  213. m_url = move(result_url);
  214. }
  215. // 6. Update href.
  216. update_href();
  217. }
  218. // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-pathname
  219. String HTMLHyperlinkElementUtils::pathname() const
  220. {
  221. // 1. Reinitialize url.
  222. reinitialize_url();
  223. // 2. Let url be this element's url.
  224. // 3. If url is null, return the empty string.
  225. if (!m_url.has_value())
  226. return String::empty();
  227. // 4. If url's cannot-be-a-base-URL is true, then return url's path[0].
  228. // 5. If url's path is empty, then return the empty string.
  229. // 6. Return "/", followed by the strings in url's path (including empty strings), separated from each other by "/".
  230. return m_url->path();
  231. }
  232. // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-pathname
  233. void HTMLHyperlinkElementUtils::set_pathname(String pathname)
  234. {
  235. // 1. Reinitialize url.
  236. reinitialize_url();
  237. // 2. Let url be this element's url.
  238. // 3. If url is null or url's cannot-be-a-base-URL is true, then return.
  239. if (!m_url.has_value() || m_url->cannot_be_a_base_url())
  240. return;
  241. // 4. Set url's path to the empty list.
  242. auto url = m_url; // We copy the URL here to follow other browser's behaviour of reverting the path change if the parse failed.
  243. url->set_paths({});
  244. // 5. Basic URL parse the given value, with url as url and path start state as state override.
  245. auto result_url = URLParser::parse(pathname, nullptr, move(url), URLParser::State::PathStart);
  246. if (result_url.is_valid())
  247. m_url = move(result_url);
  248. // 6. Update href.
  249. update_href();
  250. }
  251. String HTMLHyperlinkElementUtils::search() const
  252. {
  253. // 1. Reinitialize url.
  254. reinitialize_url();
  255. // 2. Let url be this element's url.
  256. // 3. If url is null, or url's query is either null or the empty string, return the empty string.
  257. if (!m_url.has_value() || m_url->query().is_null() || m_url->query().is_empty())
  258. return String::empty();
  259. // 4. Return "?", followed by url's query.
  260. return String::formatted("?{}", m_url->query());
  261. }
  262. void HTMLHyperlinkElementUtils::set_search(String search)
  263. {
  264. // 1. Reinitialize url.
  265. reinitialize_url();
  266. // 2. Let url be this element's url.
  267. // 3. If url is null, terminate these steps.
  268. if (!m_url.has_value())
  269. return;
  270. // 4. If the given value is the empty string, set url's query to null.
  271. if (search.is_empty()) {
  272. m_url->set_query({});
  273. } else {
  274. // 5. Otherwise:
  275. // 1. Let input be the given value with a single leading "?" removed, if any.
  276. auto input = search.substring_view(search.starts_with('?'));
  277. // 2. Set url's query to the empty string.
  278. auto url_copy = m_url; // We copy the URL here to follow other browser's behaviour of reverting the search change if the parse failed.
  279. url_copy->set_query(String::empty());
  280. // 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.
  281. auto result_url = URLParser::parse(input, nullptr, move(url_copy), URLParser::State::Query);
  282. if (result_url.is_valid())
  283. m_url = move(result_url);
  284. }
  285. // 6. Update href.
  286. update_href();
  287. }
  288. String HTMLHyperlinkElementUtils::hash() const
  289. {
  290. // 1. Reinitialize url.
  291. reinitialize_url();
  292. // 2. Let url be this element's url.
  293. // 3. If url is null, or url's fragment is either null or the empty string, return the empty string.
  294. if (!m_url.has_value() || m_url->fragment().is_null() || m_url->fragment().is_empty())
  295. return String::empty();
  296. // 4. Return "#", followed by url's fragment.
  297. return String::formatted("#{}", m_url->fragment());
  298. }
  299. void HTMLHyperlinkElementUtils::set_hash(String hash)
  300. {
  301. // 1. Reinitialize url.
  302. reinitialize_url();
  303. // 2. Let url be this element's url.
  304. // 3. If url is null, then return.
  305. if (!m_url.has_value())
  306. return;
  307. // 4. If the given value is the empty string, set url's fragment to null.
  308. if (hash.is_empty()) {
  309. m_url->set_fragment({});
  310. } else {
  311. // 5. Otherwise:
  312. // 1. Let input be the given value with a single leading "#" removed, if any.
  313. auto input = hash.substring_view(hash.starts_with('#'));
  314. // 2. Set url's fragment to the empty string.
  315. auto url_copy = m_url; // We copy the URL here to follow other browser's behaviour of reverting the hash change if the parse failed.
  316. url_copy->set_fragment(String::empty());
  317. // 3. Basic URL parse input, with url as url and fragment state as state override.
  318. auto result_url = URLParser::parse(input, nullptr, move(url_copy), URLParser::State::Fragment);
  319. if (result_url.is_valid())
  320. m_url = move(result_url);
  321. }
  322. // 6. Update href.
  323. update_href();
  324. }
  325. // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-href
  326. String HTMLHyperlinkElementUtils::href() const
  327. {
  328. // 1. Reinitialize url.
  329. reinitialize_url();
  330. // 2. Let url be this element's url.
  331. auto& url = m_url;
  332. // 3. If url is null and this element has no href content attribute, return the empty string.
  333. auto href_content_attribute = hyperlink_element_utils_href();
  334. if (!url.has_value() && href_content_attribute.is_null())
  335. return String::empty();
  336. // 4. Otherwise, if url is null, return this element's href content attribute's value.
  337. if (!url->is_valid())
  338. return href_content_attribute;
  339. // 5. Return url, serialized.
  340. return url->serialize();
  341. }
  342. // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-href
  343. void HTMLHyperlinkElementUtils::set_href(String href)
  344. {
  345. // The href attribute's setter must set this element's href content attribute's value to the given value.
  346. set_hyperlink_element_utils_href(move(href));
  347. }
  348. // https://html.spec.whatwg.org/multipage/links.html#update-href
  349. void HTMLHyperlinkElementUtils::update_href()
  350. {
  351. // To update href, set the element's href content attribute's value to the element's url, serialized.
  352. }
  353. }