Bug 1880216 - Migrate Fenix docs into Sphinx. r=owlish,geckoview-reviewers,android...
[gecko.git] / dom / html / HTMLLinkElement.h
blob4381aeea8529b12edd7156c3fd6ca2cb440589b6
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_HTMLLinkElement_h
8 #define mozilla_dom_HTMLLinkElement_h
10 #include "mozilla/Attributes.h"
11 #include "mozilla/dom/HTMLDNSPrefetch.h"
12 #include "mozilla/dom/LinkStyle.h"
13 #include "mozilla/dom/Link.h"
14 #include "mozilla/WeakPtr.h"
15 #include "nsDOMTokenList.h"
16 #include "nsGenericHTMLElement.h"
18 namespace mozilla {
19 class EventChainPostVisitor;
20 class EventChainPreVisitor;
21 class PreloaderBase;
23 namespace dom {
25 class HTMLLinkElement final : public nsGenericHTMLElement,
26 public LinkStyle,
27 public SupportsDNSPrefetch {
28 public:
29 explicit HTMLLinkElement(
30 already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo);
32 // nsISupports
33 NS_DECL_ISUPPORTS_INHERITED
35 // CC
36 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(HTMLLinkElement,
37 nsGenericHTMLElement)
39 NS_IMPL_FROMNODE_HTML_WITH_TAG(HTMLLinkElement, link);
40 NS_DECL_ADDSIZEOFEXCLUDINGTHIS
42 void LinkAdded();
44 // nsINode
45 nsresult Clone(dom::NodeInfo*, nsINode** aResult) const override;
46 JSObject* WrapNode(JSContext* aCx,
47 JS::Handle<JSObject*> aGivenProto) override;
49 // nsIContent
50 nsresult BindToTree(BindContext&, nsINode& aParent) override;
51 void UnbindFromTree(UnbindContext&) override;
52 void BeforeSetAttr(int32_t aNameSpaceID, nsAtom* aName,
53 const nsAttrValue* aValue, bool aNotify) override;
54 void AfterSetAttr(int32_t aNameSpaceID, nsAtom* aName,
55 const nsAttrValue* aValue, const nsAttrValue* aOldValue,
56 nsIPrincipal* aSubjectPrincipal, bool aNotify) override;
57 // Element
58 bool ParseAttribute(int32_t aNamespaceID, nsAtom* aAttribute,
59 const nsAString& aValue,
60 nsIPrincipal* aMaybeScriptedPrincipal,
61 nsAttrValue& aResult) override;
63 void CreateAndDispatchEvent(const nsAString& aEventName);
65 // WebIDL
66 bool Disabled() const;
67 void SetDisabled(bool aDisabled, ErrorResult& aRv);
69 nsIURI* GetURI() {
70 if (!mCachedURI) {
71 GetURIAttr(nsGkAtoms::href, nullptr, getter_AddRefs(mCachedURI));
73 return mCachedURI.get();
76 void GetHref(nsAString& aValue) {
77 GetURIAttr(nsGkAtoms::href, nullptr, aValue);
79 void SetHref(const nsAString& aHref, nsIPrincipal* aTriggeringPrincipal,
80 ErrorResult& aRv) {
81 SetHTMLAttr(nsGkAtoms::href, aHref, aTriggeringPrincipal, aRv);
83 void SetHref(const nsAString& aHref, ErrorResult& aRv) {
84 SetHTMLAttr(nsGkAtoms::href, aHref, aRv);
86 void GetCrossOrigin(nsAString& aResult) {
87 // Null for both missing and invalid defaults is ok, since we
88 // always parse to an enum value, so we don't need an invalid
89 // default, and we _want_ the missing default to be null.
90 GetEnumAttr(nsGkAtoms::crossorigin, nullptr, aResult);
92 void SetCrossOrigin(const nsAString& aCrossOrigin, ErrorResult& aError) {
93 SetOrRemoveNullableStringAttr(nsGkAtoms::crossorigin, aCrossOrigin, aError);
95 // nsAString for WebBrowserPersistLocalDocument
96 void GetRel(nsAString& aValue) { GetHTMLAttr(nsGkAtoms::rel, aValue); }
97 void SetRel(const nsAString& aRel, ErrorResult& aRv) {
98 SetHTMLAttr(nsGkAtoms::rel, aRel, aRv);
100 nsDOMTokenList* RelList();
101 void GetMedia(DOMString& aValue) { GetHTMLAttr(nsGkAtoms::media, aValue); }
102 void SetMedia(const nsAString& aMedia, ErrorResult& aRv) {
103 SetHTMLAttr(nsGkAtoms::media, aMedia, aRv);
105 void GetHreflang(DOMString& aValue) {
106 GetHTMLAttr(nsGkAtoms::hreflang, aValue);
108 void SetHreflang(const nsAString& aHreflang, ErrorResult& aRv) {
109 SetHTMLAttr(nsGkAtoms::hreflang, aHreflang, aRv);
111 void GetAs(nsAString& aResult);
112 void SetAs(const nsAString& aAs, ErrorResult& aRv) {
113 SetAttr(nsGkAtoms::as, aAs, aRv);
116 nsDOMTokenList* Sizes() {
117 if (!mSizes) {
118 mSizes = new nsDOMTokenList(this, nsGkAtoms::sizes);
120 return mSizes;
122 void GetType(nsAString& aValue) { GetHTMLAttr(nsGkAtoms::type, aValue); }
123 void SetType(const nsAString& aType, ErrorResult& aRv) {
124 SetHTMLAttr(nsGkAtoms::type, aType, aRv);
126 void GetCharset(nsAString& aValue) override {
127 GetHTMLAttr(nsGkAtoms::charset, aValue);
129 void SetCharset(const nsAString& aCharset, ErrorResult& aRv) {
130 SetHTMLAttr(nsGkAtoms::charset, aCharset, aRv);
132 void GetRev(DOMString& aValue) { GetHTMLAttr(nsGkAtoms::rev, aValue); }
133 void SetRev(const nsAString& aRev, ErrorResult& aRv) {
134 SetHTMLAttr(nsGkAtoms::rev, aRev, aRv);
136 void GetTarget(DOMString& aValue) { GetHTMLAttr(nsGkAtoms::target, aValue); }
137 void SetTarget(const nsAString& aTarget, ErrorResult& aRv) {
138 SetHTMLAttr(nsGkAtoms::target, aTarget, aRv);
140 void GetIntegrity(nsAString& aIntegrity) const {
141 GetHTMLAttr(nsGkAtoms::integrity, aIntegrity);
143 void SetIntegrity(const nsAString& aIntegrity, ErrorResult& aRv) {
144 SetHTMLAttr(nsGkAtoms::integrity, aIntegrity, aRv);
146 void SetReferrerPolicy(const nsAString& aReferrer, ErrorResult& aError) {
147 SetHTMLAttr(nsGkAtoms::referrerpolicy, aReferrer, aError);
149 void GetReferrerPolicy(nsAString& aReferrer) {
150 GetEnumAttr(nsGkAtoms::referrerpolicy, "", aReferrer);
152 void GetImageSrcset(nsAString& aImageSrcset) {
153 GetHTMLAttr(nsGkAtoms::imagesrcset, aImageSrcset);
155 void SetImageSrcset(const nsAString& aImageSrcset, ErrorResult& aError) {
156 SetHTMLAttr(nsGkAtoms::imagesrcset, aImageSrcset, aError);
158 void GetImageSizes(nsAString& aImageSizes) {
159 GetHTMLAttr(nsGkAtoms::imagesizes, aImageSizes);
161 void SetImageSizes(const nsAString& aImageSizes, ErrorResult& aError) {
162 SetHTMLAttr(nsGkAtoms::imagesizes, aImageSizes, aError);
165 CORSMode GetCORSMode() const {
166 return AttrValueToCORSMode(GetParsedAttr(nsGkAtoms::crossorigin));
169 nsDOMTokenList* Blocking();
170 bool IsPotentiallyRenderBlocking() override;
172 void NodeInfoChanged(Document* aOldDoc) final {
173 mCachedURI = nullptr;
174 nsGenericHTMLElement::NodeInfoChanged(aOldDoc);
177 protected:
178 virtual ~HTMLLinkElement();
180 void GetContentPolicyMimeTypeMedia(nsAttrValue& aAsAttr,
181 nsContentPolicyType& aPolicyType,
182 nsString& aMimeType, nsAString& aMedia);
183 void TryDNSPrefetchOrPreconnectOrPrefetchOrPreloadOrPrerender();
184 void UpdatePreload(nsAtom* aName, const nsAttrValue* aValue,
185 const nsAttrValue* aOldValue);
186 void CancelPrefetchOrPreload();
188 void StartPreload(nsContentPolicyType policyType);
189 void CancelPreload();
191 // Returns whether the type attribute specifies the text/css mime type for
192 // link elements.
193 static bool IsCSSMimeTypeAttributeForLinkElement(
194 const mozilla::dom::Element&);
196 // LinkStyle
197 nsIContent& AsContent() final { return *this; }
198 const LinkStyle* AsLinkStyle() const final { return this; }
199 Maybe<SheetInfo> GetStyleSheetInfo() final;
201 RefPtr<nsDOMTokenList> mRelList;
202 RefPtr<nsDOMTokenList> mSizes;
203 RefPtr<nsDOMTokenList> mBlocking;
205 // A weak reference to our preload is held only to cancel the preload when
206 // this node updates or unbounds from the tree. We want to prevent cycles,
207 // the preload is held alive by other means.
208 WeakPtr<PreloaderBase> mPreload;
210 // The cached href attribute value.
211 nsCOMPtr<nsIURI> mCachedURI;
213 // The "explicitly enabled" flag. This flag is set whenever the `disabled`
214 // attribute is explicitly unset, and makes alternate stylesheets not be
215 // disabled by default anymore.
217 // See https://github.com/whatwg/html/issues/3840#issuecomment-481034206.
218 bool mExplicitlyEnabled = false;
221 } // namespace dom
222 } // namespace mozilla
224 #endif // mozilla_dom_HTMLLinkElement_h