summaryrefslogtreecommitdiff
path: root/layout
diff options
context:
space:
mode:
authorMoonchild <moonchild@palemoon.org>2022-01-30 22:19:50 +0000
committerMoonchild <moonchild@palemoon.org>2022-01-30 22:19:50 +0000
commitadf896ccbc5f2064de82d277638a377a7c5e76ec (patch)
treef37c01f6025faa67cf145c3c218ac4c3196eb1ef /layout
parent87660b9ed452b709265a22bfa0b610645c32e447 (diff)
downloadaura-central-adf896ccbc5f2064de82d277638a377a7c5e76ec.tar.gz
Issue %3059 - Implement Selection.setBaseAndExtent()
Diffstat (limited to 'layout')
-rw-r--r--layout/generic/Selection.h4
-rw-r--r--layout/generic/nsSelection.cpp49
2 files changed, 53 insertions, 0 deletions
diff --git a/layout/generic/Selection.h b/layout/generic/Selection.h
index 1e703b466..83a2e2c68 100644
--- a/layout/generic/Selection.h
+++ b/layout/generic/Selection.h
@@ -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);
diff --git a/layout/generic/nsSelection.cpp b/layout/generic/nsSelection.cpp
index 3f8d11b48..301d79952 100644
--- a/layout/generic/nsSelection.cpp
+++ b/layout/generic/nsSelection.cpp
@@ -6434,6 +6434,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
*/