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
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/
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
15 * The Original Code is Mozilla Communicator client code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
23 * Mats Palmgren <mats.palmgren@bredband.net>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 /* base class for DOM objects for element.style and cssStyleRule.style */
41 #include "nsDOMCSSDeclaration.h"
42 #include "nsIDOMCSSRule.h"
43 #include "nsICSSParser.h"
44 #include "nsICSSLoader.h"
45 #include "nsIStyleRule.h"
46 #include "nsCSSDeclaration.h"
47 #include "nsCSSProps.h"
50 #include "nsReadableUtils.h"
51 #include "nsIPrincipal.h"
53 #include "nsContentUtils.h"
56 nsDOMCSSDeclaration::nsDOMCSSDeclaration()
61 nsDOMCSSDeclaration::~nsDOMCSSDeclaration()
66 // QueryInterface implementation for nsDOMCSSDeclaration
67 NS_INTERFACE_MAP_BEGIN(nsDOMCSSDeclaration
)
68 NS_INTERFACE_MAP_ENTRY(nsICSSDeclaration
)
69 NS_INTERFACE_MAP_ENTRY(nsIDOMCSSStyleDeclaration
)
70 NS_INTERFACE_MAP_ENTRY_AGGREGATED(nsIDOMCSS2Properties
, &mInner
)
71 NS_INTERFACE_MAP_ENTRY_AGGREGATED(nsIDOMNSCSS2Properties
, &mInner
)
72 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports
, nsIDOMCSSStyleDeclaration
)
73 NS_INTERFACE_MAP_ENTRY_CONTENT_CLASSINFO(CSSStyleDeclaration
)
77 nsDOMCSSDeclaration::GetPropertyValue(const nsCSSProperty aPropID
,
80 NS_PRECONDITION(aPropID
!= eCSSProperty_UNKNOWN
,
81 "Should never pass eCSSProperty_UNKNOWN around");
83 nsCSSDeclaration
*decl
;
84 nsresult result
= GetCSSDeclaration(&decl
, PR_FALSE
);
88 result
= decl
->GetValue(aPropID
, aValue
);
95 nsDOMCSSDeclaration::SetPropertyValue(const nsCSSProperty aPropID
,
96 const nsAString
& aValue
)
98 if (aValue
.IsEmpty()) {
99 // If the new value of the property is an empty string we remove the
101 return RemoveProperty(aPropID
);
104 return ParsePropertyValue(aPropID
, aValue
);
109 nsDOMCSSDeclaration::GetCssText(nsAString
& aCssText
)
111 nsCSSDeclaration
* decl
;
113 GetCSSDeclaration(&decl
, PR_FALSE
);
116 decl
->ToString(aCssText
);
123 nsDOMCSSDeclaration::SetCssText(const nsAString
& aCssText
)
125 return ParseDeclaration(aCssText
, PR_FALSE
, PR_TRUE
);
129 nsDOMCSSDeclaration::GetLength(PRUint32
* aLength
)
131 nsCSSDeclaration
*decl
;
132 nsresult result
= GetCSSDeclaration(&decl
, PR_FALSE
);
135 *aLength
= decl
->Count();
144 nsDOMCSSDeclaration::GetPropertyCSSValue(const nsAString
& aPropertyName
,
145 nsIDOMCSSValue
** aReturn
)
147 NS_ENSURE_ARG_POINTER(aReturn
);
149 // We don't support CSSValue yet so we'll just return null...
156 nsDOMCSSDeclaration::Item(PRUint32 aIndex
, nsAString
& aReturn
)
158 nsCSSDeclaration
*decl
;
159 nsresult result
= GetCSSDeclaration(&decl
, PR_FALSE
);
161 aReturn
.SetLength(0);
163 result
= decl
->GetNthProperty(aIndex
, aReturn
);
170 nsDOMCSSDeclaration::GetPropertyValue(const nsAString
& aPropertyName
,
173 const nsCSSProperty propID
= nsCSSProps::LookupProperty(aPropertyName
);
174 if (propID
== eCSSProperty_UNKNOWN
) {
179 return GetPropertyValue(propID
, aReturn
);
183 nsDOMCSSDeclaration::GetPropertyPriority(const nsAString
& aPropertyName
,
186 nsCSSDeclaration
*decl
;
187 nsresult result
= GetCSSDeclaration(&decl
, PR_FALSE
);
190 if (decl
&& decl
->GetValueIsImportant(aPropertyName
)) {
191 aReturn
.AssignLiteral("important");
198 nsDOMCSSDeclaration::SetProperty(const nsAString
& aPropertyName
,
199 const nsAString
& aValue
,
200 const nsAString
& aPriority
)
202 // In the common (and fast) cases we can use the property id
203 nsCSSProperty propID
= nsCSSProps::LookupProperty(aPropertyName
);
204 if (propID
== eCSSProperty_UNKNOWN
) {
208 if (aValue
.IsEmpty()) {
209 // If the new value of the property is an empty string we remove the
211 return RemoveProperty(propID
);
214 if (aPriority
.IsEmpty()) {
215 return ParsePropertyValue(propID
, aValue
);
218 // ParsePropertyValue does not handle priorities correctly -- it's
219 // optimized for speed. And the priority is not part of the
220 // property value anyway.... So we have to use the full-blown
221 // ParseDeclaration()
222 return ParseDeclaration(aPropertyName
+ NS_LITERAL_STRING(":") +
223 aValue
+ NS_LITERAL_STRING("!") + aPriority
,
228 nsDOMCSSDeclaration::RemoveProperty(const nsAString
& aPropertyName
,
231 const nsCSSProperty propID
= nsCSSProps::LookupProperty(aPropertyName
);
232 if (propID
== eCSSProperty_UNKNOWN
) {
237 nsresult rv
= GetPropertyValue(propID
, aReturn
);
238 NS_ENSURE_SUCCESS(rv
, rv
);
240 rv
= RemoveProperty(propID
);
245 nsDOMCSSDeclaration::ParsePropertyValue(const nsCSSProperty aPropID
,
246 const nsAString
& aPropValue
)
248 nsCSSDeclaration
* decl
;
249 nsresult result
= GetCSSDeclaration(&decl
, PR_TRUE
);
254 nsCOMPtr
<nsICSSLoader
> cssLoader
;
255 nsCOMPtr
<nsICSSParser
> cssParser
;
256 nsCOMPtr
<nsIURI
> baseURI
, sheetURI
;
257 nsCOMPtr
<nsIPrincipal
> sheetPrincipal
;
259 result
= GetCSSParsingEnvironment(getter_AddRefs(sheetURI
),
260 getter_AddRefs(baseURI
),
261 getter_AddRefs(sheetPrincipal
),
262 getter_AddRefs(cssLoader
),
263 getter_AddRefs(cssParser
));
264 if (NS_FAILED(result
)) {
269 result
= cssParser
->ParseProperty(aPropID
, aPropValue
, sheetURI
, baseURI
,
270 sheetPrincipal
, decl
, &changed
);
271 if (NS_SUCCEEDED(result
) && changed
) {
272 result
= DeclarationChanged();
276 cssLoader
->RecycleParser(cssParser
);
283 nsDOMCSSDeclaration::ParseDeclaration(const nsAString
& aDecl
,
284 PRBool aParseOnlyOneDecl
,
285 PRBool aClearOldDecl
)
287 nsCSSDeclaration
* decl
;
288 nsresult result
= GetCSSDeclaration(&decl
, PR_TRUE
);
293 nsCOMPtr
<nsICSSLoader
> cssLoader
;
294 nsCOMPtr
<nsICSSParser
> cssParser
;
295 nsCOMPtr
<nsIURI
> baseURI
, sheetURI
;
296 nsCOMPtr
<nsIPrincipal
> sheetPrincipal
;
298 result
= GetCSSParsingEnvironment(getter_AddRefs(sheetURI
),
299 getter_AddRefs(baseURI
),
300 getter_AddRefs(sheetPrincipal
),
301 getter_AddRefs(cssLoader
),
302 getter_AddRefs(cssParser
));
304 if (NS_FAILED(result
)) {
309 result
= cssParser
->ParseAndAppendDeclaration(aDecl
, sheetURI
, baseURI
,
310 sheetPrincipal
, decl
,
315 if (NS_SUCCEEDED(result
) && changed
) {
316 result
= DeclarationChanged();
320 cssLoader
->RecycleParser(cssParser
);
327 nsDOMCSSDeclaration::RemoveProperty(const nsCSSProperty aPropID
)
329 nsCSSDeclaration
* decl
;
330 nsresult rv
= GetCSSDeclaration(&decl
, PR_FALSE
);
335 rv
= decl
->RemoveProperty(aPropID
);
337 if (NS_SUCCEEDED(rv
)) {
338 rv
= DeclarationChanged();
340 // RemoveProperty used to throw in all sorts of situations -- e.g.
341 // if the property was a shorthand one. Do not propagate its return
342 // value to callers. (XXX or should we propagate it again now?)
349 //////////////////////////////////////////////////////////////////////////////
351 CSS2PropertiesTearoff::CSS2PropertiesTearoff(nsICSSDeclaration
*aOuter
)
354 NS_ASSERTION(mOuter
, "must have outer");
357 CSS2PropertiesTearoff::~CSS2PropertiesTearoff()
361 NS_IMETHODIMP_(nsrefcnt
)
362 CSS2PropertiesTearoff::AddRef(void)
364 return mOuter
->AddRef();
367 NS_IMETHODIMP_(nsrefcnt
)
368 CSS2PropertiesTearoff::Release(void)
370 return mOuter
->Release();
374 CSS2PropertiesTearoff::QueryInterface(REFNSIID aIID
, void** aInstancePtr
)
376 return mOuter
->QueryInterface(aIID
, aInstancePtr
);
379 // nsIDOMCSS2Properties
380 // nsIDOMNSCSS2Properties
382 #define CSS_PROP(name_, id_, method_, flags_, datastruct_, member_, type_, kwtable_) \
384 CSS2PropertiesTearoff::Get##method_(nsAString& aValue) \
386 return mOuter->GetPropertyValue(eCSSProperty_##id_, aValue); \
390 CSS2PropertiesTearoff::Set##method_(const nsAString& aValue) \
392 return mOuter->SetPropertyValue(eCSSProperty_##id_, aValue); \
395 #define CSS_PROP_NOTIMPLEMENTED(name_, id_, method_, flags_) \
397 CSS2PropertiesTearoff::Get##method_(nsAString& aValue) \
404 CSS2PropertiesTearoff::Set##method_(const nsAString& aValue) \
409 #define CSS_PROP_LIST_EXCLUDE_INTERNAL
410 #define CSS_PROP_SHORTHAND(name_, id_, method_, flags_) \
411 CSS_PROP(name_, id_, method_, flags_, X, X, X, X)
412 #include "nsCSSPropList.h"
415 CSS_PROP(X
, opacity
, MozOpacity
, 0, X
, X
, X
, X
)
416 CSS_PROP(X
, outline
, MozOutline
, 0, X
, X
, X
, X
)
417 CSS_PROP(X
, outline_color
, MozOutlineColor
, 0, X
, X
, X
, X
)
418 CSS_PROP(X
, outline_style
, MozOutlineStyle
, 0, X
, X
, X
, X
)
419 CSS_PROP(X
, outline_width
, MozOutlineWidth
, 0, X
, X
, X
, X
)
420 CSS_PROP(X
, outline_offset
, MozOutlineOffset
, 0, X
, X
, X
, X
)
422 #undef CSS_PROP_SHORTHAND
423 #undef CSS_PROP_NOTIMPLEMENTED
424 #undef CSS_PROP_LIST_EXCLUDE_INTERNAL