SVGForeignObjectElement.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 "nsCOMPtr.h"
  7. #include "mozilla/dom/SVGDocument.h"
  8. #include "mozilla/dom/SVGForeignObjectElement.h"
  9. #include "mozilla/dom/SVGForeignObjectElementBinding.h"
  10. NS_IMPL_NS_NEW_NAMESPACED_SVG_ELEMENT(ForeignObject)
  11. namespace mozilla {
  12. namespace dom {
  13. JSObject*
  14. SVGForeignObjectElement::WrapNode(JSContext *aCx, JS::Handle<JSObject*> aGivenProto)
  15. {
  16. return SVGForeignObjectElementBinding::Wrap(aCx, this, aGivenProto);
  17. }
  18. nsSVGElement::LengthInfo SVGForeignObjectElement::sLengthInfo[4] =
  19. {
  20. { &nsGkAtoms::x, 0, nsIDOMSVGLength::SVG_LENGTHTYPE_NUMBER, SVGContentUtils::X },
  21. { &nsGkAtoms::y, 0, nsIDOMSVGLength::SVG_LENGTHTYPE_NUMBER, SVGContentUtils::Y },
  22. { &nsGkAtoms::width, 0, nsIDOMSVGLength::SVG_LENGTHTYPE_NUMBER, SVGContentUtils::X },
  23. { &nsGkAtoms::height, 0, nsIDOMSVGLength::SVG_LENGTHTYPE_NUMBER, SVGContentUtils::Y },
  24. };
  25. //----------------------------------------------------------------------
  26. // Implementation
  27. SVGForeignObjectElement::SVGForeignObjectElement(already_AddRefed<mozilla::dom::NodeInfo>& aNodeInfo)
  28. : SVGGraphicsElement(aNodeInfo)
  29. {
  30. }
  31. //----------------------------------------------------------------------
  32. // nsIDOMNode methods
  33. NS_IMPL_ELEMENT_CLONE_WITH_INIT(SVGForeignObjectElement)
  34. //----------------------------------------------------------------------
  35. already_AddRefed<SVGAnimatedLength>
  36. SVGForeignObjectElement::X()
  37. {
  38. return mLengthAttributes[ATTR_X].ToDOMAnimatedLength(this);
  39. }
  40. already_AddRefed<SVGAnimatedLength>
  41. SVGForeignObjectElement::Y()
  42. {
  43. return mLengthAttributes[ATTR_Y].ToDOMAnimatedLength(this);
  44. }
  45. already_AddRefed<SVGAnimatedLength>
  46. SVGForeignObjectElement::Width()
  47. {
  48. return mLengthAttributes[ATTR_WIDTH].ToDOMAnimatedLength(this);
  49. }
  50. already_AddRefed<SVGAnimatedLength>
  51. SVGForeignObjectElement::Height()
  52. {
  53. return mLengthAttributes[ATTR_HEIGHT].ToDOMAnimatedLength(this);
  54. }
  55. //----------------------------------------------------------------------
  56. // nsSVGElement methods
  57. /* virtual */ gfxMatrix
  58. SVGForeignObjectElement::PrependLocalTransformsTo(
  59. const gfxMatrix &aMatrix, SVGTransformTypes aWhich) const
  60. {
  61. // 'transform' attribute:
  62. gfxMatrix fromUserSpace =
  63. SVGGraphicsElement::PrependLocalTransformsTo(aMatrix, aWhich);
  64. if (aWhich == eUserSpaceToParent) {
  65. return fromUserSpace;
  66. }
  67. // our 'x' and 'y' attributes:
  68. float x, y;
  69. const_cast<SVGForeignObjectElement*>(this)->
  70. GetAnimatedLengthValues(&x, &y, nullptr);
  71. gfxMatrix toUserSpace = gfxMatrix::Translation(x, y);
  72. if (aWhich == eChildToUserSpace) {
  73. return toUserSpace * aMatrix;
  74. }
  75. MOZ_ASSERT(aWhich == eAllTransforms, "Unknown TransformTypes");
  76. return toUserSpace * fromUserSpace;
  77. }
  78. /* virtual */ bool
  79. SVGForeignObjectElement::HasValidDimensions() const
  80. {
  81. return mLengthAttributes[ATTR_WIDTH].IsExplicitlySet() &&
  82. mLengthAttributes[ATTR_WIDTH].GetAnimValInSpecifiedUnits() > 0 &&
  83. mLengthAttributes[ATTR_HEIGHT].IsExplicitlySet() &&
  84. mLengthAttributes[ATTR_HEIGHT].GetAnimValInSpecifiedUnits() > 0;
  85. }
  86. //----------------------------------------------------------------------
  87. // nsIContent methods
  88. nsresult
  89. SVGForeignObjectElement::BindToTree(nsIDocument* aDocument,
  90. nsIContent* aParent,
  91. nsIContent* aBindingParent,
  92. bool aCompileEventHandlers)
  93. {
  94. nsresult rv = SVGGraphicsElement::BindToTree(aDocument, aParent,
  95. aBindingParent,
  96. aCompileEventHandlers);
  97. NS_ENSURE_SUCCESS(rv, rv);
  98. nsIDocument* doc = GetComposedDoc();
  99. if (doc && doc->IsSVGDocument()) {
  100. // We assume that we're going to have HTML content, so we ensure that the
  101. // UA style sheets that nsDocumentViewer::CreateStyleSet skipped when
  102. // it saw the document was an SVG document are loaded.
  103. //
  104. // We setup these style sheets during binding, not element construction,
  105. // because elements can be moved from the document that creates them to
  106. // another document.
  107. doc->AsSVGDocument()->EnsureNonSVGUserAgentStyleSheetsLoaded();
  108. }
  109. return rv;
  110. }
  111. NS_IMETHODIMP_(bool)
  112. SVGForeignObjectElement::IsAttributeMapped(const nsIAtom* name) const
  113. {
  114. static const MappedAttributeEntry* const map[] = {
  115. sFEFloodMap,
  116. sFiltersMap,
  117. sFontSpecificationMap,
  118. sGradientStopMap,
  119. sLightingEffectsMap,
  120. sMarkersMap,
  121. sTextContentElementsMap,
  122. sViewportsMap
  123. };
  124. return FindAttributeDependence(name, map) ||
  125. SVGGraphicsElement::IsAttributeMapped(name);
  126. }
  127. //----------------------------------------------------------------------
  128. // nsSVGElement methods
  129. nsSVGElement::LengthAttributesInfo
  130. SVGForeignObjectElement::GetLengthInfo()
  131. {
  132. return LengthAttributesInfo(mLengthAttributes, sLengthInfo,
  133. ArrayLength(sLengthInfo));
  134. }
  135. } // namespace dom
  136. } // namespace mozilla