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