nsDOMCaretPosition.h (2960B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef nsDOMCaretPosition_h 8 #define nsDOMCaretPosition_h 9 10 #include "nsCOMPtr.h" 11 #include "nsCycleCollectionParticipant.h" 12 #include "nsINode.h" 13 #include "nsWrapperCache.h" 14 15 namespace mozilla::dom { 16 class DOMRect; 17 } // namespace mozilla::dom 18 19 /** 20 * Implementation of a DOM Caret Position, which is a node and offset within 21 * that node, in the DOM tree. 22 * 23 * http://www.w3.org/TR/cssom-view/#dom-documentview-caretrangefrompoint 24 * 25 * @see Document::caretPositionFromPoint(float x, float y) 26 */ 27 class nsDOMCaretPosition : public nsISupports, public nsWrapperCache { 28 using DOMRect = mozilla::dom::DOMRect; 29 30 public: 31 NS_DECL_CYCLE_COLLECTING_ISUPPORTS 32 NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(nsDOMCaretPosition) 33 34 nsDOMCaretPosition(nsINode* aNode, uint32_t aOffset); 35 36 /** 37 * Retrieve the offset (character position within the DOM node) of the 38 * CaretPosition. 39 * 40 * @returns The offset within the DOM node. 41 */ 42 uint32_t Offset() const { return mOffset; } 43 44 /** 45 * Retrieve the DOM node with which this CaretPosition was established. 46 * Normally, this will be created from a point, so it will be the DOM 47 * node that lies at the point specified. 48 * 49 * @returns The DOM node of the CaretPosition. 50 * 51 * @see Document::caretPositionFromPoint(float x, float y) 52 */ 53 nsINode* GetOffsetNode() const; 54 55 /** 56 * Retrieve the bounding rectangle of this CaretPosition object. 57 * 58 * @returns An nsClientRect representing the bounding rectangle of this 59 * CaretPosition, if one can be successfully determined, otherwise 60 * nullptr. 61 */ 62 already_AddRefed<DOMRect> GetClientRect() const; 63 64 /** 65 * Set the anonymous content node that is the actual parent of this 66 * CaretPosition object. In situations where the DOM node for a CaretPosition 67 * actually lies within an anonymous content node (e.g. a textarea), the 68 * actual parent is not set as the offset node. This is used to get the 69 * correct bounding box of a CaretPosition object that lies within a textarea 70 * or input element. 71 * 72 * @param aNode A pointer to an nsINode object that is the actual element 73 * within which this CaretPosition lies, but is an anonymous content 74 * node. 75 */ 76 void SetAnonymousContentNode(nsINode* aNode) { 77 mAnonymousContentNode = aNode; 78 } 79 80 nsISupports* GetParentObject() const { return GetOffsetNode(); } 81 82 JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) final; 83 84 protected: 85 virtual ~nsDOMCaretPosition(); 86 87 uint32_t mOffset; 88 nsCOMPtr<nsINode> mOffsetNode; 89 nsCOMPtr<nsINode> mAnonymousContentNode; 90 }; 91 #endif