summaryrefslogtreecommitdiffstats
path: root/dom/svg/SVGMPathElement.cpp
blob: 8ff21f61cdd79dc76c9faea46536ede25229af43 (plain)
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
/* -*- 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/SVGMPathElement.h"

#include "nsDebug.h"
#include "mozilla/ArrayUtils.h"
#include "mozilla/SVGObserverUtils.h"
#include "mozilla/dom/Document.h"
#include "mozilla/dom/SVGAnimateMotionElement.h"
#include "mozilla/dom/SVGGeometryElement.h"
#include "nsContentUtils.h"
#include "nsIReferrerInfo.h"
#include "mozilla/dom/SVGMPathElementBinding.h"
#include "nsIURI.h"

NS_IMPL_NS_NEW_SVG_ELEMENT(MPath)

namespace mozilla::dom {

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

SVGElement::StringInfo SVGMPathElement::sStringInfo[2] = {
    {nsGkAtoms::href, kNameSpaceID_None, false},
    {nsGkAtoms::href, kNameSpaceID_XLink, false}};

// Cycle collection magic -- based on SVGUseElement
NS_IMPL_CYCLE_COLLECTION_CLASS(SVGMPathElement)

NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(SVGMPathElement,
                                                SVGMPathElementBase)
  tmp->mMPathObserver = nullptr;
NS_IMPL_CYCLE_COLLECTION_UNLINK_END

NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(SVGMPathElement,
                                                  SVGMPathElementBase)
  SVGObserverUtils::TraverseMPathObserver(tmp, &cb);
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END

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

NS_IMPL_ISUPPORTS_CYCLE_COLLECTION_INHERITED_0(SVGMPathElement,
                                               SVGMPathElementBase)

// Constructor
SVGMPathElement::SVGMPathElement(
    already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo)
    : SVGMPathElementBase(std::move(aNodeInfo)) {}

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

NS_IMPL_ELEMENT_CLONE_WITH_INIT(SVGMPathElement)

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

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

void SVGMPathElement::UnbindFromTree(UnbindContext& aContext) {
  mMPathObserver = nullptr;
  NotifyParentOfMpathChange();
  SVGMPathElementBase::UnbindFromTree(aContext);
}

void SVGMPathElement::AfterSetAttr(int32_t aNamespaceID, nsAtom* aName,
                                   const nsAttrValue* aValue,
                                   const nsAttrValue* aOldValue,
                                   nsIPrincipal* aMaybeScriptedPrincipal,
                                   bool aNotify) {
  if (aName == nsGkAtoms::href &&
      (aNamespaceID == kNameSpaceID_None ||
       (aNamespaceID == kNameSpaceID_XLink && !HasAttr(nsGkAtoms::href)))) {
    mMPathObserver = nullptr;
    NotifyParentOfMpathChange();
  }

  return SVGMPathElementBase::AfterSetAttr(
      aNamespaceID, aName, aValue, aOldValue, aMaybeScriptedPrincipal, aNotify);
}

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

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

//----------------------------------------------------------------------
// Public helper methods

void SVGMPathElement::HrefAsString(nsAString& aHref) {
  if (mStringAttributes[SVGMPathElement::HREF].IsExplicitlySet()) {
    mStringAttributes[SVGMPathElement::HREF].GetBaseValue(aHref, this);
  } else {
    mStringAttributes[SVGMPathElement::XLINK_HREF].GetBaseValue(aHref, this);
  }
}

SVGGeometryElement* SVGMPathElement::GetReferencedPath() {
  return SVGObserverUtils::GetAndObserveMPathsPath(this);
}

void SVGMPathElement::NotifyParentOfMpathChange() {
  if (auto* animateMotionParent =
          SVGAnimateMotionElement::FromNodeOrNull(GetParent())) {
    animateMotionParent->MpathChanged();
    AnimationNeedsResample();
  }
}

}  // namespace mozilla::dom