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); }
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); }