public static HtmlAttributeCollection Parse(String htmlTag) { HtmlAttributeCollection collection = new HtmlAttributeCollection(); if (String.IsNullOrEmpty(htmlTag)) { return(collection); } // We remove the name of the tag (due to our regex) and ensure there are at least one parameter int startIndex; for (startIndex = 0; startIndex < htmlTag.Length; startIndex++) { if (Char.IsWhiteSpace(htmlTag[startIndex])) { startIndex++; break; } else if (htmlTag[startIndex] == '>' || htmlTag[startIndex] == '/') { // no attribute in this tag return(collection); } } MatchCollection matches = stripAttributesRegex.Matches(htmlTag, startIndex); foreach (Match m in matches) { collection.attributes[m.Groups["tag"].Value] = m.Groups["val"].Value; } return(collection); }
/// <summary> /// Use as MoveNext() but this function will stop once the current value is equals to tag. /// </summary> /// <param name="tag">The tag to stop on (Optional).</param> /// <returns> /// If tag is null, it returns true if the enumerator was successfully advanced to the next element; false /// if the enumerator has passed the end of the collection.<br/> /// If tag is not null, it returns false as long as the tag was not found. /// </returns> public bool MoveUntilMatch(String tag) { current = currentTag = null; attributes = styleAttributes = null; bool success; // Ignore empty lines while ((success = en.MoveNext()) && (current = en.Current.Trim('\r', '\n')).Length == 0) { ; } if (success && tag != null) { return(!current.Equals(tag, StringComparison.CurrentCultureIgnoreCase)); } return(success); }
public static HtmlAttributeCollection ParseStyle(String htmlTag) { HtmlAttributeCollection collection = new HtmlAttributeCollection(); if (String.IsNullOrEmpty(htmlTag)) { return(collection); } // Encoded ':' and ';' characters are valid for browser but not handled by the regex (bug #13812 reported by robin391) // ex= <span style="text-decoration:underline;color:red"> MatchCollection matches = stripStyleAttributesRegex.Matches(HttpUtility.HtmlDecode(htmlTag)); foreach (Match m in matches) { collection.attributes[m.Groups["name"].Value] = m.Groups["val"].Value; } return(collection); }