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
18 : * Robert Longson <longsonr@gmail.com>
19 : * Portions created by the Initial Developer are Copyright (C) 2011
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either of the GNU General Public License Version 2 or later (the "GPL"),
26 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #include "DOMSVGTests.h"
39 : #include "DOMSVGStringList.h"
40 : #include "nsSVGFeatures.h"
41 : #include "nsSVGSwitchElement.h"
42 : #include "nsCharSeparatedTokenizer.h"
43 : #include "nsStyleUtil.h"
44 : #include "nsSVGUtils.h"
45 : #include "mozilla/Preferences.h"
46 :
47 : using namespace mozilla;
48 :
49 0 : NS_IMPL_ISUPPORTS1(DOMSVGTests, nsIDOMSVGTests)
50 :
51 : nsIAtom** DOMSVGTests::sStringListNames[3] =
52 : {
53 : &nsGkAtoms::requiredFeatures,
54 : &nsGkAtoms::requiredExtensions,
55 : &nsGkAtoms::systemLanguage,
56 : };
57 :
58 0 : DOMSVGTests::DOMSVGTests()
59 : {
60 0 : mStringListAttributes[LANGUAGE].SetIsCommaSeparated(true);
61 0 : }
62 :
63 : /* readonly attribute nsIDOMSVGStringList requiredFeatures; */
64 : NS_IMETHODIMP
65 0 : DOMSVGTests::GetRequiredFeatures(nsIDOMSVGStringList * *aRequiredFeatures)
66 : {
67 0 : nsCOMPtr<nsSVGElement> element = do_QueryInterface(this);
68 : *aRequiredFeatures = DOMSVGStringList::GetDOMWrapper(
69 0 : &mStringListAttributes[FEATURES], element, true, FEATURES).get();
70 0 : return NS_OK;
71 : }
72 :
73 : /* readonly attribute nsIDOMSVGStringList requiredExtensions; */
74 : NS_IMETHODIMP
75 0 : DOMSVGTests::GetRequiredExtensions(nsIDOMSVGStringList * *aRequiredExtensions)
76 : {
77 0 : nsCOMPtr<nsSVGElement> element = do_QueryInterface(this);
78 : *aRequiredExtensions = DOMSVGStringList::GetDOMWrapper(
79 0 : &mStringListAttributes[EXTENSIONS], element, true, EXTENSIONS).get();
80 0 : return NS_OK;
81 : }
82 :
83 : /* readonly attribute nsIDOMSVGStringList systemLanguage; */
84 : NS_IMETHODIMP
85 0 : DOMSVGTests::GetSystemLanguage(nsIDOMSVGStringList * *aSystemLanguage)
86 : {
87 0 : nsCOMPtr<nsSVGElement> element = do_QueryInterface(this);
88 : *aSystemLanguage = DOMSVGStringList::GetDOMWrapper(
89 0 : &mStringListAttributes[LANGUAGE], element, true, LANGUAGE).get();
90 0 : return NS_OK;
91 : }
92 :
93 : /* boolean hasExtension (in DOMString extension); */
94 : NS_IMETHODIMP
95 0 : DOMSVGTests::HasExtension(const nsAString & extension, bool *_retval)
96 : {
97 0 : *_retval = nsSVGFeatures::HasExtension(extension);
98 0 : return NS_OK;
99 : }
100 :
101 : bool
102 0 : DOMSVGTests::IsConditionalProcessingAttribute(const nsIAtom* aAttribute) const
103 : {
104 0 : for (PRUint32 i = 0; i < ArrayLength(sStringListNames); i++) {
105 0 : if (aAttribute == *sStringListNames[i]) {
106 0 : return true;
107 : }
108 : }
109 0 : return false;
110 : }
111 :
112 : PRInt32
113 0 : DOMSVGTests::GetBestLanguagePreferenceRank(const nsSubstring& aAcceptLangs) const
114 : {
115 0 : const nsDefaultStringComparator defaultComparator;
116 :
117 0 : PRInt32 lowestRank = -1;
118 :
119 0 : for (PRUint32 i = 0; i < mStringListAttributes[LANGUAGE].Length(); i++) {
120 0 : nsCharSeparatedTokenizer languageTokenizer(aAcceptLangs, ',');
121 0 : PRInt32 index = 0;
122 0 : while (languageTokenizer.hasMoreTokens()) {
123 0 : const nsSubstring &languageToken = languageTokenizer.nextToken();
124 0 : bool exactMatch = (languageToken == mStringListAttributes[LANGUAGE][i]);
125 : bool prefixOnlyMatch =
126 0 : !exactMatch &&
127 0 : nsStyleUtil::DashMatchCompare(mStringListAttributes[LANGUAGE][i],
128 0 : languageTokenizer.nextToken(),
129 0 : defaultComparator);
130 0 : if (index == 0 && exactMatch) {
131 : // best possible match
132 0 : return 0;
133 : }
134 0 : if ((exactMatch || prefixOnlyMatch) &&
135 : (lowestRank == -1 || 2 * index + prefixOnlyMatch < lowestRank)) {
136 0 : lowestRank = 2 * index + prefixOnlyMatch;
137 : }
138 0 : ++index;
139 : }
140 : }
141 0 : return lowestRank;
142 : }
143 :
144 : const nsString * const DOMSVGTests::kIgnoreSystemLanguage = (nsString *) 0x01;
145 :
146 : bool
147 0 : DOMSVGTests::PassesConditionalProcessingTests(const nsString *aAcceptLangs) const
148 : {
149 : // Required Features
150 0 : if (mStringListAttributes[FEATURES].IsExplicitlySet()) {
151 0 : if (mStringListAttributes[FEATURES].IsEmpty()) {
152 0 : return false;
153 : }
154 : nsCOMPtr<nsIContent> content(
155 0 : do_QueryInterface(const_cast<DOMSVGTests*>(this)));
156 :
157 0 : for (PRUint32 i = 0; i < mStringListAttributes[FEATURES].Length(); i++) {
158 0 : if (!nsSVGFeatures::HasFeature(content, mStringListAttributes[FEATURES][i])) {
159 0 : return false;
160 : }
161 : }
162 : }
163 :
164 : // Required Extensions
165 : //
166 : // The requiredExtensions attribute defines a list of required language
167 : // extensions. Language extensions are capabilities within a user agent that
168 : // go beyond the feature set defined in the SVG specification.
169 : // Each extension is identified by a URI reference.
170 : // For now, claim that mozilla's SVG implementation supports XHTML and MathML.
171 0 : if (mStringListAttributes[EXTENSIONS].IsExplicitlySet()) {
172 0 : if (mStringListAttributes[EXTENSIONS].IsEmpty()) {
173 0 : return false;
174 : }
175 0 : for (PRUint32 i = 0; i < mStringListAttributes[EXTENSIONS].Length(); i++) {
176 0 : if (!nsSVGFeatures::HasExtension(mStringListAttributes[EXTENSIONS][i])) {
177 0 : return false;
178 : }
179 : }
180 : }
181 :
182 0 : if (aAcceptLangs == kIgnoreSystemLanguage) {
183 0 : return true;
184 : }
185 :
186 : // systemLanguage
187 : //
188 : // Evaluates to "true" if one of the languages indicated by user preferences
189 : // exactly equals one of the languages given in the value of this parameter,
190 : // or if one of the languages indicated by user preferences exactly equals a
191 : // prefix of one of the languages given in the value of this parameter such
192 : // that the first tag character following the prefix is "-".
193 0 : if (mStringListAttributes[LANGUAGE].IsExplicitlySet()) {
194 0 : if (mStringListAttributes[LANGUAGE].IsEmpty()) {
195 0 : return false;
196 : }
197 :
198 : // Get our language preferences
199 : const nsAutoString acceptLangs(aAcceptLangs ? *aAcceptLangs :
200 0 : Preferences::GetLocalizedString("intl.accept_languages"));
201 :
202 0 : if (acceptLangs.IsEmpty()) {
203 0 : NS_WARNING("no default language specified for systemLanguage conditional test");
204 0 : return false;
205 : }
206 :
207 0 : const nsDefaultStringComparator defaultComparator;
208 :
209 0 : for (PRUint32 i = 0; i < mStringListAttributes[LANGUAGE].Length(); i++) {
210 0 : nsCharSeparatedTokenizer languageTokenizer(acceptLangs, ',');
211 0 : while (languageTokenizer.hasMoreTokens()) {
212 0 : if (nsStyleUtil::DashMatchCompare(mStringListAttributes[LANGUAGE][i],
213 0 : languageTokenizer.nextToken(),
214 0 : defaultComparator)) {
215 0 : return true;
216 : }
217 : }
218 : }
219 0 : return false;
220 : }
221 :
222 0 : return true;
223 : }
224 :
225 : bool
226 0 : DOMSVGTests::ParseConditionalProcessingAttribute(nsIAtom* aAttribute,
227 : const nsAString& aValue,
228 : nsAttrValue& aResult)
229 : {
230 0 : for (PRUint32 i = 0; i < ArrayLength(sStringListNames); i++) {
231 0 : if (aAttribute == *sStringListNames[i]) {
232 0 : nsresult rv = mStringListAttributes[i].SetValue(aValue);
233 0 : if (NS_FAILED(rv)) {
234 0 : mStringListAttributes[i].Clear();
235 : }
236 0 : MaybeInvalidate();
237 0 : return true;
238 : }
239 : }
240 0 : return false;
241 : }
242 :
243 : void
244 0 : DOMSVGTests::UnsetAttr(const nsIAtom* aAttribute)
245 : {
246 0 : for (PRUint32 i = 0; i < ArrayLength(sStringListNames); i++) {
247 0 : if (aAttribute == *sStringListNames[i]) {
248 0 : mStringListAttributes[i].Clear();
249 0 : MaybeInvalidate();
250 0 : return;
251 : }
252 : }
253 : }
254 :
255 : nsIAtom*
256 0 : DOMSVGTests::GetAttrName(PRUint8 aAttrEnum) const
257 : {
258 0 : return *sStringListNames[aAttrEnum];
259 : }
260 :
261 : void
262 0 : DOMSVGTests::GetAttrValue(PRUint8 aAttrEnum, nsAttrValue& aValue) const
263 : {
264 0 : NS_ABORT_IF_FALSE(aAttrEnum >= 0 && aAttrEnum < ArrayLength(sStringListNames),
265 : "aAttrEnum out of range");
266 0 : aValue.SetTo(mStringListAttributes[aAttrEnum], nsnull);
267 0 : }
268 :
269 : void
270 0 : DOMSVGTests::MaybeInvalidate()
271 : {
272 0 : nsCOMPtr<nsSVGElement> element = do_QueryInterface(this);
273 :
274 0 : nsIContent* parent = element->GetFlattenedTreeParent();
275 :
276 0 : if (parent &&
277 0 : parent->NodeInfo()->Equals(nsGkAtoms::svgSwitch, kNameSpaceID_SVG)) {
278 0 : static_cast<nsSVGSwitchElement*>(parent)->MaybeInvalidate();
279 : }
280 0 : }
|