2014-02-27 20:33:09 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2016-01-20 20:53:03 +03:00
|
|
|
#include "TextRange-inl.h"
|
2014-02-27 20:33:09 +04:00
|
|
|
|
2021-02-20 02:14:32 +03:00
|
|
|
#include "LocalAccessible-inl.h"
|
2020-09-22 01:52:01 +03:00
|
|
|
#include "HyperTextAccessible-inl.h"
|
2021-12-09 10:35:09 +03:00
|
|
|
#include "mozilla/IntegerRange.h"
|
2020-06-23 21:52:42 +03:00
|
|
|
#include "mozilla/dom/Selection.h"
|
2014-05-23 04:10:19 +04:00
|
|
|
#include "nsAccUtils.h"
|
2014-02-27 20:33:09 +04:00
|
|
|
|
2014-07-28 18:40:27 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace a11y {
|
2014-02-27 20:33:09 +04:00
|
|
|
|
2022-02-27 02:01:54 +03:00
|
|
|
/**
|
|
|
|
* Returns a text point for aAcc within aContainer.
|
|
|
|
*/
|
|
|
|
static void ToTextPoint(Accessible* aAcc, Accessible** aContainer,
|
|
|
|
int32_t* aOffset, bool aIsBefore = true) {
|
|
|
|
if (aAcc->IsHyperText()) {
|
|
|
|
*aContainer = aAcc;
|
|
|
|
*aOffset =
|
|
|
|
aIsBefore
|
|
|
|
? 0
|
|
|
|
: static_cast<int32_t>(aAcc->AsHyperTextBase()->CharacterCount());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Accessible* child = nullptr;
|
|
|
|
Accessible* parent = aAcc;
|
|
|
|
do {
|
|
|
|
child = parent;
|
|
|
|
parent = parent->Parent();
|
|
|
|
} while (parent && !parent->IsHyperText());
|
|
|
|
|
|
|
|
if (parent) {
|
|
|
|
*aContainer = parent;
|
|
|
|
*aOffset = parent->AsHyperTextBase()->GetChildOffset(
|
|
|
|
child->IndexInParent() + static_cast<int32_t>(!aIsBefore));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-23 04:10:19 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// TextPoint
|
|
|
|
|
|
|
|
bool TextPoint::operator<(const TextPoint& aPoint) const {
|
|
|
|
if (mContainer == aPoint.mContainer) return mOffset < aPoint.mOffset;
|
|
|
|
|
|
|
|
// Build the chain of parents
|
2022-02-27 02:01:54 +03:00
|
|
|
Accessible* p1 = mContainer;
|
|
|
|
Accessible* p2 = aPoint.mContainer;
|
|
|
|
AutoTArray<Accessible*, 30> parents1, parents2;
|
2014-05-23 04:10:19 +04:00
|
|
|
do {
|
|
|
|
parents1.AppendElement(p1);
|
2022-02-27 02:01:54 +03:00
|
|
|
p1 = p1->Parent();
|
2014-05-23 04:10:19 +04:00
|
|
|
} while (p1);
|
|
|
|
do {
|
|
|
|
parents2.AppendElement(p2);
|
2022-02-27 02:01:54 +03:00
|
|
|
p2 = p2->Parent();
|
2014-05-23 04:10:19 +04:00
|
|
|
} while (p2);
|
|
|
|
|
|
|
|
// Find where the parent chain differs
|
|
|
|
uint32_t pos1 = parents1.Length(), pos2 = parents2.Length();
|
|
|
|
for (uint32_t len = std::min(pos1, pos2); len > 0; --len) {
|
2022-02-27 02:01:54 +03:00
|
|
|
Accessible* child1 = parents1.ElementAt(--pos1);
|
|
|
|
Accessible* child2 = parents2.ElementAt(--pos2);
|
2021-02-20 02:14:32 +03:00
|
|
|
if (child1 != child2) {
|
2014-05-23 04:10:19 +04:00
|
|
|
return child1->IndexInParent() < child2->IndexInParent();
|
2021-02-20 02:14:32 +03:00
|
|
|
}
|
2014-05-23 04:10:19 +04:00
|
|
|
}
|
|
|
|
|
2020-08-11 19:26:04 +03:00
|
|
|
if (pos1 != 0) {
|
|
|
|
// If parents1 is a superset of parents2 then mContainer is a
|
|
|
|
// descendant of aPoint.mContainer. The next element down in parents1
|
|
|
|
// is mContainer's ancestor that is the child of aPoint.mContainer.
|
|
|
|
// We compare its end offset in aPoint.mContainer with aPoint.mOffset.
|
2022-02-27 02:01:54 +03:00
|
|
|
Accessible* child = parents1.ElementAt(pos1 - 1);
|
|
|
|
MOZ_ASSERT(child->Parent() == aPoint.mContainer);
|
2020-08-11 19:26:04 +03:00
|
|
|
return child->EndOffset() < static_cast<uint32_t>(aPoint.mOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pos2 != 0) {
|
|
|
|
// If parents2 is a superset of parents1 then aPoint.mContainer is a
|
|
|
|
// descendant of mContainer. The next element down in parents2
|
|
|
|
// is aPoint.mContainer's ancestor that is the child of mContainer.
|
|
|
|
// We compare its start offset in mContainer with mOffset.
|
2022-02-27 02:01:54 +03:00
|
|
|
Accessible* child = parents2.ElementAt(pos2 - 1);
|
|
|
|
MOZ_ASSERT(child->Parent() == mContainer);
|
2020-08-11 19:26:04 +03:00
|
|
|
return static_cast<uint32_t>(mOffset) < child->StartOffset();
|
|
|
|
}
|
|
|
|
|
2014-05-23 04:10:19 +04:00
|
|
|
NS_ERROR("Broken tree?!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// TextRange
|
|
|
|
|
2022-02-27 02:01:54 +03:00
|
|
|
TextRange::TextRange(Accessible* aRoot, Accessible* aStartContainer,
|
|
|
|
int32_t aStartOffset, Accessible* aEndContainer,
|
|
|
|
int32_t aEndOffset)
|
2014-02-27 20:33:09 +04:00
|
|
|
: mRoot(aRoot),
|
|
|
|
mStartContainer(aStartContainer),
|
|
|
|
mEndContainer(aEndContainer),
|
|
|
|
mStartOffset(aStartOffset),
|
|
|
|
mEndOffset(aEndOffset) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2022-02-27 02:01:54 +03:00
|
|
|
bool TextRange::Crop(Accessible* aContainer) {
|
2016-01-20 20:53:03 +03:00
|
|
|
uint32_t boundaryPos = 0, containerPos = 0;
|
2022-02-27 02:01:54 +03:00
|
|
|
AutoTArray<Accessible*, 30> boundaryParents, containerParents;
|
2016-01-20 20:53:03 +03:00
|
|
|
|
|
|
|
// Crop the start boundary.
|
2022-02-27 02:01:54 +03:00
|
|
|
Accessible* container = nullptr;
|
|
|
|
HyperTextAccessibleBase* startHyper = mStartContainer->AsHyperTextBase();
|
|
|
|
Accessible* boundary = startHyper->GetChildAtOffset(mStartOffset);
|
2016-01-20 20:53:03 +03:00
|
|
|
if (boundary != aContainer) {
|
|
|
|
CommonParent(boundary, aContainer, &boundaryParents, &boundaryPos,
|
|
|
|
&containerParents, &containerPos);
|
|
|
|
|
|
|
|
if (boundaryPos == 0) {
|
|
|
|
if (containerPos != 0) {
|
|
|
|
// The container is contained by the start boundary, reduce the range to
|
|
|
|
// the point starting at the container.
|
2022-02-27 02:01:54 +03:00
|
|
|
ToTextPoint(aContainer, &mStartContainer, &mStartOffset);
|
2016-01-20 20:53:03 +03:00
|
|
|
} else {
|
|
|
|
// The start boundary and the container are siblings.
|
|
|
|
container = aContainer;
|
|
|
|
}
|
2022-02-27 02:01:56 +03:00
|
|
|
} else {
|
2016-01-20 20:53:03 +03:00
|
|
|
// The container does not contain the start boundary.
|
|
|
|
boundary = boundaryParents[boundaryPos];
|
|
|
|
container = containerParents[containerPos];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (container) {
|
|
|
|
// If the range start is after the container, then make the range invalid.
|
|
|
|
if (boundary->IndexInParent() > container->IndexInParent()) {
|
|
|
|
return !!(mRoot = nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the range starts before the container, then reduce the range to
|
|
|
|
// the point starting at the container.
|
|
|
|
if (boundary->IndexInParent() < container->IndexInParent()) {
|
2022-02-27 02:01:54 +03:00
|
|
|
ToTextPoint(container, &mStartContainer, &mStartOffset);
|
2016-01-20 20:53:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
boundaryParents.SetLengthAndRetainStorage(0);
|
|
|
|
containerParents.SetLengthAndRetainStorage(0);
|
|
|
|
}
|
|
|
|
|
2022-02-27 02:01:54 +03:00
|
|
|
HyperTextAccessibleBase* endHyper = mEndContainer->AsHyperTextBase();
|
|
|
|
boundary = endHyper->GetChildAtOffset(mEndOffset);
|
2016-01-20 20:53:03 +03:00
|
|
|
if (boundary == aContainer) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Crop the end boundary.
|
|
|
|
container = nullptr;
|
|
|
|
CommonParent(boundary, aContainer, &boundaryParents, &boundaryPos,
|
|
|
|
&containerParents, &containerPos);
|
|
|
|
|
|
|
|
if (boundaryPos == 0) {
|
|
|
|
if (containerPos != 0) {
|
2022-02-27 02:01:54 +03:00
|
|
|
ToTextPoint(aContainer, &mEndContainer, &mEndOffset, false);
|
2016-01-20 20:53:03 +03:00
|
|
|
} else {
|
|
|
|
container = aContainer;
|
|
|
|
}
|
2022-02-27 02:01:56 +03:00
|
|
|
} else {
|
2016-01-20 20:53:03 +03:00
|
|
|
boundary = boundaryParents[boundaryPos];
|
|
|
|
container = containerParents[containerPos];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!container) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (boundary->IndexInParent() < container->IndexInParent()) {
|
|
|
|
return !!(mRoot = nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (boundary->IndexInParent() > container->IndexInParent()) {
|
2022-02-27 02:01:54 +03:00
|
|
|
ToTextPoint(container, &mEndContainer, &mEndOffset, false);
|
2016-01-20 20:53:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-09-22 01:52:01 +03:00
|
|
|
/**
|
|
|
|
* Convert the given DOM point to a DOM point in non-generated contents.
|
|
|
|
*
|
|
|
|
* If aDOMPoint is in ::before, the result is immediately after it.
|
|
|
|
* If aDOMPoint is in ::after, the result is immediately before it.
|
|
|
|
*/
|
|
|
|
static DOMPoint ClosestNotGeneratedDOMPoint(const DOMPoint& aDOMPoint,
|
|
|
|
nsIContent* aElementContent) {
|
|
|
|
MOZ_ASSERT(aDOMPoint.node, "The node must not be null");
|
|
|
|
|
|
|
|
// ::before pseudo element
|
|
|
|
if (aElementContent &&
|
|
|
|
aElementContent->IsGeneratedContentContainerForBefore()) {
|
|
|
|
MOZ_ASSERT(aElementContent->GetParent(),
|
|
|
|
"::before must have parent element");
|
|
|
|
// The first child of its parent (i.e., immediately after the ::before) is
|
|
|
|
// good point for a DOM range.
|
|
|
|
return DOMPoint(aElementContent->GetParent(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ::after pseudo element
|
|
|
|
if (aElementContent &&
|
|
|
|
aElementContent->IsGeneratedContentContainerForAfter()) {
|
|
|
|
MOZ_ASSERT(aElementContent->GetParent(),
|
|
|
|
"::after must have parent element");
|
|
|
|
// The end of its parent (i.e., immediately before the ::after) is good
|
|
|
|
// point for a DOM range.
|
|
|
|
return DOMPoint(aElementContent->GetParent(),
|
|
|
|
aElementContent->GetParent()->GetChildCount());
|
|
|
|
}
|
2014-05-23 04:10:19 +04:00
|
|
|
|
2020-09-22 01:52:01 +03:00
|
|
|
return aDOMPoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GetElementAsContentOf() returns a content representing an element which is
|
|
|
|
* or includes aNode.
|
|
|
|
*
|
|
|
|
* XXX This method is enough to retrieve ::before or ::after pseudo element.
|
|
|
|
* So, if you want to use this for other purpose, you might need to check
|
|
|
|
* ancestors too.
|
|
|
|
*/
|
|
|
|
static nsIContent* GetElementAsContentOf(nsINode* aNode) {
|
|
|
|
if (auto* element = dom::Element::FromNode(aNode)) {
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
return aNode->GetParentElement();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TextRange::AssignDOMRange(nsRange* aRange, bool* aReversed) const {
|
2022-02-27 02:01:54 +03:00
|
|
|
MOZ_ASSERT(mRoot->IsLocal(), "Not supported for RemoteAccessible");
|
2020-09-22 01:52:01 +03:00
|
|
|
bool reversed = EndPoint() < StartPoint();
|
|
|
|
if (aReversed) {
|
|
|
|
*aReversed = reversed;
|
|
|
|
}
|
|
|
|
|
2022-02-27 02:01:54 +03:00
|
|
|
HyperTextAccessible* startHyper = mStartContainer->AsLocal()->AsHyperText();
|
|
|
|
HyperTextAccessible* endHyper = mEndContainer->AsLocal()->AsHyperText();
|
|
|
|
DOMPoint startPoint = reversed ? endHyper->OffsetToDOMPoint(mEndOffset)
|
|
|
|
: startHyper->OffsetToDOMPoint(mStartOffset);
|
2020-09-22 01:52:01 +03:00
|
|
|
if (!startPoint.node) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// HyperTextAccessible manages pseudo elements generated by ::before or
|
|
|
|
// ::after. However, contents of them are not in the DOM tree normally.
|
|
|
|
// Therefore, they are not selectable and editable. So, when this creates
|
|
|
|
// a DOM range, it should not start from nor end in any pseudo contents.
|
|
|
|
|
|
|
|
nsIContent* container = GetElementAsContentOf(startPoint.node);
|
|
|
|
DOMPoint startPointForDOMRange =
|
|
|
|
ClosestNotGeneratedDOMPoint(startPoint, container);
|
|
|
|
aRange->SetStart(startPointForDOMRange.node, startPointForDOMRange.idx);
|
|
|
|
|
|
|
|
// If the caller wants collapsed range, let's collapse the range to its start.
|
|
|
|
if (mEndContainer == mStartContainer && mEndOffset == mStartOffset) {
|
|
|
|
aRange->Collapse(true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-02-27 02:01:54 +03:00
|
|
|
DOMPoint endPoint = reversed ? startHyper->OffsetToDOMPoint(mStartOffset)
|
|
|
|
: endHyper->OffsetToDOMPoint(mEndOffset);
|
2020-09-22 01:52:01 +03:00
|
|
|
if (!endPoint.node) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (startPoint.node != endPoint.node) {
|
|
|
|
container = GetElementAsContentOf(endPoint.node);
|
|
|
|
}
|
|
|
|
|
|
|
|
DOMPoint endPointForDOMRange =
|
|
|
|
ClosestNotGeneratedDOMPoint(endPoint, container);
|
|
|
|
aRange->SetEnd(endPointForDOMRange.node, endPointForDOMRange.idx);
|
|
|
|
return true;
|
|
|
|
}
|
2014-05-23 04:10:19 +04:00
|
|
|
|
2020-06-23 21:52:42 +03:00
|
|
|
void TextRange::TextRangesFromSelection(dom::Selection* aSelection,
|
|
|
|
nsTArray<TextRange>* aRanges) {
|
|
|
|
MOZ_ASSERT(aRanges->Length() == 0, "TextRange array supposed to be empty");
|
|
|
|
|
|
|
|
aRanges->SetCapacity(aSelection->RangeCount());
|
|
|
|
|
2021-12-09 10:35:09 +03:00
|
|
|
const uint32_t rangeCount = aSelection->RangeCount();
|
|
|
|
for (const uint32_t idx : IntegerRange(rangeCount)) {
|
|
|
|
MOZ_ASSERT(aSelection->RangeCount() == rangeCount);
|
2020-06-23 21:52:42 +03:00
|
|
|
const nsRange* DOMRange = aSelection->GetRangeAt(idx);
|
2021-12-09 10:35:09 +03:00
|
|
|
MOZ_ASSERT(DOMRange);
|
2020-06-23 21:52:42 +03:00
|
|
|
HyperTextAccessible* startContainer =
|
|
|
|
nsAccUtils::GetTextContainer(DOMRange->GetStartContainer());
|
|
|
|
HyperTextAccessible* endContainer =
|
|
|
|
nsAccUtils::GetTextContainer(DOMRange->GetEndContainer());
|
|
|
|
HyperTextAccessible* commonAncestor = nsAccUtils::GetTextContainer(
|
|
|
|
DOMRange->GetClosestCommonInclusiveAncestor());
|
|
|
|
if (!startContainer || !endContainer) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t startOffset = startContainer->DOMPointToOffset(
|
|
|
|
DOMRange->GetStartContainer(), DOMRange->StartOffset(), false);
|
|
|
|
int32_t endOffset = endContainer->DOMPointToOffset(
|
|
|
|
DOMRange->GetEndContainer(), DOMRange->EndOffset(), true);
|
|
|
|
|
|
|
|
TextRange tr(commonAncestor && commonAncestor->IsTextField()
|
|
|
|
? commonAncestor
|
|
|
|
: startContainer->Document(),
|
|
|
|
startContainer, startOffset, endContainer, endOffset);
|
|
|
|
*(aRanges->AppendElement()) = std::move(tr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-23 04:10:19 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// pivate
|
|
|
|
|
2022-02-27 02:01:54 +03:00
|
|
|
void TextRange::Set(Accessible* aRoot, Accessible* aStartContainer,
|
|
|
|
int32_t aStartOffset, Accessible* aEndContainer,
|
|
|
|
int32_t aEndOffset) {
|
2014-04-16 16:50:28 +04:00
|
|
|
mRoot = aRoot;
|
|
|
|
mStartContainer = aStartContainer;
|
|
|
|
mEndContainer = aEndContainer;
|
|
|
|
mStartOffset = aStartOffset;
|
|
|
|
mEndOffset = aEndOffset;
|
|
|
|
}
|
2014-05-23 04:10:19 +04:00
|
|
|
|
2022-02-27 02:01:54 +03:00
|
|
|
Accessible* TextRange::CommonParent(Accessible* aAcc1, Accessible* aAcc2,
|
|
|
|
nsTArray<Accessible*>* aParents1,
|
|
|
|
uint32_t* aPos1,
|
|
|
|
nsTArray<Accessible*>* aParents2,
|
|
|
|
uint32_t* aPos2) const {
|
2016-01-20 20:53:03 +03:00
|
|
|
if (aAcc1 == aAcc2) {
|
|
|
|
return aAcc1;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(aParents1->Length() == 0 || aParents2->Length() == 0,
|
|
|
|
"Wrong arguments");
|
|
|
|
|
|
|
|
// Build the chain of parents.
|
2022-02-27 02:01:54 +03:00
|
|
|
Accessible* p1 = aAcc1;
|
|
|
|
Accessible* p2 = aAcc2;
|
2016-01-20 20:53:03 +03:00
|
|
|
do {
|
|
|
|
aParents1->AppendElement(p1);
|
2022-02-27 02:01:54 +03:00
|
|
|
p1 = p1->Parent();
|
2016-01-20 20:53:03 +03:00
|
|
|
} while (p1);
|
|
|
|
do {
|
|
|
|
aParents2->AppendElement(p2);
|
2022-02-27 02:01:54 +03:00
|
|
|
p2 = p2->Parent();
|
2016-01-20 20:53:03 +03:00
|
|
|
} while (p2);
|
|
|
|
|
|
|
|
// Find where the parent chain differs
|
|
|
|
*aPos1 = aParents1->Length();
|
|
|
|
*aPos2 = aParents2->Length();
|
2022-02-27 02:01:54 +03:00
|
|
|
Accessible* parent = nullptr;
|
2016-01-20 20:53:03 +03:00
|
|
|
uint32_t len = 0;
|
|
|
|
for (len = std::min(*aPos1, *aPos2); len > 0; --len) {
|
2022-02-27 02:01:54 +03:00
|
|
|
Accessible* child1 = aParents1->ElementAt(--(*aPos1));
|
|
|
|
Accessible* child2 = aParents2->ElementAt(--(*aPos2));
|
2016-01-20 20:53:03 +03:00
|
|
|
if (child1 != child2) break;
|
|
|
|
|
|
|
|
parent = child1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent;
|
|
|
|
}
|
|
|
|
|
2014-07-28 18:40:27 +04:00
|
|
|
} // namespace a11y
|
|
|
|
} // namespace mozilla
|