Bug 1321623 - Implement DOM Selection.setBaseAndExtent(). r=smaug

This commit is contained in:
Mats Palmgren 2016-12-19 16:48:37 +01:00
parent 2f64fa637f
commit a028cc0e46
4 changed files with 61 additions and 0 deletions

View File

@ -294,6 +294,8 @@ nsRange::CreateRange(nsIDOMNode* aStartParent, int32_t aStartOffset,
RefPtr<nsRange> range = new nsRange(startParent);
// XXX this can be optimized by inlining SetStart/End and calling
// DoSetRange *once*.
nsresult rv = range->SetStart(startParent, aStartOffset);
NS_ENSURE_SUCCESS(rv, rv);

View File

@ -45,6 +45,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

@ -195,6 +195,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

@ -6431,6 +6431,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
*/