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/. */ 5 6 #include "inLayoutUtils.h" 7 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" 18 19 using namespace mozilla; 20 21 /////////////////////////////////////////////////////////////////////////////// 22 23 EventStateManager* GetEventStateManagerFor(nsIDOMElement * aElement)24inLayoutUtils::GetEventStateManagerFor(nsIDOMElement *aElement) 25 { 26 NS_PRECONDITION(aElement, "Passing in a null element is bad"); 27 28 nsCOMPtr<nsIDOMDocument> domDoc; 29 aElement->GetOwnerDocument(getter_AddRefs(domDoc)); 30 nsCOMPtr<nsIDocument> doc = do_QueryInterface(domDoc); 31 32 if (!doc) { 33 NS_WARNING("Could not get an nsIDocument!"); 34 return nullptr; 35 } 36 37 nsIPresShell *shell = doc->GetShell(); 38 if (!shell) 39 return nullptr; 40 41 return shell->GetPresContext()->EventStateManager(); 42 } 43 44 nsIDOMDocument* GetSubDocumentFor(nsIDOMNode * aNode)45inLayoutUtils::GetSubDocumentFor(nsIDOMNode* aNode) 46 { 47 nsCOMPtr<nsIContent> content = do_QueryInterface(aNode); 48 if (content) { 49 nsCOMPtr<nsIDocument> doc = content->GetComposedDoc(); 50 if (doc) { 51 nsCOMPtr<nsIDOMDocument> domdoc(do_QueryInterface(doc->GetSubDocumentFor(content))); 52 53 return domdoc; 54 } 55 } 56 57 return nullptr; 58 } 59 60 nsIDOMNode* GetContainerFor(const nsIDocument & aDoc)61inLayoutUtils::GetContainerFor(const nsIDocument& aDoc) 62 { 63 nsPIDOMWindowOuter* pwin = aDoc.GetWindow(); 64 if (!pwin) { 65 return nullptr; 66 } 67 68 nsCOMPtr<nsIDOMNode> node = do_QueryInterface(pwin->GetFrameElementInternal()); 69 return node; 70 } 71 72