HTMLHyperlinkElementUtils.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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/Loader/FrameLoader.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.is_null()) {
  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::empty();
  42. // 3. Return the serialization of this element's url's origin.
  43. return m_url->serialize_origin();
  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 ":"sv;
  53. // 3. Return this element's url's scheme, followed by ":".
  54. return String::formatted("{}:", m_url->scheme());
  55. }
  56. // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-protocol
  57. void HTMLHyperlinkElementUtils::set_protocol(String 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. auto result_url = URLParser::parse(String::formatted("{}:", protocol), nullptr, m_url, URLParser::State::SchemeStart);
  66. if (result_url.is_valid())
  67. m_url = move(result_url);
  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::empty();
  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(String 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(AK::URL::percent_encode(username, AK::URL::PercentEncodeSet::Userinfo));
  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::empty();
  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(String 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(move(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& url = m_url;
  132. // 3. If url or url's host is null, return the empty string.
  133. if (!url.has_value() || url->host().is_null())
  134. return String::empty();
  135. // 4. If url's port is null, return url's host, serialized.
  136. if (!url->port().has_value())
  137. return url->host();
  138. // 5. Return url's host, serialized, followed by ":" and url's port, serialized.
  139. return String::formatted("{}:{}", url->host(), url->port().value());
  140. }
  141. // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-host
  142. void HTMLHyperlinkElementUtils::set_host(String 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. auto result_url = URLParser::parse(host, nullptr, url, URLParser::State::Host);
  153. if (result_url.is_valid())
  154. m_url = move(result_url);
  155. // 5. Update href.
  156. update_href();
  157. }
  158. String HTMLHyperlinkElementUtils::hostname() const
  159. {
  160. // 1. Reinitialize url.
  161. //
  162. // 2. Let url be this element's url.
  163. //
  164. // 3. If url or url's host is null, return the empty string.
  165. //
  166. // 4. Return url's host, serialized.
  167. return AK::URL(href()).host();
  168. }
  169. void HTMLHyperlinkElementUtils::set_hostname(String hostname)
  170. {
  171. // 1. Reinitialize url.
  172. reinitialize_url();
  173. // 2. Let url be this element's url.
  174. auto& url = m_url;
  175. // 3. If url is null or url's cannot-be-a-base-URL is true, then return.
  176. if (!url.has_value() || url->cannot_be_a_base_url())
  177. return;
  178. // 4. Basic URL parse the given value, with url as url and hostname state as state override.
  179. auto result_url = URLParser::parse(hostname, nullptr, m_url, URLParser::State::Hostname);
  180. if (result_url.is_valid())
  181. m_url = move(result_url);
  182. // 5. Update href.
  183. update_href();
  184. }
  185. // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-port
  186. String HTMLHyperlinkElementUtils::port() const
  187. {
  188. // 1. Reinitialize url.
  189. reinitialize_url();
  190. // 2. Let url be this element's url.
  191. auto& url = m_url;
  192. // 3. If url or url's port is null, return the empty string.
  193. if (!url.has_value() || !url->port().has_value())
  194. return String::empty();
  195. // 4. Return url's port, serialized.
  196. return String::number(url->port().value());
  197. }
  198. // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-port
  199. void HTMLHyperlinkElementUtils::set_port(String port)
  200. {
  201. // 1. Reinitialize url.
  202. reinitialize_url();
  203. // 2. Let url be this element's url.
  204. // 3. If url is null or url cannot have a username/password/port, then return.
  205. if (!m_url.has_value() || m_url->cannot_have_a_username_or_password_or_port())
  206. return;
  207. // 4. If the given value is the empty string, then set url's port to null.
  208. if (port.is_empty()) {
  209. m_url->set_port({});
  210. } else {
  211. // 5. Otherwise, basic URL parse the given value, with url as url and port state as state override.
  212. auto result_url = URLParser::parse(port, nullptr, m_url, URLParser::State::Port);
  213. if (result_url.is_valid())
  214. m_url = move(result_url);
  215. }
  216. // 6. Update href.
  217. update_href();
  218. }
  219. // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-pathname
  220. String HTMLHyperlinkElementUtils::pathname() const
  221. {
  222. // 1. Reinitialize url.
  223. reinitialize_url();
  224. // 2. Let url be this element's url.
  225. // 3. If url is null, return the empty string.
  226. if (!m_url.has_value())
  227. return String::empty();
  228. // 4. If url's cannot-be-a-base-URL is true, then return url's path[0].
  229. // 5. If url's path is empty, then return the empty string.
  230. // 6. Return "/", followed by the strings in url's path (including empty strings), separated from each other by "/".
  231. return m_url->path();
  232. }
  233. // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-pathname
  234. void HTMLHyperlinkElementUtils::set_pathname(String 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. auto url = m_url; // We copy the URL here to follow other browser's behaviour of reverting the path change if the parse failed.
  244. url->set_paths({});
  245. // 5. Basic URL parse the given value, with url as url and path start state as state override.
  246. auto result_url = URLParser::parse(pathname, nullptr, move(url), URLParser::State::PathStart);
  247. if (result_url.is_valid())
  248. m_url = move(result_url);
  249. // 6. Update href.
  250. update_href();
  251. }
  252. String HTMLHyperlinkElementUtils::search() const
  253. {
  254. // 1. Reinitialize url.
  255. reinitialize_url();
  256. // 2. Let url be this element's url.
  257. // 3. If url is null, or url's query is either null or the empty string, return the empty string.
  258. if (!m_url.has_value() || m_url->query().is_null() || m_url->query().is_empty())
  259. return String::empty();
  260. // 4. Return "?", followed by url's query.
  261. return String::formatted("?{}", m_url->query());
  262. }
  263. void HTMLHyperlinkElementUtils::set_search(String search)
  264. {
  265. // 1. Reinitialize url.
  266. reinitialize_url();
  267. // 2. Let url be this element's url.
  268. // 3. If url is null, terminate these steps.
  269. if (!m_url.has_value())
  270. return;
  271. // 4. If the given value is the empty string, set url's query to null.
  272. if (search.is_empty()) {
  273. m_url->set_query({});
  274. } else {
  275. // 5. Otherwise:
  276. // 1. Let input be the given value with a single leading "?" removed, if any.
  277. auto input = search.substring_view(search.starts_with('?'));
  278. // 2. Set url's query to the empty string.
  279. 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.
  280. url_copy->set_query(String::empty());
  281. // 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.
  282. auto result_url = URLParser::parse(input, nullptr, move(url_copy), URLParser::State::Query);
  283. if (result_url.is_valid())
  284. m_url = move(result_url);
  285. }
  286. // 6. Update href.
  287. update_href();
  288. }
  289. String HTMLHyperlinkElementUtils::hash() const
  290. {
  291. // 1. Reinitialize url.
  292. reinitialize_url();
  293. // 2. Let url be this element's url.
  294. // 3. If url is null, or url's fragment is either null or the empty string, return the empty string.
  295. if (!m_url.has_value() || m_url->fragment().is_null() || m_url->fragment().is_empty())
  296. return String::empty();
  297. // 4. Return "#", followed by url's fragment.
  298. return String::formatted("#{}", m_url->fragment());
  299. }
  300. void HTMLHyperlinkElementUtils::set_hash(String hash)
  301. {
  302. // 1. Reinitialize url.
  303. reinitialize_url();
  304. // 2. Let url be this element's url.
  305. // 3. If url is null, then return.
  306. if (!m_url.has_value())
  307. return;
  308. // 4. If the given value is the empty string, set url's fragment to null.
  309. if (hash.is_empty()) {
  310. m_url->set_fragment({});
  311. } else {
  312. // 5. Otherwise:
  313. // 1. Let input be the given value with a single leading "#" removed, if any.
  314. auto input = hash.substring_view(hash.starts_with('#'));
  315. // 2. Set url's fragment to the empty string.
  316. 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.
  317. url_copy->set_fragment(String::empty());
  318. // 3. Basic URL parse input, with url as url and fragment state as state override.
  319. auto result_url = URLParser::parse(input, nullptr, move(url_copy), URLParser::State::Fragment);
  320. if (result_url.is_valid())
  321. m_url = move(result_url);
  322. }
  323. // 6. Update href.
  324. update_href();
  325. }
  326. // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-href
  327. String HTMLHyperlinkElementUtils::href() const
  328. {
  329. // 1. Reinitialize url.
  330. reinitialize_url();
  331. // 2. Let url be this element's url.
  332. auto& url = m_url;
  333. // 3. If url is null and this element has no href content attribute, return the empty string.
  334. auto href_content_attribute = hyperlink_element_utils_href();
  335. if (!url.has_value() && href_content_attribute.is_null())
  336. return String::empty();
  337. // 4. Otherwise, if url is null, return this element's href content attribute's value.
  338. if (!url->is_valid())
  339. return href_content_attribute;
  340. // 5. Return url, serialized.
  341. return url->serialize();
  342. }
  343. // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-href
  344. void HTMLHyperlinkElementUtils::set_href(String href)
  345. {
  346. // The href attribute's setter must set this element's href content attribute's value to the given value.
  347. set_hyperlink_element_utils_href(move(href));
  348. }
  349. // https://html.spec.whatwg.org/multipage/links.html#update-href
  350. void HTMLHyperlinkElementUtils::update_href()
  351. {
  352. // To update href, set the element's href content attribute's value to the element's url, serialized.
  353. }
  354. bool HTMLHyperlinkElementUtils::cannot_navigate() const
  355. {
  356. // An element element cannot navigate if one of the following is true:
  357. // 1. element's node document is not fully active
  358. auto const& document = const_cast<HTMLHyperlinkElementUtils*>(this)->hyperlink_element_utils_document();
  359. if (!document.is_fully_active())
  360. return true;
  361. // 2. element is not an a element and is not connected.
  362. if (!hyperlink_element_utils_is_html_anchor_element() && !hyperlink_element_utils_is_connected())
  363. return true;
  364. return false;
  365. }
  366. // https://html.spec.whatwg.org/multipage/links.html#following-hyperlinks-2
  367. void HTMLHyperlinkElementUtils::follow_the_hyperlink(Optional<String> hyperlink_suffix)
  368. {
  369. // To follow the hyperlink created by an element subject, given an optional hyperlinkSuffix (default null):
  370. // 1. If subject cannot navigate, then return.
  371. if (cannot_navigate())
  372. return;
  373. // FIXME: 2. Let replace be false.
  374. // 3. Let source be subject's node document's browsing context.
  375. auto* source = hyperlink_element_utils_document().browsing_context();
  376. if (!source)
  377. return;
  378. // 4. Let targetAttributeValue be the empty string.
  379. // 5. If subject is an a or area element, then set targetAttributeValue to
  380. // the result of getting an element's target given subject.
  381. String target_attribute_value = get_an_elements_target();
  382. // 6. Let noopener be the result of getting an element's noopener with subject and targetAttributeValue.
  383. bool noopener = get_an_elements_noopener(target_attribute_value);
  384. // 7. Let target be the first return value of applying the rules for
  385. // choosing a browsing context given targetAttributeValue, source, and
  386. // noopener.
  387. auto* target = source->choose_a_browsing_context(target_attribute_value, noopener);
  388. // 8. If target is null, then return.
  389. if (!target)
  390. return;
  391. // 9. Parse a URL given subject's href attribute, relative to subject's node
  392. // document.
  393. auto url = source->active_document()->parse_url(href());
  394. // 10. If that is successful, let URL be the resulting URL string.
  395. auto url_string = url.to_string();
  396. // 11. Otherwise, if parsing the URL failed, the user agent may report the
  397. // error to the user in a user-agent-specific manner, may queue an element
  398. // task on the DOM manipulation task source given subject to navigate the
  399. // target browsing context to an error page to report the error, or may
  400. // ignore the error and do nothing. In any case, the user agent must then
  401. // return.
  402. // 12. If hyperlinkSuffix is non-null, then append it to URL.
  403. if (hyperlink_suffix.has_value()) {
  404. StringBuilder url_builder;
  405. url_builder.append(url_string);
  406. url_builder.append(*hyperlink_suffix);
  407. url_string = url_builder.to_string();
  408. }
  409. // FIXME: 13. Let request be a new request whose URL is URL and whose
  410. // referrer policy is the current state of subject's referrerpolicy content
  411. // attribute.
  412. // FIXME: 14. If subject's link types includes the noreferrer keyword, then
  413. // set request's referrer to "no-referrer".
  414. // 15. Queue an element task on the DOM manipulation task source given
  415. // subject to navigate target to request with the source browsing context
  416. // set to source.
  417. // FIXME: "navigate" means implementing the navigation algorithm here:
  418. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#navigate
  419. hyperlink_element_utils_queue_an_element_task(Task::Source::DOMManipulation, [url_string, target] {
  420. target->loader().load(url_string, FrameLoader::Type::Navigation);
  421. });
  422. }
  423. String HTMLHyperlinkElementUtils::get_an_elements_target() const
  424. {
  425. // To get an element's target, given an a, area, or form element element, run these steps:
  426. // 1. If element has a target attribute, then return that attribute's value.
  427. if (auto target = hyperlink_element_utils_target(); !target.is_empty())
  428. return target;
  429. // FIXME: 2. If element's node document contains a base element with a
  430. // target attribute, then return the value of the target attribute of the
  431. // first such base element.
  432. // 3. Return the empty string.
  433. return "";
  434. }
  435. bool HTMLHyperlinkElementUtils::get_an_elements_noopener(StringView target) const
  436. {
  437. // To get an element's noopener, given an a, area, or form element element and a string target:
  438. // FIXME: 1. If element's link types include the noopener or noreferrer
  439. // keyword, then return true.
  440. // FIXME: 2. If element's link types do not include the opener keyword and
  441. // target is an ASCII case-insensitive match for "_blank", then return true.
  442. if (target.equals_ignoring_case("_blank"sv))
  443. return true;
  444. // 3. Return false.
  445. return false;
  446. }
  447. }