/// <summary> /// Splits the text into words and saves the result /// </summary> public static void ParseToWords(CssBox owner, List <CssBoxWord> words, string text) { words.Clear(); int startIdx = 0; while (startIdx < text.Length) { while (startIdx < text.Length && char.IsWhiteSpace(text[startIdx])) { startIdx++; } var endIdx = startIdx + 1; while (endIdx < text.Length && !char.IsWhiteSpace(text[endIdx])) { endIdx++; } if (startIdx < text.Length) { var hasSpaceBefore = startIdx > 0 && char.IsWhiteSpace(text[startIdx - 1]); var hasSpaceAfter = endIdx < text.Length && char.IsWhiteSpace(text[endIdx]); var word = HtmlUtils.DecodeHtml(text.Substring(startIdx, endIdx - startIdx)); words.Add(new CssBoxWord(owner, word, hasSpaceBefore, hasSpaceAfter)); } startIdx = endIdx + 1; } }
/// <summary> /// Extract html tag attributes from the given sub-string. /// </summary> /// <param name="source">the html source to parse</param> /// <param name="idx">the start index of the tag attributes in the source</param> /// <param name="length">the length of the tag attributes from the start index in the source</param> /// <param name="attributes">return the dictionary of tag attributes</param> private static void ExtractAttributes(string source, int idx, int length, out Dictionary <string, string> attributes) { attributes = null; int startIdx = idx; while (startIdx < idx + length) { while (startIdx < idx + length && char.IsWhiteSpace(source, startIdx)) { startIdx++; } var endIdx = startIdx + 1; while (endIdx < idx + length && !char.IsWhiteSpace(source, endIdx) && source[endIdx] != '=') { endIdx++; } if (startIdx < idx + length) { var key = source.Substring(startIdx, endIdx - startIdx); startIdx = endIdx + 1; while (startIdx < idx + length && (char.IsWhiteSpace(source, startIdx) || source[startIdx] == '=')) { startIdx++; } bool hasPChar = false; char pChar = source[startIdx]; if (pChar == '"' || pChar == '\'') { hasPChar = true; startIdx++; } endIdx = startIdx + (hasPChar ? 0 : 1); while (endIdx < idx + length && (hasPChar ? source[endIdx] != pChar : !char.IsWhiteSpace(source, endIdx))) { endIdx++; } var value = source.Substring(startIdx, endIdx - startIdx); value = HtmlUtils.DecodeHtml(value); if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(value)) { if (attributes == null) { attributes = new Dictionary <string, string>(StringComparer.InvariantCultureIgnoreCase); } attributes[key.ToLower()] = value; } startIdx = endIdx + (hasPChar ? 2 : 1); } } }