1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is the Mozilla SVG project.
16 : *
17 : * The Initial Developer of the Original Code is IBM Corporation.
18 : * Portions created by the Initial Developer are Copyright (C) 2006
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : *
23 : * Alternatively, the contents of this file may be used under the terms of
24 : * either of the GNU General Public License Version 2 or later (the "GPL"),
25 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 : * in which case the provisions of the GPL or the LGPL are applicable instead
27 : * of those above. If you wish to allow use of your version of this file only
28 : * under the terms of either the GPL or the LGPL, and not to allow others to
29 : * use your version of this file under the terms of the MPL, indicate your
30 : * decision by deleting the provisions above and replace them with the notice
31 : * and other provisions required by the GPL or the LGPL. If you do not delete
32 : * the provisions above, a recipient may use your version of this file under
33 : * the terms of any one of the MPL, the GPL or the LGPL.
34 : *
35 : * ***** END LICENSE BLOCK ***** */
36 :
37 : #include "mozilla/Util.h"
38 :
39 : #include "nsSVGSwitchElement.h"
40 : #include "DOMSVGTests.h"
41 : #include "nsIFrame.h"
42 : #include "nsSVGUtils.h"
43 : #include "mozilla/Preferences.h"
44 :
45 : using namespace mozilla;
46 :
47 : ////////////////////////////////////////////////////////////////////////
48 : // implementation
49 :
50 :
51 0 : NS_IMPL_NS_NEW_SVG_ELEMENT(Switch)
52 :
53 :
54 : //----------------------------------------------------------------------
55 : // nsISupports methods
56 :
57 1464 : NS_IMPL_CYCLE_COLLECTION_CLASS(nsSVGSwitchElement)
58 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(nsSVGSwitchElement,
59 : nsSVGSwitchElementBase)
60 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mActiveChild)
61 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
62 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(nsSVGSwitchElement,
63 : nsSVGSwitchElementBase)
64 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mActiveChild)
65 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_END
66 :
67 0 : NS_IMPL_ADDREF_INHERITED(nsSVGSwitchElement,nsSVGSwitchElementBase)
68 0 : NS_IMPL_RELEASE_INHERITED(nsSVGSwitchElement,nsSVGSwitchElementBase)
69 :
70 0 : DOMCI_NODE_DATA(SVGSwitchElement, nsSVGSwitchElement)
71 :
72 0 : NS_INTERFACE_TABLE_HEAD_CYCLE_COLLECTION_INHERITED(nsSVGSwitchElement)
73 0 : NS_NODE_INTERFACE_TABLE5(nsSVGSwitchElement, nsIDOMNode, nsIDOMElement,
74 : nsIDOMSVGElement, nsIDOMSVGSwitchElement,
75 : nsIDOMSVGTests)
76 0 : NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(SVGSwitchElement)
77 0 : NS_INTERFACE_MAP_END_INHERITING(nsSVGSwitchElementBase)
78 :
79 : //----------------------------------------------------------------------
80 : // Implementation
81 :
82 0 : nsSVGSwitchElement::nsSVGSwitchElement(already_AddRefed<nsINodeInfo> aNodeInfo)
83 0 : : nsSVGSwitchElementBase(aNodeInfo)
84 : {
85 0 : }
86 :
87 : void
88 0 : nsSVGSwitchElement::MaybeInvalidate()
89 : {
90 : // We don't reuse UpdateActiveChild() and check if mActiveChild has changed
91 : // to determine if we should invalidate. If we did that,
92 : // nsSVGUtils::UpdateGraphic would not invalidate the old mActiveChild area!
93 :
94 0 : if (FindActiveChild() == mActiveChild) {
95 0 : return;
96 : }
97 :
98 0 : nsIFrame *frame = GetPrimaryFrame();
99 0 : if (frame) {
100 0 : nsSVGUtils::UpdateGraphic(frame);
101 : }
102 : }
103 :
104 : //----------------------------------------------------------------------
105 : // nsIDOMNode methods
106 :
107 :
108 0 : NS_IMPL_ELEMENT_CLONE_WITH_INIT(nsSVGSwitchElement)
109 :
110 : //----------------------------------------------------------------------
111 : // nsINode methods
112 :
113 : nsresult
114 0 : nsSVGSwitchElement::InsertChildAt(nsIContent* aKid,
115 : PRUint32 aIndex,
116 : bool aNotify)
117 : {
118 0 : nsresult rv = nsSVGSwitchElementBase::InsertChildAt(aKid, aIndex, aNotify);
119 0 : if (NS_SUCCEEDED(rv)) {
120 0 : MaybeInvalidate();
121 : }
122 0 : return rv;
123 : }
124 :
125 : nsresult
126 0 : nsSVGSwitchElement::RemoveChildAt(PRUint32 aIndex, bool aNotify)
127 : {
128 0 : nsresult rv = nsSVGSwitchElementBase::RemoveChildAt(aIndex, aNotify);
129 0 : if (NS_SUCCEEDED(rv)) {
130 0 : MaybeInvalidate();
131 : }
132 0 : return rv;
133 : }
134 :
135 : //----------------------------------------------------------------------
136 : // nsIContent methods
137 :
138 : NS_IMETHODIMP_(bool)
139 0 : nsSVGSwitchElement::IsAttributeMapped(const nsIAtom* name) const
140 : {
141 : static const MappedAttributeEntry* const map[] = {
142 : sFEFloodMap,
143 : sFiltersMap,
144 : sFontSpecificationMap,
145 : sGradientStopMap,
146 : sLightingEffectsMap,
147 : sMarkersMap,
148 : sTextContentElementsMap,
149 : sViewportsMap
150 : };
151 :
152 0 : return FindAttributeDependence(name, map) ||
153 0 : nsSVGSwitchElementBase::IsAttributeMapped(name);
154 : }
155 :
156 : //----------------------------------------------------------------------
157 : // Implementation Helpers:
158 :
159 : nsIContent *
160 0 : nsSVGSwitchElement::FindActiveChild() const
161 : {
162 : bool allowReorder = AttrValueIs(kNameSpaceID_None,
163 : nsGkAtoms::allowReorder,
164 0 : nsGkAtoms::yes, eCaseMatters);
165 :
166 : const nsAdoptingString& acceptLangs =
167 0 : Preferences::GetLocalizedString("intl.accept_languages");
168 :
169 0 : if (allowReorder && !acceptLangs.IsEmpty()) {
170 0 : PRInt32 bestLanguagePreferenceRank = -1;
171 0 : nsIContent *bestChild = nsnull;
172 0 : for (nsIContent* child = nsINode::GetFirstChild();
173 : child;
174 0 : child = child->GetNextSibling()) {
175 :
176 0 : if (!child->IsElement()) {
177 0 : continue;
178 : }
179 0 : nsCOMPtr<DOMSVGTests> tests(do_QueryInterface(child));
180 0 : if (tests) {
181 0 : if (tests->PassesConditionalProcessingTests(
182 0 : DOMSVGTests::kIgnoreSystemLanguage)) {
183 : PRInt32 languagePreferenceRank =
184 0 : tests->GetBestLanguagePreferenceRank(acceptLangs);
185 0 : switch (languagePreferenceRank) {
186 : case 0:
187 : // best possible match
188 0 : return child;
189 : case -1:
190 : // not found
191 0 : break;
192 : default:
193 0 : if (bestLanguagePreferenceRank == -1 ||
194 : languagePreferenceRank < bestLanguagePreferenceRank) {
195 0 : bestLanguagePreferenceRank = languagePreferenceRank;
196 0 : bestChild = child;
197 : }
198 0 : break;
199 : }
200 : }
201 0 : } else if (!bestChild) {
202 0 : bestChild = child;
203 : }
204 : }
205 0 : return bestChild;
206 : }
207 :
208 0 : for (nsIContent* child = nsINode::GetFirstChild();
209 : child;
210 0 : child = child->GetNextSibling()) {
211 0 : if (!child->IsElement()) {
212 0 : continue;
213 : }
214 0 : nsCOMPtr<DOMSVGTests> tests(do_QueryInterface(child));
215 0 : if (!tests || tests->PassesConditionalProcessingTests(&acceptLangs)) {
216 0 : return child;
217 : }
218 : }
219 0 : return nsnull;
220 4392 : }
|