1 module dhtags.attrs; 2 3 public import dhtags.attrs.bundle; 4 public import dhtags.attrs.attribute : Attr; 5 public import dhtags.attrs.define : DefineAttr, DefineBooleanAttr; 6 7 unittest { 8 import dhtags.attrs.attribute : HtmlAttribute; 9 10 /** Attribute should be rendered as strings between quotes */ 11 assert( 12 HtmlAttribute.create(id="main-content").toString 13 == `id="main-content"` 14 ); 15 } 16 17 unittest { 18 import dhtags.attrs.attribute : HtmlAttribute; 19 20 /** Defining custom attributes with allowed types should be possible */ 21 mixin DefineAttr!("danger-level", int); 22 assert( 23 HtmlAttribute.create(dangerLevel=5).toString == 24 `danger-level="5"` 25 ); 26 27 /** Define custom boolean attributes should be possible */ 28 mixin DefineBooleanAttr!("finagles"); 29 assert( 30 HtmlAttribute.create(finagles).toString == 31 `finagles="finagles"` 32 ); 33 34 /** Defining custom attributes with an 'attr' function should be possible */ 35 assert( 36 HtmlAttribute.create("my-attr".attr="Hello").toString 37 == `my-attr="Hello"` 38 ); 39 } 40 41 unittest { 42 import dhtags.attrs.attribute : HtmlAttribute; 43 44 /** Boolean attribute's value should match the attribute's name */ 45 assert( 46 HtmlAttribute.create(async).toString 47 == `async="async"` 48 ); 49 50 /** True/false attributes should allow boolean values */ 51 assert( 52 HtmlAttribute.create(contenteditable=true).toString 53 == `contenteditable="true"` 54 ); 55 56 /** Integer values should be rendered as strings */ 57 assert( 58 HtmlAttribute.create(height=50).toString 59 == `height="50"` 60 ); 61 62 /** Double values should be rendered as strings without trailing zeros */ 63 assert( 64 HtmlAttribute.create(height=50.5).toString 65 == `height="50.5"` 66 ); 67 68 /** Arrays should be space separated */ 69 assert( 70 HtmlAttribute.create(className=["class1", "class2", "class3"]).toString 71 == `class="class1 class2 class3"` 72 ); 73 } 74 75 unittest { 76 import dhtags.attrs.attribute : HtmlAttribute; 77 78 /** Lower camelcase data attributes should be converted to lowercase hyphen-separated string */ 79 assert( 80 HtmlAttribute.create(data.ABSomeTESTWordCD=10).toString 81 == `data-ab-some-test-word-cd="10"` 82 ); 83 84 /** Uppercase acronyms in data attributes should be split as expected */ 85 assert( 86 HtmlAttribute.create(data.customHTMLAttr=false).toString 87 == `data-custom-html-attr="false"` 88 ); 89 90 /** Alternative data syntax should be allowed */ 91 assert( 92 HtmlAttribute.create(data("some-value")=10).toString 93 == `data-some-value="10"` 94 ); 95 }