nsSVGElement.cpp 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756
  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/DebugOnly.h"
  7. #include "mozilla/Unused.h"
  8. #include "nsSVGElement.h"
  9. #include "mozilla/dom/SVGSVGElement.h"
  10. #include "mozilla/dom/SVGTests.h"
  11. #include "nsContentUtils.h"
  12. #include "nsICSSDeclaration.h"
  13. #include "nsIDocument.h"
  14. #include "nsIDOMMutationEvent.h"
  15. #include "nsSVGPathGeometryElement.h"
  16. #include "mozilla/InternalMutationEvent.h"
  17. #include "mozAutoDocUpdate.h"
  18. #include "nsError.h"
  19. #include "nsIPresShell.h"
  20. #include "nsGkAtoms.h"
  21. #include "mozilla/css/StyleRule.h"
  22. #include "nsRuleWalker.h"
  23. #include "mozilla/css/Declaration.h"
  24. #include "nsCSSProps.h"
  25. #include "nsCSSParser.h"
  26. #include "mozilla/EventListenerManager.h"
  27. #include "nsLayoutUtils.h"
  28. #include "nsSVGAnimatedTransformList.h"
  29. #include "nsSVGLength2.h"
  30. #include "nsSVGNumber2.h"
  31. #include "nsSVGNumberPair.h"
  32. #include "nsSVGInteger.h"
  33. #include "nsSVGIntegerPair.h"
  34. #include "nsSVGAngle.h"
  35. #include "nsSVGBoolean.h"
  36. #include "nsSVGEnum.h"
  37. #include "nsSVGViewBox.h"
  38. #include "nsSVGString.h"
  39. #include "mozilla/dom/SVGAnimatedEnumeration.h"
  40. #include "SVGAnimatedNumberList.h"
  41. #include "SVGAnimatedLengthList.h"
  42. #include "SVGAnimatedPointList.h"
  43. #include "SVGAnimatedPathSegList.h"
  44. #include "SVGContentUtils.h"
  45. #include "nsIFrame.h"
  46. #include "nsQueryObject.h"
  47. #include <stdarg.h>
  48. #include "nsSMILMappedAttribute.h"
  49. #include "SVGMotionSMILAttr.h"
  50. #include "nsAttrValueOrString.h"
  51. #include "nsSMILAnimationController.h"
  52. #include "mozilla/dom/SVGElementBinding.h"
  53. #include "mozilla/Unused.h"
  54. #include "mozilla/RestyleManagerHandle.h"
  55. #include "mozilla/RestyleManagerHandleInlines.h"
  56. using namespace mozilla;
  57. using namespace mozilla::dom;
  58. // This is needed to ensure correct handling of calls to the
  59. // vararg-list methods in this file:
  60. // nsSVGElement::GetAnimated{Length,Number,Integer}Values
  61. // See bug 547964 for details:
  62. static_assert(sizeof(void*) == sizeof(nullptr),
  63. "nullptr should be the correct size");
  64. nsresult
  65. NS_NewSVGElement(Element **aResult, already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo)
  66. {
  67. RefPtr<nsSVGElement> it = new nsSVGElement(aNodeInfo);
  68. nsresult rv = it->Init();
  69. if (NS_FAILED(rv)) {
  70. return rv;
  71. }
  72. it.forget(aResult);
  73. return rv;
  74. }
  75. NS_IMPL_ELEMENT_CLONE_WITH_INIT(nsSVGElement)
  76. nsSVGEnumMapping nsSVGElement::sSVGUnitTypesMap[] = {
  77. {&nsGkAtoms::userSpaceOnUse, SVG_UNIT_TYPE_USERSPACEONUSE},
  78. {&nsGkAtoms::objectBoundingBox, SVG_UNIT_TYPE_OBJECTBOUNDINGBOX},
  79. {nullptr, 0}
  80. };
  81. nsSVGElement::nsSVGElement(already_AddRefed<mozilla::dom::NodeInfo>& aNodeInfo)
  82. : nsSVGElementBase(aNodeInfo)
  83. {
  84. }
  85. JSObject*
  86. nsSVGElement::WrapNode(JSContext *aCx, JS::Handle<JSObject*> aGivenProto)
  87. {
  88. return SVGElementBinding::Wrap(aCx, this, aGivenProto);
  89. }
  90. //----------------------------------------------------------------------
  91. NS_IMETHODIMP
  92. nsSVGElement::GetSVGClassName(nsISupports** aClassName)
  93. {
  94. *aClassName = ClassName().take();
  95. return NS_OK;
  96. }
  97. NS_IMETHODIMP
  98. nsSVGElement::GetStyle(nsIDOMCSSStyleDeclaration** aStyle)
  99. {
  100. NS_ADDREF(*aStyle = Style());
  101. return NS_OK;
  102. }
  103. //----------------------------------------------------------------------
  104. // nsSVGElement methods
  105. void
  106. nsSVGElement::DidAnimateClass()
  107. {
  108. nsAutoString src;
  109. mClassAttribute.GetAnimValue(src, this);
  110. if (!mClassAnimAttr) {
  111. mClassAnimAttr = new nsAttrValue();
  112. }
  113. mClassAnimAttr->ParseAtomArray(src);
  114. nsIPresShell* shell = OwnerDoc()->GetShell();
  115. if (shell) {
  116. shell->RestyleForAnimation(this, eRestyle_Self);
  117. }
  118. }
  119. nsresult
  120. nsSVGElement::Init()
  121. {
  122. // Set up length attributes - can't do this in the constructor
  123. // because we can't do a virtual call at that point
  124. LengthAttributesInfo lengthInfo = GetLengthInfo();
  125. uint32_t i;
  126. for (i = 0; i < lengthInfo.mLengthCount; i++) {
  127. lengthInfo.Reset(i);
  128. }
  129. NumberAttributesInfo numberInfo = GetNumberInfo();
  130. for (i = 0; i < numberInfo.mNumberCount; i++) {
  131. numberInfo.Reset(i);
  132. }
  133. NumberPairAttributesInfo numberPairInfo = GetNumberPairInfo();
  134. for (i = 0; i < numberPairInfo.mNumberPairCount; i++) {
  135. numberPairInfo.Reset(i);
  136. }
  137. IntegerAttributesInfo integerInfo = GetIntegerInfo();
  138. for (i = 0; i < integerInfo.mIntegerCount; i++) {
  139. integerInfo.Reset(i);
  140. }
  141. IntegerPairAttributesInfo integerPairInfo = GetIntegerPairInfo();
  142. for (i = 0; i < integerPairInfo.mIntegerPairCount; i++) {
  143. integerPairInfo.Reset(i);
  144. }
  145. AngleAttributesInfo angleInfo = GetAngleInfo();
  146. for (i = 0; i < angleInfo.mAngleCount; i++) {
  147. angleInfo.Reset(i);
  148. }
  149. BooleanAttributesInfo booleanInfo = GetBooleanInfo();
  150. for (i = 0; i < booleanInfo.mBooleanCount; i++) {
  151. booleanInfo.Reset(i);
  152. }
  153. EnumAttributesInfo enumInfo = GetEnumInfo();
  154. for (i = 0; i < enumInfo.mEnumCount; i++) {
  155. enumInfo.Reset(i);
  156. }
  157. nsSVGViewBox *viewBox = GetViewBox();
  158. if (viewBox) {
  159. viewBox->Init();
  160. }
  161. SVGAnimatedPreserveAspectRatio *preserveAspectRatio =
  162. GetPreserveAspectRatio();
  163. if (preserveAspectRatio) {
  164. preserveAspectRatio->Init();
  165. }
  166. LengthListAttributesInfo lengthListInfo = GetLengthListInfo();
  167. for (i = 0; i < lengthListInfo.mLengthListCount; i++) {
  168. lengthListInfo.Reset(i);
  169. }
  170. NumberListAttributesInfo numberListInfo = GetNumberListInfo();
  171. for (i = 0; i < numberListInfo.mNumberListCount; i++) {
  172. numberListInfo.Reset(i);
  173. }
  174. // No need to reset SVGPointList since the default value is always the same
  175. // (an empty list).
  176. // No need to reset SVGPathData since the default value is always the same
  177. // (an empty list).
  178. StringAttributesInfo stringInfo = GetStringInfo();
  179. for (i = 0; i < stringInfo.mStringCount; i++) {
  180. stringInfo.Reset(i);
  181. }
  182. return NS_OK;
  183. }
  184. //----------------------------------------------------------------------
  185. // nsISupports methods
  186. NS_IMPL_ISUPPORTS_INHERITED(nsSVGElement, nsSVGElementBase,
  187. nsIDOMNode, nsIDOMElement,
  188. nsIDOMSVGElement)
  189. //----------------------------------------------------------------------
  190. // Implementation
  191. //----------------------------------------------------------------------
  192. // nsIContent methods
  193. nsresult
  194. nsSVGElement::BindToTree(nsIDocument* aDocument, nsIContent* aParent,
  195. nsIContent* aBindingParent,
  196. bool aCompileEventHandlers)
  197. {
  198. nsresult rv = nsSVGElementBase::BindToTree(aDocument, aParent,
  199. aBindingParent,
  200. aCompileEventHandlers);
  201. NS_ENSURE_SUCCESS(rv, rv);
  202. if (!MayHaveStyle()) {
  203. return NS_OK;
  204. }
  205. const nsAttrValue* oldVal = mAttrsAndChildren.GetAttr(nsGkAtoms::style);
  206. if (oldVal && oldVal->Type() == nsAttrValue::eCSSDeclaration) {
  207. // we need to force a reparse because the baseURI of the document
  208. // may have changed, and in particular because we may be clones of
  209. // XBL anonymous content now being bound to the document we should
  210. // render in and due to the hacky way in which we implement the
  211. // interaction of XBL and SVG resources. Once we have a sane
  212. // ownerDocument on XBL anonymous content, this can all go away.
  213. nsAttrValue attrValue;
  214. nsAutoString stringValue;
  215. oldVal->ToString(stringValue);
  216. // Force in data doc, since we already have a style rule
  217. ParseStyleAttribute(stringValue, attrValue, true);
  218. // Don't bother going through SetInlineStyleDeclaration; we don't
  219. // want to fire off mutation events or document notifications anyway
  220. bool oldValueSet;
  221. rv = mAttrsAndChildren.SetAndSwapAttr(nsGkAtoms::style, attrValue,
  222. &oldValueSet);
  223. NS_ENSURE_SUCCESS(rv, rv);
  224. }
  225. return NS_OK;
  226. }
  227. nsresult
  228. nsSVGElement::AfterSetAttr(int32_t aNamespaceID, nsIAtom* aName,
  229. const nsAttrValue* aValue,
  230. const nsAttrValue* aOldValue, bool aNotify)
  231. {
  232. // We don't currently use nsMappedAttributes within SVG. If this changes, we
  233. // need to be very careful because some nsAttrValues used by SVG point to
  234. // member data of SVG elements and if an nsAttrValue outlives the SVG element
  235. // whose data it points to (by virtue of being stored in
  236. // mAttrsAndChildren->mMappedAttributes, meaning it's shared between
  237. // elements), the pointer will dangle. See bug 724680.
  238. MOZ_ASSERT(!mAttrsAndChildren.HasMappedAttrs(),
  239. "Unexpected use of nsMappedAttributes within SVG");
  240. // If this is an svg presentation attribute we need to map it into
  241. // the content stylerule.
  242. // XXX For some reason incremental mapping doesn't work, so for now
  243. // just delete the style rule and lazily reconstruct it in
  244. // GetContentStyleRule()
  245. if (aNamespaceID == kNameSpaceID_None && IsAttributeMapped(aName)) {
  246. mContentStyleRule = nullptr;
  247. }
  248. if (IsEventAttributeName(aName) && aValue) {
  249. MOZ_ASSERT(aValue->Type() == nsAttrValue::eString,
  250. "Expected string value for script body");
  251. nsresult rv = SetEventHandler(GetEventNameForAttr(aName),
  252. aValue->GetStringValue());
  253. NS_ENSURE_SUCCESS(rv, rv);
  254. }
  255. return nsSVGElementBase::AfterSetAttr(aNamespaceID, aName, aValue, aOldValue,
  256. aNotify);
  257. }
  258. bool
  259. nsSVGElement::ParseAttribute(int32_t aNamespaceID,
  260. nsIAtom* aAttribute,
  261. const nsAString& aValue,
  262. nsAttrValue& aResult)
  263. {
  264. nsresult rv = NS_OK;
  265. bool foundMatch = false;
  266. bool didSetResult = false;
  267. if (aNamespaceID == kNameSpaceID_None) {
  268. // Check for nsSVGLength2 attribute
  269. LengthAttributesInfo lengthInfo = GetLengthInfo();
  270. uint32_t i;
  271. for (i = 0; i < lengthInfo.mLengthCount; i++) {
  272. if (aAttribute == *lengthInfo.mLengthInfo[i].mName) {
  273. rv = lengthInfo.mLengths[i].SetBaseValueString(aValue, this, false);
  274. if (NS_FAILED(rv)) {
  275. lengthInfo.Reset(i);
  276. } else {
  277. aResult.SetTo(lengthInfo.mLengths[i], &aValue);
  278. didSetResult = true;
  279. }
  280. foundMatch = true;
  281. break;
  282. }
  283. }
  284. if (!foundMatch) {
  285. // Check for SVGAnimatedLengthList attribute
  286. LengthListAttributesInfo lengthListInfo = GetLengthListInfo();
  287. for (i = 0; i < lengthListInfo.mLengthListCount; i++) {
  288. if (aAttribute == *lengthListInfo.mLengthListInfo[i].mName) {
  289. rv = lengthListInfo.mLengthLists[i].SetBaseValueString(aValue);
  290. if (NS_FAILED(rv)) {
  291. lengthListInfo.Reset(i);
  292. } else {
  293. aResult.SetTo(lengthListInfo.mLengthLists[i].GetBaseValue(),
  294. &aValue);
  295. didSetResult = true;
  296. }
  297. foundMatch = true;
  298. break;
  299. }
  300. }
  301. }
  302. if (!foundMatch) {
  303. // Check for SVGAnimatedNumberList attribute
  304. NumberListAttributesInfo numberListInfo = GetNumberListInfo();
  305. for (i = 0; i < numberListInfo.mNumberListCount; i++) {
  306. if (aAttribute == *numberListInfo.mNumberListInfo[i].mName) {
  307. rv = numberListInfo.mNumberLists[i].SetBaseValueString(aValue);
  308. if (NS_FAILED(rv)) {
  309. numberListInfo.Reset(i);
  310. } else {
  311. aResult.SetTo(numberListInfo.mNumberLists[i].GetBaseValue(),
  312. &aValue);
  313. didSetResult = true;
  314. }
  315. foundMatch = true;
  316. break;
  317. }
  318. }
  319. }
  320. if (!foundMatch) {
  321. // Check for SVGAnimatedPointList attribute
  322. if (GetPointListAttrName() == aAttribute) {
  323. SVGAnimatedPointList* pointList = GetAnimatedPointList();
  324. if (pointList) {
  325. pointList->SetBaseValueString(aValue);
  326. // The spec says we parse everything up to the failure, so we DON'T
  327. // need to check the result of SetBaseValueString or call
  328. // pointList->ClearBaseValue() if it fails
  329. aResult.SetTo(pointList->GetBaseValue(), &aValue);
  330. didSetResult = true;
  331. foundMatch = true;
  332. }
  333. }
  334. }
  335. if (!foundMatch) {
  336. // Check for SVGAnimatedPathSegList attribute
  337. if (GetPathDataAttrName() == aAttribute) {
  338. SVGAnimatedPathSegList* segList = GetAnimPathSegList();
  339. if (segList) {
  340. segList->SetBaseValueString(aValue);
  341. // The spec says we parse everything up to the failure, so we DON'T
  342. // need to check the result of SetBaseValueString or call
  343. // segList->ClearBaseValue() if it fails
  344. aResult.SetTo(segList->GetBaseValue(), &aValue);
  345. didSetResult = true;
  346. foundMatch = true;
  347. }
  348. }
  349. }
  350. if (!foundMatch) {
  351. // Check for nsSVGNumber2 attribute
  352. NumberAttributesInfo numberInfo = GetNumberInfo();
  353. for (i = 0; i < numberInfo.mNumberCount; i++) {
  354. if (aAttribute == *numberInfo.mNumberInfo[i].mName) {
  355. rv = numberInfo.mNumbers[i].SetBaseValueString(aValue, this);
  356. if (NS_FAILED(rv)) {
  357. numberInfo.Reset(i);
  358. } else {
  359. aResult.SetTo(numberInfo.mNumbers[i].GetBaseValue(), &aValue);
  360. didSetResult = true;
  361. }
  362. foundMatch = true;
  363. break;
  364. }
  365. }
  366. }
  367. if (!foundMatch) {
  368. // Check for nsSVGNumberPair attribute
  369. NumberPairAttributesInfo numberPairInfo = GetNumberPairInfo();
  370. for (i = 0; i < numberPairInfo.mNumberPairCount; i++) {
  371. if (aAttribute == *numberPairInfo.mNumberPairInfo[i].mName) {
  372. rv = numberPairInfo.mNumberPairs[i].SetBaseValueString(aValue, this);
  373. if (NS_FAILED(rv)) {
  374. numberPairInfo.Reset(i);
  375. } else {
  376. aResult.SetTo(numberPairInfo.mNumberPairs[i], &aValue);
  377. didSetResult = true;
  378. }
  379. foundMatch = true;
  380. break;
  381. }
  382. }
  383. }
  384. if (!foundMatch) {
  385. // Check for nsSVGInteger attribute
  386. IntegerAttributesInfo integerInfo = GetIntegerInfo();
  387. for (i = 0; i < integerInfo.mIntegerCount; i++) {
  388. if (aAttribute == *integerInfo.mIntegerInfo[i].mName) {
  389. rv = integerInfo.mIntegers[i].SetBaseValueString(aValue, this);
  390. if (NS_FAILED(rv)) {
  391. integerInfo.Reset(i);
  392. } else {
  393. aResult.SetTo(integerInfo.mIntegers[i].GetBaseValue(), &aValue);
  394. didSetResult = true;
  395. }
  396. foundMatch = true;
  397. break;
  398. }
  399. }
  400. }
  401. if (!foundMatch) {
  402. // Check for nsSVGIntegerPair attribute
  403. IntegerPairAttributesInfo integerPairInfo = GetIntegerPairInfo();
  404. for (i = 0; i < integerPairInfo.mIntegerPairCount; i++) {
  405. if (aAttribute == *integerPairInfo.mIntegerPairInfo[i].mName) {
  406. rv =
  407. integerPairInfo.mIntegerPairs[i].SetBaseValueString(aValue, this);
  408. if (NS_FAILED(rv)) {
  409. integerPairInfo.Reset(i);
  410. } else {
  411. aResult.SetTo(integerPairInfo.mIntegerPairs[i], &aValue);
  412. didSetResult = true;
  413. }
  414. foundMatch = true;
  415. break;
  416. }
  417. }
  418. }
  419. if (!foundMatch) {
  420. // Check for nsSVGAngle attribute
  421. AngleAttributesInfo angleInfo = GetAngleInfo();
  422. for (i = 0; i < angleInfo.mAngleCount; i++) {
  423. if (aAttribute == *angleInfo.mAngleInfo[i].mName) {
  424. rv = angleInfo.mAngles[i].SetBaseValueString(aValue, this, false);
  425. if (NS_FAILED(rv)) {
  426. angleInfo.Reset(i);
  427. } else {
  428. aResult.SetTo(angleInfo.mAngles[i], &aValue);
  429. didSetResult = true;
  430. }
  431. foundMatch = true;
  432. break;
  433. }
  434. }
  435. }
  436. if (!foundMatch) {
  437. // Check for nsSVGBoolean attribute
  438. BooleanAttributesInfo booleanInfo = GetBooleanInfo();
  439. for (i = 0; i < booleanInfo.mBooleanCount; i++) {
  440. if (aAttribute == *booleanInfo.mBooleanInfo[i].mName) {
  441. nsIAtom *valAtom = NS_GetStaticAtom(aValue);
  442. rv = valAtom ? booleanInfo.mBooleans[i].SetBaseValueAtom(valAtom, this) :
  443. NS_ERROR_DOM_SYNTAX_ERR;
  444. if (NS_FAILED(rv)) {
  445. booleanInfo.Reset(i);
  446. } else {
  447. aResult.SetTo(valAtom);
  448. didSetResult = true;
  449. }
  450. foundMatch = true;
  451. break;
  452. }
  453. }
  454. }
  455. if (!foundMatch) {
  456. // Check for nsSVGEnum attribute
  457. EnumAttributesInfo enumInfo = GetEnumInfo();
  458. for (i = 0; i < enumInfo.mEnumCount; i++) {
  459. if (aAttribute == *enumInfo.mEnumInfo[i].mName) {
  460. nsCOMPtr<nsIAtom> valAtom = NS_Atomize(aValue);
  461. rv = enumInfo.mEnums[i].SetBaseValueAtom(valAtom, this);
  462. if (NS_FAILED(rv)) {
  463. enumInfo.Reset(i);
  464. } else {
  465. aResult.SetTo(valAtom);
  466. didSetResult = true;
  467. }
  468. foundMatch = true;
  469. break;
  470. }
  471. }
  472. }
  473. if (!foundMatch) {
  474. // Check for conditional processing attributes
  475. nsCOMPtr<SVGTests> tests = do_QueryObject(this);
  476. if (tests && tests->ParseConditionalProcessingAttribute(
  477. aAttribute, aValue, aResult)) {
  478. foundMatch = true;
  479. }
  480. }
  481. if (!foundMatch) {
  482. // Check for StringList attribute
  483. StringListAttributesInfo stringListInfo = GetStringListInfo();
  484. for (i = 0; i < stringListInfo.mStringListCount; i++) {
  485. if (aAttribute == *stringListInfo.mStringListInfo[i].mName) {
  486. rv = stringListInfo.mStringLists[i].SetValue(aValue);
  487. if (NS_FAILED(rv)) {
  488. stringListInfo.Reset(i);
  489. } else {
  490. aResult.SetTo(stringListInfo.mStringLists[i], &aValue);
  491. didSetResult = true;
  492. }
  493. foundMatch = true;
  494. break;
  495. }
  496. }
  497. }
  498. if (!foundMatch) {
  499. // Check for nsSVGViewBox attribute
  500. if (aAttribute == nsGkAtoms::viewBox) {
  501. nsSVGViewBox* viewBox = GetViewBox();
  502. if (viewBox) {
  503. rv = viewBox->SetBaseValueString(aValue, this, false);
  504. if (NS_FAILED(rv)) {
  505. viewBox->Init();
  506. } else {
  507. aResult.SetTo(*viewBox, &aValue);
  508. didSetResult = true;
  509. }
  510. foundMatch = true;
  511. }
  512. // Check for SVGAnimatedPreserveAspectRatio attribute
  513. } else if (aAttribute == nsGkAtoms::preserveAspectRatio) {
  514. SVGAnimatedPreserveAspectRatio *preserveAspectRatio =
  515. GetPreserveAspectRatio();
  516. if (preserveAspectRatio) {
  517. rv = preserveAspectRatio->SetBaseValueString(aValue, this, false);
  518. if (NS_FAILED(rv)) {
  519. preserveAspectRatio->Init();
  520. } else {
  521. aResult.SetTo(*preserveAspectRatio, &aValue);
  522. didSetResult = true;
  523. }
  524. foundMatch = true;
  525. }
  526. // Check for SVGAnimatedTransformList attribute
  527. } else if (GetTransformListAttrName() == aAttribute) {
  528. // The transform attribute is being set, so we must ensure that the
  529. // nsSVGAnimatedTransformList is/has been allocated:
  530. nsSVGAnimatedTransformList *transformList =
  531. GetAnimatedTransformList(DO_ALLOCATE);
  532. rv = transformList->SetBaseValueString(aValue);
  533. if (NS_FAILED(rv)) {
  534. transformList->ClearBaseValue();
  535. } else {
  536. aResult.SetTo(transformList->GetBaseValue(), &aValue);
  537. didSetResult = true;
  538. }
  539. foundMatch = true;
  540. } else if (aAttribute == nsGkAtoms::tabindex) {
  541. didSetResult = aResult.ParseIntValue(aValue);
  542. foundMatch = true;
  543. }
  544. }
  545. if (aAttribute == nsGkAtoms::_class) {
  546. mClassAttribute.SetBaseValue(aValue, this, false);
  547. aResult.ParseAtomArray(aValue);
  548. return true;
  549. }
  550. }
  551. if (!foundMatch) {
  552. // Check for nsSVGString attribute
  553. StringAttributesInfo stringInfo = GetStringInfo();
  554. for (uint32_t i = 0; i < stringInfo.mStringCount; i++) {
  555. if (aNamespaceID == stringInfo.mStringInfo[i].mNamespaceID &&
  556. aAttribute == *stringInfo.mStringInfo[i].mName) {
  557. stringInfo.mStrings[i].SetBaseValue(aValue, this, false);
  558. foundMatch = true;
  559. break;
  560. }
  561. }
  562. }
  563. if (foundMatch) {
  564. if (NS_FAILED(rv)) {
  565. ReportAttributeParseFailure(OwnerDoc(), aAttribute, aValue);
  566. return false;
  567. }
  568. if (!didSetResult) {
  569. aResult.SetTo(aValue);
  570. }
  571. return true;
  572. }
  573. return nsSVGElementBase::ParseAttribute(aNamespaceID, aAttribute, aValue,
  574. aResult);
  575. }
  576. void
  577. nsSVGElement::UnsetAttrInternal(int32_t aNamespaceID, nsIAtom* aName,
  578. bool aNotify)
  579. {
  580. // XXXbz there's a bunch of redundancy here with AfterSetAttr.
  581. // Maybe consolidate?
  582. if (aNamespaceID == kNameSpaceID_None) {
  583. // If this is an svg presentation attribute, remove rule to force an update
  584. if (IsAttributeMapped(aName))
  585. mContentStyleRule = nullptr;
  586. if (IsEventAttributeName(aName)) {
  587. EventListenerManager* manager = GetExistingListenerManager();
  588. if (manager) {
  589. nsIAtom* eventName = GetEventNameForAttr(aName);
  590. manager->RemoveEventHandler(eventName, EmptyString());
  591. }
  592. return;
  593. }
  594. // Check if this is a length attribute going away
  595. LengthAttributesInfo lenInfo = GetLengthInfo();
  596. for (uint32_t i = 0; i < lenInfo.mLengthCount; i++) {
  597. if (aName == *lenInfo.mLengthInfo[i].mName) {
  598. MaybeSerializeAttrBeforeRemoval(aName, aNotify);
  599. lenInfo.Reset(i);
  600. return;
  601. }
  602. }
  603. // Check if this is a length list attribute going away
  604. LengthListAttributesInfo lengthListInfo = GetLengthListInfo();
  605. for (uint32_t i = 0; i < lengthListInfo.mLengthListCount; i++) {
  606. if (aName == *lengthListInfo.mLengthListInfo[i].mName) {
  607. MaybeSerializeAttrBeforeRemoval(aName, aNotify);
  608. lengthListInfo.Reset(i);
  609. return;
  610. }
  611. }
  612. // Check if this is a number list attribute going away
  613. NumberListAttributesInfo numberListInfo = GetNumberListInfo();
  614. for (uint32_t i = 0; i < numberListInfo.mNumberListCount; i++) {
  615. if (aName == *numberListInfo.mNumberListInfo[i].mName) {
  616. MaybeSerializeAttrBeforeRemoval(aName, aNotify);
  617. numberListInfo.Reset(i);
  618. return;
  619. }
  620. }
  621. // Check if this is a point list attribute going away
  622. if (GetPointListAttrName() == aName) {
  623. SVGAnimatedPointList *pointList = GetAnimatedPointList();
  624. if (pointList) {
  625. MaybeSerializeAttrBeforeRemoval(aName, aNotify);
  626. pointList->ClearBaseValue();
  627. return;
  628. }
  629. }
  630. // Check if this is a path segment list attribute going away
  631. if (GetPathDataAttrName() == aName) {
  632. SVGAnimatedPathSegList *segList = GetAnimPathSegList();
  633. if (segList) {
  634. MaybeSerializeAttrBeforeRemoval(aName, aNotify);
  635. segList->ClearBaseValue();
  636. return;
  637. }
  638. }
  639. // Check if this is a number attribute going away
  640. NumberAttributesInfo numInfo = GetNumberInfo();
  641. for (uint32_t i = 0; i < numInfo.mNumberCount; i++) {
  642. if (aName == *numInfo.mNumberInfo[i].mName) {
  643. numInfo.Reset(i);
  644. return;
  645. }
  646. }
  647. // Check if this is a number pair attribute going away
  648. NumberPairAttributesInfo numPairInfo = GetNumberPairInfo();
  649. for (uint32_t i = 0; i < numPairInfo.mNumberPairCount; i++) {
  650. if (aName == *numPairInfo.mNumberPairInfo[i].mName) {
  651. MaybeSerializeAttrBeforeRemoval(aName, aNotify);
  652. numPairInfo.Reset(i);
  653. return;
  654. }
  655. }
  656. // Check if this is an integer attribute going away
  657. IntegerAttributesInfo intInfo = GetIntegerInfo();
  658. for (uint32_t i = 0; i < intInfo.mIntegerCount; i++) {
  659. if (aName == *intInfo.mIntegerInfo[i].mName) {
  660. intInfo.Reset(i);
  661. return;
  662. }
  663. }
  664. // Check if this is an integer pair attribute going away
  665. IntegerPairAttributesInfo intPairInfo = GetIntegerPairInfo();
  666. for (uint32_t i = 0; i < intPairInfo.mIntegerPairCount; i++) {
  667. if (aName == *intPairInfo.mIntegerPairInfo[i].mName) {
  668. MaybeSerializeAttrBeforeRemoval(aName, aNotify);
  669. intPairInfo.Reset(i);
  670. return;
  671. }
  672. }
  673. // Check if this is an angle attribute going away
  674. AngleAttributesInfo angleInfo = GetAngleInfo();
  675. for (uint32_t i = 0; i < angleInfo.mAngleCount; i++) {
  676. if (aName == *angleInfo.mAngleInfo[i].mName) {
  677. MaybeSerializeAttrBeforeRemoval(aName, aNotify);
  678. angleInfo.Reset(i);
  679. return;
  680. }
  681. }
  682. // Check if this is a boolean attribute going away
  683. BooleanAttributesInfo boolInfo = GetBooleanInfo();
  684. for (uint32_t i = 0; i < boolInfo.mBooleanCount; i++) {
  685. if (aName == *boolInfo.mBooleanInfo[i].mName) {
  686. boolInfo.Reset(i);
  687. return;
  688. }
  689. }
  690. // Check if this is an enum attribute going away
  691. EnumAttributesInfo enumInfo = GetEnumInfo();
  692. for (uint32_t i = 0; i < enumInfo.mEnumCount; i++) {
  693. if (aName == *enumInfo.mEnumInfo[i].mName) {
  694. enumInfo.Reset(i);
  695. return;
  696. }
  697. }
  698. // Check if this is a nsViewBox attribute going away
  699. if (aName == nsGkAtoms::viewBox) {
  700. nsSVGViewBox* viewBox = GetViewBox();
  701. if (viewBox) {
  702. MaybeSerializeAttrBeforeRemoval(aName, aNotify);
  703. viewBox->Init();
  704. return;
  705. }
  706. }
  707. // Check if this is a preserveAspectRatio attribute going away
  708. if (aName == nsGkAtoms::preserveAspectRatio) {
  709. SVGAnimatedPreserveAspectRatio *preserveAspectRatio =
  710. GetPreserveAspectRatio();
  711. if (preserveAspectRatio) {
  712. MaybeSerializeAttrBeforeRemoval(aName, aNotify);
  713. preserveAspectRatio->Init();
  714. return;
  715. }
  716. }
  717. // Check if this is a transform list attribute going away
  718. if (GetTransformListAttrName() == aName) {
  719. nsSVGAnimatedTransformList *transformList = GetAnimatedTransformList();
  720. if (transformList) {
  721. MaybeSerializeAttrBeforeRemoval(aName, aNotify);
  722. transformList->ClearBaseValue();
  723. return;
  724. }
  725. }
  726. // Check for conditional processing attributes
  727. nsCOMPtr<SVGTests> tests = do_QueryObject(this);
  728. if (tests && tests->IsConditionalProcessingAttribute(aName)) {
  729. MaybeSerializeAttrBeforeRemoval(aName, aNotify);
  730. tests->UnsetAttr(aName);
  731. return;
  732. }
  733. // Check if this is a string list attribute going away
  734. StringListAttributesInfo stringListInfo = GetStringListInfo();
  735. for (uint32_t i = 0; i < stringListInfo.mStringListCount; i++) {
  736. if (aName == *stringListInfo.mStringListInfo[i].mName) {
  737. MaybeSerializeAttrBeforeRemoval(aName, aNotify);
  738. stringListInfo.Reset(i);
  739. return;
  740. }
  741. }
  742. if (aName == nsGkAtoms::_class) {
  743. mClassAttribute.Init();
  744. return;
  745. }
  746. }
  747. // Check if this is a string attribute going away
  748. StringAttributesInfo stringInfo = GetStringInfo();
  749. for (uint32_t i = 0; i < stringInfo.mStringCount; i++) {
  750. if (aNamespaceID == stringInfo.mStringInfo[i].mNamespaceID &&
  751. aName == *stringInfo.mStringInfo[i].mName) {
  752. stringInfo.Reset(i);
  753. return;
  754. }
  755. }
  756. }
  757. nsresult
  758. nsSVGElement::UnsetAttr(int32_t aNamespaceID, nsIAtom* aName,
  759. bool aNotify)
  760. {
  761. UnsetAttrInternal(aNamespaceID, aName, aNotify);
  762. return nsSVGElementBase::UnsetAttr(aNamespaceID, aName, aNotify);
  763. }
  764. nsChangeHint
  765. nsSVGElement::GetAttributeChangeHint(const nsIAtom* aAttribute,
  766. int32_t aModType) const
  767. {
  768. nsChangeHint retval =
  769. nsSVGElementBase::GetAttributeChangeHint(aAttribute, aModType);
  770. nsCOMPtr<SVGTests> tests = do_QueryObject(const_cast<nsSVGElement*>(this));
  771. if (tests && tests->IsConditionalProcessingAttribute(aAttribute)) {
  772. // It would be nice to only reconstruct the frame if the value returned by
  773. // SVGTests::PassesConditionalProcessingTests has changed, but we don't
  774. // know that
  775. retval |= nsChangeHint_ReconstructFrame;
  776. }
  777. return retval;
  778. }
  779. bool
  780. nsSVGElement::IsNodeOfType(uint32_t aFlags) const
  781. {
  782. return !(aFlags & ~eCONTENT);
  783. }
  784. NS_IMETHODIMP
  785. nsSVGElement::WalkContentStyleRules(nsRuleWalker* aRuleWalker)
  786. {
  787. #ifdef DEBUG
  788. // printf("nsSVGElement(%p)::WalkContentStyleRules()\n", this);
  789. #endif
  790. if (!mContentStyleRule)
  791. UpdateContentStyleRule();
  792. if (mContentStyleRule) {
  793. css::Declaration* declaration = mContentStyleRule->GetDeclaration();
  794. declaration->SetImmutable();
  795. aRuleWalker->Forward(declaration);
  796. }
  797. return NS_OK;
  798. }
  799. void
  800. nsSVGElement::WalkAnimatedContentStyleRules(nsRuleWalker* aRuleWalker)
  801. {
  802. // Update & walk the animated content style rule, to include style from
  803. // animated mapped attributes. But first, get nsPresContext to check
  804. // whether this is a "no-animation restyle". (This should match the check
  805. // in nsHTMLCSSStyleSheet::RulesMatching(), where we determine whether to
  806. // apply the SMILOverrideStyle.)
  807. RestyleManagerHandle restyleManager =
  808. aRuleWalker->PresContext()->RestyleManager();
  809. MOZ_ASSERT(restyleManager->IsGecko(),
  810. "stylo: Servo-backed style system should not be calling "
  811. "WalkAnimatedContentStyleRules");
  812. if (!restyleManager->AsGecko()->SkipAnimationRules()) {
  813. // update/walk the animated content style rule.
  814. css::StyleRule* animContentStyleRule = GetAnimatedContentStyleRule();
  815. if (!animContentStyleRule) {
  816. UpdateAnimatedContentStyleRule();
  817. animContentStyleRule = GetAnimatedContentStyleRule();
  818. }
  819. if (animContentStyleRule) {
  820. css::Declaration* declaration = animContentStyleRule->GetDeclaration();
  821. declaration->SetImmutable();
  822. aRuleWalker->Forward(declaration);
  823. }
  824. }
  825. }
  826. NS_IMETHODIMP_(bool)
  827. nsSVGElement::IsAttributeMapped(const nsIAtom* name) const
  828. {
  829. if (name == nsGkAtoms::lang) {
  830. return true;
  831. }
  832. return nsSVGElementBase::IsAttributeMapped(name);
  833. }
  834. // PresentationAttributes-FillStroke
  835. /* static */ const Element::MappedAttributeEntry
  836. nsSVGElement::sFillStrokeMap[] = {
  837. { &nsGkAtoms::fill },
  838. { &nsGkAtoms::fill_opacity },
  839. { &nsGkAtoms::fill_rule },
  840. { &nsGkAtoms::paint_order },
  841. { &nsGkAtoms::stroke },
  842. { &nsGkAtoms::stroke_dasharray },
  843. { &nsGkAtoms::stroke_dashoffset },
  844. { &nsGkAtoms::stroke_linecap },
  845. { &nsGkAtoms::stroke_linejoin },
  846. { &nsGkAtoms::stroke_miterlimit },
  847. { &nsGkAtoms::stroke_opacity },
  848. { &nsGkAtoms::stroke_width },
  849. { &nsGkAtoms::vector_effect },
  850. { nullptr }
  851. };
  852. // PresentationAttributes-Graphics
  853. /* static */ const Element::MappedAttributeEntry
  854. nsSVGElement::sGraphicsMap[] = {
  855. { &nsGkAtoms::clip_path },
  856. { &nsGkAtoms::clip_rule },
  857. { &nsGkAtoms::colorInterpolation },
  858. { &nsGkAtoms::cursor },
  859. { &nsGkAtoms::display },
  860. { &nsGkAtoms::filter },
  861. { &nsGkAtoms::image_rendering },
  862. { &nsGkAtoms::mask },
  863. { &nsGkAtoms::opacity },
  864. { &nsGkAtoms::pointer_events },
  865. { &nsGkAtoms::shape_rendering },
  866. { &nsGkAtoms::text_rendering },
  867. { &nsGkAtoms::visibility },
  868. { nullptr }
  869. };
  870. // PresentationAttributes-TextContentElements
  871. /* static */ const Element::MappedAttributeEntry
  872. nsSVGElement::sTextContentElementsMap[] = {
  873. // Properties that we don't support are commented out.
  874. // { &nsGkAtoms::alignment_baseline },
  875. // { &nsGkAtoms::baseline_shift },
  876. { &nsGkAtoms::direction },
  877. { &nsGkAtoms::dominant_baseline },
  878. { &nsGkAtoms::letter_spacing },
  879. { &nsGkAtoms::text_anchor },
  880. { &nsGkAtoms::text_decoration },
  881. { &nsGkAtoms::unicode_bidi },
  882. { &nsGkAtoms::word_spacing },
  883. { &nsGkAtoms::writing_mode },
  884. { nullptr }
  885. };
  886. // PresentationAttributes-FontSpecification
  887. /* static */ const Element::MappedAttributeEntry
  888. nsSVGElement::sFontSpecificationMap[] = {
  889. { &nsGkAtoms::font_family },
  890. { &nsGkAtoms::font_size },
  891. { &nsGkAtoms::font_size_adjust },
  892. { &nsGkAtoms::font_stretch },
  893. { &nsGkAtoms::font_style },
  894. { &nsGkAtoms::font_variant },
  895. { &nsGkAtoms::fontWeight },
  896. { nullptr }
  897. };
  898. // PresentationAttributes-GradientStop
  899. /* static */ const Element::MappedAttributeEntry
  900. nsSVGElement::sGradientStopMap[] = {
  901. { &nsGkAtoms::stop_color },
  902. { &nsGkAtoms::stop_opacity },
  903. { nullptr }
  904. };
  905. // PresentationAttributes-Viewports
  906. /* static */ const Element::MappedAttributeEntry
  907. nsSVGElement::sViewportsMap[] = {
  908. { &nsGkAtoms::overflow },
  909. { &nsGkAtoms::clip },
  910. { nullptr }
  911. };
  912. // PresentationAttributes-Makers
  913. /* static */ const Element::MappedAttributeEntry
  914. nsSVGElement::sMarkersMap[] = {
  915. { &nsGkAtoms::marker_end },
  916. { &nsGkAtoms::marker_mid },
  917. { &nsGkAtoms::marker_start },
  918. { nullptr }
  919. };
  920. // PresentationAttributes-Color
  921. /* static */ const Element::MappedAttributeEntry
  922. nsSVGElement::sColorMap[] = {
  923. { &nsGkAtoms::color },
  924. { nullptr }
  925. };
  926. // PresentationAttributes-Filters
  927. /* static */ const Element::MappedAttributeEntry
  928. nsSVGElement::sFiltersMap[] = {
  929. { &nsGkAtoms::colorInterpolationFilters },
  930. { nullptr }
  931. };
  932. // PresentationAttributes-feFlood
  933. /* static */ const Element::MappedAttributeEntry
  934. nsSVGElement::sFEFloodMap[] = {
  935. { &nsGkAtoms::flood_color },
  936. { &nsGkAtoms::flood_opacity },
  937. { nullptr }
  938. };
  939. // PresentationAttributes-LightingEffects
  940. /* static */ const Element::MappedAttributeEntry
  941. nsSVGElement::sLightingEffectsMap[] = {
  942. { &nsGkAtoms::lighting_color },
  943. { nullptr }
  944. };
  945. // PresentationAttributes-mask
  946. /* static */ const Element::MappedAttributeEntry
  947. nsSVGElement::sMaskMap[] = {
  948. { &nsGkAtoms::mask_type },
  949. { nullptr }
  950. };
  951. //----------------------------------------------------------------------
  952. // nsIDOMElement methods
  953. // forwarded to Element implementations
  954. //----------------------------------------------------------------------
  955. // nsIDOMSVGElement methods
  956. NS_IMETHODIMP
  957. nsSVGElement::GetOwnerSVGElement(nsIDOMSVGElement * *aOwnerSVGElement)
  958. {
  959. NS_IF_ADDREF(*aOwnerSVGElement = GetOwnerSVGElement());
  960. return NS_OK;
  961. }
  962. SVGSVGElement*
  963. nsSVGElement::GetOwnerSVGElement()
  964. {
  965. return GetCtx(); // this may return nullptr
  966. }
  967. NS_IMETHODIMP
  968. nsSVGElement::GetViewportElement(nsIDOMSVGElement * *aViewportElement)
  969. {
  970. nsSVGElement* elem = GetViewportElement();
  971. NS_ADDREF(*aViewportElement = elem);
  972. return NS_OK;
  973. }
  974. nsSVGElement*
  975. nsSVGElement::GetViewportElement()
  976. {
  977. return SVGContentUtils::GetNearestViewportElement(this);
  978. }
  979. already_AddRefed<SVGAnimatedString>
  980. nsSVGElement::ClassName()
  981. {
  982. return mClassAttribute.ToDOMAnimatedString(this);
  983. }
  984. bool
  985. nsSVGElement::IsFocusableInternal(int32_t* aTabIndex, bool)
  986. {
  987. int32_t index = TabIndex();
  988. if (index == -1) {
  989. return false;
  990. }
  991. *aTabIndex = index;
  992. return true;
  993. }
  994. //------------------------------------------------------------------------
  995. // Helper class: MappedAttrParser, for parsing values of mapped attributes
  996. namespace {
  997. class MOZ_STACK_CLASS MappedAttrParser {
  998. public:
  999. MappedAttrParser(css::Loader* aLoader,
  1000. nsIURI* aDocURI,
  1001. already_AddRefed<nsIURI> aBaseURI,
  1002. nsSVGElement* aElement);
  1003. ~MappedAttrParser();
  1004. // Parses a mapped attribute value.
  1005. void ParseMappedAttrValue(nsIAtom* aMappedAttrName,
  1006. const nsAString& aMappedAttrValue);
  1007. // If we've parsed any values for mapped attributes, this method returns
  1008. // a new already_AddRefed css::StyleRule that incorporates the parsed
  1009. // values. Otherwise, this method returns null.
  1010. already_AddRefed<css::StyleRule> CreateStyleRule();
  1011. private:
  1012. // MEMBER DATA
  1013. // -----------
  1014. nsCSSParser mParser;
  1015. // Arguments for nsCSSParser::ParseProperty
  1016. nsIURI* mDocURI;
  1017. nsCOMPtr<nsIURI> mBaseURI;
  1018. // Declaration for storing parsed values (lazily initialized)
  1019. css::Declaration* mDecl;
  1020. // For reporting use counters
  1021. nsSVGElement* mElement;
  1022. };
  1023. MappedAttrParser::MappedAttrParser(css::Loader* aLoader,
  1024. nsIURI* aDocURI,
  1025. already_AddRefed<nsIURI> aBaseURI,
  1026. nsSVGElement* aElement)
  1027. : mParser(aLoader), mDocURI(aDocURI), mBaseURI(aBaseURI),
  1028. mDecl(nullptr), mElement(aElement)
  1029. {
  1030. }
  1031. MappedAttrParser::~MappedAttrParser()
  1032. {
  1033. MOZ_ASSERT(!mDecl,
  1034. "If mDecl was initialized, it should have been converted "
  1035. "into a style rule (and had its pointer cleared)");
  1036. }
  1037. void
  1038. MappedAttrParser::ParseMappedAttrValue(nsIAtom* aMappedAttrName,
  1039. const nsAString& aMappedAttrValue)
  1040. {
  1041. if (!mDecl) {
  1042. mDecl = new css::Declaration();
  1043. mDecl->InitializeEmpty();
  1044. }
  1045. // Get the nsCSSPropertyID ID for our mapped attribute.
  1046. nsCSSPropertyID propertyID =
  1047. nsCSSProps::LookupProperty(nsDependentAtomString(aMappedAttrName),
  1048. CSSEnabledState::eForAllContent);
  1049. if (propertyID != eCSSProperty_UNKNOWN) {
  1050. bool changed = false; // outparam for ParseProperty.
  1051. mParser.ParseProperty(propertyID, aMappedAttrValue, mDocURI, mBaseURI,
  1052. mElement->NodePrincipal(), mDecl, &changed, false, true);
  1053. if (changed) {
  1054. // The normal reporting of use counters by the nsCSSParser won't happen
  1055. // since it doesn't have a sheet.
  1056. if (nsCSSProps::IsShorthand(propertyID)) {
  1057. CSSPROPS_FOR_SHORTHAND_SUBPROPERTIES(subprop, propertyID,
  1058. CSSEnabledState::eForAllContent) {
  1059. UseCounter useCounter = nsCSSProps::UseCounterFor(*subprop);
  1060. if (useCounter != eUseCounter_UNKNOWN) {
  1061. mElement->OwnerDoc()->SetDocumentAndPageUseCounter(useCounter);
  1062. }
  1063. }
  1064. } else {
  1065. UseCounter useCounter = nsCSSProps::UseCounterFor(propertyID);
  1066. if (useCounter != eUseCounter_UNKNOWN) {
  1067. mElement->OwnerDoc()->SetDocumentAndPageUseCounter(useCounter);
  1068. }
  1069. }
  1070. }
  1071. return;
  1072. }
  1073. MOZ_ASSERT(aMappedAttrName == nsGkAtoms::lang,
  1074. "Only 'lang' should be unrecognized!");
  1075. // nsCSSParser doesn't know about 'lang', so we need to handle it specially.
  1076. if (aMappedAttrName == nsGkAtoms::lang) {
  1077. propertyID = eCSSProperty__x_lang;
  1078. nsCSSExpandedDataBlock block;
  1079. mDecl->ExpandTo(&block);
  1080. nsCSSValue cssValue(PromiseFlatString(aMappedAttrValue), eCSSUnit_Ident);
  1081. block.AddLonghandProperty(propertyID, cssValue);
  1082. mDecl->ValueAppended(propertyID);
  1083. mDecl->CompressFrom(&block);
  1084. }
  1085. }
  1086. already_AddRefed<css::StyleRule>
  1087. MappedAttrParser::CreateStyleRule()
  1088. {
  1089. if (!mDecl) {
  1090. return nullptr; // No mapped attributes were parsed
  1091. }
  1092. RefPtr<css::StyleRule> rule = new css::StyleRule(nullptr, mDecl, 0, 0);
  1093. mDecl = nullptr; // We no longer own the declaration -- drop our pointer to it
  1094. return rule.forget();
  1095. }
  1096. } // namespace
  1097. //----------------------------------------------------------------------
  1098. // Implementation Helpers:
  1099. void
  1100. nsSVGElement::UpdateContentStyleRule()
  1101. {
  1102. NS_ASSERTION(!mContentStyleRule, "we already have a content style rule");
  1103. uint32_t attrCount = mAttrsAndChildren.AttrCount();
  1104. if (!attrCount) {
  1105. // nothing to do
  1106. return;
  1107. }
  1108. nsIDocument* doc = OwnerDoc();
  1109. MappedAttrParser mappedAttrParser(doc->CSSLoader(), doc->GetDocumentURI(),
  1110. GetBaseURI(), this);
  1111. for (uint32_t i = 0; i < attrCount; ++i) {
  1112. const nsAttrName* attrName = mAttrsAndChildren.AttrNameAt(i);
  1113. if (!attrName->IsAtom() || !IsAttributeMapped(attrName->Atom()))
  1114. continue;
  1115. if (attrName->NamespaceID() != kNameSpaceID_None &&
  1116. !attrName->Equals(nsGkAtoms::lang, kNameSpaceID_XML)) {
  1117. continue;
  1118. }
  1119. if (attrName->Equals(nsGkAtoms::lang, kNameSpaceID_None) &&
  1120. HasAttr(kNameSpaceID_XML, nsGkAtoms::lang)) {
  1121. continue; // xml:lang has precedence
  1122. }
  1123. nsAutoString value;
  1124. mAttrsAndChildren.AttrAt(i)->ToString(value);
  1125. mappedAttrParser.ParseMappedAttrValue(attrName->Atom(), value);
  1126. }
  1127. mContentStyleRule = mappedAttrParser.CreateStyleRule();
  1128. }
  1129. static void
  1130. ParseMappedAttrAnimValueCallback(void* aObject,
  1131. nsIAtom* aPropertyName,
  1132. void* aPropertyValue,
  1133. void* aData)
  1134. {
  1135. MOZ_ASSERT(aPropertyName != SMIL_MAPPED_ATTR_STYLERULE_ATOM,
  1136. "animated content style rule should have been removed "
  1137. "from properties table already (we're rebuilding it now)");
  1138. MappedAttrParser* mappedAttrParser = static_cast<MappedAttrParser*>(aData);
  1139. MOZ_ASSERT(mappedAttrParser, "parser should be non-null");
  1140. nsStringBuffer* animValBuf = static_cast<nsStringBuffer*>(aPropertyValue);
  1141. MOZ_ASSERT(animValBuf, "animated value should be non-null");
  1142. nsString animValStr;
  1143. nsContentUtils::PopulateStringFromStringBuffer(animValBuf, animValStr);
  1144. mappedAttrParser->ParseMappedAttrValue(aPropertyName, animValStr);
  1145. }
  1146. // Callback for freeing animated content style rule, in property table.
  1147. static void
  1148. ReleaseStyleRule(void* aObject, /* unused */
  1149. nsIAtom* aPropertyName,
  1150. void* aPropertyValue,
  1151. void* aData /* unused */)
  1152. {
  1153. MOZ_ASSERT(aPropertyName == SMIL_MAPPED_ATTR_STYLERULE_ATOM,
  1154. "unexpected property name, for animated content style rule");
  1155. css::StyleRule* styleRule = static_cast<css::StyleRule*>(aPropertyValue);
  1156. MOZ_ASSERT(styleRule, "unexpected null style rule");
  1157. styleRule->Release();
  1158. }
  1159. void
  1160. nsSVGElement::UpdateAnimatedContentStyleRule()
  1161. {
  1162. MOZ_ASSERT(!GetAnimatedContentStyleRule(),
  1163. "Animated content style rule already set");
  1164. nsIDocument* doc = OwnerDoc();
  1165. if (!doc) {
  1166. NS_ERROR("SVG element without owner document");
  1167. return;
  1168. }
  1169. MappedAttrParser mappedAttrParser(doc->CSSLoader(), doc->GetDocumentURI(),
  1170. GetBaseURI(), this);
  1171. doc->PropertyTable(SMIL_MAPPED_ATTR_ANIMVAL)->
  1172. Enumerate(this, ParseMappedAttrAnimValueCallback, &mappedAttrParser);
  1173. RefPtr<css::StyleRule>
  1174. animContentStyleRule(mappedAttrParser.CreateStyleRule());
  1175. if (animContentStyleRule) {
  1176. #ifdef DEBUG
  1177. nsresult rv =
  1178. #endif
  1179. SetProperty(SMIL_MAPPED_ATTR_ANIMVAL,
  1180. SMIL_MAPPED_ATTR_STYLERULE_ATOM,
  1181. animContentStyleRule.get(),
  1182. ReleaseStyleRule);
  1183. Unused << animContentStyleRule.forget();
  1184. MOZ_ASSERT(rv == NS_OK,
  1185. "SetProperty failed (or overwrote something)");
  1186. }
  1187. }
  1188. css::StyleRule*
  1189. nsSVGElement::GetAnimatedContentStyleRule()
  1190. {
  1191. return
  1192. static_cast<css::StyleRule*>(GetProperty(SMIL_MAPPED_ATTR_ANIMVAL,
  1193. SMIL_MAPPED_ATTR_STYLERULE_ATOM,
  1194. nullptr));
  1195. }
  1196. /**
  1197. * Helper methods for the type-specific WillChangeXXX methods.
  1198. *
  1199. * This method sends out appropriate pre-change notifications so that selector
  1200. * restyles (e.g. due to changes that cause |elem[attr="val"]| to start/stop
  1201. * matching) work, and it returns an nsAttrValue that _may_ contain the
  1202. * attribute's pre-change value.
  1203. *
  1204. * The nsAttrValue returned by this method depends on whether there are
  1205. * mutation event listeners listening for changes to this element's attributes.
  1206. * If not, then the object returned is empty. If there are, then the
  1207. * nsAttrValue returned contains a serialized copy of the attribute's value
  1208. * prior to the change, and this object should be passed to the corresponding
  1209. * DidChangeXXX method call (assuming a WillChangeXXX call is required for the
  1210. * SVG type - see comment below). This is necessary so that the 'prevValue'
  1211. * property of the mutation event that is dispatched will correctly contain the
  1212. * old value.
  1213. *
  1214. * The reason we need to serialize the old value if there are mutation
  1215. * event listeners is because the underlying nsAttrValue for the attribute
  1216. * points directly to a parsed representation of the attribute (e.g. an
  1217. * SVGAnimatedLengthList*) that is a member of the SVG element. That object
  1218. * will have changed by the time DidChangeXXX has been called, so without the
  1219. * serialization of the old attribute value that we provide, DidChangeXXX
  1220. * would have no way to get the old value to pass to SetAttrAndNotify.
  1221. *
  1222. * We only return the old value when there are mutation event listeners because
  1223. * it's not needed otherwise, and because it's expensive to serialize the old
  1224. * value. This is especially true for list type attributes, which may be built
  1225. * up via the SVG DOM resulting in a large number of Will/DidModifyXXX calls
  1226. * before the script finally finishes setting the attribute.
  1227. *
  1228. * Note that unlike using SetParsedAttr, using Will/DidChangeXXX does NOT check
  1229. * and filter out redundant changes. Before calling WillChangeXXX, the caller
  1230. * should check whether the new and old values are actually the same, and skip
  1231. * calling Will/DidChangeXXX if they are.
  1232. *
  1233. * Also note that not all SVG types use this scheme. For types that can be
  1234. * represented by an nsAttrValue without pointing back to an SVG object (e.g.
  1235. * enums, booleans, integers) we can simply use SetParsedAttr which will do all
  1236. * of the above for us. For such types there is no matching WillChangeXXX
  1237. * method, only DidChangeXXX which calls SetParsedAttr.
  1238. */
  1239. nsAttrValue
  1240. nsSVGElement::WillChangeValue(nsIAtom* aName)
  1241. {
  1242. // We need an empty attr value:
  1243. // a) to pass to BeforeSetAttr when GetParsedAttr returns nullptr
  1244. // b) to store the old value in the case we have mutation listeners
  1245. //
  1246. // We can use the same value for both purposes, because if GetParsedAttr
  1247. // returns non-null its return value is what will get passed to BeforeSetAttr,
  1248. // not matter what our mutation listener situation is.
  1249. //
  1250. // Also, we should be careful to always return this value to benefit from
  1251. // return value optimization.
  1252. nsAttrValue emptyOrOldAttrValue;
  1253. const nsAttrValue* attrValue = GetParsedAttr(aName);
  1254. // We only need to set the old value if we have listeners since otherwise it
  1255. // isn't used.
  1256. if (attrValue &&
  1257. nsContentUtils::HasMutationListeners(this,
  1258. NS_EVENT_BITS_MUTATION_ATTRMODIFIED,
  1259. this)) {
  1260. emptyOrOldAttrValue.SetToSerialized(*attrValue);
  1261. }
  1262. uint8_t modType = attrValue
  1263. ? static_cast<uint8_t>(nsIDOMMutationEvent::MODIFICATION)
  1264. : static_cast<uint8_t>(nsIDOMMutationEvent::ADDITION);
  1265. nsNodeUtils::AttributeWillChange(this, kNameSpaceID_None, aName, modType,
  1266. nullptr);
  1267. // This is not strictly correct--the attribute value parameter for
  1268. // BeforeSetAttr should reflect the value that *will* be set but that implies
  1269. // allocating, e.g. an extra nsSVGLength2, and isn't necessary at the moment
  1270. // since no SVG elements overload BeforeSetAttr. For now we just pass the
  1271. // current value.
  1272. nsAttrValueOrString attrStringOrValue(attrValue ? *attrValue
  1273. : emptyOrOldAttrValue);
  1274. DebugOnly<nsresult> rv =
  1275. BeforeSetAttr(kNameSpaceID_None, aName, &attrStringOrValue,
  1276. kNotifyDocumentObservers);
  1277. // SVG elements aren't expected to overload BeforeSetAttr in such a way that
  1278. // it may fail. So long as this is the case we don't need to check and pass on
  1279. // the return value which simplifies the calling code significantly.
  1280. MOZ_ASSERT(NS_SUCCEEDED(rv), "Unexpected failure from BeforeSetAttr");
  1281. return emptyOrOldAttrValue;
  1282. }
  1283. /**
  1284. * Helper methods for the type-specific DidChangeXXX methods.
  1285. *
  1286. * aEmptyOrOldValue will normally be the object returned from the corresponding
  1287. * WillChangeXXX call. This is because:
  1288. * a) WillChangeXXX will ensure the object is set when we have mutation
  1289. * listeners, and
  1290. * b) WillChangeXXX will ensure the object represents a serialized version of
  1291. * the old attribute value so that the value doesn't change when the
  1292. * underlying SVG type is updated.
  1293. *
  1294. * aNewValue is replaced with the old value.
  1295. */
  1296. void
  1297. nsSVGElement::DidChangeValue(nsIAtom* aName,
  1298. const nsAttrValue& aEmptyOrOldValue,
  1299. nsAttrValue& aNewValue)
  1300. {
  1301. bool hasListeners =
  1302. nsContentUtils::HasMutationListeners(this,
  1303. NS_EVENT_BITS_MUTATION_ATTRMODIFIED,
  1304. this);
  1305. uint8_t modType = HasAttr(kNameSpaceID_None, aName)
  1306. ? static_cast<uint8_t>(nsIDOMMutationEvent::MODIFICATION)
  1307. : static_cast<uint8_t>(nsIDOMMutationEvent::ADDITION);
  1308. nsIDocument* document = GetComposedDoc();
  1309. mozAutoDocUpdate updateBatch(document, UPDATE_CONTENT_MODEL,
  1310. kNotifyDocumentObservers);
  1311. // XXX Really, the fourth argument to SetAttrAndNotify should be null if
  1312. // aEmptyOrOldValue does not represent the actual previous value of the
  1313. // attribute, but currently SVG elements do not even use the old attribute
  1314. // value in |AfterSetAttr|, so this should be ok.
  1315. SetAttrAndNotify(kNameSpaceID_None, aName, nullptr, &aEmptyOrOldValue,
  1316. aNewValue, modType, hasListeners, kNotifyDocumentObservers,
  1317. kCallAfterSetAttr, document, updateBatch);
  1318. }
  1319. void
  1320. nsSVGElement::MaybeSerializeAttrBeforeRemoval(nsIAtom* aName, bool aNotify)
  1321. {
  1322. if (!aNotify ||
  1323. !nsContentUtils::HasMutationListeners(this,
  1324. NS_EVENT_BITS_MUTATION_ATTRMODIFIED,
  1325. this)) {
  1326. return;
  1327. }
  1328. const nsAttrValue* attrValue = mAttrsAndChildren.GetAttr(aName);
  1329. if (!attrValue)
  1330. return;
  1331. nsAutoString serializedValue;
  1332. attrValue->ToString(serializedValue);
  1333. nsAttrValue oldAttrValue(serializedValue);
  1334. bool oldValueSet;
  1335. mAttrsAndChildren.SetAndSwapAttr(aName, oldAttrValue, &oldValueSet);
  1336. }
  1337. /* static */
  1338. nsIAtom* nsSVGElement::GetEventNameForAttr(nsIAtom* aAttr)
  1339. {
  1340. if (aAttr == nsGkAtoms::onload)
  1341. return nsGkAtoms::onSVGLoad;
  1342. if (aAttr == nsGkAtoms::onunload)
  1343. return nsGkAtoms::onSVGUnload;
  1344. if (aAttr == nsGkAtoms::onresize)
  1345. return nsGkAtoms::onSVGResize;
  1346. if (aAttr == nsGkAtoms::onscroll)
  1347. return nsGkAtoms::onSVGScroll;
  1348. if (aAttr == nsGkAtoms::onzoom)
  1349. return nsGkAtoms::onSVGZoom;
  1350. if (aAttr == nsGkAtoms::onbegin)
  1351. return nsGkAtoms::onbeginEvent;
  1352. if (aAttr == nsGkAtoms::onrepeat)
  1353. return nsGkAtoms::onrepeatEvent;
  1354. if (aAttr == nsGkAtoms::onend)
  1355. return nsGkAtoms::onendEvent;
  1356. return aAttr;
  1357. }
  1358. SVGSVGElement *
  1359. nsSVGElement::GetCtx() const
  1360. {
  1361. nsIContent* ancestor = GetFlattenedTreeParent();
  1362. while (ancestor && ancestor->IsSVGElement()) {
  1363. if (ancestor->IsSVGElement(nsGkAtoms::foreignObject)) {
  1364. return nullptr;
  1365. }
  1366. if (ancestor->IsSVGElement(nsGkAtoms::svg)) {
  1367. return static_cast<SVGSVGElement*>(ancestor);
  1368. }
  1369. ancestor = ancestor->GetFlattenedTreeParent();
  1370. }
  1371. // we don't have an ancestor <svg> element...
  1372. return nullptr;
  1373. }
  1374. /* virtual */ gfxMatrix
  1375. nsSVGElement::PrependLocalTransformsTo(
  1376. const gfxMatrix &aMatrix, SVGTransformTypes aWhich) const
  1377. {
  1378. return aMatrix;
  1379. }
  1380. nsSVGElement::LengthAttributesInfo
  1381. nsSVGElement::GetLengthInfo()
  1382. {
  1383. return LengthAttributesInfo(nullptr, nullptr, 0);
  1384. }
  1385. void nsSVGElement::LengthAttributesInfo::Reset(uint8_t aAttrEnum)
  1386. {
  1387. mLengths[aAttrEnum].Init(mLengthInfo[aAttrEnum].mCtxType,
  1388. aAttrEnum,
  1389. mLengthInfo[aAttrEnum].mDefaultValue,
  1390. mLengthInfo[aAttrEnum].mDefaultUnitType);
  1391. }
  1392. void
  1393. nsSVGElement::SetLength(nsIAtom* aName, const nsSVGLength2 &aLength)
  1394. {
  1395. LengthAttributesInfo lengthInfo = GetLengthInfo();
  1396. for (uint32_t i = 0; i < lengthInfo.mLengthCount; i++) {
  1397. if (aName == *lengthInfo.mLengthInfo[i].mName) {
  1398. lengthInfo.mLengths[i] = aLength;
  1399. DidAnimateLength(i);
  1400. return;
  1401. }
  1402. }
  1403. MOZ_ASSERT(false, "no length found to set");
  1404. }
  1405. nsAttrValue
  1406. nsSVGElement::WillChangeLength(uint8_t aAttrEnum)
  1407. {
  1408. return WillChangeValue(*GetLengthInfo().mLengthInfo[aAttrEnum].mName);
  1409. }
  1410. void
  1411. nsSVGElement::DidChangeLength(uint8_t aAttrEnum,
  1412. const nsAttrValue& aEmptyOrOldValue)
  1413. {
  1414. LengthAttributesInfo info = GetLengthInfo();
  1415. NS_ASSERTION(info.mLengthCount > 0,
  1416. "DidChangeLength on element with no length attribs");
  1417. NS_ASSERTION(aAttrEnum < info.mLengthCount, "aAttrEnum out of range");
  1418. nsAttrValue newValue;
  1419. newValue.SetTo(info.mLengths[aAttrEnum], nullptr);
  1420. DidChangeValue(*info.mLengthInfo[aAttrEnum].mName, aEmptyOrOldValue,
  1421. newValue);
  1422. }
  1423. void
  1424. nsSVGElement::DidAnimateLength(uint8_t aAttrEnum)
  1425. {
  1426. ClearAnyCachedPath();
  1427. nsIFrame* frame = GetPrimaryFrame();
  1428. if (frame) {
  1429. LengthAttributesInfo info = GetLengthInfo();
  1430. frame->AttributeChanged(kNameSpaceID_None,
  1431. *info.mLengthInfo[aAttrEnum].mName,
  1432. nsIDOMMutationEvent::MODIFICATION);
  1433. }
  1434. }
  1435. nsSVGLength2*
  1436. nsSVGElement::GetAnimatedLength(const nsIAtom *aAttrName)
  1437. {
  1438. LengthAttributesInfo lengthInfo = GetLengthInfo();
  1439. for (uint32_t i = 0; i < lengthInfo.mLengthCount; i++) {
  1440. if (aAttrName == *lengthInfo.mLengthInfo[i].mName) {
  1441. return &lengthInfo.mLengths[i];
  1442. }
  1443. }
  1444. MOZ_ASSERT(false, "no matching length found");
  1445. return nullptr;
  1446. }
  1447. void
  1448. nsSVGElement::GetAnimatedLengthValues(float *aFirst, ...)
  1449. {
  1450. LengthAttributesInfo info = GetLengthInfo();
  1451. NS_ASSERTION(info.mLengthCount > 0,
  1452. "GetAnimatedLengthValues on element with no length attribs");
  1453. SVGSVGElement *ctx = nullptr;
  1454. float *f = aFirst;
  1455. uint32_t i = 0;
  1456. va_list args;
  1457. va_start(args, aFirst);
  1458. while (f && i < info.mLengthCount) {
  1459. uint8_t type = info.mLengths[i].GetSpecifiedUnitType();
  1460. if (!ctx) {
  1461. if (type != nsIDOMSVGLength::SVG_LENGTHTYPE_NUMBER &&
  1462. type != nsIDOMSVGLength::SVG_LENGTHTYPE_PX)
  1463. ctx = GetCtx();
  1464. }
  1465. if (type == nsIDOMSVGLength::SVG_LENGTHTYPE_EMS ||
  1466. type == nsIDOMSVGLength::SVG_LENGTHTYPE_EXS)
  1467. *f = info.mLengths[i++].GetAnimValue(this);
  1468. else
  1469. *f = info.mLengths[i++].GetAnimValue(ctx);
  1470. f = va_arg(args, float*);
  1471. }
  1472. va_end(args);
  1473. }
  1474. nsSVGElement::LengthListAttributesInfo
  1475. nsSVGElement::GetLengthListInfo()
  1476. {
  1477. return LengthListAttributesInfo(nullptr, nullptr, 0);
  1478. }
  1479. void
  1480. nsSVGElement::LengthListAttributesInfo::Reset(uint8_t aAttrEnum)
  1481. {
  1482. mLengthLists[aAttrEnum].ClearBaseValue(aAttrEnum);
  1483. // caller notifies
  1484. }
  1485. nsAttrValue
  1486. nsSVGElement::WillChangeLengthList(uint8_t aAttrEnum)
  1487. {
  1488. return WillChangeValue(*GetLengthListInfo().mLengthListInfo[aAttrEnum].mName);
  1489. }
  1490. void
  1491. nsSVGElement::DidChangeLengthList(uint8_t aAttrEnum,
  1492. const nsAttrValue& aEmptyOrOldValue)
  1493. {
  1494. LengthListAttributesInfo info = GetLengthListInfo();
  1495. NS_ASSERTION(info.mLengthListCount > 0,
  1496. "DidChangeLengthList on element with no length list attribs");
  1497. NS_ASSERTION(aAttrEnum < info.mLengthListCount, "aAttrEnum out of range");
  1498. nsAttrValue newValue;
  1499. newValue.SetTo(info.mLengthLists[aAttrEnum].GetBaseValue(), nullptr);
  1500. DidChangeValue(*info.mLengthListInfo[aAttrEnum].mName, aEmptyOrOldValue,
  1501. newValue);
  1502. }
  1503. void
  1504. nsSVGElement::DidAnimateLengthList(uint8_t aAttrEnum)
  1505. {
  1506. nsIFrame* frame = GetPrimaryFrame();
  1507. if (frame) {
  1508. LengthListAttributesInfo info = GetLengthListInfo();
  1509. frame->AttributeChanged(kNameSpaceID_None,
  1510. *info.mLengthListInfo[aAttrEnum].mName,
  1511. nsIDOMMutationEvent::MODIFICATION);
  1512. }
  1513. }
  1514. void
  1515. nsSVGElement::GetAnimatedLengthListValues(SVGUserUnitList *aFirst, ...)
  1516. {
  1517. LengthListAttributesInfo info = GetLengthListInfo();
  1518. NS_ASSERTION(info.mLengthListCount > 0,
  1519. "GetAnimatedLengthListValues on element with no length list attribs");
  1520. SVGUserUnitList *list = aFirst;
  1521. uint32_t i = 0;
  1522. va_list args;
  1523. va_start(args, aFirst);
  1524. while (list && i < info.mLengthListCount) {
  1525. list->Init(&(info.mLengthLists[i].GetAnimValue()), this, info.mLengthListInfo[i].mAxis);
  1526. ++i;
  1527. list = va_arg(args, SVGUserUnitList*);
  1528. }
  1529. va_end(args);
  1530. }
  1531. SVGAnimatedLengthList*
  1532. nsSVGElement::GetAnimatedLengthList(uint8_t aAttrEnum)
  1533. {
  1534. LengthListAttributesInfo info = GetLengthListInfo();
  1535. if (aAttrEnum < info.mLengthListCount) {
  1536. return &(info.mLengthLists[aAttrEnum]);
  1537. }
  1538. NS_NOTREACHED("Bad attrEnum");
  1539. return nullptr;
  1540. }
  1541. nsSVGElement::NumberListAttributesInfo
  1542. nsSVGElement::GetNumberListInfo()
  1543. {
  1544. return NumberListAttributesInfo(nullptr, nullptr, 0);
  1545. }
  1546. void
  1547. nsSVGElement::NumberListAttributesInfo::Reset(uint8_t aAttrEnum)
  1548. {
  1549. MOZ_ASSERT(aAttrEnum < mNumberListCount, "Bad attr enum");
  1550. mNumberLists[aAttrEnum].ClearBaseValue(aAttrEnum);
  1551. // caller notifies
  1552. }
  1553. nsAttrValue
  1554. nsSVGElement::WillChangeNumberList(uint8_t aAttrEnum)
  1555. {
  1556. return WillChangeValue(*GetNumberListInfo().mNumberListInfo[aAttrEnum].mName);
  1557. }
  1558. void
  1559. nsSVGElement::DidChangeNumberList(uint8_t aAttrEnum,
  1560. const nsAttrValue& aEmptyOrOldValue)
  1561. {
  1562. NumberListAttributesInfo info = GetNumberListInfo();
  1563. MOZ_ASSERT(info.mNumberListCount > 0,
  1564. "DidChangeNumberList on element with no number list attribs");
  1565. MOZ_ASSERT(aAttrEnum < info.mNumberListCount,
  1566. "aAttrEnum out of range");
  1567. nsAttrValue newValue;
  1568. newValue.SetTo(info.mNumberLists[aAttrEnum].GetBaseValue(), nullptr);
  1569. DidChangeValue(*info.mNumberListInfo[aAttrEnum].mName, aEmptyOrOldValue,
  1570. newValue);
  1571. }
  1572. void
  1573. nsSVGElement::DidAnimateNumberList(uint8_t aAttrEnum)
  1574. {
  1575. nsIFrame* frame = GetPrimaryFrame();
  1576. if (frame) {
  1577. NumberListAttributesInfo info = GetNumberListInfo();
  1578. MOZ_ASSERT(aAttrEnum < info.mNumberListCount, "aAttrEnum out of range");
  1579. frame->AttributeChanged(kNameSpaceID_None,
  1580. *info.mNumberListInfo[aAttrEnum].mName,
  1581. nsIDOMMutationEvent::MODIFICATION);
  1582. }
  1583. }
  1584. SVGAnimatedNumberList*
  1585. nsSVGElement::GetAnimatedNumberList(uint8_t aAttrEnum)
  1586. {
  1587. NumberListAttributesInfo info = GetNumberListInfo();
  1588. if (aAttrEnum < info.mNumberListCount) {
  1589. return &(info.mNumberLists[aAttrEnum]);
  1590. }
  1591. MOZ_ASSERT(false, "Bad attrEnum");
  1592. return nullptr;
  1593. }
  1594. SVGAnimatedNumberList*
  1595. nsSVGElement::GetAnimatedNumberList(nsIAtom *aAttrName)
  1596. {
  1597. NumberListAttributesInfo info = GetNumberListInfo();
  1598. for (uint32_t i = 0; i < info.mNumberListCount; i++) {
  1599. if (aAttrName == *info.mNumberListInfo[i].mName) {
  1600. return &info.mNumberLists[i];
  1601. }
  1602. }
  1603. MOZ_ASSERT(false, "Bad caller");
  1604. return nullptr;
  1605. }
  1606. nsAttrValue
  1607. nsSVGElement::WillChangePointList()
  1608. {
  1609. MOZ_ASSERT(GetPointListAttrName(),
  1610. "Changing non-existent point list?");
  1611. return WillChangeValue(GetPointListAttrName());
  1612. }
  1613. void
  1614. nsSVGElement::DidChangePointList(const nsAttrValue& aEmptyOrOldValue)
  1615. {
  1616. MOZ_ASSERT(GetPointListAttrName(),
  1617. "Changing non-existent point list?");
  1618. nsAttrValue newValue;
  1619. newValue.SetTo(GetAnimatedPointList()->GetBaseValue(), nullptr);
  1620. DidChangeValue(GetPointListAttrName(), aEmptyOrOldValue, newValue);
  1621. }
  1622. void
  1623. nsSVGElement::DidAnimatePointList()
  1624. {
  1625. MOZ_ASSERT(GetPointListAttrName(),
  1626. "Animating non-existent path data?");
  1627. ClearAnyCachedPath();
  1628. nsIFrame* frame = GetPrimaryFrame();
  1629. if (frame) {
  1630. frame->AttributeChanged(kNameSpaceID_None,
  1631. GetPointListAttrName(),
  1632. nsIDOMMutationEvent::MODIFICATION);
  1633. }
  1634. }
  1635. nsAttrValue
  1636. nsSVGElement::WillChangePathSegList()
  1637. {
  1638. MOZ_ASSERT(GetPathDataAttrName(),
  1639. "Changing non-existent path seg list?");
  1640. return WillChangeValue(GetPathDataAttrName());
  1641. }
  1642. void
  1643. nsSVGElement::DidChangePathSegList(const nsAttrValue& aEmptyOrOldValue)
  1644. {
  1645. MOZ_ASSERT(GetPathDataAttrName(),
  1646. "Changing non-existent path seg list?");
  1647. nsAttrValue newValue;
  1648. newValue.SetTo(GetAnimPathSegList()->GetBaseValue(), nullptr);
  1649. DidChangeValue(GetPathDataAttrName(), aEmptyOrOldValue, newValue);
  1650. }
  1651. void
  1652. nsSVGElement::DidAnimatePathSegList()
  1653. {
  1654. MOZ_ASSERT(GetPathDataAttrName(),
  1655. "Animating non-existent path data?");
  1656. ClearAnyCachedPath();
  1657. nsIFrame* frame = GetPrimaryFrame();
  1658. if (frame) {
  1659. frame->AttributeChanged(kNameSpaceID_None,
  1660. GetPathDataAttrName(),
  1661. nsIDOMMutationEvent::MODIFICATION);
  1662. }
  1663. }
  1664. nsSVGElement::NumberAttributesInfo
  1665. nsSVGElement::GetNumberInfo()
  1666. {
  1667. return NumberAttributesInfo(nullptr, nullptr, 0);
  1668. }
  1669. void nsSVGElement::NumberAttributesInfo::Reset(uint8_t aAttrEnum)
  1670. {
  1671. mNumbers[aAttrEnum].Init(aAttrEnum,
  1672. mNumberInfo[aAttrEnum].mDefaultValue);
  1673. }
  1674. void
  1675. nsSVGElement::DidChangeNumber(uint8_t aAttrEnum)
  1676. {
  1677. NumberAttributesInfo info = GetNumberInfo();
  1678. NS_ASSERTION(info.mNumberCount > 0,
  1679. "DidChangeNumber on element with no number attribs");
  1680. NS_ASSERTION(aAttrEnum < info.mNumberCount, "aAttrEnum out of range");
  1681. nsAttrValue attrValue;
  1682. attrValue.SetTo(info.mNumbers[aAttrEnum].GetBaseValue(), nullptr);
  1683. SetParsedAttr(kNameSpaceID_None, *info.mNumberInfo[aAttrEnum].mName, nullptr,
  1684. attrValue, true);
  1685. }
  1686. void
  1687. nsSVGElement::DidAnimateNumber(uint8_t aAttrEnum)
  1688. {
  1689. nsIFrame* frame = GetPrimaryFrame();
  1690. if (frame) {
  1691. NumberAttributesInfo info = GetNumberInfo();
  1692. frame->AttributeChanged(kNameSpaceID_None,
  1693. *info.mNumberInfo[aAttrEnum].mName,
  1694. nsIDOMMutationEvent::MODIFICATION);
  1695. }
  1696. }
  1697. void
  1698. nsSVGElement::GetAnimatedNumberValues(float *aFirst, ...)
  1699. {
  1700. NumberAttributesInfo info = GetNumberInfo();
  1701. NS_ASSERTION(info.mNumberCount > 0,
  1702. "GetAnimatedNumberValues on element with no number attribs");
  1703. float *f = aFirst;
  1704. uint32_t i = 0;
  1705. va_list args;
  1706. va_start(args, aFirst);
  1707. while (f && i < info.mNumberCount) {
  1708. *f = info.mNumbers[i++].GetAnimValue();
  1709. f = va_arg(args, float*);
  1710. }
  1711. va_end(args);
  1712. }
  1713. nsSVGElement::NumberPairAttributesInfo
  1714. nsSVGElement::GetNumberPairInfo()
  1715. {
  1716. return NumberPairAttributesInfo(nullptr, nullptr, 0);
  1717. }
  1718. void nsSVGElement::NumberPairAttributesInfo::Reset(uint8_t aAttrEnum)
  1719. {
  1720. mNumberPairs[aAttrEnum].Init(aAttrEnum,
  1721. mNumberPairInfo[aAttrEnum].mDefaultValue1,
  1722. mNumberPairInfo[aAttrEnum].mDefaultValue2);
  1723. }
  1724. nsAttrValue
  1725. nsSVGElement::WillChangeNumberPair(uint8_t aAttrEnum)
  1726. {
  1727. return WillChangeValue(*GetNumberPairInfo().mNumberPairInfo[aAttrEnum].mName);
  1728. }
  1729. void
  1730. nsSVGElement::DidChangeNumberPair(uint8_t aAttrEnum,
  1731. const nsAttrValue& aEmptyOrOldValue)
  1732. {
  1733. NumberPairAttributesInfo info = GetNumberPairInfo();
  1734. NS_ASSERTION(info.mNumberPairCount > 0,
  1735. "DidChangePairNumber on element with no number pair attribs");
  1736. NS_ASSERTION(aAttrEnum < info.mNumberPairCount, "aAttrEnum out of range");
  1737. nsAttrValue newValue;
  1738. newValue.SetTo(info.mNumberPairs[aAttrEnum], nullptr);
  1739. DidChangeValue(*info.mNumberPairInfo[aAttrEnum].mName, aEmptyOrOldValue,
  1740. newValue);
  1741. }
  1742. void
  1743. nsSVGElement::DidAnimateNumberPair(uint8_t aAttrEnum)
  1744. {
  1745. nsIFrame* frame = GetPrimaryFrame();
  1746. if (frame) {
  1747. NumberPairAttributesInfo info = GetNumberPairInfo();
  1748. frame->AttributeChanged(kNameSpaceID_None,
  1749. *info.mNumberPairInfo[aAttrEnum].mName,
  1750. nsIDOMMutationEvent::MODIFICATION);
  1751. }
  1752. }
  1753. nsSVGElement::IntegerAttributesInfo
  1754. nsSVGElement::GetIntegerInfo()
  1755. {
  1756. return IntegerAttributesInfo(nullptr, nullptr, 0);
  1757. }
  1758. void nsSVGElement::IntegerAttributesInfo::Reset(uint8_t aAttrEnum)
  1759. {
  1760. mIntegers[aAttrEnum].Init(aAttrEnum,
  1761. mIntegerInfo[aAttrEnum].mDefaultValue);
  1762. }
  1763. void
  1764. nsSVGElement::DidChangeInteger(uint8_t aAttrEnum)
  1765. {
  1766. IntegerAttributesInfo info = GetIntegerInfo();
  1767. NS_ASSERTION(info.mIntegerCount > 0,
  1768. "DidChangeInteger on element with no integer attribs");
  1769. NS_ASSERTION(aAttrEnum < info.mIntegerCount, "aAttrEnum out of range");
  1770. nsAttrValue attrValue;
  1771. attrValue.SetTo(info.mIntegers[aAttrEnum].GetBaseValue(), nullptr);
  1772. SetParsedAttr(kNameSpaceID_None, *info.mIntegerInfo[aAttrEnum].mName, nullptr,
  1773. attrValue, true);
  1774. }
  1775. void
  1776. nsSVGElement::DidAnimateInteger(uint8_t aAttrEnum)
  1777. {
  1778. nsIFrame* frame = GetPrimaryFrame();
  1779. if (frame) {
  1780. IntegerAttributesInfo info = GetIntegerInfo();
  1781. frame->AttributeChanged(kNameSpaceID_None,
  1782. *info.mIntegerInfo[aAttrEnum].mName,
  1783. nsIDOMMutationEvent::MODIFICATION);
  1784. }
  1785. }
  1786. void
  1787. nsSVGElement::GetAnimatedIntegerValues(int32_t *aFirst, ...)
  1788. {
  1789. IntegerAttributesInfo info = GetIntegerInfo();
  1790. NS_ASSERTION(info.mIntegerCount > 0,
  1791. "GetAnimatedIntegerValues on element with no integer attribs");
  1792. int32_t *n = aFirst;
  1793. uint32_t i = 0;
  1794. va_list args;
  1795. va_start(args, aFirst);
  1796. while (n && i < info.mIntegerCount) {
  1797. *n = info.mIntegers[i++].GetAnimValue();
  1798. n = va_arg(args, int32_t*);
  1799. }
  1800. va_end(args);
  1801. }
  1802. nsSVGElement::IntegerPairAttributesInfo
  1803. nsSVGElement::GetIntegerPairInfo()
  1804. {
  1805. return IntegerPairAttributesInfo(nullptr, nullptr, 0);
  1806. }
  1807. void nsSVGElement::IntegerPairAttributesInfo::Reset(uint8_t aAttrEnum)
  1808. {
  1809. mIntegerPairs[aAttrEnum].Init(aAttrEnum,
  1810. mIntegerPairInfo[aAttrEnum].mDefaultValue1,
  1811. mIntegerPairInfo[aAttrEnum].mDefaultValue2);
  1812. }
  1813. nsAttrValue
  1814. nsSVGElement::WillChangeIntegerPair(uint8_t aAttrEnum)
  1815. {
  1816. return WillChangeValue(
  1817. *GetIntegerPairInfo().mIntegerPairInfo[aAttrEnum].mName);
  1818. }
  1819. void
  1820. nsSVGElement::DidChangeIntegerPair(uint8_t aAttrEnum,
  1821. const nsAttrValue& aEmptyOrOldValue)
  1822. {
  1823. IntegerPairAttributesInfo info = GetIntegerPairInfo();
  1824. NS_ASSERTION(info.mIntegerPairCount > 0,
  1825. "DidChangeIntegerPair on element with no integer pair attribs");
  1826. NS_ASSERTION(aAttrEnum < info.mIntegerPairCount, "aAttrEnum out of range");
  1827. nsAttrValue newValue;
  1828. newValue.SetTo(info.mIntegerPairs[aAttrEnum], nullptr);
  1829. DidChangeValue(*info.mIntegerPairInfo[aAttrEnum].mName, aEmptyOrOldValue,
  1830. newValue);
  1831. }
  1832. void
  1833. nsSVGElement::DidAnimateIntegerPair(uint8_t aAttrEnum)
  1834. {
  1835. nsIFrame* frame = GetPrimaryFrame();
  1836. if (frame) {
  1837. IntegerPairAttributesInfo info = GetIntegerPairInfo();
  1838. frame->AttributeChanged(kNameSpaceID_None,
  1839. *info.mIntegerPairInfo[aAttrEnum].mName,
  1840. nsIDOMMutationEvent::MODIFICATION);
  1841. }
  1842. }
  1843. nsSVGElement::AngleAttributesInfo
  1844. nsSVGElement::GetAngleInfo()
  1845. {
  1846. return AngleAttributesInfo(nullptr, nullptr, 0);
  1847. }
  1848. void nsSVGElement::AngleAttributesInfo::Reset(uint8_t aAttrEnum)
  1849. {
  1850. mAngles[aAttrEnum].Init(aAttrEnum,
  1851. mAngleInfo[aAttrEnum].mDefaultValue,
  1852. mAngleInfo[aAttrEnum].mDefaultUnitType);
  1853. }
  1854. nsAttrValue
  1855. nsSVGElement::WillChangeAngle(uint8_t aAttrEnum)
  1856. {
  1857. return WillChangeValue(*GetAngleInfo().mAngleInfo[aAttrEnum].mName);
  1858. }
  1859. void
  1860. nsSVGElement::DidChangeAngle(uint8_t aAttrEnum,
  1861. const nsAttrValue& aEmptyOrOldValue)
  1862. {
  1863. AngleAttributesInfo info = GetAngleInfo();
  1864. NS_ASSERTION(info.mAngleCount > 0,
  1865. "DidChangeAngle on element with no angle attribs");
  1866. NS_ASSERTION(aAttrEnum < info.mAngleCount, "aAttrEnum out of range");
  1867. nsAttrValue newValue;
  1868. newValue.SetTo(info.mAngles[aAttrEnum], nullptr);
  1869. DidChangeValue(*info.mAngleInfo[aAttrEnum].mName, aEmptyOrOldValue, newValue);
  1870. }
  1871. void
  1872. nsSVGElement::DidAnimateAngle(uint8_t aAttrEnum)
  1873. {
  1874. nsIFrame* frame = GetPrimaryFrame();
  1875. if (frame) {
  1876. AngleAttributesInfo info = GetAngleInfo();
  1877. frame->AttributeChanged(kNameSpaceID_None,
  1878. *info.mAngleInfo[aAttrEnum].mName,
  1879. nsIDOMMutationEvent::MODIFICATION);
  1880. }
  1881. }
  1882. nsSVGElement::BooleanAttributesInfo
  1883. nsSVGElement::GetBooleanInfo()
  1884. {
  1885. return BooleanAttributesInfo(nullptr, nullptr, 0);
  1886. }
  1887. void nsSVGElement::BooleanAttributesInfo::Reset(uint8_t aAttrEnum)
  1888. {
  1889. mBooleans[aAttrEnum].Init(aAttrEnum,
  1890. mBooleanInfo[aAttrEnum].mDefaultValue);
  1891. }
  1892. void
  1893. nsSVGElement::DidChangeBoolean(uint8_t aAttrEnum)
  1894. {
  1895. BooleanAttributesInfo info = GetBooleanInfo();
  1896. NS_ASSERTION(info.mBooleanCount > 0,
  1897. "DidChangeBoolean on element with no boolean attribs");
  1898. NS_ASSERTION(aAttrEnum < info.mBooleanCount, "aAttrEnum out of range");
  1899. nsAttrValue attrValue(info.mBooleans[aAttrEnum].GetBaseValueAtom());
  1900. SetParsedAttr(kNameSpaceID_None, *info.mBooleanInfo[aAttrEnum].mName, nullptr,
  1901. attrValue, true);
  1902. }
  1903. void
  1904. nsSVGElement::DidAnimateBoolean(uint8_t aAttrEnum)
  1905. {
  1906. nsIFrame* frame = GetPrimaryFrame();
  1907. if (frame) {
  1908. BooleanAttributesInfo info = GetBooleanInfo();
  1909. frame->AttributeChanged(kNameSpaceID_None,
  1910. *info.mBooleanInfo[aAttrEnum].mName,
  1911. nsIDOMMutationEvent::MODIFICATION);
  1912. }
  1913. }
  1914. nsSVGElement::EnumAttributesInfo
  1915. nsSVGElement::GetEnumInfo()
  1916. {
  1917. return EnumAttributesInfo(nullptr, nullptr, 0);
  1918. }
  1919. void nsSVGElement::EnumAttributesInfo::Reset(uint8_t aAttrEnum)
  1920. {
  1921. mEnums[aAttrEnum].Init(aAttrEnum,
  1922. mEnumInfo[aAttrEnum].mDefaultValue);
  1923. }
  1924. void
  1925. nsSVGElement::DidChangeEnum(uint8_t aAttrEnum)
  1926. {
  1927. EnumAttributesInfo info = GetEnumInfo();
  1928. NS_ASSERTION(info.mEnumCount > 0,
  1929. "DidChangeEnum on element with no enum attribs");
  1930. NS_ASSERTION(aAttrEnum < info.mEnumCount, "aAttrEnum out of range");
  1931. nsAttrValue attrValue(info.mEnums[aAttrEnum].GetBaseValueAtom(this));
  1932. SetParsedAttr(kNameSpaceID_None, *info.mEnumInfo[aAttrEnum].mName, nullptr,
  1933. attrValue, true);
  1934. }
  1935. void
  1936. nsSVGElement::DidAnimateEnum(uint8_t aAttrEnum)
  1937. {
  1938. nsIFrame* frame = GetPrimaryFrame();
  1939. if (frame) {
  1940. EnumAttributesInfo info = GetEnumInfo();
  1941. frame->AttributeChanged(kNameSpaceID_None,
  1942. *info.mEnumInfo[aAttrEnum].mName,
  1943. nsIDOMMutationEvent::MODIFICATION);
  1944. }
  1945. }
  1946. nsSVGViewBox *
  1947. nsSVGElement::GetViewBox()
  1948. {
  1949. return nullptr;
  1950. }
  1951. nsAttrValue
  1952. nsSVGElement::WillChangeViewBox()
  1953. {
  1954. return WillChangeValue(nsGkAtoms::viewBox);
  1955. }
  1956. void
  1957. nsSVGElement::DidChangeViewBox(const nsAttrValue& aEmptyOrOldValue)
  1958. {
  1959. nsSVGViewBox *viewBox = GetViewBox();
  1960. NS_ASSERTION(viewBox, "DidChangeViewBox on element with no viewBox attrib");
  1961. nsAttrValue newValue;
  1962. newValue.SetTo(*viewBox, nullptr);
  1963. DidChangeValue(nsGkAtoms::viewBox, aEmptyOrOldValue, newValue);
  1964. }
  1965. void
  1966. nsSVGElement::DidAnimateViewBox()
  1967. {
  1968. nsIFrame* frame = GetPrimaryFrame();
  1969. if (frame) {
  1970. frame->AttributeChanged(kNameSpaceID_None,
  1971. nsGkAtoms::viewBox,
  1972. nsIDOMMutationEvent::MODIFICATION);
  1973. }
  1974. }
  1975. SVGAnimatedPreserveAspectRatio *
  1976. nsSVGElement::GetPreserveAspectRatio()
  1977. {
  1978. return nullptr;
  1979. }
  1980. nsAttrValue
  1981. nsSVGElement::WillChangePreserveAspectRatio()
  1982. {
  1983. return WillChangeValue(nsGkAtoms::preserveAspectRatio);
  1984. }
  1985. void
  1986. nsSVGElement::DidChangePreserveAspectRatio(const nsAttrValue& aEmptyOrOldValue)
  1987. {
  1988. SVGAnimatedPreserveAspectRatio *preserveAspectRatio =
  1989. GetPreserveAspectRatio();
  1990. NS_ASSERTION(preserveAspectRatio,
  1991. "DidChangePreserveAspectRatio on element with no "
  1992. "preserveAspectRatio attrib");
  1993. nsAttrValue newValue;
  1994. newValue.SetTo(*preserveAspectRatio, nullptr);
  1995. DidChangeValue(nsGkAtoms::preserveAspectRatio, aEmptyOrOldValue, newValue);
  1996. }
  1997. void
  1998. nsSVGElement::DidAnimatePreserveAspectRatio()
  1999. {
  2000. nsIFrame* frame = GetPrimaryFrame();
  2001. if (frame) {
  2002. frame->AttributeChanged(kNameSpaceID_None,
  2003. nsGkAtoms::preserveAspectRatio,
  2004. nsIDOMMutationEvent::MODIFICATION);
  2005. }
  2006. }
  2007. nsAttrValue
  2008. nsSVGElement::WillChangeTransformList()
  2009. {
  2010. return WillChangeValue(GetTransformListAttrName());
  2011. }
  2012. void
  2013. nsSVGElement::DidChangeTransformList(const nsAttrValue& aEmptyOrOldValue)
  2014. {
  2015. MOZ_ASSERT(GetTransformListAttrName(),
  2016. "Changing non-existent transform list?");
  2017. // The transform attribute is being set, so we must ensure that the
  2018. // SVGAnimatedTransformList is/has been allocated:
  2019. nsAttrValue newValue;
  2020. newValue.SetTo(GetAnimatedTransformList(DO_ALLOCATE)->GetBaseValue(), nullptr);
  2021. DidChangeValue(GetTransformListAttrName(), aEmptyOrOldValue, newValue);
  2022. }
  2023. void
  2024. nsSVGElement::DidAnimateTransformList(int32_t aModType)
  2025. {
  2026. MOZ_ASSERT(GetTransformListAttrName(),
  2027. "Animating non-existent transform data?");
  2028. nsIFrame* frame = GetPrimaryFrame();
  2029. if (frame) {
  2030. nsIAtom *transformAttr = GetTransformListAttrName();
  2031. frame->AttributeChanged(kNameSpaceID_None,
  2032. transformAttr,
  2033. aModType);
  2034. // When script changes the 'transform' attribute, Element::SetAttrAndNotify
  2035. // will call nsNodeUtills::AttributeChanged, under which
  2036. // SVGTransformableElement::GetAttributeChangeHint will be called and an
  2037. // appropriate change event posted to update our frame's overflow rects.
  2038. // The SetAttrAndNotify doesn't happen for transform changes caused by
  2039. // 'animateTransform' though (and sending out the mutation events that
  2040. // nsNodeUtills::AttributeChanged dispatches would be inappropriate
  2041. // anyway), so we need to post the change event ourself.
  2042. nsChangeHint changeHint = GetAttributeChangeHint(transformAttr, aModType);
  2043. if (changeHint) {
  2044. nsLayoutUtils::PostRestyleEvent(this, nsRestyleHint(0), changeHint);
  2045. }
  2046. }
  2047. }
  2048. nsSVGElement::StringAttributesInfo
  2049. nsSVGElement::GetStringInfo()
  2050. {
  2051. return StringAttributesInfo(nullptr, nullptr, 0);
  2052. }
  2053. void nsSVGElement::StringAttributesInfo::Reset(uint8_t aAttrEnum)
  2054. {
  2055. mStrings[aAttrEnum].Init(aAttrEnum);
  2056. }
  2057. void nsSVGElement::GetStringBaseValue(uint8_t aAttrEnum, nsAString& aResult) const
  2058. {
  2059. nsSVGElement::StringAttributesInfo info = const_cast<nsSVGElement*>(this)->GetStringInfo();
  2060. NS_ASSERTION(info.mStringCount > 0,
  2061. "GetBaseValue on element with no string attribs");
  2062. NS_ASSERTION(aAttrEnum < info.mStringCount, "aAttrEnum out of range");
  2063. GetAttr(info.mStringInfo[aAttrEnum].mNamespaceID,
  2064. *info.mStringInfo[aAttrEnum].mName, aResult);
  2065. }
  2066. void nsSVGElement::SetStringBaseValue(uint8_t aAttrEnum, const nsAString& aValue)
  2067. {
  2068. nsSVGElement::StringAttributesInfo info = GetStringInfo();
  2069. NS_ASSERTION(info.mStringCount > 0,
  2070. "SetBaseValue on element with no string attribs");
  2071. NS_ASSERTION(aAttrEnum < info.mStringCount, "aAttrEnum out of range");
  2072. SetAttr(info.mStringInfo[aAttrEnum].mNamespaceID,
  2073. *info.mStringInfo[aAttrEnum].mName, aValue, true);
  2074. }
  2075. void
  2076. nsSVGElement::DidAnimateString(uint8_t aAttrEnum)
  2077. {
  2078. nsIFrame* frame = GetPrimaryFrame();
  2079. if (frame) {
  2080. StringAttributesInfo info = GetStringInfo();
  2081. frame->AttributeChanged(info.mStringInfo[aAttrEnum].mNamespaceID,
  2082. *info.mStringInfo[aAttrEnum].mName,
  2083. nsIDOMMutationEvent::MODIFICATION);
  2084. }
  2085. }
  2086. nsSVGElement::StringListAttributesInfo
  2087. nsSVGElement::GetStringListInfo()
  2088. {
  2089. return StringListAttributesInfo(nullptr, nullptr, 0);
  2090. }
  2091. nsAttrValue
  2092. nsSVGElement::WillChangeStringList(bool aIsConditionalProcessingAttribute,
  2093. uint8_t aAttrEnum)
  2094. {
  2095. nsIAtom* name;
  2096. if (aIsConditionalProcessingAttribute) {
  2097. nsCOMPtr<SVGTests> tests(do_QueryInterface(static_cast<nsIDOMSVGElement*>(this)));
  2098. name = tests->GetAttrName(aAttrEnum);
  2099. } else {
  2100. name = *GetStringListInfo().mStringListInfo[aAttrEnum].mName;
  2101. }
  2102. return WillChangeValue(name);
  2103. }
  2104. void
  2105. nsSVGElement::DidChangeStringList(bool aIsConditionalProcessingAttribute,
  2106. uint8_t aAttrEnum,
  2107. const nsAttrValue& aEmptyOrOldValue)
  2108. {
  2109. nsIAtom* name;
  2110. nsAttrValue newValue;
  2111. nsCOMPtr<SVGTests> tests;
  2112. if (aIsConditionalProcessingAttribute) {
  2113. tests = do_QueryObject(this);
  2114. name = tests->GetAttrName(aAttrEnum);
  2115. tests->GetAttrValue(aAttrEnum, newValue);
  2116. } else {
  2117. StringListAttributesInfo info = GetStringListInfo();
  2118. NS_ASSERTION(info.mStringListCount > 0,
  2119. "DidChangeStringList on element with no string list attribs");
  2120. NS_ASSERTION(aAttrEnum < info.mStringListCount, "aAttrEnum out of range");
  2121. name = *info.mStringListInfo[aAttrEnum].mName;
  2122. newValue.SetTo(info.mStringLists[aAttrEnum], nullptr);
  2123. }
  2124. DidChangeValue(name, aEmptyOrOldValue, newValue);
  2125. if (aIsConditionalProcessingAttribute) {
  2126. tests->MaybeInvalidate();
  2127. }
  2128. }
  2129. void
  2130. nsSVGElement::StringListAttributesInfo::Reset(uint8_t aAttrEnum)
  2131. {
  2132. mStringLists[aAttrEnum].Clear();
  2133. // caller notifies
  2134. }
  2135. nsresult
  2136. nsSVGElement::ReportAttributeParseFailure(nsIDocument* aDocument,
  2137. nsIAtom* aAttribute,
  2138. const nsAString& aValue)
  2139. {
  2140. const nsAFlatString& attributeValue = PromiseFlatString(aValue);
  2141. const char16_t *strings[] = { aAttribute->GetUTF16String(),
  2142. attributeValue.get() };
  2143. return SVGContentUtils::ReportToConsole(aDocument,
  2144. "AttributeParseWarning",
  2145. strings, ArrayLength(strings));
  2146. }
  2147. void
  2148. nsSVGElement::RecompileScriptEventListeners()
  2149. {
  2150. int32_t i, count = mAttrsAndChildren.AttrCount();
  2151. for (i = 0; i < count; ++i) {
  2152. const nsAttrName *name = mAttrsAndChildren.AttrNameAt(i);
  2153. // Eventlistenener-attributes are always in the null namespace
  2154. if (!name->IsAtom()) {
  2155. continue;
  2156. }
  2157. nsIAtom *attr = name->Atom();
  2158. if (!IsEventAttributeName(attr)) {
  2159. continue;
  2160. }
  2161. nsAutoString value;
  2162. GetAttr(kNameSpaceID_None, attr, value);
  2163. SetEventHandler(GetEventNameForAttr(attr), value, true);
  2164. }
  2165. }
  2166. nsISMILAttr*
  2167. nsSVGElement::GetAnimatedAttr(int32_t aNamespaceID, nsIAtom* aName)
  2168. {
  2169. if (aNamespaceID == kNameSpaceID_None) {
  2170. // We check mapped-into-style attributes first so that animations
  2171. // targeting width/height on outer-<svg> don't appear to be ignored
  2172. // because we returned a nsISMILAttr for the corresponding
  2173. // SVGAnimatedLength.
  2174. // Mapped attributes:
  2175. if (IsAttributeMapped(aName)) {
  2176. nsCSSPropertyID prop =
  2177. nsCSSProps::LookupProperty(nsDependentAtomString(aName),
  2178. CSSEnabledState::eForAllContent);
  2179. // Check IsPropertyAnimatable to avoid attributes that...
  2180. // - map to explicitly unanimatable properties (e.g. 'direction')
  2181. // - map to unsupported attributes (e.g. 'glyph-orientation-horizontal')
  2182. if (nsSMILCSSProperty::IsPropertyAnimatable(prop)) {
  2183. return new nsSMILMappedAttribute(prop, this);
  2184. }
  2185. }
  2186. // Transforms:
  2187. if (GetTransformListAttrName() == aName) {
  2188. // The transform attribute is being animated, so we must ensure that the
  2189. // SVGAnimatedTransformList is/has been allocated:
  2190. return GetAnimatedTransformList(DO_ALLOCATE)->ToSMILAttr(this);
  2191. }
  2192. // Motion (fake 'attribute' for animateMotion)
  2193. if (aName == nsGkAtoms::mozAnimateMotionDummyAttr) {
  2194. return new SVGMotionSMILAttr(this);
  2195. }
  2196. // Lengths:
  2197. LengthAttributesInfo info = GetLengthInfo();
  2198. for (uint32_t i = 0; i < info.mLengthCount; i++) {
  2199. if (aName == *info.mLengthInfo[i].mName) {
  2200. return info.mLengths[i].ToSMILAttr(this);
  2201. }
  2202. }
  2203. // Numbers:
  2204. {
  2205. NumberAttributesInfo info = GetNumberInfo();
  2206. for (uint32_t i = 0; i < info.mNumberCount; i++) {
  2207. if (aName == *info.mNumberInfo[i].mName) {
  2208. return info.mNumbers[i].ToSMILAttr(this);
  2209. }
  2210. }
  2211. }
  2212. // Number Pairs:
  2213. {
  2214. NumberPairAttributesInfo info = GetNumberPairInfo();
  2215. for (uint32_t i = 0; i < info.mNumberPairCount; i++) {
  2216. if (aName == *info.mNumberPairInfo[i].mName) {
  2217. return info.mNumberPairs[i].ToSMILAttr(this);
  2218. }
  2219. }
  2220. }
  2221. // Integers:
  2222. {
  2223. IntegerAttributesInfo info = GetIntegerInfo();
  2224. for (uint32_t i = 0; i < info.mIntegerCount; i++) {
  2225. if (aName == *info.mIntegerInfo[i].mName) {
  2226. return info.mIntegers[i].ToSMILAttr(this);
  2227. }
  2228. }
  2229. }
  2230. // Integer Pairs:
  2231. {
  2232. IntegerPairAttributesInfo info = GetIntegerPairInfo();
  2233. for (uint32_t i = 0; i < info.mIntegerPairCount; i++) {
  2234. if (aName == *info.mIntegerPairInfo[i].mName) {
  2235. return info.mIntegerPairs[i].ToSMILAttr(this);
  2236. }
  2237. }
  2238. }
  2239. // Enumerations:
  2240. {
  2241. EnumAttributesInfo info = GetEnumInfo();
  2242. for (uint32_t i = 0; i < info.mEnumCount; i++) {
  2243. if (aName == *info.mEnumInfo[i].mName) {
  2244. return info.mEnums[i].ToSMILAttr(this);
  2245. }
  2246. }
  2247. }
  2248. // Booleans:
  2249. {
  2250. BooleanAttributesInfo info = GetBooleanInfo();
  2251. for (uint32_t i = 0; i < info.mBooleanCount; i++) {
  2252. if (aName == *info.mBooleanInfo[i].mName) {
  2253. return info.mBooleans[i].ToSMILAttr(this);
  2254. }
  2255. }
  2256. }
  2257. // Angles:
  2258. {
  2259. AngleAttributesInfo info = GetAngleInfo();
  2260. for (uint32_t i = 0; i < info.mAngleCount; i++) {
  2261. if (aName == *info.mAngleInfo[i].mName) {
  2262. return info.mAngles[i].ToSMILAttr(this);
  2263. }
  2264. }
  2265. }
  2266. // viewBox:
  2267. if (aName == nsGkAtoms::viewBox) {
  2268. nsSVGViewBox *viewBox = GetViewBox();
  2269. return viewBox ? viewBox->ToSMILAttr(this) : nullptr;
  2270. }
  2271. // preserveAspectRatio:
  2272. if (aName == nsGkAtoms::preserveAspectRatio) {
  2273. SVGAnimatedPreserveAspectRatio *preserveAspectRatio =
  2274. GetPreserveAspectRatio();
  2275. return preserveAspectRatio ?
  2276. preserveAspectRatio->ToSMILAttr(this) : nullptr;
  2277. }
  2278. // NumberLists:
  2279. {
  2280. NumberListAttributesInfo info = GetNumberListInfo();
  2281. for (uint32_t i = 0; i < info.mNumberListCount; i++) {
  2282. if (aName == *info.mNumberListInfo[i].mName) {
  2283. MOZ_ASSERT(i <= UCHAR_MAX, "Too many attributes");
  2284. return info.mNumberLists[i].ToSMILAttr(this, uint8_t(i));
  2285. }
  2286. }
  2287. }
  2288. // LengthLists:
  2289. {
  2290. LengthListAttributesInfo info = GetLengthListInfo();
  2291. for (uint32_t i = 0; i < info.mLengthListCount; i++) {
  2292. if (aName == *info.mLengthListInfo[i].mName) {
  2293. MOZ_ASSERT(i <= UCHAR_MAX, "Too many attributes");
  2294. return info.mLengthLists[i].ToSMILAttr(this,
  2295. uint8_t(i),
  2296. info.mLengthListInfo[i].mAxis,
  2297. info.mLengthListInfo[i].mCouldZeroPadList);
  2298. }
  2299. }
  2300. }
  2301. // PointLists:
  2302. {
  2303. if (GetPointListAttrName() == aName) {
  2304. SVGAnimatedPointList *pointList = GetAnimatedPointList();
  2305. if (pointList) {
  2306. return pointList->ToSMILAttr(this);
  2307. }
  2308. }
  2309. }
  2310. // PathSegLists:
  2311. {
  2312. if (GetPathDataAttrName() == aName) {
  2313. SVGAnimatedPathSegList *segList = GetAnimPathSegList();
  2314. if (segList) {
  2315. return segList->ToSMILAttr(this);
  2316. }
  2317. }
  2318. }
  2319. if (aName == nsGkAtoms::_class) {
  2320. return mClassAttribute.ToSMILAttr(this);
  2321. }
  2322. }
  2323. // Strings
  2324. {
  2325. StringAttributesInfo info = GetStringInfo();
  2326. for (uint32_t i = 0; i < info.mStringCount; i++) {
  2327. if (aNamespaceID == info.mStringInfo[i].mNamespaceID &&
  2328. aName == *info.mStringInfo[i].mName) {
  2329. return info.mStrings[i].ToSMILAttr(this);
  2330. }
  2331. }
  2332. }
  2333. return nullptr;
  2334. }
  2335. void
  2336. nsSVGElement::AnimationNeedsResample()
  2337. {
  2338. nsIDocument* doc = GetComposedDoc();
  2339. if (doc && doc->HasAnimationController()) {
  2340. doc->GetAnimationController()->SetResampleNeeded();
  2341. }
  2342. }
  2343. void
  2344. nsSVGElement::FlushAnimations()
  2345. {
  2346. nsIDocument* doc = GetComposedDoc();
  2347. if (doc && doc->HasAnimationController()) {
  2348. doc->GetAnimationController()->FlushResampleRequests();
  2349. }
  2350. }