Bug 1890277: part 2) Add `require-trusted-types-for` directive to CSP parser, guarded...
[gecko.git] / accessible / xpcom / xpcAccessibleTextRange.cpp
blob1ecfdd6c2b287f488852616a05a7b8e9a1edc939
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=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 file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "xpcAccessibleTextRange.h"
9 #include "TextRange-inl.h"
11 #include "nsQueryObject.h"
12 #include "xpcAccessibleDocument.h"
14 using namespace mozilla;
15 using namespace mozilla::a11y;
17 // nsISupports and cycle collection
19 NS_INTERFACE_MAP_BEGIN(xpcAccessibleTextRange)
20 NS_INTERFACE_MAP_ENTRY(nsIAccessibleTextRange)
21 NS_INTERFACE_MAP_ENTRY(xpcAccessibleTextRange)
22 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIAccessibleTextRange)
23 NS_INTERFACE_MAP_END
25 NS_IMPL_ADDREF(xpcAccessibleTextRange)
26 NS_IMPL_RELEASE(xpcAccessibleTextRange)
28 a11y::TextRange xpcAccessibleTextRange::Range() {
29 return a11y::TextRange(mRoot->ToInternalGeneric(),
30 mStartContainer->ToInternalGeneric(), mStartOffset,
31 mEndContainer->ToInternalGeneric(), mEndOffset);
34 void xpcAccessibleTextRange::SetRange(TextRange& aRange) {
35 mRoot = ToXPCText(aRange.Root());
36 mStartContainer = ToXPCText(aRange.StartContainer());
37 mStartOffset = aRange.StartOffset();
38 mEndContainer = ToXPCText(aRange.EndContainer());
39 mEndOffset = aRange.EndOffset();
42 // nsIAccessibleTextRange
44 NS_IMETHODIMP
45 xpcAccessibleTextRange::GetStartContainer(nsIAccessibleText** aAnchor) {
46 NS_ENSURE_ARG_POINTER(aAnchor);
47 NS_IF_ADDREF(*aAnchor = mStartContainer);
48 return NS_OK;
51 NS_IMETHODIMP
52 xpcAccessibleTextRange::GetStartOffset(int32_t* aOffset) {
53 NS_ENSURE_ARG_POINTER(aOffset);
54 *aOffset = mStartOffset;
55 return NS_OK;
58 NS_IMETHODIMP
59 xpcAccessibleTextRange::GetEndContainer(nsIAccessibleText** aAnchor) {
60 NS_ENSURE_ARG_POINTER(aAnchor);
61 NS_IF_ADDREF(*aAnchor = mEndContainer);
62 return NS_OK;
65 NS_IMETHODIMP
66 xpcAccessibleTextRange::GetEndOffset(int32_t* aOffset) {
67 NS_ENSURE_ARG_POINTER(aOffset);
68 *aOffset = mEndOffset;
69 return NS_OK;
72 NS_IMETHODIMP
73 xpcAccessibleTextRange::GetContainer(nsIAccessible** aContainer) {
74 NS_ENSURE_ARG_POINTER(aContainer);
75 NS_IF_ADDREF(*aContainer = ToXPC(Range().Container()));
76 return NS_OK;
79 NS_IMETHODIMP
80 xpcAccessibleTextRange::Compare(nsIAccessibleTextRange* aOtherRange,
81 bool* aResult) {
82 RefPtr<xpcAccessibleTextRange> xpcRange(do_QueryObject(aOtherRange));
83 if (!xpcRange || !aResult) return NS_ERROR_INVALID_ARG;
85 *aResult = (Range() == xpcRange->Range());
86 return NS_OK;
89 NS_IMETHODIMP
90 xpcAccessibleTextRange::CompareEndPoints(uint32_t aEndPoint,
91 nsIAccessibleTextRange* aOtherRange,
92 uint32_t aOtherRangeEndPoint,
93 int32_t* aResult) {
94 RefPtr<xpcAccessibleTextRange> xpcRange(do_QueryObject(aOtherRange));
95 if (!xpcRange || !aResult) return NS_ERROR_INVALID_ARG;
97 TextRange thisRange = Range();
98 TextRange otherRange = xpcRange->Range();
99 TextPoint p = (aEndPoint == EndPoint_Start) ? thisRange.StartPoint()
100 : thisRange.EndPoint();
101 TextPoint otherPoint = (aOtherRangeEndPoint == EndPoint_Start)
102 ? otherRange.StartPoint()
103 : otherRange.EndPoint();
105 if (p == otherPoint) {
106 *aResult = 0;
107 } else {
108 *aResult = p < otherPoint ? -1 : 1;
111 return NS_OK;
114 NS_IMETHODIMP
115 xpcAccessibleTextRange::Crop(nsIAccessible* aContainer, bool* aSuccess) {
116 Accessible* container = aContainer->ToInternalGeneric();
117 NS_ENSURE_TRUE(container, NS_ERROR_INVALID_ARG);
119 TextRange range = Range();
120 *aSuccess = range.Crop(container);
121 if (*aSuccess) {
122 SetRange(range);
124 return NS_OK;