1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/base/src/nsDOMCaretPosition.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,75 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#include "nsDOMCaretPosition.h" 1.9 + 1.10 +#include "mozilla/dom/CaretPositionBinding.h" 1.11 +#include "mozilla/dom/DOMRect.h" 1.12 +#include "nsRange.h" 1.13 + 1.14 +using namespace mozilla::dom; 1.15 + 1.16 +nsDOMCaretPosition::nsDOMCaretPosition(nsINode* aNode, uint32_t aOffset) 1.17 + : mOffset(aOffset), mOffsetNode(aNode), mAnonymousContentNode(nullptr) 1.18 +{ 1.19 + SetIsDOMBinding(); 1.20 +} 1.21 + 1.22 +nsDOMCaretPosition::~nsDOMCaretPosition() 1.23 +{ 1.24 +} 1.25 + 1.26 +nsINode* nsDOMCaretPosition::GetOffsetNode() const 1.27 +{ 1.28 + return mOffsetNode; 1.29 +} 1.30 + 1.31 +already_AddRefed<DOMRect> 1.32 +nsDOMCaretPosition::GetClientRect() const 1.33 +{ 1.34 + if (!mOffsetNode) { 1.35 + return nullptr; 1.36 + } 1.37 + 1.38 + nsRefPtr<DOMRect> rect; 1.39 + nsRefPtr<nsRange> domRange; 1.40 + nsCOMPtr<nsINode> node; 1.41 + 1.42 + if (mAnonymousContentNode) { 1.43 + node = mAnonymousContentNode; 1.44 + } else { 1.45 + node = mOffsetNode; 1.46 + } 1.47 + 1.48 + nsresult creationRv = nsRange::CreateRange(node, mOffset, node, 1.49 + mOffset, 1.50 + getter_AddRefs<nsRange>(domRange)); 1.51 + if (!NS_SUCCEEDED(creationRv)) { 1.52 + return nullptr; 1.53 + } 1.54 + 1.55 + NS_ASSERTION(domRange, "unable to retrieve valid dom range from CaretPosition"); 1.56 + 1.57 + rect = domRange->GetBoundingClientRect(); 1.58 + 1.59 + return rect.forget(); 1.60 +} 1.61 + 1.62 +JSObject* 1.63 +nsDOMCaretPosition::WrapObject(JSContext *aCx) 1.64 +{ 1.65 + return mozilla::dom::CaretPositionBinding::Wrap(aCx, this); 1.66 +} 1.67 + 1.68 +NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_2(nsDOMCaretPosition, 1.69 + mOffsetNode, mAnonymousContentNode) 1.70 + 1.71 +NS_IMPL_CYCLE_COLLECTING_ADDREF(nsDOMCaretPosition) 1.72 +NS_IMPL_CYCLE_COLLECTING_RELEASE(nsDOMCaretPosition) 1.73 + 1.74 +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsDOMCaretPosition) 1.75 + NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY 1.76 + NS_INTERFACE_MAP_ENTRY(nsISupports) 1.77 +NS_INTERFACE_MAP_END 1.78 +