HTMLLinkElement.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright (c) 2018-2023, Andreas Kling <andreas@ladybird.org>
  3. * Copyright (c) 2021, the SerenityOS developers.
  4. * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
  5. * Copyright (c) 2023, Srikavin Ramkumar <me@srikavin.me>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #pragma once
  10. #include <LibWeb/DOM/DocumentLoadEventDelayer.h>
  11. #include <LibWeb/Fetch/Infrastructure/FetchAlgorithms.h>
  12. #include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
  13. #include <LibWeb/HTML/CORSSettingAttribute.h>
  14. #include <LibWeb/HTML/HTMLElement.h>
  15. #include <LibWeb/Loader/Resource.h>
  16. namespace Web::HTML {
  17. class HTMLLinkElement final
  18. : public HTMLElement
  19. , public ResourceClient {
  20. WEB_PLATFORM_OBJECT(HTMLLinkElement, HTMLElement);
  21. GC_DECLARE_ALLOCATOR(HTMLLinkElement);
  22. public:
  23. virtual ~HTMLLinkElement() override;
  24. virtual void inserted() override;
  25. virtual void removed_from(Node* old_parent) override;
  26. String rel() const { return get_attribute_value(HTML::AttributeNames::rel); }
  27. String type() const { return get_attribute_value(HTML::AttributeNames::type); }
  28. String href() const { return get_attribute_value(HTML::AttributeNames::href); }
  29. String as() const;
  30. WebIDL::ExceptionOr<void> set_as(String const&);
  31. GC::Ref<DOM::DOMTokenList> rel_list();
  32. GC::Ref<DOM::DOMTokenList> sizes();
  33. bool has_loaded_icon() const;
  34. bool load_favicon_and_use_if_window_is_active();
  35. static WebIDL::ExceptionOr<void> load_fallback_favicon_if_needed(GC::Ref<DOM::Document>);
  36. private:
  37. HTMLLinkElement(DOM::Document&, DOM::QualifiedName);
  38. virtual void initialize(JS::Realm&) override;
  39. virtual void attribute_changed(FlyString const& name, Optional<String> const& old_value, Optional<String> const& value, Optional<FlyString> const& namespace_) override;
  40. // ^ResourceClient
  41. virtual void resource_did_fail() override;
  42. virtual void resource_did_load() override;
  43. // ^DOM::Node
  44. virtual bool is_html_link_element() const override { return true; }
  45. // ^HTMLElement
  46. virtual void visit_edges(Cell::Visitor&) override;
  47. struct LinkProcessingOptions {
  48. // href (default the empty string)
  49. String href {};
  50. // destination (default the empty string)
  51. Optional<Fetch::Infrastructure::Request::Destination> destination {};
  52. // initiator (default "link")
  53. Optional<Fetch::Infrastructure::Request::InitiatorType> initiator { Fetch::Infrastructure::Request::InitiatorType::Link };
  54. // integrity (default the empty string)
  55. String integrity {};
  56. // type (default the empty string)
  57. String type {};
  58. // cryptographic nonce metadata (default the empty string)
  59. // A string
  60. String cryptographic_nonce_metadata {};
  61. // crossorigin (default No CORS)
  62. // A CORS settings attribute state
  63. CORSSettingAttribute crossorigin { CORSSettingAttribute::NoCORS };
  64. // referrer policy (default the empty string)
  65. // A referrer policy
  66. ReferrerPolicy::ReferrerPolicy referrer_policy { ReferrerPolicy::ReferrerPolicy::EmptyString };
  67. // FIXME: source set (default null)
  68. // Null or a source set
  69. // base URL
  70. // A URL
  71. URL::URL base_url;
  72. // origin
  73. // An origin
  74. URL::Origin origin;
  75. // environment
  76. // An environment
  77. GC::Ptr<HTML::EnvironmentSettingsObject> environment;
  78. // policy container
  79. // A policy container
  80. HTML::PolicyContainer policy_container;
  81. // document (default null)
  82. // Null or a Document
  83. GC::Ptr<Web::DOM::Document> document;
  84. // FIXME: on document ready (default null)
  85. // Null or an algorithm accepting a Document
  86. // fetch priority (default auto)
  87. // A fetch priority attribute state
  88. Fetch::Infrastructure::Request::Priority fetch_priority { Fetch::Infrastructure::Request::Priority::Auto };
  89. };
  90. // https://html.spec.whatwg.org/multipage/semantics.html#create-link-options-from-element
  91. LinkProcessingOptions create_link_options();
  92. // https://html.spec.whatwg.org/multipage/semantics.html#create-a-link-request
  93. GC::Ptr<Fetch::Infrastructure::Request> create_link_request(LinkProcessingOptions const&);
  94. // https://html.spec.whatwg.org/multipage/semantics.html#linked-resource-fetch-setup-steps
  95. bool linked_resource_fetch_setup_steps(Fetch::Infrastructure::Request&);
  96. // https://html.spec.whatwg.org/multipage/links.html#link-type-stylesheet:linked-resource-fetch-setup-steps
  97. bool stylesheet_linked_resource_fetch_setup_steps(Fetch::Infrastructure::Request&);
  98. // https://html.spec.whatwg.org/multipage/semantics.html#fetch-and-process-the-linked-resource
  99. void fetch_and_process_linked_resource();
  100. // https://html.spec.whatwg.org/multipage/semantics.html#process-the-linked-resource
  101. void process_linked_resource(bool success, Fetch::Infrastructure::Response const&, Variant<Empty, Fetch::Infrastructure::FetchAlgorithms::ConsumeBodyFailureTag, ByteBuffer>);
  102. // https://html.spec.whatwg.org/multipage/links.html#link-type-stylesheet:process-the-linked-resource
  103. void process_stylesheet_resource(bool success, Fetch::Infrastructure::Response const&, Variant<Empty, Fetch::Infrastructure::FetchAlgorithms::ConsumeBodyFailureTag, ByteBuffer>);
  104. // https://html.spec.whatwg.org/multipage/semantics.html#default-fetch-and-process-the-linked-resource
  105. void default_fetch_and_process_linked_resource();
  106. void resource_did_load_favicon();
  107. struct Relationship {
  108. enum {
  109. Alternate = 1 << 0,
  110. Stylesheet = 1 << 1,
  111. Preload = 1 << 2,
  112. DNSPrefetch = 1 << 3,
  113. Preconnect = 1 << 4,
  114. Icon = 1 << 5,
  115. };
  116. };
  117. GC::Ptr<Fetch::Infrastructure::FetchController> m_fetch_controller;
  118. GC::Ptr<CSS::CSSStyleSheet> m_loaded_style_sheet;
  119. Optional<DOM::DocumentLoadEventDelayer> m_document_load_event_delayer;
  120. GC::Ptr<DOM::DOMTokenList> m_rel_list;
  121. GC::Ptr<DOM::DOMTokenList> m_sizes;
  122. unsigned m_relationship { 0 };
  123. // https://html.spec.whatwg.org/multipage/semantics.html#explicitly-enabled
  124. bool m_explicitly_enabled { false };
  125. };
  126. }