SVGMPathElement.cpp (4485B)
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/. */ 6 7 #include "mozilla/dom/SVGMPathElement.h" 8 9 #include "mozilla/SVGObserverUtils.h" 10 #include "mozilla/dom/Document.h" 11 #include "mozilla/dom/SVGAnimateMotionElement.h" 12 #include "mozilla/dom/SVGGeometryElement.h" 13 #include "mozilla/dom/SVGMPathElementBinding.h" 14 #include "nsContentUtils.h" 15 #include "nsDebug.h" 16 #include "nsIReferrerInfo.h" 17 #include "nsIURI.h" 18 19 NS_IMPL_NS_NEW_SVG_ELEMENT(MPath) 20 21 namespace mozilla::dom { 22 23 JSObject* SVGMPathElement::WrapNode(JSContext* aCx, 24 JS::Handle<JSObject*> aGivenProto) { 25 return SVGMPathElement_Binding::Wrap(aCx, this, aGivenProto); 26 } 27 28 SVGElement::StringInfo SVGMPathElement::sStringInfo[2] = { 29 {nsGkAtoms::href, kNameSpaceID_None, false}, 30 {nsGkAtoms::href, kNameSpaceID_XLink, false}}; 31 32 // Cycle collection magic -- based on SVGUseElement 33 NS_IMPL_CYCLE_COLLECTION_CLASS(SVGMPathElement) 34 35 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(SVGMPathElement, 36 SVGMPathElementBase) 37 tmp->mMPathObserver = nullptr; 38 NS_IMPL_CYCLE_COLLECTION_UNLINK_END 39 40 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(SVGMPathElement, 41 SVGMPathElementBase) 42 SVGObserverUtils::TraverseMPathObserver(tmp, &cb); 43 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END 44 45 //---------------------------------------------------------------------- 46 // nsISupports methods 47 48 NS_IMPL_ISUPPORTS_CYCLE_COLLECTION_INHERITED_0(SVGMPathElement, 49 SVGMPathElementBase) 50 51 // Constructor 52 SVGMPathElement::SVGMPathElement( 53 already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo) 54 : SVGMPathElementBase(std::move(aNodeInfo)) {} 55 56 //---------------------------------------------------------------------- 57 // nsINode methods 58 59 NS_IMPL_ELEMENT_CLONE_WITH_INIT(SVGMPathElement) 60 61 already_AddRefed<DOMSVGAnimatedString> SVGMPathElement::Href() { 62 return mStringAttributes[HREF].IsExplicitlySet() || 63 !mStringAttributes[XLINK_HREF].IsExplicitlySet() 64 ? mStringAttributes[HREF].ToDOMAnimatedString(this) 65 : mStringAttributes[XLINK_HREF].ToDOMAnimatedString(this); 66 } 67 68 //---------------------------------------------------------------------- 69 // nsIContent methods 70 71 void SVGMPathElement::UnbindFromTree(UnbindContext& aContext) { 72 mMPathObserver = nullptr; 73 NotifyParentOfMpathChange(); 74 SVGMPathElementBase::UnbindFromTree(aContext); 75 } 76 77 void SVGMPathElement::AfterSetAttr(int32_t aNamespaceID, nsAtom* aName, 78 const nsAttrValue* aValue, 79 const nsAttrValue* aOldValue, 80 nsIPrincipal* aMaybeScriptedPrincipal, 81 bool aNotify) { 82 if (aName == nsGkAtoms::href && 83 (aNamespaceID == kNameSpaceID_None || 84 (aNamespaceID == kNameSpaceID_XLink && !HasAttr(nsGkAtoms::href)))) { 85 mMPathObserver = nullptr; 86 NotifyParentOfMpathChange(); 87 } 88 89 return SVGMPathElementBase::AfterSetAttr( 90 aNamespaceID, aName, aValue, aOldValue, aMaybeScriptedPrincipal, aNotify); 91 } 92 93 //---------------------------------------------------------------------- 94 // SVGElement methods 95 96 SVGElement::StringAttributesInfo SVGMPathElement::GetStringInfo() { 97 return StringAttributesInfo(mStringAttributes, sStringInfo, 98 std::size(sStringInfo)); 99 } 100 101 //---------------------------------------------------------------------- 102 // Public helper methods 103 104 void SVGMPathElement::HrefAsString(nsAString& aHref) { 105 if (mStringAttributes[SVGMPathElement::HREF].IsExplicitlySet()) { 106 mStringAttributes[SVGMPathElement::HREF].GetBaseValue(aHref, this); 107 } else { 108 mStringAttributes[SVGMPathElement::XLINK_HREF].GetBaseValue(aHref, this); 109 } 110 } 111 112 SVGGeometryElement* SVGMPathElement::GetReferencedPath() { 113 return SVGObserverUtils::GetAndObserveMPathsPath(this); 114 } 115 116 void SVGMPathElement::NotifyParentOfMpathChange() { 117 if (auto* animateMotionParent = 118 SVGAnimateMotionElement::FromNodeOrNull(GetParent())) { 119 animateMotionParent->MpathChanged(); 120 AnimationNeedsResample(); 121 } 122 } 123 124 } // namespace mozilla::dom