Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / dom / base / nsDOMCaretPosition.cpp
blob69fdfa5d02b4e682f6e6978cc522d0fd781ce264
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/. */
7 #include "nsDOMCaretPosition.h"
9 #include "mozilla/dom/CaretPositionBinding.h"
10 #include "mozilla/dom/DOMRect.h"
11 #include "mozilla/ErrorResult.h"
12 #include "nsRange.h"
14 using namespace mozilla::dom;
16 nsDOMCaretPosition::nsDOMCaretPosition(nsINode* aNode, uint32_t aOffset)
17 : mOffset(aOffset), mOffsetNode(aNode), mAnonymousContentNode(nullptr) {}
19 nsDOMCaretPosition::~nsDOMCaretPosition() = default;
21 nsINode* nsDOMCaretPosition::GetOffsetNode() const { return mOffsetNode; }
23 already_AddRefed<DOMRect> nsDOMCaretPosition::GetClientRect() const {
24 if (!mOffsetNode) {
25 return nullptr;
28 nsCOMPtr<nsINode> node;
29 if (mAnonymousContentNode) {
30 node = mAnonymousContentNode;
31 } else {
32 node = mOffsetNode;
35 RefPtr<nsRange> range =
36 nsRange::Create(node, mOffset, node, mOffset, mozilla::IgnoreErrors());
37 if (!range) {
38 return nullptr;
40 RefPtr<DOMRect> rect = range->GetBoundingClientRect(false);
41 return rect.forget();
44 JSObject* nsDOMCaretPosition::WrapObject(JSContext* aCx,
45 JS::Handle<JSObject*> aGivenProto) {
46 return mozilla::dom::CaretPosition_Binding::Wrap(aCx, this, aGivenProto);
49 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(nsDOMCaretPosition, mOffsetNode,
50 mAnonymousContentNode)
52 NS_IMPL_CYCLE_COLLECTING_ADDREF(nsDOMCaretPosition)
53 NS_IMPL_CYCLE_COLLECTING_RELEASE(nsDOMCaretPosition)
55 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsDOMCaretPosition)
56 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
57 NS_INTERFACE_MAP_ENTRY(nsISupports)
58 NS_INTERFACE_MAP_END