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/. */
7 #include "mozilla/dom/SVGSwitchElement.h"
9 #include "nsLayoutUtils.h"
10 #include "mozilla/Preferences.h"
11 #include "mozilla/SVGUtils.h"
12 #include "mozilla/dom/SVGSwitchElementBinding.h"
16 NS_IMPL_NS_NEW_SVG_ELEMENT(Switch
)
21 JSObject
* SVGSwitchElement::WrapNode(JSContext
* aCx
,
22 JS::Handle
<JSObject
*> aGivenProto
) {
23 return SVGSwitchElement_Binding::Wrap(aCx
, this, aGivenProto
);
26 //----------------------------------------------------------------------
27 // nsISupports methods
29 NS_IMPL_CYCLE_COLLECTION_INHERITED(SVGSwitchElement
, SVGSwitchElementBase
,
32 NS_IMPL_ADDREF_INHERITED(SVGSwitchElement
, SVGSwitchElementBase
)
33 NS_IMPL_RELEASE_INHERITED(SVGSwitchElement
, SVGSwitchElementBase
)
35 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(SVGSwitchElement
)
36 NS_INTERFACE_MAP_END_INHERITING(SVGSwitchElementBase
)
38 //----------------------------------------------------------------------
41 SVGSwitchElement::SVGSwitchElement(
42 already_AddRefed
<mozilla::dom::NodeInfo
>&& aNodeInfo
)
43 : SVGSwitchElementBase(std::move(aNodeInfo
)) {}
45 void SVGSwitchElement::MaybeInvalidate() {
46 // We must not change mActiveChild until after
47 // InvalidateAndScheduleBoundsUpdate has been called, otherwise
48 // it will not correctly invalidate the old mActiveChild area.
50 nsIContent
* newActiveChild
= FindActiveChild();
52 if (newActiveChild
== mActiveChild
) {
56 nsIFrame
* frame
= GetPrimaryFrame();
58 nsLayoutUtils::PostRestyleEvent(this, RestyleHint
{0},
59 nsChangeHint_InvalidateRenderingObservers
);
60 SVGUtils::ScheduleReflowSVG(frame
);
63 mActiveChild
= newActiveChild
;
66 //----------------------------------------------------------------------
69 NS_IMPL_ELEMENT_CLONE_WITH_INIT(SVGSwitchElement
)
71 //----------------------------------------------------------------------
74 nsresult
SVGSwitchElement::InsertChildBefore(nsIContent
* aKid
,
75 nsIContent
* aBeforeThis
,
78 SVGSwitchElementBase::InsertChildBefore(aKid
, aBeforeThis
, aNotify
);
79 if (NS_SUCCEEDED(rv
)) {
85 void SVGSwitchElement::RemoveChildNode(nsIContent
* aKid
, bool aNotify
) {
86 SVGSwitchElementBase::RemoveChildNode(aKid
, aNotify
);
90 //----------------------------------------------------------------------
94 SVGSwitchElement::IsAttributeMapped(const nsAtom
* name
) const {
95 static const MappedAttributeEntry
* const map
[] = {sFEFloodMap
,
97 sFontSpecificationMap
,
101 sTextContentElementsMap
,
104 return FindAttributeDependence(name
, map
) ||
105 SVGSwitchElementBase::IsAttributeMapped(name
);
108 //----------------------------------------------------------------------
109 // Implementation Helpers:
111 nsIContent
* SVGSwitchElement::FindActiveChild() const {
112 nsAutoString acceptLangs
;
113 Preferences::GetLocalizedString("intl.accept_languages", acceptLangs
);
115 int32_t bestLanguagePreferenceRank
= -1;
116 nsIContent
* bestChild
= nullptr;
117 nsIContent
* defaultChild
= nullptr;
118 for (nsIContent
* child
= nsINode::GetFirstChild(); child
;
119 child
= child
->GetNextSibling()) {
120 if (!child
->IsElement()) {
123 nsCOMPtr
<SVGTests
> tests(do_QueryInterface(child
));
125 if (tests
->PassesConditionalProcessingTestsIgnoringSystemLanguage()) {
126 int32_t languagePreferenceRank
=
127 tests
->GetBestLanguagePreferenceRank(acceptLangs
);
128 switch (languagePreferenceRank
) {
130 // best possible match
136 // no systemLanguage attribute. If there's nothing better
137 // we'll use the first such child.
139 defaultChild
= child
;
143 if (bestLanguagePreferenceRank
== -1 ||
144 languagePreferenceRank
< bestLanguagePreferenceRank
) {
145 bestLanguagePreferenceRank
= languagePreferenceRank
;
151 } else if (!bestChild
) {
155 return bestChild
? bestChild
: defaultChild
;
159 } // namespace mozilla