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: #include "nsDOMCaretPosition.h" michael@0: michael@0: #include "mozilla/dom/CaretPositionBinding.h" michael@0: #include "mozilla/dom/DOMRect.h" michael@0: #include "nsRange.h" michael@0: michael@0: using namespace mozilla::dom; michael@0: michael@0: nsDOMCaretPosition::nsDOMCaretPosition(nsINode* aNode, uint32_t aOffset) michael@0: : mOffset(aOffset), mOffsetNode(aNode), mAnonymousContentNode(nullptr) michael@0: { michael@0: SetIsDOMBinding(); michael@0: } michael@0: michael@0: nsDOMCaretPosition::~nsDOMCaretPosition() michael@0: { michael@0: } michael@0: michael@0: nsINode* nsDOMCaretPosition::GetOffsetNode() const michael@0: { michael@0: return mOffsetNode; michael@0: } michael@0: michael@0: already_AddRefed michael@0: nsDOMCaretPosition::GetClientRect() const michael@0: { michael@0: if (!mOffsetNode) { michael@0: return nullptr; michael@0: } michael@0: michael@0: nsRefPtr rect; michael@0: nsRefPtr domRange; michael@0: nsCOMPtr node; michael@0: michael@0: if (mAnonymousContentNode) { michael@0: node = mAnonymousContentNode; michael@0: } else { michael@0: node = mOffsetNode; michael@0: } michael@0: michael@0: nsresult creationRv = nsRange::CreateRange(node, mOffset, node, michael@0: mOffset, michael@0: getter_AddRefs(domRange)); michael@0: if (!NS_SUCCEEDED(creationRv)) { michael@0: return nullptr; michael@0: } michael@0: michael@0: NS_ASSERTION(domRange, "unable to retrieve valid dom range from CaretPosition"); michael@0: michael@0: rect = domRange->GetBoundingClientRect(); michael@0: michael@0: return rect.forget(); michael@0: } michael@0: michael@0: JSObject* michael@0: nsDOMCaretPosition::WrapObject(JSContext *aCx) michael@0: { michael@0: return mozilla::dom::CaretPositionBinding::Wrap(aCx, this); michael@0: } michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_2(nsDOMCaretPosition, michael@0: mOffsetNode, mAnonymousContentNode) michael@0: michael@0: NS_IMPL_CYCLE_COLLECTING_ADDREF(nsDOMCaretPosition) michael@0: NS_IMPL_CYCLE_COLLECTING_RELEASE(nsDOMCaretPosition) michael@0: michael@0: NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsDOMCaretPosition) michael@0: NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY michael@0: NS_INTERFACE_MAP_ENTRY(nsISupports) michael@0: NS_INTERFACE_MAP_END michael@0: