SVGMPathElement.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "mozilla/ArrayUtils.h"
  6. #include "mozilla/dom/SVGMPathElement.h"
  7. #include "nsDebug.h"
  8. #include "mozilla/dom/SVGAnimateMotionElement.h"
  9. #include "mozilla/dom/SVGPathElement.h"
  10. #include "nsContentUtils.h"
  11. #include "mozilla/dom/SVGMPathElementBinding.h"
  12. #include "nsIURI.h"
  13. NS_IMPL_NS_NEW_NAMESPACED_SVG_ELEMENT(MPath)
  14. namespace mozilla {
  15. namespace dom {
  16. JSObject*
  17. SVGMPathElement::WrapNode(JSContext *aCx, JS::Handle<JSObject*> aGivenProto)
  18. {
  19. return SVGMPathElementBinding::Wrap(aCx, this, aGivenProto);
  20. }
  21. nsSVGElement::StringInfo SVGMPathElement::sStringInfo[2] =
  22. {
  23. { &nsGkAtoms::href, kNameSpaceID_None, false },
  24. { &nsGkAtoms::href, kNameSpaceID_XLink, false }
  25. };
  26. // Cycle collection magic -- based on SVGUseElement
  27. NS_IMPL_CYCLE_COLLECTION_CLASS(SVGMPathElement)
  28. NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(SVGMPathElement,
  29. SVGMPathElementBase)
  30. tmp->UnlinkHrefTarget(false);
  31. NS_IMPL_CYCLE_COLLECTION_UNLINK_END
  32. NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(SVGMPathElement,
  33. SVGMPathElementBase)
  34. tmp->mHrefTarget.Traverse(&cb);
  35. NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
  36. //----------------------------------------------------------------------
  37. // nsISupports methods
  38. NS_IMPL_ADDREF_INHERITED(SVGMPathElement,SVGMPathElementBase)
  39. NS_IMPL_RELEASE_INHERITED(SVGMPathElement,SVGMPathElementBase)
  40. NS_INTERFACE_TABLE_HEAD_CYCLE_COLLECTION_INHERITED(SVGMPathElement)
  41. NS_INTERFACE_TABLE_INHERITED(SVGMPathElement, nsIDOMNode, nsIDOMElement,
  42. nsIDOMSVGElement,
  43. nsIMutationObserver)
  44. NS_INTERFACE_TABLE_TAIL_INHERITING(SVGMPathElementBase)
  45. // Constructor
  46. SVGMPathElement::SVGMPathElement(already_AddRefed<mozilla::dom::NodeInfo>& aNodeInfo)
  47. : SVGMPathElementBase(aNodeInfo),
  48. mHrefTarget(this)
  49. {
  50. }
  51. SVGMPathElement::~SVGMPathElement()
  52. {
  53. UnlinkHrefTarget(false);
  54. }
  55. //----------------------------------------------------------------------
  56. // nsIDOMNode methods
  57. NS_IMPL_ELEMENT_CLONE_WITH_INIT(SVGMPathElement)
  58. already_AddRefed<SVGAnimatedString>
  59. SVGMPathElement::Href()
  60. {
  61. return mStringAttributes[HREF].IsExplicitlySet()
  62. ? mStringAttributes[HREF].ToDOMAnimatedString(this)
  63. : mStringAttributes[XLINK_HREF].ToDOMAnimatedString(this);
  64. }
  65. //----------------------------------------------------------------------
  66. // nsIContent methods
  67. nsresult
  68. SVGMPathElement::BindToTree(nsIDocument* aDocument,
  69. nsIContent* aParent,
  70. nsIContent* aBindingParent,
  71. bool aCompileEventHandlers)
  72. {
  73. MOZ_ASSERT(!mHrefTarget.get(),
  74. "Shouldn't have href-target yet (or it should've been cleared)");
  75. nsresult rv = SVGMPathElementBase::BindToTree(aDocument, aParent,
  76. aBindingParent,
  77. aCompileEventHandlers);
  78. NS_ENSURE_SUCCESS(rv,rv);
  79. if (aDocument) {
  80. const nsAttrValue* hrefAttrValue =
  81. HasAttr(kNameSpaceID_None, nsGkAtoms::href)
  82. ? mAttrsAndChildren.GetAttr(nsGkAtoms::href, kNameSpaceID_None)
  83. : mAttrsAndChildren.GetAttr(nsGkAtoms::href, kNameSpaceID_XLink);
  84. if (hrefAttrValue) {
  85. UpdateHrefTarget(aParent, hrefAttrValue->GetStringValue());
  86. }
  87. }
  88. return NS_OK;
  89. }
  90. void
  91. SVGMPathElement::UnbindFromTree(bool aDeep, bool aNullParent)
  92. {
  93. UnlinkHrefTarget(true);
  94. SVGMPathElementBase::UnbindFromTree(aDeep, aNullParent);
  95. }
  96. bool
  97. SVGMPathElement::ParseAttribute(int32_t aNamespaceID,
  98. nsIAtom* aAttribute,
  99. const nsAString& aValue,
  100. nsAttrValue& aResult)
  101. {
  102. bool returnVal =
  103. SVGMPathElementBase::ParseAttribute(aNamespaceID, aAttribute,
  104. aValue, aResult);
  105. if ((aNamespaceID == kNameSpaceID_XLink ||
  106. aNamespaceID == kNameSpaceID_None ) &&
  107. aAttribute == nsGkAtoms::href &&
  108. IsInUncomposedDoc()) {
  109. // Note: If we fail the IsInDoc call, it's ok -- we'll update the target
  110. // on next BindToTree call.
  111. // Note: "href" takes priority over xlink:href. So if "xlink:href" is being
  112. // set here, we only let that update our target if "href" is *unset*.
  113. if (aNamespaceID != kNameSpaceID_XLink ||
  114. !mStringAttributes[HREF].IsExplicitlySet()) {
  115. UpdateHrefTarget(GetParent(), aValue);
  116. }
  117. }
  118. return returnVal;
  119. }
  120. nsresult
  121. SVGMPathElement::UnsetAttr(int32_t aNamespaceID,
  122. nsIAtom* aAttribute, bool aNotify)
  123. {
  124. nsresult rv = SVGMPathElementBase::UnsetAttr(aNamespaceID, aAttribute,
  125. aNotify);
  126. NS_ENSURE_SUCCESS(rv, rv);
  127. if (aAttribute == nsGkAtoms::href) {
  128. if (aNamespaceID == kNameSpaceID_None) {
  129. UnlinkHrefTarget(true);
  130. // After unsetting href, we may still have xlink:href, so we should
  131. // try to add it back.
  132. const nsAttrValue* xlinkHref =
  133. mAttrsAndChildren.GetAttr(nsGkAtoms::href, kNameSpaceID_XLink);
  134. if (xlinkHref) {
  135. UpdateHrefTarget(GetParent(), xlinkHref->GetStringValue());
  136. }
  137. } else if (!HasAttr(kNameSpaceID_None, nsGkAtoms::href)) {
  138. UnlinkHrefTarget(true);
  139. } // else: we unset xlink:href, but we still have href attribute, so keep
  140. // the target linking to href.
  141. }
  142. return NS_OK;
  143. }
  144. //----------------------------------------------------------------------
  145. // nsSVGElement methods
  146. nsSVGElement::StringAttributesInfo
  147. SVGMPathElement::GetStringInfo()
  148. {
  149. return StringAttributesInfo(mStringAttributes, sStringInfo,
  150. ArrayLength(sStringInfo));
  151. }
  152. //----------------------------------------------------------------------
  153. // nsIMutationObserver methods
  154. void
  155. SVGMPathElement::AttributeChanged(nsIDocument* aDocument,
  156. Element* aElement,
  157. int32_t aNameSpaceID,
  158. nsIAtom* aAttribute,
  159. int32_t aModType,
  160. const nsAttrValue* aOldValue)
  161. {
  162. if (aNameSpaceID == kNameSpaceID_None) {
  163. if (aAttribute == nsGkAtoms::d) {
  164. NotifyParentOfMpathChange(GetParent());
  165. }
  166. }
  167. }
  168. //----------------------------------------------------------------------
  169. // Public helper methods
  170. SVGPathElement*
  171. SVGMPathElement::GetReferencedPath()
  172. {
  173. if (!HasAttr(kNameSpaceID_XLink, nsGkAtoms::href) &&
  174. !HasAttr(kNameSpaceID_None, nsGkAtoms::href)) {
  175. MOZ_ASSERT(!mHrefTarget.get(),
  176. "We shouldn't have a href target "
  177. "if we don't have an xlink:href or href attribute");
  178. return nullptr;
  179. }
  180. nsIContent* genericTarget = mHrefTarget.get();
  181. if (genericTarget && genericTarget->IsSVGElement(nsGkAtoms::path)) {
  182. return static_cast<SVGPathElement*>(genericTarget);
  183. }
  184. return nullptr;
  185. }
  186. //----------------------------------------------------------------------
  187. // Protected helper methods
  188. void
  189. SVGMPathElement::UpdateHrefTarget(nsIContent* aParent,
  190. const nsAString& aHrefStr)
  191. {
  192. nsCOMPtr<nsIURI> targetURI;
  193. nsCOMPtr<nsIURI> baseURI = GetBaseURI();
  194. nsContentUtils::NewURIWithDocumentCharset(getter_AddRefs(targetURI),
  195. aHrefStr, OwnerDoc(), baseURI);
  196. // Stop observing old target (if any)
  197. if (mHrefTarget.get()) {
  198. mHrefTarget.get()->RemoveMutationObserver(this);
  199. }
  200. if (aParent) {
  201. // Pass in |aParent| instead of |this| -- first argument is only used
  202. // for a call to GetComposedDoc(), and |this| might not have a current
  203. // document yet (if our caller is BindToTree).
  204. mHrefTarget.Reset(aParent, targetURI);
  205. } else {
  206. // if we don't have a parent, then there's no animateMotion element
  207. // depending on our target, so there's no point tracking it right now.
  208. mHrefTarget.Unlink();
  209. }
  210. // Start observing new target (if any)
  211. if (mHrefTarget.get()) {
  212. mHrefTarget.get()->AddMutationObserver(this);
  213. }
  214. NotifyParentOfMpathChange(aParent);
  215. }
  216. void
  217. SVGMPathElement::UnlinkHrefTarget(bool aNotifyParent)
  218. {
  219. // Stop observing old target (if any)
  220. if (mHrefTarget.get()) {
  221. mHrefTarget.get()->RemoveMutationObserver(this);
  222. }
  223. mHrefTarget.Unlink();
  224. if (aNotifyParent) {
  225. NotifyParentOfMpathChange(GetParent());
  226. }
  227. }
  228. void
  229. SVGMPathElement::NotifyParentOfMpathChange(nsIContent* aParent)
  230. {
  231. if (aParent && aParent->IsSVGElement(nsGkAtoms::animateMotion)) {
  232. SVGAnimateMotionElement* animateMotionParent =
  233. static_cast<SVGAnimateMotionElement*>(aParent);
  234. animateMotionParent->MpathChanged();
  235. AnimationNeedsResample();
  236. }
  237. }
  238. } // namespace dom
  239. } // namespace mozilla