DXR will be turned off on Tuesday, December 29th. It will redirect to Searchfox.
See the announcement on Discourse.

DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Implementation

Mercurial (c68fe15a81fc)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_dom_HTMLLinkElement_h
#define mozilla_dom_HTMLLinkElement_h


#include "mozilla/Attributes.h"
#include "mozilla/dom/Link.h"
#include "mozilla/dom/LinkStyle.h"
#include "mozilla/WeakPtr.h"
#include "nsGenericHTMLElement.h"
#include "nsStyleLinkElement.h"
#include "nsDOMTokenList.h"

namespace mozilla {
class EventChainPostVisitor;
class EventChainPreVisitor;
class EventChainPreVisitor;
namespace dom {

// NOTE(emilio): If we stop inheriting from Link, we need to remove the
// IsHTMLElement(nsGkAtoms::link) checks in Link.cpp.
class HTMLLinkElement final : public nsGenericHTMLElement,
class HTMLLinkElement final : public nsGenericHTMLElement,
                              public LinkStyle,
                              public Link {
 public:
  explicit HTMLLinkElement(
      already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo);

  // nsISupports
  NS_DECL_ISUPPORTS_INHERITED
  NS_DECL_ISUPPORTS_INHERITED

  // CC
  NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(HTMLLinkElement,
                                           nsGenericHTMLElement)


  NS_IMPL_FROMNODE_HTML_WITH_TAG(HTMLLinkElement, link);
  NS_DECL_ADDSIZEOFEXCLUDINGTHIS

  void LinkAdded();
  void LinkRemoved();
  void LinkRemoved();

  // EventTarget
  void GetEventTargetParent(EventChainPreVisitor& aVisitor) override;
  MOZ_CAN_RUN_SCRIPT
  nsresult PostHandleEvent(EventChainPostVisitor& aVisitor) override;
  nsresult PostHandleEvent(EventChainPostVisitor& aVisitor) override;

  // nsINode
  virtual nsresult Clone(dom::NodeInfo*, nsINode** aResult) const override;
  virtual JSObject* WrapNode(JSContext* aCx,
  virtual JSObject* WrapNode(JSContext* aCx,
                             JS::Handle<JSObject*> aGivenProto) override;

  // nsIContent
  virtual nsresult BindToTree(BindContext&, nsINode& aParent) override;
  virtual void UnbindFromTree(bool aNullParent = true) override;
  virtual void UnbindFromTree(bool aNullParent = true) override;
  virtual nsresult BeforeSetAttr(int32_t aNameSpaceID, nsAtom* aName,
                                 const nsAttrValueOrString* aValue,
                                 bool aNotify) override;
  virtual nsresult AfterSetAttr(int32_t aNameSpaceID, nsAtom* aName,
                                const nsAttrValue* aValue,
                                const nsAttrValue* aValue,
                                const nsAttrValue* aOldValue,
                                nsIPrincipal* aSubjectPrincipal,
                                bool aNotify) override;
  virtual bool IsLink(nsIURI** aURI) const override;
  virtual bool IsLink(nsIURI** aURI) const override;
  virtual already_AddRefed<nsIURI> GetHrefURI() const override;

  // Element
  virtual bool ParseAttribute(int32_t aNamespaceID, nsAtom* aAttribute,
                              const nsAString& aValue,
                              const nsAString& aValue,
                              nsIPrincipal* aMaybeScriptedPrincipal,
                              nsAttrValue& aResult) override;
  virtual void GetLinkTarget(nsAString& aTarget) override;
  virtual EventStates IntrinsicState() const override;


  void CreateAndDispatchEvent(Document* aDoc, const nsAString& aEventName);

  virtual void OnDNSPrefetchDeferred() override;
  virtual void OnDNSPrefetchRequested() override;
  virtual bool HasDeferredDNSPrefetchRequest() override;
  virtual bool HasDeferredDNSPrefetchRequest() override;

  // WebIDL
  bool Disabled() const;
  void SetDisabled(bool aDisabled, ErrorResult& aRv);


  void GetHref(nsAString& aValue) {
    GetURIAttr(nsGkAtoms::href, nullptr, aValue);
  }
  void SetHref(const nsAString& aHref, nsIPrincipal* aTriggeringPrincipal,
               ErrorResult& aRv) {
    SetHTMLAttr(nsGkAtoms::href, aHref, aTriggeringPrincipal, aRv);
  }
  void SetHref(const nsAString& aHref, ErrorResult& aRv) {
    SetHTMLAttr(nsGkAtoms::href, aHref, aRv);
    SetHTMLAttr(nsGkAtoms::href, aHref, aRv);
  }
  void GetCrossOrigin(nsAString& aResult) {
    // Null for both missing and invalid defaults is ok, since we
    // always parse to an enum value, so we don't need an invalid
    // default, and we _want_ the missing default to be null.
    // default, and we _want_ the missing default to be null.
    GetEnumAttr(nsGkAtoms::crossorigin, nullptr, aResult);
  }
  void SetCrossOrigin(const nsAString& aCrossOrigin, ErrorResult& aError) {
    SetOrRemoveNullableStringAttr(nsGkAtoms::crossorigin, aCrossOrigin, aError);
    SetOrRemoveNullableStringAttr(nsGkAtoms::crossorigin, aCrossOrigin, aError);
  }
  // nsAString for WebBrowserPersistLocalDocument
  void GetRel(nsAString& aValue) { GetHTMLAttr(nsGkAtoms::rel, aValue); }
  void SetRel(const nsAString& aRel, ErrorResult& aRv) {
    SetHTMLAttr(nsGkAtoms::rel, aRel, aRv);
    SetHTMLAttr(nsGkAtoms::rel, aRel, aRv);
  }
  nsDOMTokenList* RelList();
  void GetMedia(DOMString& aValue) { GetHTMLAttr(nsGkAtoms::media, aValue); }
  void SetMedia(const nsAString& aMedia, ErrorResult& aRv) {
    SetHTMLAttr(nsGkAtoms::media, aMedia, aRv);
    SetHTMLAttr(nsGkAtoms::media, aMedia, aRv);
  }
  void GetHreflang(DOMString& aValue) {
    GetHTMLAttr(nsGkAtoms::hreflang, aValue);
  }
  void SetHreflang(const nsAString& aHreflang, ErrorResult& aRv) {
  void SetHreflang(const nsAString& aHreflang, ErrorResult& aRv) {
    SetHTMLAttr(nsGkAtoms::hreflang, aHreflang, aRv);
  }
  void GetAs(nsAString& aResult);
  void SetAs(const nsAString& aAs, ErrorResult& aRv) {
    SetAttr(nsGkAtoms::as, aAs, aRv);
    SetAttr(nsGkAtoms::as, aAs, aRv);
  }

  static void ParseAsValue(const nsAString& aValue, nsAttrValue& aResult);
  static nsContentPolicyType AsValueToContentPolicy(const nsAttrValue& aValue);


  nsDOMTokenList* Sizes() {
    if (!mSizes) {
      mSizes = new nsDOMTokenList(this, nsGkAtoms::sizes);
    }
    return mSizes;
  }
  void GetType(DOMString& aValue) { GetHTMLAttr(nsGkAtoms::type, aValue); }
  void GetType(DOMString& aValue) { GetHTMLAttr(nsGkAtoms::type, aValue); }
  void SetType(const nsAString& aType, ErrorResult& aRv) {
    SetHTMLAttr(nsGkAtoms::type, aType, aRv);
  }
  void GetCharset(nsAString& aValue) override {
    GetHTMLAttr(nsGkAtoms::charset, aValue);
    GetHTMLAttr(nsGkAtoms::charset, aValue);
  }
  void SetCharset(const nsAString& aCharset, ErrorResult& aRv) {
    SetHTMLAttr(nsGkAtoms::charset, aCharset, aRv);
  }
  void GetRev(DOMString& aValue) { GetHTMLAttr(nsGkAtoms::rev, aValue); }
  void GetRev(DOMString& aValue) { GetHTMLAttr(nsGkAtoms::rev, aValue); }
  void SetRev(const nsAString& aRev, ErrorResult& aRv) {
    SetHTMLAttr(nsGkAtoms::rev, aRev, aRv);
  }
  void GetTarget(DOMString& aValue) { GetHTMLAttr(nsGkAtoms::target, aValue); }
  void SetTarget(const nsAString& aTarget, ErrorResult& aRv) {
  void SetTarget(const nsAString& aTarget, ErrorResult& aRv) {
    SetHTMLAttr(nsGkAtoms::target, aTarget, aRv);
  }
  void GetIntegrity(nsAString& aIntegrity) const {
    GetHTMLAttr(nsGkAtoms::integrity, aIntegrity);
  }
  }
  void SetIntegrity(const nsAString& aIntegrity, ErrorResult& aRv) {
    SetHTMLAttr(nsGkAtoms::integrity, aIntegrity, aRv);
  }
  void SetReferrerPolicy(const nsAString& aReferrer, ErrorResult& aError) {
    SetHTMLAttr(nsGkAtoms::referrerpolicy, aReferrer, aError);
    SetHTMLAttr(nsGkAtoms::referrerpolicy, aReferrer, aError);
  }
  void GetReferrerPolicy(nsAString& aReferrer) {
    GetEnumAttr(nsGkAtoms::referrerpolicy, EmptyCString().get(), aReferrer);
    GetEnumAttr(nsGkAtoms::referrerpolicy, EmptyCString().get(), aReferrer);
  }
  }
  void GetImageSrcset(nsAString& aImageSrcset) {
    GetHTMLAttr(nsGkAtoms::imagesrcset, aImageSrcset);
  }
  void SetImageSrcset(const nsAString& aImageSrcset, ErrorResult& aError) {
    SetHTMLAttr(nsGkAtoms::imagesrcset, aImageSrcset, aError);
  }
  }
  void GetImageSizes(nsAString& aImageSizes) {
    GetHTMLAttr(nsGkAtoms::imagesizes, aImageSizes);
  }
  void SetImageSizes(const nsAString& aImageSizes, ErrorResult& aError) {
    SetHTMLAttr(nsGkAtoms::imagesizes, aImageSizes, aError);
    SetHTMLAttr(nsGkAtoms::imagesizes, aImageSizes, aError);
  }

  CORSMode GetCORSMode() const {
    return AttrValueToCORSMode(GetParsedAttr(nsGkAtoms::crossorigin));
  }
  }

