SVGScriptElement.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 "nsGkAtoms.h"
  6. #include "nsNetUtil.h"
  7. #include "nsContentUtils.h"
  8. #include "mozilla/dom/SVGScriptElement.h"
  9. #include "mozilla/dom/SVGScriptElementBinding.h"
  10. NS_IMPL_NS_NEW_NAMESPACED_SVG_ELEMENT_CHECK_PARSER(Script)
  11. namespace mozilla {
  12. namespace dom {
  13. JSObject*
  14. SVGScriptElement::WrapNode(JSContext *aCx, JS::Handle<JSObject*> aGivenProto)
  15. {
  16. return SVGScriptElementBinding::Wrap(aCx, this, aGivenProto);
  17. }
  18. nsSVGElement::StringInfo SVGScriptElement::sStringInfo[2] =
  19. {
  20. { &nsGkAtoms::href, kNameSpaceID_None, false },
  21. { &nsGkAtoms::href, kNameSpaceID_XLink, false }
  22. };
  23. //----------------------------------------------------------------------
  24. // nsISupports methods
  25. NS_IMPL_ISUPPORTS_INHERITED(SVGScriptElement, SVGScriptElementBase,
  26. nsIDOMNode, nsIDOMElement,
  27. nsIDOMSVGElement,
  28. nsIScriptLoaderObserver,
  29. nsIScriptElement, nsIMutationObserver)
  30. //----------------------------------------------------------------------
  31. // Implementation
  32. SVGScriptElement::SVGScriptElement(already_AddRefed<mozilla::dom::NodeInfo>& aNodeInfo,
  33. FromParser aFromParser)
  34. : SVGScriptElementBase(aNodeInfo)
  35. , ScriptElement(aFromParser)
  36. {
  37. AddMutationObserver(this);
  38. }
  39. SVGScriptElement::~SVGScriptElement()
  40. {
  41. }
  42. //----------------------------------------------------------------------
  43. // nsIDOMNode methods
  44. nsresult
  45. SVGScriptElement::Clone(mozilla::dom::NodeInfo *aNodeInfo, nsINode **aResult) const
  46. {
  47. *aResult = nullptr;
  48. already_AddRefed<mozilla::dom::NodeInfo> ni = RefPtr<mozilla::dom::NodeInfo>(aNodeInfo).forget();
  49. SVGScriptElement* it = new SVGScriptElement(ni, NOT_FROM_PARSER);
  50. nsCOMPtr<nsINode> kungFuDeathGrip = it;
  51. nsresult rv1 = it->Init();
  52. nsresult rv2 = const_cast<SVGScriptElement*>(this)->CopyInnerTo(it);
  53. NS_ENSURE_SUCCESS(rv1, rv1);
  54. NS_ENSURE_SUCCESS(rv2, rv2);
  55. // The clone should be marked evaluated if we are.
  56. it->mAlreadyStarted = mAlreadyStarted;
  57. it->mLineNumber = mLineNumber;
  58. it->mMalformed = mMalformed;
  59. kungFuDeathGrip.swap(*aResult);
  60. return NS_OK;
  61. }
  62. //----------------------------------------------------------------------
  63. void
  64. SVGScriptElement::GetType(nsAString & aType)
  65. {
  66. GetScriptType(aType);
  67. }
  68. void
  69. SVGScriptElement::SetType(const nsAString & aType, ErrorResult& rv)
  70. {
  71. rv = SetAttr(kNameSpaceID_None, nsGkAtoms::type, aType, true);
  72. }
  73. void
  74. SVGScriptElement::GetCrossOrigin(nsAString & aCrossOrigin)
  75. {
  76. // Null for both missing and invalid defaults is ok, since we
  77. // always parse to an enum value, so we don't need an invalid
  78. // default, and we _want_ the missing default to be null.
  79. GetEnumAttr(nsGkAtoms::crossorigin, nullptr, aCrossOrigin);
  80. }
  81. void
  82. SVGScriptElement::SetCrossOrigin(const nsAString & aCrossOrigin,
  83. ErrorResult& aError)
  84. {
  85. SetOrRemoveNullableStringAttr(nsGkAtoms::crossorigin, aCrossOrigin, aError);
  86. }
  87. already_AddRefed<SVGAnimatedString>
  88. SVGScriptElement::Href()
  89. {
  90. return mStringAttributes[HREF].IsExplicitlySet()
  91. ? mStringAttributes[HREF].ToDOMAnimatedString(this)
  92. : mStringAttributes[XLINK_HREF].ToDOMAnimatedString(this);
  93. }
  94. //----------------------------------------------------------------------
  95. // nsIScriptElement methods
  96. bool
  97. SVGScriptElement::GetScriptType(nsAString& type)
  98. {
  99. return GetAttr(kNameSpaceID_None, nsGkAtoms::type, type);
  100. }
  101. void
  102. SVGScriptElement::GetScriptText(nsAString& text)
  103. {
  104. nsContentUtils::GetNodeTextContent(this, false, text);
  105. }
  106. void
  107. SVGScriptElement::GetScriptCharset(nsAString& charset)
  108. {
  109. charset.Truncate();
  110. }
  111. void
  112. SVGScriptElement::FreezeExecutionAttrs(nsIDocument* aOwnerDoc)
  113. {
  114. if (mFrozen) {
  115. return;
  116. }
  117. if (mStringAttributes[HREF].IsExplicitlySet() ||
  118. mStringAttributes[XLINK_HREF].IsExplicitlySet()) {
  119. // variation of this code in nsHTMLScriptElement - check if changes
  120. // need to be transfered when modifying
  121. nsAutoString src;
  122. if (mStringAttributes[HREF].IsExplicitlySet()) {
  123. mStringAttributes[HREF].GetAnimValue(src, this);
  124. } else {
  125. mStringAttributes[XLINK_HREF].GetAnimValue(src, this);
  126. }
  127. // Empty src should be treated as invalid URL.
  128. if (!src.IsEmpty()) {
  129. nsCOMPtr<nsIURI> baseURI = GetBaseURI();
  130. NS_NewURI(getter_AddRefs(mUri), src, nullptr, baseURI);
  131. }
  132. // At this point mUri will be null for invalid URLs.
  133. mExternal = true;
  134. }
  135. mFrozen = true;
  136. }
  137. //----------------------------------------------------------------------
  138. // ScriptElement methods
  139. bool
  140. SVGScriptElement::HasScriptContent()
  141. {
  142. return (mFrozen ? mExternal
  143. : mStringAttributes[HREF].IsExplicitlySet() ||
  144. mStringAttributes[XLINK_HREF].IsExplicitlySet()) ||
  145. nsContentUtils::HasNonEmptyTextContent(this);
  146. }
  147. //----------------------------------------------------------------------
  148. // nsSVGElement methods
  149. nsSVGElement::StringAttributesInfo
  150. SVGScriptElement::GetStringInfo()
  151. {
  152. return StringAttributesInfo(mStringAttributes, sStringInfo,
  153. ArrayLength(sStringInfo));
  154. }
  155. //----------------------------------------------------------------------
  156. // nsIContent methods
  157. nsresult
  158. SVGScriptElement::BindToTree(nsIDocument* aDocument, nsIContent* aParent,
  159. nsIContent* aBindingParent,
  160. bool aCompileEventHandlers)
  161. {
  162. nsresult rv = SVGScriptElementBase::BindToTree(aDocument, aParent,
  163. aBindingParent,
  164. aCompileEventHandlers);
  165. NS_ENSURE_SUCCESS(rv, rv);
  166. if (aDocument) {
  167. MaybeProcessScript();
  168. }
  169. return NS_OK;
  170. }
  171. nsresult
  172. SVGScriptElement::AfterSetAttr(int32_t aNamespaceID, nsIAtom* aName,
  173. const nsAttrValue* aValue,
  174. const nsAttrValue* aOldValue, bool aNotify)
  175. {
  176. if ((aNamespaceID == kNameSpaceID_XLink ||
  177. aNamespaceID == kNameSpaceID_None) &&
  178. aName == nsGkAtoms::href) {
  179. MaybeProcessScript();
  180. }
  181. return SVGScriptElementBase::AfterSetAttr(aNamespaceID, aName,
  182. aValue, aOldValue, aNotify);
  183. }
  184. bool
  185. SVGScriptElement::ParseAttribute(int32_t aNamespaceID,
  186. nsIAtom* aAttribute,
  187. const nsAString& aValue,
  188. nsAttrValue& aResult)
  189. {
  190. if (aNamespaceID == kNameSpaceID_None &&
  191. aAttribute == nsGkAtoms::crossorigin) {
  192. ParseCORSValue(aValue, aResult);
  193. return true;
  194. }
  195. return SVGScriptElementBase::ParseAttribute(aNamespaceID, aAttribute,
  196. aValue, aResult);
  197. }
  198. CORSMode
  199. SVGScriptElement::GetCORSMode() const
  200. {
  201. return AttrValueToCORSMode(GetParsedAttr(nsGkAtoms::crossorigin));
  202. }
  203. } // namespace dom
  204. } // namespace mozilla