private static Dictionary<char, string> BuildEscapeMap(ref string rawSelector)
{
var ret = new Dictionary<char, string>();
var cleaned = new List<char>();
bool inEscape = false;
int escapeStarts = -1;
for (var i = 0; i < rawSelector.Length; i++)
{
var c = rawSelector[i];
if (c == '\\')
{
inEscape = true;
escapeStarts = i;
continue;
}
if (!inEscape)
{
cleaned.Add(c);
continue;
}
// special character! (maybe!)
if (!char.IsLetterOrDigit(c) && i == escapeStarts + 1)
{
var replacement = NextSpecialChar(ret.Keys, rawSelector);
ret[replacement] = "\\" + c;
cleaned.Add(replacement);
inEscape = false;
continue;
}
if (c == ' ' || (i - escapeStarts == 7))
{
var sequence = rawSelector.Substring(escapeStarts, i - escapeStarts + 1);
var replacement = NextSpecialChar(ret.Keys, rawSelector);
ret[replacement] = sequence;
cleaned.Add(replacement);
inEscape = false;
continue;
}
}
rawSelector = new string(cleaned.ToArray());
return ret;
}