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.

Header

Mercurial (b6d82b1a6b02)

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 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
/* -*- 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/. */

#include "mozilla/dom/SVGAElement.h"

#include "mozilla/Attributes.h"
#include "mozilla/EventDispatcher.h"
#include "mozilla/EventStates.h"
#include "mozilla/dom/BindContext.h"
#include "mozilla/dom/DocumentInlines.h"
#include "mozilla/dom/SVGAElementBinding.h"
#include "nsCOMPtr.h"
#include "nsContentUtils.h"
#include "nsGkAtoms.h"
#include "nsIContentInlines.h"
#include "nsIURI.h"

NS_IMPL_NS_NEW_SVG_ELEMENT(A)

namespace mozilla {
namespace dom {

JSObject* SVGAElement::WrapNode(JSContext* aCx,
                                JS::Handle<JSObject*> aGivenProto) {
  return SVGAElement_Binding::Wrap(aCx, this, aGivenProto);
}

SVGElement::StringInfo SVGAElement::sStringInfo[3] = {
    {nsGkAtoms::href, kNameSpaceID_None, true},
    {nsGkAtoms::href, kNameSpaceID_XLink, true},
    {nsGkAtoms::target, kNameSpaceID_None, true}};

// static
const DOMTokenListSupportedToken SVGAElement::sSupportedRelValues[] = {
    "noreferrer", "noopener", nullptr};

//----------------------------------------------------------------------
// nsISupports methods

NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(SVGAElement)
  NS_INTERFACE_MAP_ENTRY(Link)
NS_INTERFACE_MAP_END_INHERITING(SVGAElementBase)

NS_IMPL_CYCLE_COLLECTION_INHERITED(SVGAElement, SVGAElementBase, mRelList)

NS_IMPL_ADDREF_INHERITED(SVGAElement, SVGAElementBase)
NS_IMPL_RELEASE_INHERITED(SVGAElement, SVGAElementBase)

//----------------------------------------------------------------------
// Implementation

SVGAElement::SVGAElement(already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo)
    : SVGAElementBase(std::move(aNodeInfo)), Link(this) {}

already_AddRefed<DOMSVGAnimatedString> SVGAElement::Href() {
  return mStringAttributes[HREF].IsExplicitlySet()
             ? mStringAttributes[HREF].ToDOMAnimatedString(this)
             : mStringAttributes[XLINK_HREF].ToDOMAnimatedString(this);
}

//----------------------------------------------------------------------
// Link methods

bool SVGAElement::ElementHasHref() const {
  return mStringAttributes[HREF].IsExplicitlySet() ||
         mStringAttributes[XLINK_HREF].IsExplicitlySet();
}

//----------------------------------------------------------------------
// nsINode methods

void SVGAElement::GetEventTargetParent(EventChainPreVisitor& aVisitor) {
  Element::GetEventTargetParent(aVisitor);

  GetEventTargetParentForLinks(aVisitor);
}

nsresult SVGAElement::PostHandleEvent(EventChainPostVisitor& aVisitor) {
  return PostHandleEventForLinks(aVisitor);
}

NS_IMPL_ELEMENT_CLONE_WITH_INIT(SVGAElement)

//----------------------------------------------------------------------

already_AddRefed<DOMSVGAnimatedString> SVGAElement::Target() {
  return mStringAttributes[TARGET].ToDOMAnimatedString(this);
}

void SVGAElement::GetDownload(nsAString& aDownload) {
  GetAttr(nsGkAtoms::download, aDownload);
}

void SVGAElement::SetDownload(const nsAString& aDownload, ErrorResult& rv) {
  SetAttr(nsGkAtoms::download, aDownload, rv);
}

void SVGAElement::GetPing(nsAString& aPing) { GetAttr(nsGkAtoms::ping, aPing); }

void SVGAElement::SetPing(const nsAString& aPing, ErrorResult& rv) {
  SetAttr(nsGkAtoms::ping, aPing, rv);
}

void SVGAElement::GetRel(nsAString& aRel) { GetAttr(nsGkAtoms::rel, aRel); }

void SVGAElement::SetRel(const nsAString& aRel, ErrorResult& rv) {
  SetAttr(nsGkAtoms::rel, aRel, rv);
}

void SVGAElement::GetReferrerPolicy(nsAString& aPolicy) {
  GetEnumAttr(nsGkAtoms::referrerpolicy, EmptyCString().get(), aPolicy);
}

void SVGAElement::SetReferrerPolicy(const nsAString& aPolicy,
                                    mozilla::ErrorResult& rv) {
  SetAttr(nsGkAtoms::referrerpolicy, aPolicy, rv);
}

nsDOMTokenList* SVGAElement::RelList() {
  if (!mRelList) {
    mRelList = new nsDOMTokenList(this, nsGkAtoms::rel, sSupportedRelValues);
  }
  return mRelList;
}

void SVGAElement::GetHreflang(nsAString& aHreflang) {
  GetAttr(nsGkAtoms::hreflang, aHreflang);
}

void SVGAElement::SetHreflang(const nsAString& aHreflang,
                              mozilla::ErrorResult& rv) {
  SetAttr(nsGkAtoms::hreflang, aHreflang, rv);
}

void SVGAElement::GetType(nsAString& aType) { GetAttr(nsGkAtoms::type, aType); }

void SVGAElement::SetType(const nsAString& aType, mozilla::ErrorResult& rv) {
  SetAttr(nsGkAtoms::type, aType, rv);
}

void SVGAElement::GetText(nsAString& aText, mozilla::ErrorResult& rv) {
  if (NS_WARN_IF(
          !nsContentUtils::GetNodeTextContent(this, true, aText, fallible))) {
    rv.Throw(NS_ERROR_OUT_OF_MEMORY);
  }
}

void SVGAElement::SetText(const nsAString& aText, mozilla::ErrorResult& rv) {
  rv = nsContentUtils::SetNodeTextContent(this, aText, false);
}

//----------------------------------------------------------------------
// nsIContent methods

nsresult SVGAElement::BindToTree(BindContext& aContext, nsINode& aParent) {
  Link::ResetLinkState(false, Link::ElementHasHref());

  nsresult rv = SVGAElementBase::BindToTree(aContext, aParent);
  NS_ENSURE_SUCCESS(rv, rv);

  if (Document* doc = aContext.GetComposedDoc()) {
    doc->RegisterPendingLinkUpdate(this);
  }

  return NS_OK;
}

void SVGAElement::UnbindFromTree(bool aNullParent) {
  // Without removing the link state we risk a dangling pointer
  // in the mStyledLinks hashtable
  Link::ResetLinkState(false, Link::ElementHasHref());

  SVGAElementBase::UnbindFromTree(aNullParent);
}

already_AddRefed<nsIURI> SVGAElement::GetHrefURI() const {
  nsCOMPtr<nsIURI> hrefURI;
  return IsLink(getter_AddRefs(hrefURI)) ? hrefURI.forget() : nullptr;
}

NS_IMETHODIMP_(bool)
SVGAElement::IsAttributeMapped(const nsAtom* name) const {
  static const MappedAttributeEntry* const map[] = {sFEFloodMap,
                                                    sFiltersMap,
                                                    sFontSpecificationMap,
                                                    sGradientStopMap,
                                                    sLightingEffectsMap,
                                                    sMarkersMap,
                                                    sTextContentElementsMap,
                                                    sViewportsMap};

  return FindAttributeDependence(name, map) ||
         SVGAElementBase::IsAttributeMapped(name);
}

int32_t SVGAElement::TabIndexDefault() { return 0; }

static bool IsNodeInEditableRegion(nsINode* aNode) {
  while (aNode) {
    if (aNode->IsEditable()) {
      return true;
    }
    aNode = aNode->GetParent();
  }
  return false;
}

bool SVGAElement::IsFocusableInternal(int32_t* aTabIndex, bool aWithMouse) {
  bool isFocusable = false;
  if (IsSVGFocusable(&isFocusable, aTabIndex)) {
    return isFocusable;
  }

  if (!OwnerDoc()->LinkHandlingEnabled()) {
    return false;
  }

  // Links that are in an editable region should never be focusable, even if
  // they are in a contenteditable="false" region.
  if (IsNodeInEditableRegion(this)) {
    if (aTabIndex) {
      *aTabIndex = -1;
    }
    return false;
  }

  if (!HasAttr(kNameSpaceID_None, nsGkAtoms::tabindex)) {
    // check whether we're actually a link
    if (!Link::HasURI()) {
      // Not tabbable or focusable without href (bug 17605), unless
      // forced to be via presence of nonnegative tabindex attribute
      if (aTabIndex) {
        *aTabIndex = -1;
      }
      return false;
    }
  }

  if (aTabIndex && (sTabFocusModel & eTabFocus_linksMask) == 0) {
    *aTabIndex = -1;
  }

  return true;
}

bool SVGAElement::IsLink(nsIURI** aURI) const {
  // To be a clickable XLink for styling and interaction purposes, we require:
  //
  //   xlink:href    - must be set
  //   xlink:type    - must be unset or set to "" or set to "simple"
  //   xlink:show    - must be unset or set to "", "new" or "replace"
  //   xlink:actuate - must be unset or set to "" or "onRequest"
  //
  // For any other values, we're either not a *clickable* XLink, or the end
  // result is poorly specified. Either way, we return false.

  static Element::AttrValuesArray sTypeVals[] = {nsGkAtoms::_empty,
                                                 nsGkAtoms::simple, nullptr};

  static Element::AttrValuesArray sShowVals[] = {
      nsGkAtoms::_empty, nsGkAtoms::_new, nsGkAtoms::replace, nullptr};

  static Element::AttrValuesArray sActuateVals[] = {
      nsGkAtoms::_empty, nsGkAtoms::onRequest, nullptr};

  // Optimization: check for href first for early return
  bool useBareHref = mStringAttributes[HREF].IsExplicitlySet();

  if ((useBareHref || mStringAttributes[XLINK_HREF].IsExplicitlySet()) &&
      FindAttrValueIn(kNameSpaceID_XLink, nsGkAtoms::type, sTypeVals,
                      eCaseMatters) != Element::ATTR_VALUE_NO_MATCH &&
      FindAttrValueIn(kNameSpaceID_XLink, nsGkAtoms::show, sShowVals,
                      eCaseMatters) != Element::ATTR_VALUE_NO_MATCH &&
      FindAttrValueIn(kNameSpaceID_XLink, nsGkAtoms::actuate, sActuateVals,
                      eCaseMatters) != Element::ATTR_VALUE_NO_MATCH) {
    // Get absolute URI
    nsAutoString str;
    const uint8_t idx = useBareHref ? HREF : XLINK_HREF;
    mStringAttributes[idx].GetAnimValue(str, this);
    nsContentUtils::NewURIWithDocumentCharset(aURI, str, OwnerDoc(),
                                              GetBaseURI());
    // must promise out param is non-null if we return true
    return !!*aURI;
  }

  *aURI = nullptr;
  return false;
}

void SVGAElement::GetLinkTarget(nsAString& aTarget) {
  mStringAttributes[TARGET].GetAnimValue(aTarget, this);
  if (aTarget.IsEmpty()) {
    static Element::AttrValuesArray sShowVals[] = {nsGkAtoms::_new,
                                                   nsGkAtoms::replace, nullptr};

    switch (FindAttrValueIn(kNameSpaceID_XLink, nsGkAtoms::show, sShowVals,
                            eCaseMatters)) {
      case 0:
        aTarget.AssignLiteral("_blank");
        return;
      case 1:
        return;
    }
    Document* ownerDoc = OwnerDoc();
    if (ownerDoc) {
      ownerDoc->GetBaseTarget(aTarget);
    }
  }
}

EventStates SVGAElement::IntrinsicState() const {
  return Link::LinkState() | SVGAElementBase::IntrinsicState();
}

nsresult SVGAElement::AfterSetAttr(int32_t aNameSpaceID, nsAtom* aName,
                                   const nsAttrValue* aValue,
                                   const nsAttrValue* aOldValue,
                                   nsIPrincipal* aMaybeScriptedPrincipal,
                                   bool aNotify) {
  if (aName == nsGkAtoms::href && (aNameSpaceID == kNameSpaceID_XLink ||
                                   aNameSpaceID == kNameSpaceID_None)) {
    // We can't assume that null aValue means we no longer have an href, because
    // we could be unsetting xlink:href but still have a null-namespace href, or
    // vice versa.  But we can fast-path the case when we _do_ have a new value.
    Link::ResetLinkState(aNotify, aValue || Link::ElementHasHref());
  }

  return SVGAElementBase::AfterSetAttr(aNameSpaceID, aName, aValue, aOldValue,
                                       aMaybeScriptedPrincipal, aNotify);
}

//----------------------------------------------------------------------
// SVGElement methods

SVGElement::StringAttributesInfo SVGAElement::GetStringInfo() {
  return StringAttributesInfo(mStringAttributes, sStringInfo,
                              ArrayLength(sStringInfo));
}

}  // namespace dom
}  // namespace mozilla