SVGForeignObjectElement.cpp (4373B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "mozilla/dom/SVGForeignObjectElement.h" 8 9 #include "SVGGeometryProperty.h" 10 #include "mozilla/AlreadyAddRefed.h" 11 #include "mozilla/dom/SVGDocument.h" 12 #include "mozilla/dom/SVGForeignObjectElementBinding.h" 13 #include "mozilla/dom/SVGLengthBinding.h" 14 15 NS_IMPL_NS_NEW_SVG_ELEMENT(ForeignObject) 16 17 namespace mozilla::dom { 18 19 JSObject* SVGForeignObjectElement::WrapNode(JSContext* aCx, 20 JS::Handle<JSObject*> aGivenProto) { 21 return SVGForeignObjectElement_Binding::Wrap(aCx, this, aGivenProto); 22 } 23 24 SVGElement::LengthInfo SVGForeignObjectElement::sLengthInfo[4] = { 25 {nsGkAtoms::x, 0, SVGLength_Binding::SVG_LENGTHTYPE_NUMBER, 26 SVGContentUtils::X}, 27 {nsGkAtoms::y, 0, SVGLength_Binding::SVG_LENGTHTYPE_NUMBER, 28 SVGContentUtils::Y}, 29 {nsGkAtoms::width, 0, SVGLength_Binding::SVG_LENGTHTYPE_NUMBER, 30 SVGContentUtils::X}, 31 {nsGkAtoms::height, 0, SVGLength_Binding::SVG_LENGTHTYPE_NUMBER, 32 SVGContentUtils::Y}, 33 }; 34 35 //---------------------------------------------------------------------- 36 // Implementation 37 38 SVGForeignObjectElement::SVGForeignObjectElement( 39 already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo) 40 : SVGGraphicsElement(std::move(aNodeInfo)) {} 41 42 namespace SVGT = SVGGeometryProperty::Tags; 43 44 //---------------------------------------------------------------------- 45 // nsINode methods 46 47 NS_IMPL_ELEMENT_CLONE_WITH_INIT(SVGForeignObjectElement) 48 49 //---------------------------------------------------------------------- 50 51 already_AddRefed<DOMSVGAnimatedLength> SVGForeignObjectElement::X() { 52 return mLengthAttributes[ATTR_X].ToDOMAnimatedLength(this); 53 } 54 55 already_AddRefed<DOMSVGAnimatedLength> SVGForeignObjectElement::Y() { 56 return mLengthAttributes[ATTR_Y].ToDOMAnimatedLength(this); 57 } 58 59 already_AddRefed<DOMSVGAnimatedLength> SVGForeignObjectElement::Width() { 60 return mLengthAttributes[ATTR_WIDTH].ToDOMAnimatedLength(this); 61 } 62 63 already_AddRefed<DOMSVGAnimatedLength> SVGForeignObjectElement::Height() { 64 return mLengthAttributes[ATTR_HEIGHT].ToDOMAnimatedLength(this); 65 } 66 67 //---------------------------------------------------------------------- 68 // SVGElement methods 69 70 gfxMatrix SVGForeignObjectElement::ChildToUserSpaceTransform() const { 71 // our 'x' and 'y' attributes: 72 float x, y; 73 if (!SVGGeometryProperty::ResolveAll<SVGT::X, SVGT::Y>(this, &x, &y)) { 74 // This function might be called for element in display:none subtree 75 // (e.g. getScreenCTM), we fall back to use SVG attributes. 76 const_cast<SVGForeignObjectElement*>(this)->GetAnimatedLengthValues( 77 &x, &y, nullptr); 78 } 79 return gfxMatrix::Translation(x, y); 80 } 81 82 /* virtual */ 83 bool SVGForeignObjectElement::HasValidDimensions() const { 84 float width, height; 85 86 DebugOnly<bool> ok = 87 SVGGeometryProperty::ResolveAll<SVGT::Width, SVGT::Height>(this, &width, 88 &height); 89 MOZ_ASSERT(ok, "SVGGeometryProperty::ResolveAll failed"); 90 return width > 0 && height > 0; 91 } 92 93 //---------------------------------------------------------------------- 94 // nsIContent methods 95 96 NS_IMETHODIMP_(bool) 97 SVGForeignObjectElement::IsAttributeMapped(const nsAtom* name) const { 98 return IsInLengthInfo(name, sLengthInfo) || 99 SVGGraphicsElement::IsAttributeMapped(name); 100 } 101 102 //---------------------------------------------------------------------- 103 // SVGElement methods 104 105 SVGElement::LengthAttributesInfo SVGForeignObjectElement::GetLengthInfo() { 106 return LengthAttributesInfo(mLengthAttributes, sLengthInfo, 107 std::size(sLengthInfo)); 108 } 109 110 NonCustomCSSPropertyId SVGForeignObjectElement::GetCSSPropertyIdForAttrEnum( 111 uint8_t aAttrEnum) { 112 switch (aAttrEnum) { 113 case ATTR_X: 114 return eCSSProperty_x; 115 case ATTR_Y: 116 return eCSSProperty_y; 117 case ATTR_WIDTH: 118 return eCSSProperty_width; 119 case ATTR_HEIGHT: 120 return eCSSProperty_height; 121 default: 122 MOZ_ASSERT_UNREACHABLE("Unknown attr enum"); 123 return eCSSProperty_UNKNOWN; 124 } 125 } 126 127 } // namespace mozilla::dom