Issue #3059 - Implement Selection.setBaseAndExtent()

This commit is contained in:
Moonchild 2022-01-30 22:19:50 +00:00 committed by roytam1
commit 602bb0a877
3 changed files with 59 additions and 0 deletions

View file

@ -46,6 +46,12 @@ interface Selection {
[Throws]
boolean containsNode(Node node, boolean allowPartialContainment);
[Throws]
void setBaseAndExtent(Node anchorNode,
unsigned long anchorOffset,
Node focusNode,
unsigned long focusOffset);
stringifier;
};

View file

@ -207,6 +207,10 @@ public:
void Modify(const nsAString& aAlter, const nsAString& aDirection,
const nsAString& aGranularity, mozilla::ErrorResult& aRv);
void SetBaseAndExtent(nsINode& aAnchorNode, uint32_t aAnchorOffset,
nsINode& aFocusNode, uint32_t aFocusOffset,
mozilla::ErrorResult& aRv);
bool GetInterlinePosition(mozilla::ErrorResult& aRv);
void SetInterlinePosition(bool aValue, mozilla::ErrorResult& aRv);

View file

@ -6435,6 +6435,55 @@ Selection::Modify(const nsAString& aAlter, const nsAString& aDirection,
}
}
void
Selection::SetBaseAndExtent(nsINode& aAnchorNode, uint32_t aAnchorOffset,
nsINode& aFocusNode, uint32_t aFocusOffset,
ErrorResult& aRv)
{
if (!mFrameSelection) {
return;
}
SelectionBatcher batch(this);
int32_t relativePosition =
nsContentUtils::ComparePoints(&aAnchorNode, aAnchorOffset,
&aFocusNode, aFocusOffset);
nsINode* start = &aAnchorNode;
nsINode* end = &aFocusNode;
uint32_t startOffset = aAnchorOffset;
uint32_t endOffset = aFocusOffset;
if (relativePosition > 0) {
start = &aFocusNode;
end = &aAnchorNode;
startOffset = aFocusOffset;
endOffset = aAnchorOffset;
}
RefPtr<nsRange> newRange;
nsresult rv = nsRange::CreateRange(start, startOffset, end, endOffset,
getter_AddRefs(newRange));
// CreateRange returns IndexSizeError if any offset is out of bounds.
if (NS_FAILED(rv)) {
aRv.Throw(rv);
return;
}
rv = RemoveAllRanges();
if (NS_FAILED(rv)) {
aRv.Throw(rv);
return;
}
rv = AddRange(newRange);
if (NS_FAILED(rv)) {
aRv.Throw(rv);
return;
}
SetDirection(relativePosition > 0 ? eDirPrevious : eDirNext);
}
/** SelectionLanguageChange modifies the cursor Bidi level after a change in keyboard direction
* @param aLangRTL is true if the new language is right-to-left or false if the new language is left-to-right
*/