|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Text.RegularExpressions; |
| 6 | + |
| 7 | +namespace CodeTranslator.Attributes |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// Represents a base class for the different attribute types. |
| 11 | + /// </summary> |
| 12 | + public class HtmlAttribute |
| 13 | + { |
| 14 | + /// <summary> |
| 15 | + /// The attribute name, e. g. style |
| 16 | + /// </summary> |
| 17 | + public string Name { get; private set; } |
| 18 | + /// <summary> |
| 19 | + /// The value for the current attribute, e. g. |
| 20 | + /// for the font-size, width, etc. value of the style attribute. |
| 21 | + /// </summary> |
| 22 | + public AttributeOption Value { get; private set; } |
| 23 | + /// <summary> |
| 24 | + /// Tells if the current attribute has a value (part which we use.) |
| 25 | + /// </summary> |
| 26 | + public bool HasValue { get { return Value != null; } } |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// Creates a new html attribute without a specific option (takes the whole value). |
| 30 | + /// </summary> |
| 31 | + /// <param name="name">The name of the attribute</param> |
| 32 | + public HtmlAttribute(string name) |
| 33 | + { |
| 34 | + if (string.IsNullOrEmpty(name.Trim())) throw new ArgumentNullException("name"); |
| 35 | + Name = name.Trim(); |
| 36 | + } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Creates a new html attribute with a specific option (takes a part of the value). |
| 40 | + /// </summary> |
| 41 | + /// <param name="name">The name of the attribute.</param> |
| 42 | + /// <param name="value">The value we're interested in.</param> |
| 43 | + public HtmlAttribute(string name, AttributeOption value) |
| 44 | + : this(name) |
| 45 | + { |
| 46 | + if (value == null) throw new ArgumentNullException("value"); |
| 47 | + Value = value; |
| 48 | + } |
| 49 | + |
| 50 | + public override bool Equals(object obj) |
| 51 | + { |
| 52 | + HtmlAttribute attr = obj as HtmlAttribute; |
| 53 | + if (attr == null) return false; |
| 54 | + return this.GetHashCode() == attr.GetHashCode(); |
| 55 | + } |
| 56 | + |
| 57 | + public override int GetHashCode() |
| 58 | + { |
| 59 | + //here I use the Name only, because: |
| 60 | + //if we have two pairs of options: |
| 61 | + //A(x)-B and A-B, where A(x) means only the option 'x' from attribute A and just A means the whole attribute value, |
| 62 | + //we won't know which of them to use. We will have to add additional options, which I don't need right now. |
| 63 | + //I need to use the value here! |
| 64 | + return Name.GetHashCode() ^ (Value != null ? Value.GetHashCode() : -1); |
| 65 | + } |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Gets the tag representation and returns all attributes that are currently used. |
| 69 | + /// </summary> |
| 70 | + /// <param name="tagRepresentation">The tag representation - usually without <>, but will work with them, too...</param> |
| 71 | + public static HtmlAttribute[] GetAttributes(string tagRepresentation) |
| 72 | + { |
| 73 | + if (string.IsNullOrEmpty(tagRepresentation)) throw new ArgumentNullException("tagRepresentation"); |
| 74 | + List<HtmlAttribute> result = new List<HtmlAttribute>(); |
| 75 | + Match attribute = Regex.Match(tagRepresentation, "\\w+=\"[^\"]*\""); |
| 76 | + //make all possible combinations - the whole value and all possible attribute values. |
| 77 | + while (attribute.Success) |
| 78 | + { |
| 79 | + string attributeName = attribute.Value.Substring(0, attribute.Value.IndexOf('=')); |
| 80 | + result.Add(new HtmlAttribute(attributeName)); |
| 81 | + foreach (Match attributeValue in Regex.Matches(attribute.Value.Substring(attribute.Value.IndexOf('=')),"[\\w\\-]+\\s*" + AttributeOption.ValueSeparator)) //the attributes are usually words or words with '-' ending on ':' - if the value separator is different than ":" there may be a need to change something!!! |
| 82 | + { |
| 83 | + result.Add(new HtmlAttribute(attributeName, new AttributeOption(attributeValue.Value.Substring(0, attributeValue.Value.IndexOf(AttributeOption.ValueSeparator)).Trim(), ";")));//the option delimiter here is not important |
| 84 | + } |
| 85 | + attribute = attribute.NextMatch(); |
| 86 | + } |
| 87 | + return result.ToArray(); |
| 88 | + } |
| 89 | + |
| 90 | + /// <summary> |
| 91 | + /// Returns the value of the given attribute in the given tag representation |
| 92 | + /// </summary> |
| 93 | + /// <param name="tagRepresentation">The tag representation.</param> |
| 94 | + /// <param name="attribute">The attribute which value we need.</param> |
| 95 | + public static string GetAttributeValue(string tagRepresentation, HtmlAttribute attribute) |
| 96 | + { |
| 97 | + if (string.IsNullOrEmpty(tagRepresentation)) throw new ArgumentNullException("tagRepresentation"); |
| 98 | + if (attribute == null) throw new ArgumentNullException("attribute"); |
| 99 | + //there can be a problem if the attribute name contains some regex symbols. |
| 100 | + Match attr = Regex.Match(tagRepresentation, attribute.Name + "=\"[^\"]*\""); |
| 101 | + if (!attr.Success) return null; //don't have such attribute. |
| 102 | + 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... |
| 103 | + string attributeNameWithSeparator = attribute.Value.Name + AttributeOption.ValueSeparator; |
| 104 | + if (!attr.Value.Contains(attributeNameWithSeparator)) return null; |
| 105 | + string attrVal = attr.Value.Substring(attr.Value.IndexOf(attributeNameWithSeparator) + attributeNameWithSeparator.Length).Trim(); //get everything after the name |
| 106 | + if (!attrVal.Contains(attribute.Value.OptionDelimiter)) return attrVal; //if there is no delimiter, return everything else. |
| 107 | + return attrVal.Substring(0, attrVal.IndexOf(attribute.Value.OptionDelimiter)).Trim(); |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments