/// <summary> /// Parses tag code into attributes /// </summary> private static List <HTMLAttribute> ParseAttributes(string tagCode) { List <HTMLAttribute> attributesList = new List <HTMLAttribute>(); List <int> spaceIndexes = Utils.GetIndexes(tagCode, " "); // Get indexes of all spaces foreach (int spaceIndex in spaceIndexes) { int index = spaceIndex + 1; char nextSign = tagCode[index]; // Loop is used for parsing attributes next to each other // For example // message="example"user="******"readonly while (true) { // Check if there is attribute not separated with space if (nextSign != ' ' && nextSign != '>') { HTMLAttribute attribute = ParseAttribute(tagCode, index); // Check if during attribute parsing, occurred an error if (attribute == null) { return(new List <HTMLAttribute>()); } attributesList.Add(attribute); if (attribute.HasValue) { index = attribute.ValueEndIndex + 1; } else { break; } } else { break; } } } // Parsed attributes usually aren't correct, so incorrect attributes must be removed return(FixHTMLAttributes(attributesList)); }
/// <summary> /// Fixes attributes list /// </summary> private static List <HTMLAttribute> FixHTMLAttributes(List <HTMLAttribute> list) { List <HTMLAttribute> fixedList = new List <HTMLAttribute>(); // Add first attribute if (list.Count > 0) { fixedList.Add(list[0]); } HTMLAttribute lastAttributeWithValue = new HTMLAttribute(); for (int i = 0; i < list.Count; i++) { HTMLAttribute attribute = list[i]; if (i > 0 && lastAttributeWithValue.PropertyStartIndex != 1 && attribute.Property.Length > 0) { // If parsed attribute's start index is less than end index of value of latest attribute containing value is incorrect // For example // Actual start index = 18 // End index of value of latest attribute = 21 // 18 < 21 so it's incorrect if (attribute.PropertyStartIndex > lastAttributeWithValue.ValueEndIndex) { fixedList.Add(attribute); } } if (attribute.HasValue) { lastAttributeWithValue = attribute; } } return(fixedList); }
/// <summary> /// Parses html tag code into attribute /// </summary> private static HTMLAttribute ParseAttribute(string code, int startIndex) { HTMLAttribute attribute = new HTMLAttribute(); attribute.PropertyStartIndex = startIndex; // Check if attribute contains value and get property end index for (int i = startIndex; i < code.Length; i++) { char sign = code[i]; // Attribute has value if (sign == '=') { attribute.PropertyEndIndex = i; attribute.HasValue = true; break; } else if (sign == ' ' || sign == '>') // Attribute hasn't value { attribute.PropertyEndIndex = i; break; } } // Get property attribute.Property = code.Substring(attribute.PropertyStartIndex, attribute.PropertyEndIndex - attribute.PropertyStartIndex).ToLower(); // Gets value if (attribute.HasValue) { char sign = code[attribute.PropertyEndIndex + 1]; // If value doesn't starts with quotation mark if (sign != '"') { attribute.ValueStartIndex = attribute.PropertyEndIndex + 1; // Gets index closing value for (int i = attribute.PropertyEndIndex + 1; i < code.Length; i++) { char _sign = code[i]; if (_sign == '"' || _sign == '>' || _sign == ' ') { attribute.ValueEndIndex = i; break; } } } else // If value starts with quotation mark { attribute.ValueStartIndex = attribute.PropertyEndIndex + 2; // Get quotation mark closing value attribute.ValueEndIndex = ParseAttributesGetQuotationMarkIndex(code, attribute.ValueStartIndex); } // Get value try { attribute.Value = code.Substring(attribute.ValueStartIndex, attribute.ValueEndIndex - attribute.ValueStartIndex).ToLower(); } catch { Console.WriteLine("Attribute is incorrect (" + attribute.Property + ")"); return(null); } } return(attribute); }