// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
using System;
using System.Collections.Generic;
namespace CPF.Razor
{
///
/// Helper class for types that accept inline text spans. This type collects text spans
/// and returns the string represented by the contained text spans.
///
public class TextSpanContainer
{
private readonly List _textSpans = new List();
public TextSpanContainer(bool trimWhitespace = true)
{
TrimWhitespace = trimWhitespace;
}
public bool TrimWhitespace { get; }
///
/// Updates the text spans with the new text at the new index and returns the new
/// string represented by the contained text spans.
///
///
///
///
public string GetUpdatedText(int index, string text)
{
if (index < 0)
{
throw new ArgumentOutOfRangeException(nameof(index));
}
if (index >= _textSpans.Count)
{
// Expand the list to allow for the new text's index to exist
_textSpans.AddRange(new string[index - _textSpans.Count + 1]);
}
_textSpans[index] = text;
var allText = string.Join(string.Empty, _textSpans);
return TrimWhitespace
? allText?.Trim()
: allText;
}
}
}