/// <summary> /// Creates a new option describing how to present a certain tag depending on html attributes. /// </summary> /// <param name="openBBCodeTag">The open bbcode tag. No []</param> /// <param name="closeBBCodeTag">The close bbcode tag. No [], but something like /b</param> /// <param name="contentHtmlAttribute">The html attribute that gets the content.</param> /// <param name="optionHtmlAttribute">The html attribute that gets the option.</param> public HtmlOption(string openBBCodeTag, string closeBBCodeTag, HtmlAttribute contentHtmlAttribute, HtmlAttribute optionHtmlAttribute) : base(openBBCodeTag, closeBBCodeTag) { //we can have bbcode without closing tags, though... //there is no need in attribute validations. See the commented IsAttributesValid function and see why. ContentHtmlAttribute = contentHtmlAttribute; OptionHtmlAttribute = optionHtmlAttribute; }
/// <summary> /// Returns the value of the given attribute in the given tag representation /// </summary> /// <param name="tagRepresentation">The tag representation.</param> /// <param name="attribute">The attribute which value we need.</param> public static string GetAttributeValue(string tagRepresentation, HtmlAttribute attribute) { if (string.IsNullOrEmpty(tagRepresentation)) throw new ArgumentNullException("tagRepresentation"); if (attribute == null) throw new ArgumentNullException("attribute"); //there can be a problem if the attribute name contains some regex symbols. Match attr = Regex.Match(tagRepresentation, attribute.Name + "=\"[^\"]*\""); if (!attr.Success) return null; //don't have such attribute. if (!attribute.HasValue) return attr.Value.Substring(attr.Value.IndexOf('=') + 1).Trim('"');//if we want to get a whole value attribute - return only the value without the quotation marks... string attributeNameWithSeparator = attribute.Value.Name + AttributeOption.ValueSeparator; if (!attr.Value.Contains(attributeNameWithSeparator)) return null; string attrVal = attr.Value.Substring(attr.Value.IndexOf(attributeNameWithSeparator) + attributeNameWithSeparator.Length).Trim(); //get everything after the name if (!attrVal.Contains(attribute.Value.OptionDelimiter)) return attrVal; //if there is no delimiter, return everything else. return attrVal.Substring(0, attrVal.IndexOf(attribute.Value.OptionDelimiter)).Trim(); }