HTMLHyperlinkElementUtils.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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/Infra/CharacterTypes.h>
  10. #include <LibWeb/Infra/Strings.h>
  11. #include <LibWeb/Loader/FrameLoader.h>
  12. namespace Web::HTML {
  13. HTMLHyperlinkElementUtils::~HTMLHyperlinkElementUtils() = default;
  14. // https://html.spec.whatwg.org/multipage/links.html#reinitialise-url
  15. void HTMLHyperlinkElementUtils::reinitialize_url() const
  16. {
  17. // 1. If element's url is non-null, its scheme is "blob", and its cannot-be-a-basFe-URL is true, terminate these steps.
  18. if (m_url.has_value() && m_url->scheme() == "blob"sv && m_url->cannot_be_a_base_url())
  19. return;
  20. // 2. Set the url.
  21. const_cast<HTMLHyperlinkElementUtils*>(this)->set_the_url();
  22. }
  23. // https://html.spec.whatwg.org/multipage/links.html#concept-hyperlink-url-set
  24. void HTMLHyperlinkElementUtils::set_the_url()
  25. {
  26. // 1. If this element's href content attribute is absent, set this element's url to null.
  27. auto href_content_attribute = hyperlink_element_utils_href();
  28. if (href_content_attribute.is_null()) {
  29. m_url = {};
  30. return;
  31. }
  32. // 2. Otherwise, parse this element's href content attribute value relative to this element's node document.
  33. // If parsing is successful, set this element's url to the result; otherwise, set this element's url to null.
  34. m_url = hyperlink_element_utils_document().parse_url(href_content_attribute);
  35. }
  36. // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-origin
  37. DeprecatedString 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 DeprecatedString::empty();
  44. // 3. Return the serialization of this element's url's origin.
  45. return m_url->serialize_origin();
  46. }
  47. // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-protocol
  48. DeprecatedString 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 ":"sv;
  55. // 3. Return this element's url's scheme, followed by ":".
  56. return DeprecatedString::formatted("{}:", m_url->scheme());
  57. }
  58. // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-protocol
  59. void HTMLHyperlinkElementUtils::set_protocol(DeprecatedString 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. auto result_url = URLParser::parse(DeprecatedString::formatted("{}:", protocol), {}, m_url, URLParser::State::SchemeStart);
  68. if (result_url.is_valid())
  69. m_url = move(result_url);
  70. // 4. Update href.
  71. update_href();
  72. }
  73. // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-username
  74. DeprecatedString HTMLHyperlinkElementUtils::username() const
  75. {
  76. // 1. Reinitialize url.
  77. reinitialize_url();
  78. // 2. If this element's url is null, return the empty string.
  79. if (!m_url.has_value())
  80. return DeprecatedString::empty();
  81. // 3. Return this element's url's username.
  82. return m_url->username();
  83. }
  84. // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-username
  85. void HTMLHyperlinkElementUtils::set_username(DeprecatedString username)
  86. {
  87. // 1. Reinitialize url.
  88. reinitialize_url();
  89. // 2. Let url be this element's url.
  90. auto& url = m_url;
  91. // 3. If url is null or url cannot have a username/password/port, then return.
  92. if (!url.has_value() || url->cannot_have_a_username_or_password_or_port())
  93. return;
  94. // 4. Set the username given this’s URL and the given value.
  95. url->set_username(AK::URL::percent_encode(username, AK::URL::PercentEncodeSet::Userinfo));
  96. // 5. Update href.
  97. update_href();
  98. }
  99. // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-password
  100. DeprecatedString HTMLHyperlinkElementUtils::password() const
  101. {
  102. // 1. Reinitialize url.
  103. reinitialize_url();
  104. // 2. Let url be this element's url.
  105. auto& url = m_url;
  106. // 3. If url is null, then return the empty string.
  107. if (!url.has_value())
  108. return DeprecatedString::empty();
  109. // 4. Return url's password.
  110. return url->password();
  111. }
  112. // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-password
  113. void HTMLHyperlinkElementUtils::set_password(DeprecatedString password)
  114. {
  115. // 1. Reinitialize url.
  116. reinitialize_url();
  117. // 2. Let url be this element's url.
  118. auto& url = m_url;
  119. // 3. If url is null or url cannot have a username/password/port, then return.
  120. if (!url.has_value() || url->cannot_have_a_username_or_password_or_port())
  121. return;
  122. // 4. Set the password, given url and the given value.
  123. url->set_password(move(password));
  124. // 5. Update href.
  125. update_href();
  126. }
  127. // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-host
  128. DeprecatedString HTMLHyperlinkElementUtils::host() const
  129. {
  130. // 1. Reinitialize url.
  131. reinitialize_url();
  132. // 2. Let url be this element's url.
  133. auto& url = m_url;
  134. // 3. If url or url's host is null, return the empty string.
  135. if (!url.has_value() || url->host().is_null())
  136. return DeprecatedString::empty();
  137. // 4. If url's port is null, return url's host, serialized.
  138. if (!url->port().has_value())
  139. return url->host();
  140. // 5. Return url's host, serialized, followed by ":" and url's port, serialized.
  141. return DeprecatedString::formatted("{}:{}", url->host(), url->port().value());
  142. }
  143. // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-host
  144. void HTMLHyperlinkElementUtils::set_host(DeprecatedString host)
  145. {
  146. // 1. Reinitialize url.
  147. reinitialize_url();
  148. // 2. Let url be this element's url.
  149. auto& url = m_url;
  150. // 3. If url is null or url's cannot-be-a-base-URL is true, then return.
  151. if (!url.has_value() || url->cannot_be_a_base_url())
  152. return;
  153. // 4. Basic URL parse the given value, with url as url and host state as state override.
  154. auto result_url = URLParser::parse(host, {}, url, URLParser::State::Host);
  155. if (result_url.is_valid())
  156. m_url = move(result_url);
  157. // 5. Update href.
  158. update_href();
  159. }
  160. DeprecatedString HTMLHyperlinkElementUtils::hostname() const
  161. {
  162. // 1. Reinitialize url.
  163. //
  164. // 2. Let url be this element's url.
  165. //
  166. // 3. If url or url's host is null, return the empty string.
  167. //
  168. // 4. Return url's host, serialized.
  169. return AK::URL(href()).host();
  170. }
  171. void HTMLHyperlinkElementUtils::set_hostname(DeprecatedString hostname)
  172. {
  173. // 1. Reinitialize url.
  174. reinitialize_url();
  175. // 2. Let url be this element's url.
  176. auto& url = m_url;
  177. // 3. If url is null or url's cannot-be-a-base-URL is true, then return.
  178. if (!url.has_value() || url->cannot_be_a_base_url())
  179. return;
  180. // 4. Basic URL parse the given value, with url as url and hostname state as state override.
  181. auto result_url = URLParser::parse(hostname, {}, m_url, URLParser::State::Hostname);
  182. if (result_url.is_valid())
  183. m_url = move(result_url);
  184. // 5. Update href.
  185. update_href();
  186. }
  187. // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-port
  188. DeprecatedString HTMLHyperlinkElementUtils::port() const
  189. {
  190. // 1. Reinitialize url.
  191. reinitialize_url();
  192. // 2. Let url be this element's url.
  193. auto& url = m_url;
  194. // 3. If url or url's port is null, return the empty string.
  195. if (!url.has_value() || !url->port().has_value())
  196. return DeprecatedString::empty();
  197. // 4. Return url's port, serialized.
  198. return DeprecatedString::number(url->port().value());
  199. }
  200. // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-port
  201. void HTMLHyperlinkElementUtils::set_port(DeprecatedString port)
  202. {
  203. // 1. Reinitialize url.
  204. reinitialize_url();
  205. // 2. Let url be this element's url.
  206. // 3. If url is null or url cannot have a username/password/port, then return.
  207. if (!m_url.has_value() || m_url->cannot_have_a_username_or_password_or_port())
  208. return;
  209. // 4. If the given value is the empty string, then set url's port to null.
  210. if (port.is_empty()) {
  211. m_url->set_port({});
  212. } else {
  213. // 5. Otherwise, basic URL parse the given value, with url as url and port state as state override.
  214. auto result_url = URLParser::parse(port, {}, m_url, URLParser::State::Port);
  215. if (result_url.is_valid())
  216. m_url = move(result_url);
  217. }
  218. // 6. Update href.
  219. update_href();
  220. }
  221. // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-pathname
  222. DeprecatedString 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 DeprecatedString::empty();
  230. // 4. If url's cannot-be-a-base-URL is true, then return url's path[0].
  231. // 5. If url's path is empty, then return the empty string.
  232. // 6. Return "/", followed by the strings in url's path (including empty strings), separated from each other by "/".
  233. return m_url->path();
  234. }
  235. // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-pathname
  236. void HTMLHyperlinkElementUtils::set_pathname(DeprecatedString pathname)
  237. {
  238. // 1. Reinitialize url.
  239. reinitialize_url();
  240. // 2. Let url be this element's url.
  241. // 3. If url is null or url's cannot-be-a-base-URL is true, then return.
  242. if (!m_url.has_value() || m_url->cannot_be_a_base_url())
  243. return;
  244. // 4. Set url's path to the empty list.
  245. auto url = m_url; // We copy the URL here to follow other browser's behaviour of reverting the path change if the parse failed.
  246. url->set_paths({});
  247. // 5. Basic URL parse the given value, with url as url and path start state as state override.
  248. auto result_url = URLParser::parse(pathname, {}, move(url), URLParser::State::PathStart);
  249. if (result_url.is_valid())
  250. m_url = move(result_url);
  251. // 6. Update href.
  252. update_href();
  253. }
  254. DeprecatedString HTMLHyperlinkElementUtils::search() const
  255. {
  256. // 1. Reinitialize url.
  257. reinitialize_url();
  258. // 2. Let url be this element's url.
  259. // 3. If url is null, or url's query is either null or the empty string, return the empty string.
  260. if (!m_url.has_value() || m_url->query().is_null() || m_url->query().is_empty())
  261. return DeprecatedString::empty();
  262. // 4. Return "?", followed by url's query.
  263. return DeprecatedString::formatted("?{}", m_url->query());
  264. }
  265. void HTMLHyperlinkElementUtils::set_search(DeprecatedString search)
  266. {
  267. // 1. Reinitialize url.
  268. reinitialize_url();
  269. // 2. Let url be this element's url.
  270. // 3. If url is null, terminate these steps.
  271. if (!m_url.has_value())
  272. return;
  273. // 4. If the given value is the empty string, set url's query to null.
  274. if (search.is_empty()) {
  275. m_url->set_query({});
  276. } else {
  277. // 5. Otherwise:
  278. // 1. Let input be the given value with a single leading "?" removed, if any.
  279. auto input = search.substring_view(search.starts_with('?'));
  280. // 2. Set url's query to the empty string.
  281. 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.
  282. url_copy->set_query(DeprecatedString::empty());
  283. // 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.
  284. auto result_url = URLParser::parse(input, {}, move(url_copy), URLParser::State::Query);
  285. if (result_url.is_valid())
  286. m_url = move(result_url);
  287. }
  288. // 6. Update href.
  289. update_href();
  290. }
  291. DeprecatedString HTMLHyperlinkElementUtils::hash() const
  292. {
  293. // 1. Reinitialize url.
  294. reinitialize_url();
  295. // 2. Let url be this element's url.
  296. // 3. If url is null, or url's fragment is either null or the empty string, return the empty string.
  297. if (!m_url.has_value() || m_url->fragment().is_null() || m_url->fragment().is_empty())
  298. return DeprecatedString::empty();
  299. // 4. Return "#", followed by url's fragment.
  300. return DeprecatedString::formatted("#{}", m_url->fragment());
  301. }
  302. void HTMLHyperlinkElementUtils::set_hash(DeprecatedString hash)
  303. {
  304. // 1. Reinitialize url.
  305. reinitialize_url();
  306. // 2. Let url be this element's url.
  307. // 3. If url is null, then return.
  308. if (!m_url.has_value())
  309. return;
  310. // 4. If the given value is the empty string, set url's fragment to null.
  311. if (hash.is_empty()) {
  312. m_url->set_fragment({});
  313. } else {
  314. // 5. Otherwise:
  315. // 1. Let input be the given value with a single leading "#" removed, if any.
  316. auto input = hash.substring_view(hash.starts_with('#'));
  317. // 2. Set url's fragment to the empty string.
  318. 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.
  319. url_copy->set_fragment(DeprecatedString::empty());
  320. // 3. Basic URL parse input, with url as url and fragment state as state override.
  321. auto result_url = URLParser::parse(input, {}, move(url_copy), URLParser::State::Fragment);
  322. if (result_url.is_valid())
  323. m_url = move(result_url);
  324. }
  325. // 6. Update href.
  326. update_href();
  327. }
  328. // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-href
  329. DeprecatedString HTMLHyperlinkElementUtils::href() const
  330. {
  331. // 1. Reinitialize url.
  332. reinitialize_url();
  333. // 2. Let url be this element's url.
  334. auto& url = m_url;
  335. // 3. If url is null and this element has no href content attribute, return the empty string.
  336. auto href_content_attribute = hyperlink_element_utils_href();
  337. if (!url.has_value() && href_content_attribute.is_null())
  338. return DeprecatedString::empty();
  339. // 4. Otherwise, if url is null, return this element's href content attribute's value.
  340. if (!url->is_valid())
  341. return href_content_attribute;
  342. // 5. Return url, serialized.
  343. return url->serialize();
  344. }
  345. // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-href
  346. void HTMLHyperlinkElementUtils::set_href(DeprecatedString href)
  347. {
  348. // The href attribute's setter must set this element's href content attribute's value to the given value.
  349. set_hyperlink_element_utils_href(move(href));
  350. }
  351. // https://html.spec.whatwg.org/multipage/links.html#update-href
  352. void HTMLHyperlinkElementUtils::update_href()
  353. {
  354. // To update href, set the element's href content attribute's value to the element's url, serialized.
  355. }
  356. bool HTMLHyperlinkElementUtils::cannot_navigate() const
  357. {
  358. // An element element cannot navigate if one of the following is true:
  359. // 1. element's node document is not fully active
  360. auto const& document = const_cast<HTMLHyperlinkElementUtils*>(this)->hyperlink_element_utils_document();
  361. if (!document.is_fully_active())
  362. return true;
  363. // 2. element is not an a element and is not connected.
  364. if (!hyperlink_element_utils_is_html_anchor_element() && !hyperlink_element_utils_is_connected())
  365. return true;
  366. return false;
  367. }
  368. // https://html.spec.whatwg.org/multipage/links.html#following-hyperlinks-2
  369. void HTMLHyperlinkElementUtils::follow_the_hyperlink(Optional<DeprecatedString> hyperlink_suffix)
  370. {
  371. // To follow the hyperlink created by an element subject, given an optional hyperlinkSuffix (default null):
  372. // 1. If subject cannot navigate, then return.
  373. if (cannot_navigate())
  374. return;
  375. // FIXME: 2. Let replace be false.
  376. // 3. Let source be subject's node document's browsing context.
  377. auto* source = hyperlink_element_utils_document().browsing_context();
  378. if (!source)
  379. return;
  380. // 4. Let targetAttributeValue be the empty string.
  381. // 5. If subject is an a or area element, then set targetAttributeValue to
  382. // the result of getting an element's target given subject.
  383. DeprecatedString target_attribute_value = get_an_elements_target();
  384. // 6. Let noopener be the result of getting an element's noopener with subject and targetAttributeValue.
  385. auto noopener = get_an_elements_noopener(target_attribute_value);
  386. // 7. Let target be the first return value of applying the rules for
  387. // choosing a browsing context given targetAttributeValue, source, and
  388. // noopener.
  389. auto target = source->choose_a_browsing_context(target_attribute_value, noopener).browsing_context;
  390. // 8. If target is null, then return.
  391. if (!target)
  392. return;
  393. // 9. Parse a URL given subject's href attribute, relative to subject's node
  394. // document.
  395. auto url = source->active_document()->parse_url(href());
  396. // 10. If that is successful, let URL be the resulting URL string.
  397. auto url_string = url.to_deprecated_string();
  398. // 11. Otherwise, if parsing the URL failed, the user agent may report the
  399. // error to the user in a user-agent-specific manner, may queue an element
  400. // task on the DOM manipulation task source given subject to navigate the
  401. // target browsing context to an error page to report the error, or may
  402. // ignore the error and do nothing. In any case, the user agent must then
  403. // return.
  404. // 12. If hyperlinkSuffix is non-null, then append it to URL.
  405. if (hyperlink_suffix.has_value()) {
  406. StringBuilder url_builder;
  407. url_builder.append(url_string);
  408. url_builder.append(*hyperlink_suffix);
  409. url_string = url_builder.to_deprecated_string();
  410. }
  411. // FIXME: 13. Let request be a new request whose URL is URL and whose
  412. // referrer policy is the current state of subject's referrerpolicy content
  413. // attribute.
  414. // FIXME: 14. If subject's link types includes the noreferrer keyword, then
  415. // set request's referrer to "no-referrer".
  416. // 15. Queue an element task on the DOM manipulation task source given
  417. // subject to navigate target to request with the source browsing context
  418. // set to source.
  419. // FIXME: "navigate" means implementing the navigation algorithm here:
  420. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#navigate
  421. hyperlink_element_utils_queue_an_element_task(Task::Source::DOMManipulation, [url_string, target] {
  422. verify_cast<BrowsingContext>(*target).loader().load(url_string, FrameLoader::Type::Navigation);
  423. });
  424. }
  425. DeprecatedString HTMLHyperlinkElementUtils::get_an_elements_target() const
  426. {
  427. // To get an element's target, given an a, area, or form element element, run these steps:
  428. // 1. If element has a target attribute, then return that attribute's value.
  429. if (auto target = hyperlink_element_utils_target(); !target.is_empty())
  430. return target;
  431. // FIXME: 2. If element's node document contains a base element with a
  432. // target attribute, then return the value of the target attribute of the
  433. // first such base element.
  434. // 3. Return the empty string.
  435. return "";
  436. }
  437. // https://html.spec.whatwg.org/multipage/links.html#get-an-element's-noopener
  438. TokenizedFeature::NoOpener HTMLHyperlinkElementUtils::get_an_elements_noopener(StringView target) const
  439. {
  440. // To get an element's noopener, given an a, area, or form element element and a string target:
  441. auto rel = hyperlink_element_utils_rel().to_lowercase();
  442. auto link_types = rel.view().split_view_if(Infra::is_ascii_whitespace);
  443. // 1. If element's link types include the noopener or noreferrer keyword, then return true.
  444. if (link_types.contains_slow("noopener"sv) || link_types.contains_slow("noreferrer"sv))
  445. return TokenizedFeature::NoOpener::Yes;
  446. // 2. If element's link types do not include the opener keyword and
  447. // target is an ASCII case-insensitive match for "_blank", then return true.
  448. if (!link_types.contains_slow("opener"sv) && Infra::is_ascii_case_insensitive_match(target, "_blank"sv))
  449. return TokenizedFeature::NoOpener::Yes;
  450. // 3. Return false.
  451. return TokenizedFeature::NoOpener::No;
  452. }
  453. }