  void NodeInfoChanged(Document* aOldDoc) final {
    ClearHasPendingLinkUpdate();
    nsGenericHTMLElement::NodeInfoChanged(aOldDoc);
  }
  }

  static bool CheckPreloadAttrs(const nsAttrValue& aAs, const nsAString& aType,
                                const nsAString& aMedia, Document* aDocument);

 protected:
 protected:
  virtual ~HTMLLinkElement();

  void GetContentPolicyMimeTypeMedia(nsAttrValue& aAsAttr,
                                     nsContentPolicyType& aPolicyType,
                                     nsContentPolicyType& aPolicyType,
                                     nsString& aMimeType, nsAString& aMedia);
  void TryDNSPrefetchOrPreconnectOrPrefetchOrPreloadOrPrerender();
  void UpdatePreload(nsAtom* aName, const nsAttrValue* aValue,
                     const nsAttrValue* aOldValue);
  void CancelPrefetchOrPreload();
  void CancelPrefetchOrPreload();

  void StartPreload(nsContentPolicyType policyType);
  void CancelPreload();

  // Returns whether the type attribute specifies the text/css mime type for
  // link elements.
  // link elements.
  static bool IsCSSMimeTypeAttributeForLinkElement(
      const mozilla::dom::Element&);

  // LinkStyle
  nsIContent& AsContent() final { return *this; }
  Maybe<SheetInfo> GetStyleSheetInfo() final;
  const LinkStyle* AsLinkStyle() const final { return this; }
  Maybe<SheetInfo> GetStyleSheetInfo() final;


  RefPtr<nsDOMTokenList> mRelList;
  RefPtr<nsDOMTokenList> mSizes;

  // A weak reference to our preload is held only to cancel the preload when
  // this node updates or unbounds from the tree.  We want to prevent cycles,

  // the preload is held alive by other means.
  WeakPtr<PreloaderBase> mPreload;

  // The "explicitly enabled" flag. This flag is set whenever the `disabled`
  // attribute is explicitly unset, and makes alternate stylesheets not be
  // See https://github.com/whatwg/html/issues/3840#issuecomment-481034206.
  // disabled by default anymore.
  //
  // See https://github.com/whatwg/html/issues/3840#issuecomment-481034206.
  bool mExplicitlyEnabled = false;
};
}  // namespace mozilla

}  // namespace dom
}  // namespace mozilla

#endif  // mozilla_dom_HTMLLinkElement_h