From fcaa250c72cbbb691911456a1be0691663e41c63 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Mon, 3 Feb 2020 13:52:43 +0100 Subject: [PATCH] Ported fix and test from WinUI. https://github.com/microsoft/microsoft-ui-xaml/pull/1922 --- .../Utils/SelectionTreeHelper.cs | 18 +++++++---- .../SelectionModelTests.cs | 31 +++++++++++++++++++ 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/src/Avalonia.Controls/Utils/SelectionTreeHelper.cs b/src/Avalonia.Controls/Utils/SelectionTreeHelper.cs index 93102a7b5b..430ecabbb8 100644 --- a/src/Avalonia.Controls/Utils/SelectionTreeHelper.cs +++ b/src/Avalonia.Controls/Utils/SelectionTreeHelper.cs @@ -132,15 +132,21 @@ namespace Avalonia.Controls.Utils private static bool IsSubSet(IndexPath path, IndexPath subset) { - bool isSubset = true; - for (int i = 0; i < subset.GetSize(); i++) + var subsetSize = subset.GetSize(); + if (path.GetSize() < subsetSize) { - isSubset = path.GetAt(i) == subset.GetAt(i); - if (!isSubset) - break; + return false; + } + + for (int i = 0; i < subsetSize; i++) + { + if (path.GetAt(i) != subset.GetAt(i)) + { + return false; + } } - return isSubset; + return true; } private static IndexPath StartPath(IndexPath path, int length) diff --git a/tests/Avalonia.Controls.UnitTests/SelectionModelTests.cs b/tests/Avalonia.Controls.UnitTests/SelectionModelTests.cs index 6c3137c636..208d85d8fd 100644 --- a/tests/Avalonia.Controls.UnitTests/SelectionModelTests.cs +++ b/tests/Avalonia.Controls.UnitTests/SelectionModelTests.cs @@ -898,6 +898,37 @@ namespace Avalonia.Controls.UnitTests }); } + [Fact] + public void SelectRangeRegressionTest() + { + RunOnUIThread.Execute(() => + { + var selectionModel = new SelectionModel() + { + Source = CreateNestedData(1, 2, 3) + }; + + // length of start smaller than end used to cause an out of range error. + selectionModel.SelectRange(IndexPath.CreateFrom(0), IndexPath.CreateFrom(1, 1)); + + ValidateSelection(selectionModel, + new List() + { + Path(0, 0), + Path(0, 1), + Path(0, 2), + Path(0), + Path(1, 0), + Path(1, 1) + }, + new List() + { + Path(), + Path(1) + }, + 1 /* selectedInnerNodes */); + }); + } [Fact] public void Disposing_Unhooks_CollectionChanged_Handlers()