SVGViewFrame.cpp (3797B)
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 // Keep in (case-insensitive) order: 8 #include "mozilla/PresShell.h" 9 #include "mozilla/SVGOuterSVGFrame.h" 10 #include "mozilla/SVGUtils.h" 11 #include "mozilla/dom/SVGSVGElement.h" 12 #include "mozilla/dom/SVGViewElement.h" 13 #include "nsGkAtoms.h" 14 #include "nsIFrame.h" 15 16 using namespace mozilla::dom; 17 18 nsIFrame* NS_NewSVGViewFrame(mozilla::PresShell* aPresShell, 19 mozilla::ComputedStyle* aStyle); 20 21 namespace mozilla { 22 23 /** 24 * While views are not directly rendered in SVG they can be linked to 25 * and thereby override attributes of an <svg> element via a fragment 26 * identifier. The SVGViewFrame class passes on any attribute changes 27 * the view receives to the overridden <svg> element (if there is one). 28 **/ 29 class SVGViewFrame final : public nsIFrame { 30 friend nsIFrame* ::NS_NewSVGViewFrame(mozilla::PresShell* aPresShell, 31 ComputedStyle* aStyle); 32 33 protected: 34 explicit SVGViewFrame(ComputedStyle* aStyle, nsPresContext* aPresContext) 35 : nsIFrame(aStyle, aPresContext, kClassID) { 36 AddStateBits(NS_FRAME_SVG_LAYOUT | NS_FRAME_IS_NONDISPLAY); 37 } 38 39 public: 40 NS_DECL_FRAMEARENA_HELPERS(SVGViewFrame) 41 42 #ifdef DEBUG 43 void Init(nsIContent* aContent, nsContainerFrame* aParent, 44 nsIFrame* aPrevInFlow) override; 45 #endif 46 47 #ifdef DEBUG_FRAME_DUMP 48 nsresult GetFrameName(nsAString& aResult) const override { 49 return MakeFrameName(u"SVGView"_ns, aResult); 50 } 51 #endif 52 53 nsresult AttributeChanged(int32_t aNameSpaceID, nsAtom* aAttribute, 54 AttrModType aModType) override; 55 56 bool ComputeCustomOverflow(OverflowAreas& aOverflowAreas) override { 57 // We don't maintain a ink overflow rect 58 return false; 59 } 60 }; 61 62 } // namespace mozilla 63 64 nsIFrame* NS_NewSVGViewFrame(mozilla::PresShell* aPresShell, 65 mozilla::ComputedStyle* aStyle) { 66 return new (aPresShell) 67 mozilla::SVGViewFrame(aStyle, aPresShell->GetPresContext()); 68 } 69 70 namespace mozilla { 71 72 NS_IMPL_FRAMEARENA_HELPERS(SVGViewFrame) 73 74 #ifdef DEBUG 75 void SVGViewFrame::Init(nsIContent* aContent, nsContainerFrame* aParent, 76 nsIFrame* aPrevInFlow) { 77 NS_ASSERTION(aContent->IsSVGElement(nsGkAtoms::view), 78 "Content is not an SVG view"); 79 80 nsIFrame::Init(aContent, aParent, aPrevInFlow); 81 } 82 #endif /* DEBUG */ 83 84 nsresult SVGViewFrame::AttributeChanged(int32_t aNameSpaceID, 85 nsAtom* aAttribute, 86 AttrModType aModType) { 87 // Ignore zoomAndPan as it does not cause the <svg> element to re-render 88 89 if (aNameSpaceID == kNameSpaceID_None && 90 (aAttribute == nsGkAtoms::preserveAspectRatio || 91 aAttribute == nsGkAtoms::viewBox)) { 92 SVGOuterSVGFrame* outerSVGFrame = SVGUtils::GetOuterSVGFrame(this); 93 NS_ASSERTION(outerSVGFrame->GetContent()->IsSVGElement(nsGkAtoms::svg), 94 "Expecting an <svg> element"); 95 96 SVGSVGElement* svgElement = 97 static_cast<SVGSVGElement*>(outerSVGFrame->GetContent()); 98 99 nsAutoString viewID; 100 mContent->AsElement()->GetAttr(nsGkAtoms::id, viewID); 101 102 if (svgElement->IsOverriddenBy(viewID)) { 103 // We're the view that's providing overrides, so pretend that the frame 104 // we're overriding was updated. 105 outerSVGFrame->AttributeChanged(aNameSpaceID, aAttribute, aModType); 106 } 107 } 108 109 return nsIFrame::AttributeChanged(aNameSpaceID, aAttribute, aModType); 110 } 111 112 } // namespace mozilla