1 : /* ***** BEGIN LICENSE BLOCK *****
2 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 : *
4 : * The contents of this file are subject to the Mozilla Public License Version
5 : * 1.1 (the "License"); you may not use this file except in compliance with
6 : * the License. You may obtain a copy of the License at
7 : * http://www.mozilla.org/MPL/
8 : *
9 : * Software distributed under the License is distributed on an "AS IS" basis,
10 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 : * for the specific language governing rights and limitations under the
12 : * License.
13 : *
14 : * The Original Code is mozilla.org code.
15 : *
16 : * The Initial Developer of the Original Code is
17 : * Netscape Communications Corporation.
18 : * Portions created by the Initial Developer are Copyright (C) 2001
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : * Joe Hewitt <hewitt@netscape.com> (original author)
23 : * Christopher A. Aillon <christopher@aillon.com>
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either the GNU General Public License Version 2 or later (the "GPL"), or
27 : * 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 : #include "inDOMUtils.h"
40 : #include "inLayoutUtils.h"
41 :
42 : #include "nsIServiceManager.h"
43 : #include "nsString.h"
44 : #include "nsIDOMElement.h"
45 : #include "nsIDocument.h"
46 : #include "nsIPresShell.h"
47 : #include "nsIDOMDocument.h"
48 : #include "nsIDOMCharacterData.h"
49 : #include "nsRuleNode.h"
50 : #include "nsIStyleRule.h"
51 : #include "mozilla/css/StyleRule.h"
52 : #include "nsICSSStyleRuleDOMWrapper.h"
53 : #include "nsIDOMWindow.h"
54 : #include "nsXBLBinding.h"
55 : #include "nsXBLPrototypeBinding.h"
56 : #include "nsIMutableArray.h"
57 : #include "nsBindingManager.h"
58 : #include "nsComputedDOMStyle.h"
59 : #include "nsEventStateManager.h"
60 : #include "nsIAtom.h"
61 : #include "nsRange.h"
62 : #include "mozilla/dom/Element.h"
63 :
64 :
65 : ///////////////////////////////////////////////////////////////////////////////
66 :
67 0 : inDOMUtils::inDOMUtils()
68 : {
69 0 : }
70 :
71 0 : inDOMUtils::~inDOMUtils()
72 : {
73 0 : }
74 :
75 0 : NS_IMPL_ISUPPORTS1(inDOMUtils, inIDOMUtils)
76 :
77 : ///////////////////////////////////////////////////////////////////////////////
78 : // inIDOMUtils
79 :
80 : NS_IMETHODIMP
81 0 : inDOMUtils::IsIgnorableWhitespace(nsIDOMCharacterData *aDataNode,
82 : bool *aReturn)
83 : {
84 0 : NS_PRECONDITION(aReturn, "Must have an out parameter");
85 :
86 0 : NS_ENSURE_ARG_POINTER(aDataNode);
87 :
88 0 : *aReturn = false;
89 :
90 0 : nsCOMPtr<nsIContent> content = do_QueryInterface(aDataNode);
91 0 : NS_ASSERTION(content, "Does not implement nsIContent!");
92 :
93 0 : if (!content->TextIsOnlyWhitespace()) {
94 0 : return NS_OK;
95 : }
96 :
97 : // Okay. We have only white space. Let's check the white-space
98 : // property now and make sure that this isn't preformatted text...
99 :
100 0 : nsCOMPtr<nsIDOMWindow> win = inLayoutUtils::GetWindowFor(aDataNode);
101 0 : if (!win) {
102 : // Hmm. Things are screwy if we have no window...
103 0 : NS_ERROR("No window!");
104 0 : return NS_OK;
105 : }
106 :
107 0 : nsIFrame* frame = content->GetPrimaryFrame();
108 0 : if (frame) {
109 0 : const nsStyleText* text = frame->GetStyleText();
110 0 : *aReturn = !text->WhiteSpaceIsSignificant();
111 : }
112 : else {
113 : // empty inter-tag text node without frame, e.g., in between <table>\n<tr>
114 0 : *aReturn = true;
115 : }
116 :
117 0 : return NS_OK;
118 : }
119 :
120 : NS_IMETHODIMP
121 0 : inDOMUtils::GetParentForNode(nsIDOMNode* aNode,
122 : bool aShowingAnonymousContent,
123 : nsIDOMNode** aParent)
124 : {
125 0 : NS_ENSURE_ARG_POINTER(aNode);
126 :
127 : // First do the special cases -- document nodes and anonymous content
128 0 : nsCOMPtr<nsIDOMDocument> doc(do_QueryInterface(aNode));
129 0 : nsCOMPtr<nsIDOMNode> parent;
130 :
131 0 : if (doc) {
132 0 : parent = inLayoutUtils::GetContainerFor(doc);
133 0 : } else if (aShowingAnonymousContent) {
134 0 : nsCOMPtr<nsIContent> content = do_QueryInterface(aNode);
135 0 : if (content) {
136 0 : nsIContent* bparent = nsnull;
137 0 : nsRefPtr<nsBindingManager> bindingManager = inLayoutUtils::GetBindingManagerFor(aNode);
138 0 : if (bindingManager) {
139 0 : bparent = bindingManager->GetInsertionParent(content);
140 : }
141 :
142 0 : parent = do_QueryInterface(bparent);
143 : }
144 : }
145 :
146 0 : if (!parent) {
147 : // Ok, just get the normal DOM parent node
148 0 : aNode->GetParentNode(getter_AddRefs(parent));
149 : }
150 :
151 0 : NS_IF_ADDREF(*aParent = parent);
152 0 : return NS_OK;
153 : }
154 :
155 : NS_IMETHODIMP
156 0 : inDOMUtils::GetChildrenForNode(nsIDOMNode* aNode,
157 : bool aShowingAnonymousContent,
158 : nsIDOMNodeList** aChildren)
159 : {
160 0 : NS_ENSURE_ARG_POINTER(aNode);
161 0 : NS_PRECONDITION(aChildren, "Must have an out parameter");
162 :
163 0 : nsCOMPtr<nsIDOMNodeList> kids;
164 :
165 0 : if (aShowingAnonymousContent) {
166 0 : nsCOMPtr<nsIContent> content = do_QueryInterface(aNode);
167 0 : if (content) {
168 : nsRefPtr<nsBindingManager> bindingManager =
169 0 : inLayoutUtils::GetBindingManagerFor(aNode);
170 0 : if (bindingManager) {
171 0 : bindingManager->GetAnonymousNodesFor(content, getter_AddRefs(kids));
172 0 : if (!kids) {
173 0 : bindingManager->GetContentListFor(content, getter_AddRefs(kids));
174 : }
175 : }
176 : }
177 : }
178 :
179 0 : if (!kids) {
180 0 : aNode->GetChildNodes(getter_AddRefs(kids));
181 : }
182 :
183 0 : kids.forget(aChildren);
184 0 : return NS_OK;
185 : }
186 :
187 : NS_IMETHODIMP
188 0 : inDOMUtils::GetCSSStyleRules(nsIDOMElement *aElement,
189 : const nsAString& aPseudo,
190 : nsISupportsArray **_retval)
191 : {
192 0 : NS_ENSURE_ARG_POINTER(aElement);
193 :
194 0 : *_retval = nsnull;
195 :
196 0 : nsCOMPtr<nsIAtom> pseudoElt;
197 0 : if (!aPseudo.IsEmpty()) {
198 0 : pseudoElt = do_GetAtom(aPseudo);
199 : }
200 :
201 0 : nsRuleNode* ruleNode = nsnull;
202 0 : nsCOMPtr<nsIContent> content = do_QueryInterface(aElement);
203 0 : nsRefPtr<nsStyleContext> styleContext;
204 0 : GetRuleNodeForContent(content, pseudoElt, getter_AddRefs(styleContext), &ruleNode);
205 0 : if (!ruleNode) {
206 : // This can fail for content nodes that are not in the document or
207 : // if the document they're in doesn't have a presshell. Bail out.
208 0 : return NS_OK;
209 : }
210 :
211 0 : nsCOMPtr<nsISupportsArray> rules;
212 0 : NS_NewISupportsArray(getter_AddRefs(rules));
213 0 : if (!rules) return NS_ERROR_OUT_OF_MEMORY;
214 :
215 0 : nsRefPtr<mozilla::css::StyleRule> cssRule;
216 0 : for ( ; !ruleNode->IsRoot(); ruleNode = ruleNode->GetParent()) {
217 0 : cssRule = do_QueryObject(ruleNode->GetRule());
218 0 : if (cssRule) {
219 0 : nsCOMPtr<nsIDOMCSSRule> domRule = cssRule->GetDOMRule();
220 0 : if (domRule)
221 0 : rules->InsertElementAt(domRule, 0);
222 : }
223 : }
224 :
225 0 : *_retval = rules;
226 0 : NS_ADDREF(*_retval);
227 :
228 0 : return NS_OK;
229 : }
230 :
231 : NS_IMETHODIMP
232 0 : inDOMUtils::GetRuleLine(nsIDOMCSSStyleRule *aRule, PRUint32 *_retval)
233 : {
234 0 : *_retval = 0;
235 :
236 0 : NS_ENSURE_ARG_POINTER(aRule);
237 :
238 0 : nsCOMPtr<nsICSSStyleRuleDOMWrapper> rule = do_QueryInterface(aRule);
239 0 : nsRefPtr<mozilla::css::StyleRule> cssrule;
240 0 : nsresult rv = rule->GetCSSStyleRule(getter_AddRefs(cssrule));
241 0 : NS_ENSURE_SUCCESS(rv, rv);
242 0 : NS_ENSURE_TRUE(cssrule != nsnull, NS_ERROR_FAILURE);
243 0 : *_retval = cssrule->GetLineNumber();
244 0 : return NS_OK;
245 : }
246 :
247 : NS_IMETHODIMP
248 0 : inDOMUtils::IsInheritedProperty(const nsAString &aPropertyName, bool *_retval)
249 : {
250 0 : nsCSSProperty prop = nsCSSProps::LookupProperty(aPropertyName);
251 0 : if (prop == eCSSProperty_UNKNOWN) {
252 0 : *_retval = false;
253 0 : return NS_OK;
254 : }
255 :
256 0 : if (nsCSSProps::IsShorthand(prop)) {
257 0 : prop = nsCSSProps::SubpropertyEntryFor(prop)[0];
258 : }
259 :
260 0 : nsStyleStructID sid = nsCSSProps::kSIDTable[prop];
261 0 : *_retval = !nsCachedStyleData::IsReset(sid);
262 0 : return NS_OK;
263 : }
264 :
265 : NS_IMETHODIMP
266 0 : inDOMUtils::GetBindingURLs(nsIDOMElement *aElement, nsIArray **_retval)
267 : {
268 0 : NS_ENSURE_ARG_POINTER(aElement);
269 :
270 0 : *_retval = nsnull;
271 :
272 0 : nsCOMPtr<nsIMutableArray> urls = do_CreateInstance(NS_ARRAY_CONTRACTID);
273 0 : if (!urls)
274 0 : return NS_ERROR_FAILURE;
275 :
276 0 : nsCOMPtr<nsIContent> content = do_QueryInterface(aElement);
277 0 : NS_ASSERTION(content, "elements must implement nsIContent");
278 :
279 0 : nsIDocument *ownerDoc = content->OwnerDoc();
280 0 : nsXBLBinding *binding = ownerDoc->BindingManager()->GetBinding(content);
281 :
282 0 : while (binding) {
283 0 : urls->AppendElement(binding->PrototypeBinding()->BindingURI(), false);
284 0 : binding = binding->GetBaseBinding();
285 : }
286 :
287 0 : NS_ADDREF(*_retval = urls);
288 0 : return NS_OK;
289 : }
290 :
291 : NS_IMETHODIMP
292 0 : inDOMUtils::SetContentState(nsIDOMElement *aElement, nsEventStates::InternalType aState)
293 : {
294 0 : NS_ENSURE_ARG_POINTER(aElement);
295 :
296 0 : nsRefPtr<nsEventStateManager> esm = inLayoutUtils::GetEventStateManagerFor(aElement);
297 0 : if (esm) {
298 0 : nsCOMPtr<nsIContent> content;
299 0 : content = do_QueryInterface(aElement);
300 :
301 0 : return esm->SetContentState(content, nsEventStates(aState));
302 : }
303 :
304 0 : return NS_ERROR_FAILURE;
305 : }
306 :
307 : NS_IMETHODIMP
308 0 : inDOMUtils::GetContentState(nsIDOMElement *aElement, nsEventStates::InternalType* aState)
309 : {
310 0 : *aState = 0;
311 0 : nsCOMPtr<nsIContent> content = do_QueryInterface(aElement);
312 0 : NS_ENSURE_ARG_POINTER(content);
313 :
314 : // NOTE: if this method is removed,
315 : // please remove GetInternalValue from nsEventStates
316 0 : *aState = content->AsElement()->State().GetInternalValue();
317 0 : return NS_OK;
318 : }
319 :
320 : /* static */ nsresult
321 0 : inDOMUtils::GetRuleNodeForContent(nsIContent* aContent,
322 : nsIAtom* aPseudo,
323 : nsStyleContext** aStyleContext,
324 : nsRuleNode** aRuleNode)
325 : {
326 0 : *aRuleNode = nsnull;
327 0 : *aStyleContext = nsnull;
328 :
329 0 : if (!aContent->IsElement()) {
330 0 : return NS_ERROR_UNEXPECTED;
331 : }
332 :
333 0 : nsIDocument* doc = aContent->GetDocument();
334 0 : NS_ENSURE_TRUE(doc, NS_ERROR_UNEXPECTED);
335 :
336 0 : nsIPresShell *presShell = doc->GetShell();
337 0 : NS_ENSURE_TRUE(presShell, NS_ERROR_UNEXPECTED);
338 :
339 0 : nsPresContext *presContext = presShell->GetPresContext();
340 0 : NS_ENSURE_TRUE(presContext, NS_ERROR_UNEXPECTED);
341 :
342 0 : bool safe = presContext->EnsureSafeToHandOutCSSRules();
343 0 : NS_ENSURE_TRUE(safe, NS_ERROR_OUT_OF_MEMORY);
344 :
345 : nsRefPtr<nsStyleContext> sContext =
346 0 : nsComputedDOMStyle::GetStyleContextForElement(aContent->AsElement(), aPseudo, presShell);
347 0 : if (sContext) {
348 0 : *aRuleNode = sContext->GetRuleNode();
349 0 : sContext.forget(aStyleContext);
350 : }
351 0 : return NS_OK;
352 : }
353 :
354 : NS_IMETHODIMP
355 0 : inDOMUtils::GetUsedFontFaces(nsIDOMRange* aRange,
356 : nsIDOMFontFaceList** aFontFaceList)
357 : {
358 0 : return static_cast<nsRange*>(aRange)->GetUsedFontFaces(aFontFaceList);
359 : }
360 :
361 : static nsEventStates
362 0 : GetStatesForPseudoClass(const nsAString& aStatePseudo)
363 : {
364 : // An array of the states that are relevant for various pseudoclasses.
365 : // XXXbz this duplicates code in nsCSSRuleProcessor
366 : static const nsEventStates sPseudoClassStates[] = {
367 : #define CSS_PSEUDO_CLASS(_name, _value) \
368 : nsEventStates(),
369 : #define CSS_STATE_PSEUDO_CLASS(_name, _value, _states) \
370 : _states,
371 : #include "nsCSSPseudoClassList.h"
372 : #undef CSS_STATE_PSEUDO_CLASS
373 : #undef CSS_PSEUDO_CLASS
374 :
375 : // Add more entries for our fake values to make sure we can't
376 : // index out of bounds into this array no matter what.
377 : nsEventStates(),
378 : nsEventStates()
379 0 : };
380 : PR_STATIC_ASSERT(NS_ARRAY_LENGTH(sPseudoClassStates) ==
381 : nsCSSPseudoClasses::ePseudoClass_NotPseudoClass + 1);
382 :
383 0 : nsCOMPtr<nsIAtom> atom = do_GetAtom(aStatePseudo);
384 :
385 : // Ignore :moz-any-link so we don't give the element simultaneous
386 : // visited and unvisited style state
387 0 : if (nsCSSPseudoClasses::GetPseudoType(atom) ==
388 : nsCSSPseudoClasses::ePseudoClass_mozAnyLink) {
389 0 : return nsEventStates();
390 : }
391 : // Our array above is long enough that indexing into it with
392 : // NotPseudoClass is ok.
393 0 : return sPseudoClassStates[nsCSSPseudoClasses::GetPseudoType(atom)];
394 : }
395 :
396 : NS_IMETHODIMP
397 0 : inDOMUtils::AddPseudoClassLock(nsIDOMElement *aElement,
398 : const nsAString &aPseudoClass)
399 : {
400 0 : NS_ENSURE_ARG_POINTER(aElement);
401 :
402 0 : nsEventStates state = GetStatesForPseudoClass(aPseudoClass);
403 0 : if (state.IsEmpty()) {
404 0 : return NS_OK;
405 : }
406 :
407 0 : nsCOMPtr<mozilla::dom::Element> element = do_QueryInterface(aElement);
408 0 : element->LockStyleStates(state);
409 :
410 0 : return NS_OK;
411 : }
412 :
413 : NS_IMETHODIMP
414 0 : inDOMUtils::RemovePseudoClassLock(nsIDOMElement *aElement,
415 : const nsAString &aPseudoClass)
416 : {
417 0 : NS_ENSURE_ARG_POINTER(aElement);
418 :
419 0 : nsEventStates state = GetStatesForPseudoClass(aPseudoClass);
420 0 : if (state.IsEmpty()) {
421 0 : return NS_OK;
422 : }
423 :
424 0 : nsCOMPtr<mozilla::dom::Element> element = do_QueryInterface(aElement);
425 0 : element->UnlockStyleStates(state);
426 :
427 0 : return NS_OK;
428 : }
429 :
430 : NS_IMETHODIMP
431 0 : inDOMUtils::HasPseudoClassLock(nsIDOMElement *aElement,
432 : const nsAString &aPseudoClass,
433 : bool *_retval)
434 : {
435 0 : NS_ENSURE_ARG_POINTER(aElement);
436 :
437 0 : nsEventStates state = GetStatesForPseudoClass(aPseudoClass);
438 0 : if (state.IsEmpty()) {
439 0 : *_retval = false;
440 0 : return NS_OK;
441 : }
442 :
443 0 : nsCOMPtr<mozilla::dom::Element> element = do_QueryInterface(aElement);
444 0 : nsEventStates locks = element->LockedStyleStates();
445 :
446 0 : *_retval = locks.HasAllStates(state);
447 0 : return NS_OK;
448 : }
449 :
450 : NS_IMETHODIMP
451 0 : inDOMUtils::ClearPseudoClassLocks(nsIDOMElement *aElement)
452 : {
453 0 : NS_ENSURE_ARG_POINTER(aElement);
454 :
455 0 : nsCOMPtr<mozilla::dom::Element> element = do_QueryInterface(aElement);
456 0 : element->ClearStyleStateLocks();
457 :
458 0 : return NS_OK;
459 : }
|