/// <summary>Trim leaf elements, and sanitize.</summary>
/// <param name="leafElements">the leaf elements</param>
/// <returns>the trimmed and sanitized list</returns>
internal static IList <IElement> TrimLeafElementsAndSanitize(IList <IElement> leafElements)
{
List <IElement> waitingLeaves = new List <IElement>(leafElements);
TrimSubList(waitingLeaves, 0, waitingLeaves.Count, false);
TrimSubList(waitingLeaves, 0, waitingLeaves.Count, true);
int pos = 0;
while (pos < waitingLeaves.Count - 1)
{
if (waitingLeaves[pos] is Text)
{
Text first = (Text)waitingLeaves[pos];
if (IsElementFloating(first))
{
TrimTextElement(first, false);
TrimTextElement(first, true);
}
else
{
int firstEnd = GetIndexAfterLastNonSpace(first);
if (firstEnd < first.GetText().Length)
{
TrimSubList(waitingLeaves, pos + 1, waitingLeaves.Count, false);
first.SetText(first.GetText().JSubstring(0, firstEnd + 1));
}
}
}
pos++;
}
return(waitingLeaves);
}