tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

nsDOMCaretPosition.cpp (1898B)


      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 #include "nsDOMCaretPosition.h"
      8 
      9 #include "mozilla/ErrorResult.h"
     10 #include "mozilla/dom/CaretPositionBinding.h"
     11 #include "mozilla/dom/DOMRect.h"
     12 #include "nsRange.h"
     13 
     14 using namespace mozilla::dom;
     15 
     16 nsDOMCaretPosition::nsDOMCaretPosition(nsINode* aNode, uint32_t aOffset)
     17    : mOffset(aOffset), mOffsetNode(aNode), mAnonymousContentNode(nullptr) {}
     18 
     19 nsDOMCaretPosition::~nsDOMCaretPosition() = default;
     20 
     21 nsINode* nsDOMCaretPosition::GetOffsetNode() const { return mOffsetNode; }
     22 
     23 already_AddRefed<DOMRect> nsDOMCaretPosition::GetClientRect() const {
     24  if (!mOffsetNode) {
     25    return nullptr;
     26  }
     27 
     28  nsCOMPtr<nsINode> node;
     29  if (mAnonymousContentNode) {
     30    node = mAnonymousContentNode;
     31  } else {
     32    node = mOffsetNode;
     33  }
     34 
     35  RefPtr<nsRange> range =
     36      nsRange::Create(node, mOffset, node, mOffset, mozilla::IgnoreErrors());
     37  if (!range) {
     38    return nullptr;
     39  }
     40  RefPtr<DOMRect> rect = range->GetBoundingClientRect(false);
     41  return rect.forget();
     42 }
     43 
     44 JSObject* nsDOMCaretPosition::WrapObject(JSContext* aCx,
     45                                         JS::Handle<JSObject*> aGivenProto) {
     46  return mozilla::dom::CaretPosition_Binding::Wrap(aCx, this, aGivenProto);
     47 }
     48 
     49 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(nsDOMCaretPosition, mOffsetNode,
     50                                      mAnonymousContentNode)
     51 
     52 NS_IMPL_CYCLE_COLLECTING_ADDREF(nsDOMCaretPosition)
     53 NS_IMPL_CYCLE_COLLECTING_RELEASE(nsDOMCaretPosition)
     54 
     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