Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "inLayoutUtils.h"
8 #include "nsIDocument.h"
9 #include "nsIDOMDocument.h"
10 #include "nsIContent.h"
11 #include "nsIContentViewer.h"
12 #include "nsPIDOMWindow.h"
13 #include "nsIDocShell.h"
14 #include "nsIPresShell.h"
15 #include "nsPresContext.h"
16 #include "mozilla/EventStateManager.h"
17 #include "mozilla/dom/Element.h"
19 using namespace mozilla;
21 ///////////////////////////////////////////////////////////////////////////////
23 nsIDOMWindow*
24 inLayoutUtils::GetWindowFor(nsIDOMNode* aNode)
25 {
26 nsCOMPtr<nsIDOMDocument> doc1;
27 aNode->GetOwnerDocument(getter_AddRefs(doc1));
28 return GetWindowFor(doc1.get());
29 }
31 nsIDOMWindow*
32 inLayoutUtils::GetWindowFor(nsIDOMDocument* aDoc)
33 {
34 nsCOMPtr<nsIDOMWindow> window;
35 aDoc->GetDefaultView(getter_AddRefs(window));
36 return window;
37 }
39 nsIPresShell*
40 inLayoutUtils::GetPresShellFor(nsISupports* aThing)
41 {
42 nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aThing);
44 return window->GetDocShell()->GetPresShell();
45 }
47 /*static*/
48 nsIFrame*
49 inLayoutUtils::GetFrameFor(nsIDOMElement* aElement)
50 {
51 nsCOMPtr<nsIContent> content = do_QueryInterface(aElement);
52 return content->GetPrimaryFrame();
53 }
55 EventStateManager*
56 inLayoutUtils::GetEventStateManagerFor(nsIDOMElement *aElement)
57 {
58 NS_PRECONDITION(aElement, "Passing in a null element is bad");
60 nsCOMPtr<nsIDOMDocument> domDoc;
61 aElement->GetOwnerDocument(getter_AddRefs(domDoc));
62 nsCOMPtr<nsIDocument> doc = do_QueryInterface(domDoc);
64 if (!doc) {
65 NS_WARNING("Could not get an nsIDocument!");
66 return nullptr;
67 }
69 nsIPresShell *shell = doc->GetShell();
70 if (!shell)
71 return nullptr;
73 return shell->GetPresContext()->EventStateManager();
74 }
76 nsIDOMDocument*
77 inLayoutUtils::GetSubDocumentFor(nsIDOMNode* aNode)
78 {
79 nsCOMPtr<nsIContent> content = do_QueryInterface(aNode);
80 if (content) {
81 nsCOMPtr<nsIDocument> doc = content->GetDocument();
82 if (doc) {
83 nsCOMPtr<nsIDOMDocument> domdoc(do_QueryInterface(doc->GetSubDocumentFor(content)));
85 return domdoc;
86 }
87 }
89 return nullptr;
90 }
92 nsIDOMNode*
93 inLayoutUtils::GetContainerFor(const nsIDocument& aDoc)
94 {
95 nsPIDOMWindow* pwin = aDoc.GetWindow();
96 if (!pwin) {
97 return nullptr;
98 }
100 nsCOMPtr<nsIDOMNode> node = do_QueryInterface(pwin->GetFrameElementInternal());
101 return node;
102 }