michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef nsCoreUtils_h_ michael@0: #define nsCoreUtils_h_ michael@0: michael@0: #include "nsIContent.h" michael@0: #include "nsIDocument.h" michael@0: #include "nsIPresShell.h" michael@0: michael@0: #include "nsPoint.h" michael@0: #include "nsTArray.h" michael@0: michael@0: class nsRange; michael@0: class nsIBoxObject; michael@0: class nsIFrame; michael@0: class nsIDocShell; michael@0: class nsITreeColumn; michael@0: class nsITreeBoxObject; michael@0: class nsIWidget; michael@0: michael@0: /** michael@0: * Core utils. michael@0: */ michael@0: class nsCoreUtils michael@0: { michael@0: public: michael@0: /** michael@0: * Return true if the given node has registered click, mousedown or mouseup michael@0: * event listeners. michael@0: */ michael@0: static bool HasClickListener(nsIContent *aContent); michael@0: michael@0: /** michael@0: * Dispatch click event to XUL tree cell. michael@0: * michael@0: * @param aTreeBoxObj [in] tree box object michael@0: * @param aRowIndex [in] row index michael@0: * @param aColumn [in] column object michael@0: * @param aPseudoElm [in] pseudo elemenet inside the cell, see michael@0: * nsITreeBoxObject for available values michael@0: */ michael@0: static void DispatchClickEvent(nsITreeBoxObject *aTreeBoxObj, michael@0: int32_t aRowIndex, nsITreeColumn *aColumn, michael@0: const nsCString& aPseudoElt = EmptyCString()); michael@0: michael@0: /** michael@0: * Send mouse event to the given element. michael@0: * michael@0: * @param aEventType [in] an event type (see BasicEvents.h for constants) michael@0: * @param aX [in] x coordinate in dev pixels michael@0: * @param aY [in] y coordinate in dev pixels michael@0: * @param aContent [in] the element michael@0: * @param aFrame [in] frame of the element michael@0: * @param aPresShell [in] the presshell for the element michael@0: * @param aRootWidget [in] the root widget of the element michael@0: */ michael@0: static void DispatchMouseEvent(uint32_t aEventType, int32_t aX, int32_t aY, michael@0: nsIContent *aContent, nsIFrame *aFrame, michael@0: nsIPresShell *aPresShell, nsIWidget *aRootWidget); michael@0: michael@0: /** michael@0: * Send a touch event with a single touch point to the given element. michael@0: * michael@0: * @param aEventType [in] an event type (see BasicEvents.h for constants) michael@0: * @param aX [in] x coordinate in dev pixels michael@0: * @param aY [in] y coordinate in dev pixels michael@0: * @param aContent [in] the element michael@0: * @param aFrame [in] frame of the element michael@0: * @param aPresShell [in] the presshell for the element michael@0: * @param aRootWidget [in] the root widget of the element michael@0: */ michael@0: static void DispatchTouchEvent(uint32_t aEventType, int32_t aX, int32_t aY, michael@0: nsIContent* aContent, nsIFrame* aFrame, michael@0: nsIPresShell* aPresShell, nsIWidget* aRootWidget); michael@0: michael@0: /** michael@0: * Return an accesskey registered on the given element by michael@0: * EventStateManager or 0 if there is no registered accesskey. michael@0: * michael@0: * @param aContent - the given element. michael@0: */ michael@0: static uint32_t GetAccessKeyFor(nsIContent *aContent); michael@0: michael@0: /** michael@0: * Return DOM element related with the given node, i.e. michael@0: * a) itself if it is DOM element michael@0: * b) parent element if it is text node michael@0: * c) otherwise nullptr michael@0: * michael@0: * @param aNode [in] the given DOM node michael@0: */ michael@0: static nsIContent* GetDOMElementFor(nsIContent *aContent); michael@0: michael@0: /** michael@0: * Return DOM node for the given DOM point. michael@0: */ michael@0: static nsINode *GetDOMNodeFromDOMPoint(nsINode *aNode, uint32_t aOffset); michael@0: michael@0: /** michael@0: * Return the nsIContent* to check for ARIA attributes on -- this may not michael@0: * always be the DOM node for the accessible. Specifically, for doc michael@0: * accessibles, it is not the document node, but either the root element or michael@0: * in HTML. michael@0: * michael@0: * @param aNode [in] DOM node for the accessible that may be affected by ARIA michael@0: * @return the nsIContent which may have ARIA markup michael@0: */ michael@0: static nsIContent* GetRoleContent(nsINode *aNode); michael@0: michael@0: /** michael@0: * Is the first passed in node an ancestor of the second? michael@0: * Note: A node is not considered to be the ancestor of itself. michael@0: * michael@0: * @param aPossibleAncestorNode [in] node to test for ancestor-ness of michael@0: * aPossibleDescendantNode michael@0: * @param aPossibleDescendantNode [in] node to test for descendant-ness of michael@0: * aPossibleAncestorNode michael@0: * @param aRootNode [in, optional] the root node that search michael@0: * search should be performed within michael@0: * @return true if aPossibleAncestorNode is an ancestor of michael@0: * aPossibleDescendantNode michael@0: */ michael@0: static bool IsAncestorOf(nsINode *aPossibleAncestorNode, michael@0: nsINode *aPossibleDescendantNode, michael@0: nsINode *aRootNode = nullptr); michael@0: michael@0: /** michael@0: * Helper method to scroll range into view, used for implementation of michael@0: * nsIAccessibleText::scrollSubstringTo(). michael@0: * michael@0: * @param aFrame the frame for accessible the range belongs to. michael@0: * @param aRange the range to scroll to michael@0: * @param aScrollType the place a range should be scrolled to michael@0: */ michael@0: static nsresult ScrollSubstringTo(nsIFrame* aFrame, nsRange* aRange, michael@0: uint32_t aScrollType); michael@0: michael@0: /** Helper method to scroll range into view, used for implementation of michael@0: * nsIAccessibleText::scrollSubstringTo[Point](). michael@0: * michael@0: * @param aFrame the frame for accessible the range belongs to. michael@0: * @param aRange the range to scroll to michael@0: * @param aVertical how to align vertically, specified in percents, and when. michael@0: * @param aHorizontal how to align horizontally, specified in percents, and when. michael@0: */ michael@0: static nsresult ScrollSubstringTo(nsIFrame* aFrame, nsRange* aRange, michael@0: nsIPresShell::ScrollAxis aVertical, michael@0: nsIPresShell::ScrollAxis aHorizontal); michael@0: michael@0: /** michael@0: * Scrolls the given frame to the point, used for implememntation of michael@0: * nsIAccessible::scrollToPoint and nsIAccessibleText::scrollSubstringToPoint. michael@0: * michael@0: * @param aScrollableFrame the scrollable frame michael@0: * @param aFrame the frame to scroll michael@0: * @param aPoint the point scroll to michael@0: */ michael@0: static void ScrollFrameToPoint(nsIFrame *aScrollableFrame, michael@0: nsIFrame *aFrame, const nsIntPoint& aPoint); michael@0: michael@0: /** michael@0: * Converts scroll type constant defined in nsIAccessibleScrollType to michael@0: * vertical and horizontal parameters. michael@0: */ michael@0: static void ConvertScrollTypeToPercents(uint32_t aScrollType, michael@0: nsIPresShell::ScrollAxis *aVertical, michael@0: nsIPresShell::ScrollAxis *aHorizontal); michael@0: michael@0: /** michael@0: * Returns coordinates in device pixels relative screen for the top level michael@0: * window. michael@0: * michael@0: * @param aNode the DOM node hosted in the window. michael@0: */ michael@0: static nsIntPoint GetScreenCoordsForWindow(nsINode *aNode); michael@0: michael@0: /** michael@0: * Return document shell for the given DOM node. michael@0: */ michael@0: static already_AddRefed GetDocShellFor(nsINode *aNode); michael@0: michael@0: /** michael@0: * Return true if the given document is root document. michael@0: */ michael@0: static bool IsRootDocument(nsIDocument *aDocument); michael@0: michael@0: /** michael@0: * Return true if the given document is content document (not chrome). michael@0: */ michael@0: static bool IsContentDocument(nsIDocument *aDocument); michael@0: michael@0: /** michael@0: * Return true if the given document node is for tab document accessible. michael@0: */ michael@0: static bool IsTabDocument(nsIDocument* aDocumentNode); michael@0: michael@0: /** michael@0: * Return true if the given document is an error page. michael@0: */ michael@0: static bool IsErrorPage(nsIDocument *aDocument); michael@0: michael@0: /** michael@0: * Return presShell for the document containing the given DOM node. michael@0: */ michael@0: static nsIPresShell *GetPresShellFor(nsINode *aNode) michael@0: { michael@0: return aNode->OwnerDoc()->GetShell(); michael@0: } michael@0: michael@0: /** michael@0: * Get the ID for an element, in some types of XML this may not be the ID attribute michael@0: * @param aContent Node to get the ID for michael@0: * @param aID Where to put ID string michael@0: * @return true if there is an ID set for this node michael@0: */ michael@0: static bool GetID(nsIContent *aContent, nsAString& aID); michael@0: michael@0: /** michael@0: * Convert attribute value of the given node to positive integer. If no michael@0: * attribute or wrong value then false is returned. michael@0: */ michael@0: static bool GetUIntAttr(nsIContent *aContent, nsIAtom *aAttr, michael@0: int32_t *aUInt); michael@0: michael@0: /** michael@0: * Returns language for the given node. michael@0: * michael@0: * @param aContent [in] the given node michael@0: * @param aRootContent [in] container of the given node michael@0: * @param aLanguage [out] language michael@0: */ michael@0: static void GetLanguageFor(nsIContent *aContent, nsIContent *aRootContent, michael@0: nsAString& aLanguage); michael@0: michael@0: /** michael@0: * Return box object for XUL treechildren element by tree box object. michael@0: */ michael@0: static already_AddRefed michael@0: GetTreeBodyBoxObject(nsITreeBoxObject *aTreeBoxObj); michael@0: michael@0: /** michael@0: * Return tree box object from any levels DOMNode under the XUL tree. michael@0: */ michael@0: static already_AddRefed michael@0: GetTreeBoxObject(nsIContent* aContent); michael@0: michael@0: /** michael@0: * Return first sensible column for the given tree box object. michael@0: */ michael@0: static already_AddRefed michael@0: GetFirstSensibleColumn(nsITreeBoxObject *aTree); michael@0: michael@0: /** michael@0: * Return sensible columns count for the given tree box object. michael@0: */ michael@0: static uint32_t GetSensibleColumnCount(nsITreeBoxObject *aTree); michael@0: michael@0: /** michael@0: * Return sensible column at the given index for the given tree box object. michael@0: */ michael@0: static already_AddRefed michael@0: GetSensibleColumnAt(nsITreeBoxObject *aTree, uint32_t aIndex); michael@0: michael@0: /** michael@0: * Return next sensible column for the given column. michael@0: */ michael@0: static already_AddRefed michael@0: GetNextSensibleColumn(nsITreeColumn *aColumn); michael@0: michael@0: /** michael@0: * Return previous sensible column for the given column. michael@0: */ michael@0: static already_AddRefed michael@0: GetPreviousSensibleColumn(nsITreeColumn *aColumn); michael@0: michael@0: /** michael@0: * Return true if the given column is hidden (i.e. not sensible). michael@0: */ michael@0: static bool IsColumnHidden(nsITreeColumn *aColumn); michael@0: michael@0: /** michael@0: * Scroll content into view. michael@0: */ michael@0: static void ScrollTo(nsIPresShell* aPresShell, nsIContent* aContent, michael@0: uint32_t aScrollType); michael@0: michael@0: /** michael@0: * Return true if the given node is table header element. michael@0: */ michael@0: static bool IsHTMLTableHeader(nsIContent *aContent) michael@0: { michael@0: return aContent->NodeInfo()->Equals(nsGkAtoms::th) || michael@0: aContent->HasAttr(kNameSpaceID_None, nsGkAtoms::scope); michael@0: } michael@0: michael@0: /** michael@0: * Returns true if the given string is empty or contains whitespace symbols michael@0: * only. In contrast to nsWhitespaceTokenizer class it takes into account michael@0: * non-breaking space (0xa0). michael@0: */ michael@0: static bool IsWhitespaceString(const nsSubstring& aString); michael@0: michael@0: /** michael@0: * Returns true if the given character is whitespace symbol. michael@0: */ michael@0: static bool IsWhitespace(char16_t aChar) michael@0: { michael@0: return aChar == ' ' || aChar == '\n' || michael@0: aChar == '\r' || aChar == '\t' || aChar == 0xa0; michael@0: } michael@0: }; michael@0: michael@0: #endif michael@0: