Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set _items in the ShapeViewModel to prevent exception #14899

Merged
merged 3 commits into from
Dec 14, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,51 +16,48 @@ public ShapeViewModel()

public ShapeViewModel(string shapeType)
{
if (string.IsNullOrEmpty(shapeType))
{
throw new ArgumentException($"The {nameof(shapeType)} cannot be empty");
}
ArgumentException.ThrowIfNullOrEmpty(shapeType, nameof(shapeType));

Metadata.Type = shapeType;
}

private ShapeMetadata _metadata;

public ShapeMetadata Metadata => _metadata ??= new ShapeMetadata();

public string Position
{
get
{
return Metadata.Position;
}

get => Metadata.Position;
set
{
Metadata.Position = value;
}
}

public string Id { get; set; }

public string TagName { get; set; }

private List<string> _classes;
public IList<string> Classes => _classes ??= new List<string>();

public IList<string> Classes => _classes ??= [];

private Dictionary<string, string> _attributes;
public IDictionary<string, string> Attributes => _attributes ??= new Dictionary<string, string>();

public IDictionary<string, string> Attributes => _attributes ??= [];

private Dictionary<string, object> _properties;
public IDictionary<string, object> Properties => _properties ??= new Dictionary<string, object>();

public IDictionary<string, object> Properties => _properties ??= [];

private bool _sorted = false;

private List<IPositioned> _items;
private List<IPositioned> _items = [];

public IReadOnlyList<IPositioned> Items
{
get
{
_items ??= new List<IPositioned>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep this and copy it when necessary


if (!_sorted)
{
_items = _items.OrderBy(x => x, FlatPositionComparer.Instance).ToList();
Expand All @@ -78,7 +75,7 @@ public ValueTask<IShape> AddAsync(object item, string position)
return new ValueTask<IShape>(this);
}

position ??= "";
position ??= string.Empty;
_sorted = false;

if (item is IHtmlContent)
Expand All @@ -91,8 +88,7 @@ public ValueTask<IShape> AddAsync(object item, string position)
}
else
{
var shape = item as IPositioned;
if (shape != null)
if (item is IPositioned shape)
{
if (position != null)
{
Expand Down