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 "nsGenericHTMLElement.h"
16 #include "nsDOMTokenList.h"
19 class EventChainPostVisitor
;
20 class EventChainPreVisitor
;
25 class HTMLLinkElement final
: public nsGenericHTMLElement
,
27 public SupportsDNSPrefetch
{
29 explicit HTMLLinkElement(
30 already_AddRefed
<mozilla::dom::NodeInfo
>&& aNodeInfo
);
33 NS_DECL_ISUPPORTS_INHERITED
36 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(HTMLLinkElement
,
39 NS_IMPL_FROMNODE_HTML_WITH_TAG(HTMLLinkElement
, link
);
40 NS_DECL_ADDSIZEOFEXCLUDINGTHIS
46 nsresult
Clone(dom::NodeInfo
*, nsINode
** aResult
) const override
;
47 JSObject
* WrapNode(JSContext
* aCx
,
48 JS::Handle
<JSObject
*> aGivenProto
) override
;
51 nsresult
BindToTree(BindContext
&, nsINode
& aParent
) override
;
52 void UnbindFromTree(bool aNullParent
= true) override
;
53 nsresult
BeforeSetAttr(int32_t aNameSpaceID
, nsAtom
* aName
,
54 const nsAttrValueOrString
* aValue
,
55 bool aNotify
) override
;
56 nsresult
AfterSetAttr(int32_t aNameSpaceID
, nsAtom
* aName
,
57 const nsAttrValue
* aValue
, const nsAttrValue
* aOldValue
,
58 nsIPrincipal
* aSubjectPrincipal
, bool aNotify
) override
;
60 bool ParseAttribute(int32_t aNamespaceID
, nsAtom
* aAttribute
,
61 const nsAString
& aValue
,
62 nsIPrincipal
* aMaybeScriptedPrincipal
,
63 nsAttrValue
& aResult
) override
;
65 void CreateAndDispatchEvent(Document
* aDoc
, const nsAString
& aEventName
);
68 bool Disabled() const;
69 void SetDisabled(bool aDisabled
, ErrorResult
& aRv
);
73 GetURIAttr(nsGkAtoms::href
, nullptr, getter_AddRefs(mCachedURI
));
75 return mCachedURI
.get();
78 void GetHref(nsAString
& aValue
) {
79 GetURIAttr(nsGkAtoms::href
, nullptr, aValue
);
81 void SetHref(const nsAString
& aHref
, nsIPrincipal
* aTriggeringPrincipal
,
83 SetHTMLAttr(nsGkAtoms::href
, aHref
, aTriggeringPrincipal
, aRv
);
85 void SetHref(const nsAString
& aHref
, ErrorResult
& aRv
) {
86 SetHTMLAttr(nsGkAtoms::href
, aHref
, aRv
);
88 void GetCrossOrigin(nsAString
& aResult
) {
89 // Null for both missing and invalid defaults is ok, since we
90 // always parse to an enum value, so we don't need an invalid
91 // default, and we _want_ the missing default to be null.
92 GetEnumAttr(nsGkAtoms::crossorigin
, nullptr, aResult
);
94 void SetCrossOrigin(const nsAString
& aCrossOrigin
, ErrorResult
& aError
) {
95 SetOrRemoveNullableStringAttr(nsGkAtoms::crossorigin
, aCrossOrigin
, aError
);
97 // nsAString for WebBrowserPersistLocalDocument
98 void GetRel(nsAString
& aValue
) { GetHTMLAttr(nsGkAtoms::rel
, aValue
); }
99 void SetRel(const nsAString
& aRel
, ErrorResult
& aRv
) {
100 SetHTMLAttr(nsGkAtoms::rel
, aRel
, aRv
);
102 nsDOMTokenList
* RelList();
103 void GetMedia(DOMString
& aValue
) { GetHTMLAttr(nsGkAtoms::media
, aValue
); }
104 void SetMedia(const nsAString
& aMedia
, ErrorResult
& aRv
) {
105 SetHTMLAttr(nsGkAtoms::media
, aMedia
, aRv
);
107 void GetHreflang(DOMString
& aValue
) {
108 GetHTMLAttr(nsGkAtoms::hreflang
, aValue
);
110 void SetHreflang(const nsAString
& aHreflang
, ErrorResult
& aRv
) {
111 SetHTMLAttr(nsGkAtoms::hreflang
, aHreflang
, aRv
);
113 void GetAs(nsAString
& aResult
);
114 void SetAs(const nsAString
& aAs
, ErrorResult
& aRv
) {
115 SetAttr(nsGkAtoms::as
, aAs
, aRv
);
118 nsDOMTokenList
* Sizes() {
120 mSizes
= new nsDOMTokenList(this, nsGkAtoms::sizes
);
124 void GetType(nsAString
& aValue
) { GetHTMLAttr(nsGkAtoms::type
, aValue
); }
125 void SetType(const nsAString
& aType
, ErrorResult
& aRv
) {
126 SetHTMLAttr(nsGkAtoms::type
, aType
, aRv
);
128 void GetCharset(nsAString
& aValue
) override
{
129 GetHTMLAttr(nsGkAtoms::charset
, aValue
);
131 void SetCharset(const nsAString
& aCharset
, ErrorResult
& aRv
) {
132 SetHTMLAttr(nsGkAtoms::charset
, aCharset
, aRv
);
134 void GetRev(DOMString
& aValue
) { GetHTMLAttr(nsGkAtoms::rev
, aValue
); }
135 void SetRev(const nsAString
& aRev
, ErrorResult
& aRv
) {
136 SetHTMLAttr(nsGkAtoms::rev
, aRev
, aRv
);
138 void GetTarget(DOMString
& aValue
) { GetHTMLAttr(nsGkAtoms::target
, aValue
); }
139 void SetTarget(const nsAString
& aTarget
, ErrorResult
& aRv
) {
140 SetHTMLAttr(nsGkAtoms::target
, aTarget
, aRv
);
142 void GetIntegrity(nsAString
& aIntegrity
) const {
143 GetHTMLAttr(nsGkAtoms::integrity
, aIntegrity
);
145 void SetIntegrity(const nsAString
& aIntegrity
, ErrorResult
& aRv
) {
146 SetHTMLAttr(nsGkAtoms::integrity
, aIntegrity
, aRv
);
148 void SetReferrerPolicy(const nsAString
& aReferrer
, ErrorResult
& aError
) {
149 SetHTMLAttr(nsGkAtoms::referrerpolicy
, aReferrer
, aError
);
151 void GetReferrerPolicy(nsAString
& aReferrer
) {
152 GetEnumAttr(nsGkAtoms::referrerpolicy
, "", aReferrer
);
154 void GetImageSrcset(nsAString
& aImageSrcset
) {
155 GetHTMLAttr(nsGkAtoms::imagesrcset
, aImageSrcset
);
157 void SetImageSrcset(const nsAString
& aImageSrcset
, ErrorResult
& aError
) {
158 SetHTMLAttr(nsGkAtoms::imagesrcset
, aImageSrcset
, aError
);
160 void GetImageSizes(nsAString
& aImageSizes
) {
161 GetHTMLAttr(nsGkAtoms::imagesizes
, aImageSizes
);
163 void SetImageSizes(const nsAString
& aImageSizes
, ErrorResult
& aError
) {
164 SetHTMLAttr(nsGkAtoms::imagesizes
, aImageSizes
, aError
);
167 CORSMode
GetCORSMode() const {
168 return AttrValueToCORSMode(GetParsedAttr(nsGkAtoms::crossorigin
));
171 void NodeInfoChanged(Document
* aOldDoc
) final
{
172 mCachedURI
= nullptr;
173 nsGenericHTMLElement::NodeInfoChanged(aOldDoc
);
177 virtual ~HTMLLinkElement();
179 void GetContentPolicyMimeTypeMedia(nsAttrValue
& aAsAttr
,
180 nsContentPolicyType
& aPolicyType
,
181 nsString
& aMimeType
, nsAString
& aMedia
);
182 void TryDNSPrefetchOrPreconnectOrPrefetchOrPreloadOrPrerender();
183 void UpdatePreload(nsAtom
* aName
, const nsAttrValue
* aValue
,
184 const nsAttrValue
* aOldValue
);
185 void CancelPrefetchOrPreload();
187 void StartPreload(nsContentPolicyType policyType
);
188 void CancelPreload();
190 // Returns whether the type attribute specifies the text/css mime type for
192 static bool IsCSSMimeTypeAttributeForLinkElement(
193 const mozilla::dom::Element
&);
196 nsIContent
& AsContent() final
{ return *this; }
197 const LinkStyle
* AsLinkStyle() const final
{ return this; }
198 Maybe
<SheetInfo
> GetStyleSheetInfo() final
;
200 RefPtr
<nsDOMTokenList
> mRelList
;
201 RefPtr
<nsDOMTokenList
> mSizes
;
203 // A weak reference to our preload is held only to cancel the preload when
204 // this node updates or unbounds from the tree. We want to prevent cycles,
205 // the preload is held alive by other means.
206 WeakPtr
<PreloaderBase
> mPreload
;
208 // The cached href attribute value.
209 nsCOMPtr
<nsIURI
> mCachedURI
;
211 // The "explicitly enabled" flag. This flag is set whenever the `disabled`
212 // attribute is explicitly unset, and makes alternate stylesheets not be
213 // disabled by default anymore.
215 // See https://github.com/whatwg/html/issues/3840#issuecomment-481034206.
216 bool mExplicitlyEnabled
= false;
220 } // namespace mozilla
222 #endif // mozilla_dom_HTMLLinkElement_h