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 Mozilla Communicator client code.
16 : *
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.
21 : *
22 : * Contributor(s):
23 : * Ms2ger <ms2ger@gmail.com>
24 : *
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.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : /* DOM object for element.style */
40 :
41 : #include "nsDOMCSSAttrDeclaration.h"
42 :
43 : #include "mozilla/css/Declaration.h"
44 : #include "mozilla/css/Loader.h"
45 : #include "mozilla/css/StyleRule.h"
46 : #include "mozilla/dom/Element.h"
47 : #include "nsIDocument.h"
48 : #include "nsIDOMMutationEvent.h"
49 : #include "nsIPrincipal.h"
50 : #include "nsIURI.h"
51 : #include "nsNodeUtils.h"
52 : #include "nsGenericElement.h"
53 :
54 : namespace css = mozilla::css;
55 : namespace dom = mozilla::dom;
56 :
57 0 : nsDOMCSSAttributeDeclaration::nsDOMCSSAttributeDeclaration(dom::Element* aElement,
58 : bool aIsSMILOverride)
59 : : mElement(aElement)
60 0 : , mIsSMILOverride(aIsSMILOverride)
61 : {
62 0 : MOZ_COUNT_CTOR(nsDOMCSSAttributeDeclaration);
63 :
64 0 : NS_ASSERTION(aElement, "Inline style for a NULL element?");
65 0 : }
66 :
67 0 : nsDOMCSSAttributeDeclaration::~nsDOMCSSAttributeDeclaration()
68 : {
69 0 : MOZ_COUNT_DTOR(nsDOMCSSAttributeDeclaration);
70 0 : }
71 :
72 : // If nsDOMCSSAttributeDeclaration is changed so that any additional
73 : // fields are traversed by the cycle collector (for instance, if
74 : // wrapper cache handling is changed) then CAN_SKIP must be updated.
75 1464 : NS_IMPL_CYCLE_COLLECTION_1(nsDOMCSSAttributeDeclaration, mElement)
76 :
77 : // nsDOMCSSAttributeDeclaration has only one cycle collected field, so
78 : // if mElement is going to be skipped, the attribute declaration can't
79 : // be part of a garbage cycle.
80 0 : NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_BEGIN(nsDOMCSSAttributeDeclaration)
81 0 : return !tmp->mElement || nsGenericElement::CanSkip(tmp->mElement, true);
82 : NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_END
83 :
84 0 : NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_BEGIN(nsDOMCSSAttributeDeclaration)
85 0 : return !tmp->mElement || nsGenericElement::CanSkipInCC(tmp->mElement);
86 : NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_END
87 :
88 : // CanSkipThis returns false to avoid problems with incomplete unlinking.
89 0 : NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_BEGIN(nsDOMCSSAttributeDeclaration)
90 0 : NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_END
91 :
92 0 : NS_INTERFACE_MAP_BEGIN(nsDOMCSSAttributeDeclaration)
93 0 : NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
94 0 : NS_INTERFACE_MAP_ENTRIES_CYCLE_COLLECTION(nsDOMCSSAttributeDeclaration)
95 0 : NS_IMPL_QUERY_TAIL_INHERITING(nsDOMCSSDeclaration)
96 :
97 0 : NS_IMPL_CYCLE_COLLECTING_ADDREF(nsDOMCSSAttributeDeclaration)
98 0 : NS_IMPL_CYCLE_COLLECTING_RELEASE(nsDOMCSSAttributeDeclaration)
99 :
100 : nsresult
101 0 : nsDOMCSSAttributeDeclaration::SetCSSDeclaration(css::Declaration* aDecl)
102 : {
103 0 : NS_ASSERTION(mElement, "Must have Element to set the declaration!");
104 : css::StyleRule* oldRule =
105 0 : mIsSMILOverride ? mElement->GetSMILOverrideStyleRule() :
106 0 : mElement->GetInlineStyleRule();
107 0 : NS_ASSERTION(oldRule, "Element must have rule");
108 :
109 : nsRefPtr<css::StyleRule> newRule =
110 0 : oldRule->DeclarationChanged(aDecl, false);
111 0 : if (!newRule) {
112 0 : return NS_ERROR_OUT_OF_MEMORY;
113 : }
114 :
115 : return
116 0 : mIsSMILOverride ? mElement->SetSMILOverrideStyleRule(newRule, true) :
117 0 : mElement->SetInlineStyleRule(newRule, true);
118 : }
119 :
120 : nsIDocument*
121 0 : nsDOMCSSAttributeDeclaration::DocToUpdate()
122 : {
123 : // XXXbz this is a bit of a hack, especially doing it before the
124 : // BeginUpdate(), but this is a good chokepoint where we know we
125 : // plan to modify the CSSDeclaration, so need to notify
126 : // AttributeWillChange if this is inline style.
127 0 : if (!mIsSMILOverride) {
128 : nsNodeUtils::AttributeWillChange(mElement, kNameSpaceID_None,
129 : nsGkAtoms::style,
130 0 : nsIDOMMutationEvent::MODIFICATION);
131 : }
132 :
133 : // We need OwnerDoc() rather than GetCurrentDoc() because it might
134 : // be the BeginUpdate call that inserts mElement into the document.
135 0 : return mElement->OwnerDoc();
136 : }
137 :
138 : css::Declaration*
139 0 : nsDOMCSSAttributeDeclaration::GetCSSDeclaration(bool aAllocate)
140 : {
141 0 : if (!mElement)
142 0 : return nsnull;
143 :
144 : css::StyleRule* cssRule;
145 0 : if (mIsSMILOverride)
146 0 : cssRule = mElement->GetSMILOverrideStyleRule();
147 : else
148 0 : cssRule = mElement->GetInlineStyleRule();
149 :
150 0 : if (cssRule) {
151 0 : return cssRule->GetDeclaration();
152 : }
153 0 : if (!aAllocate) {
154 0 : return nsnull;
155 : }
156 :
157 : // cannot fail
158 0 : css::Declaration *decl = new css::Declaration();
159 0 : decl->InitializeEmpty();
160 0 : nsRefPtr<css::StyleRule> newRule = new css::StyleRule(nsnull, decl);
161 :
162 : // this *can* fail (inside SetAttrAndNotify, at least).
163 : nsresult rv;
164 0 : if (mIsSMILOverride)
165 0 : rv = mElement->SetSMILOverrideStyleRule(newRule, false);
166 : else
167 0 : rv = mElement->SetInlineStyleRule(newRule, false);
168 :
169 0 : if (NS_FAILED(rv)) {
170 0 : return nsnull; // the decl will be destroyed along with the style rule
171 : }
172 :
173 0 : return decl;
174 : }
175 :
176 : void
177 0 : nsDOMCSSAttributeDeclaration::GetCSSParsingEnvironment(CSSParsingEnvironment& aCSSParseEnv)
178 : {
179 0 : NS_ASSERTION(mElement, "Something is severely broken -- there should be an Element here!");
180 :
181 0 : nsIDocument* doc = mElement->OwnerDoc();
182 0 : aCSSParseEnv.mSheetURI = doc->GetDocumentURI();
183 0 : aCSSParseEnv.mBaseURI = mElement->GetBaseURI();
184 0 : aCSSParseEnv.mPrincipal = mElement->NodePrincipal();
185 0 : aCSSParseEnv.mCSSLoader = doc->CSSLoader();
186 0 : }
187 :
188 : NS_IMETHODIMP
189 0 : nsDOMCSSAttributeDeclaration::GetParentRule(nsIDOMCSSRule **aParent)
190 : {
191 0 : NS_ENSURE_ARG_POINTER(aParent);
192 :
193 0 : *aParent = nsnull;
194 0 : return NS_OK;
195 : }
196 :
197 : /* virtual */ nsINode*
198 0 : nsDOMCSSAttributeDeclaration::GetParentObject()
199 : {
200 0 : return mElement;
201 4392 : }
|