1 module elemi.attribute; 2 3 import std.string; 4 5 import elemi.internal; 6 7 8 pure @safe: 9 10 11 /// Represents an attribute. 12 struct Attribute { 13 14 pure: 15 16 /// Name of the attribute. 17 string name; 18 19 /// Value assigned to the attribute. 20 string value; 21 22 /// Assign a new value 23 Attribute opAssign(string newValue) { 24 25 value = newValue; 26 return this; 27 28 } 29 30 Attribute opAssign(string[] newValues) { 31 32 value = newValues.join(" "); 33 return this; 34 35 } 36 37 string toString() { 38 39 return format!q{%s="%s"}(name, value.escapeHTML); 40 41 } 42 43 alias toString this; 44 45 } 46 47 Attribute attr(string name) { 48 49 return Attribute(name); 50 51 } 52 53 Attribute attr(string name)() { 54 55 return Attribute(name); 56 57 } 58 59 Attribute attr(string name, string value) { 60 61 return Attribute(name, value); 62 63 } 64 65 Attribute attr(string name)(string value) { 66 67 return Attribute(name, value); 68 69 } 70 71 pure unittest { 72 73 import elemi.html; 74 75 assert(elem!"div"( 76 attr("id") = "name", 77 attr("class") = ["hello", "world"], 78 ) == `<div id="name" class="hello world"></div>`); 79 80 }