1 module deepmagic.dom.xml.comment; 2 3 import deepmagic.dom; 4 5 class Comment : Item 6 { 7 private string content; 8 9 /** 10 * Construct a comment 11 * 12 * Params: 13 * content = the body of the comment 14 * 15 * Throws: CommentException if the comment body is illegal (contains "--" 16 * or exactly equals "-") 17 * 18 * Examples: 19 * -------------- 20 * auto item = new Comment("This is a comment"); 21 * // constructs <!--This is a comment--> 22 * -------------- 23 */ 24 this(string content) 25 { 26 if (content == "-" || content.indexOf("==") != -1) 27 throw new CommentException(content); 28 this.content = content; 29 } 30 31 /** 32 * Compares two comments for equality 33 * 34 * Examples: 35 * -------------- 36 * Comment item1,item2; 37 * if (item1 == item2) { } 38 * -------------- 39 */ 40 override bool opEquals(Object o) 41 { 42 const item = toType!(const Item)(o); 43 const t = cast(Comment)item; 44 return t !is null && content == t.content; 45 } 46 47 /** 48 * Compares two comments 49 * 50 * You should rarely need to call this function. It exists so that Comments 51 * can be used as associative array keys. 52 * 53 * Examples: 54 * -------------- 55 * Comment item1,item2; 56 * if (item1 < item2) { } 57 * -------------- 58 */ 59 override int opCmp(Object o) 60 { 61 const item = toType!(const Item)(o); 62 const t = cast(Comment)item; 63 return t !is null && (content != t.content 64 ? (content < t.content ? -1 : 1 ) : 0 ); 65 } 66 67 /** 68 * Returns the hash of a Comment 69 * 70 * You should rarely need to call this function. It exists so that Comments 71 * can be used as associative array keys. 72 */ 73 override size_t toHash() const { return hash(content); } 74 75 /** 76 * Returns a string representation of this comment 77 */ 78 override string toString() const { return "<!--" ~ content ~ "-->"; } 79 80 override @property bool isEmptyXML() const { return false; } /// Returns false always 81 }