_Templated.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <html>
  2. <head>
  3. <title>_Templated tests</title>
  4. <script type="text/javascript" src="../../dojo/dojo.js"
  5. djConfig="parseOnLoad: true, isDebug: true"></script>
  6. <script type="text/javascript">
  7. dojo.require("doh.runner");
  8. dojo.require("dijit._Widget");
  9. dojo.require("dijit._Templated");
  10. function getOuterHTML(/*DomNode*/ node){
  11. var wrapper = dojo.doc.createElement("div");
  12. wrapper.appendChild(node);
  13. return wrapper.innerHTML.toLowerCase(); // IE prints <BUTTON> rather than <button>; normalize it.
  14. }
  15. dojo.addOnLoad(function(){
  16. // Template with no variables (should be cached as a DOM tree)
  17. dojo.declare("SimpleTemplate", [dijit._Widget, dijit._Templated], {
  18. attributeMap: {},
  19. id: "test1",
  20. templateString: "<button><span>hello &gt; world</span></button>"
  21. });
  22. // Template with variables
  23. dojo.declare("VariableTemplate", [dijit._Widget, dijit._Templated], {
  24. attributeMap: {},
  25. id: "test2",
  26. num: 5,
  27. text: "hello ><\"' world",
  28. templateString: "<button><span num=\"${num}\">${text}</span></button>"
  29. });
  30. // Template that starts with special node (has to be constructed inside a <tbody>)
  31. dojo.declare("TableRowTemplate", [dijit._Widget, dijit._Templated], {
  32. attributeMap: {},
  33. id: "test3",
  34. text: "bar",
  35. templateString: "<tr><td>${text}</td></tr>"
  36. });
  37. // Illegal subsitition variable name
  38. dojo.declare("IllegalSubstitution", [dijit._Widget, dijit._Templated], {
  39. templateString: "<tr><td>${fake}</td></tr>"
  40. });
  41. // dojoAttachPoint
  42. dojo.declare("AttachPoint", [dijit._Widget, dijit._Templated], {
  43. attributeMap: {foo: "", style: "", bar: "buttonNode"},
  44. templateString: "<div style='border: 1px solid red'>" +
  45. "<button dojoAttachPoint='buttonNode,focusNode'>hi</button>" +
  46. '<span><input dojoAttachPoint="inputNode" value="input"></span>' +
  47. "<span dojoAttachPoint='containerNode'></span>" +
  48. "</div>"
  49. });
  50. // dojoAttachEvent
  51. dojo.declare("AttachEvent", [dijit._Widget, dijit._Templated], {
  52. click: function(){ this.clickCalled=true; },
  53. onfocus: function(){ this.focusCalled=true; },
  54. focus2: function(){ this.focus2Called=true; },
  55. templateString: "<table style='border: 1px solid blue'><tr>" +
  56. "<td><button dojoAttachPoint='left' dojoAttachEvent='onclick: click, onfocus'>left</button></td>" +
  57. "<td><button dojoAttachPoint='right' dojoAttachEvent='onclick: click, onfocus: focus2'>right</button></td>" +
  58. "</tr></table>"
  59. });
  60. // TODO:
  61. // TemplatePath
  62. var testW;
  63. doh.register("dijit.tests._Templated.html",
  64. [
  65. function simple(t){
  66. var widget=new SimpleTemplate();
  67. var wrapper=dojo.byId("simpleWrapper");
  68. wrapper.appendChild(widget.domNode);
  69. t.is('<button widgetid=\"test1\"><span>hello &gt; world</span></button>', wrapper.innerHTML.toLowerCase());
  70. },
  71. function variables(t){
  72. var widget=new VariableTemplate();
  73. var wrapper=dojo.byId("variables1Wrapper");
  74. wrapper.appendChild(widget.domNode);
  75. t.is('<button widgetid=\"test2\"><span num="5">hello &gt;&lt;"\' world</span></button>', wrapper.innerHTML.toLowerCase());
  76. },
  77. function variables2(t){
  78. var widget = new VariableTemplate({id: "myid", num: -5, text: ""});
  79. var wrapper=dojo.byId("variables2Wrapper");
  80. wrapper.appendChild(widget.domNode);
  81. t.is('<button widgetid=\"myid\"><span num="-5"></span></button>', wrapper.innerHTML.toLowerCase());
  82. },
  83. function table(t){
  84. var widget=new TableRowTemplate({text: "hello"});
  85. var wrapper = dojo.byId("trWrapper");
  86. wrapper.appendChild(widget.domNode);
  87. var actual = wrapper.innerHTML.toLowerCase().replace(/\r/g, "").replace(/\n/g, "");
  88. t.is('<tr widgetid="test3"><td>hello</td></tr>', actual);
  89. },
  90. function illegal(t){
  91. var hadException=false;
  92. try{
  93. var widget=new IllegalSubstitution();
  94. }catch(e){
  95. console.log(e);
  96. hadException=true;
  97. }
  98. t.t(hadException);
  99. },
  100. function attachPoint(t){
  101. var widget=new AttachPoint();
  102. var wrapper = dojo.byId("attachPointWrapper");
  103. wrapper.appendChild(widget.domNode);
  104. t.is(widget.containerNode.tagName.toLowerCase(), "span");
  105. t.is(widget.buttonNode.tagName.toLowerCase(), "button");
  106. t.is(widget.focusNode.tagName.toLowerCase(), "button");
  107. t.is(widget.inputNode.tagName.toLowerCase(), "input");
  108. },
  109. function attributeMap(t){
  110. var widget=new AttachPoint({foo:"value1", bar:"value2", style:"color: blue"});
  111. var wrapper = dojo.byId("attributeMapWrapper");
  112. wrapper.appendChild(widget.domNode);
  113. t.is("value1", widget.domNode.getAttribute("foo"));
  114. t.is("value2", widget.buttonNode.getAttribute("bar"));
  115. // TODO: this is() check is unreliable, IE returns a string like
  116. // border-right: red 1px solid; border-top: red 1px solid; border-left: red 1px solid; color: blue; border-bottom: red 1px solid
  117. // t.is("border: 1px solid red; color: blue;", widget.domNode.style.cssText.toLowerCase());
  118. },
  119. function attachEvent(t){
  120. var deferred = new doh.Deferred();
  121. var widget=new AttachEvent();
  122. var wrapper = dojo.byId("attachEventWrapper");
  123. wrapper.appendChild(widget.domNode);
  124. widget.left.focus();
  125. widget.right.focus();
  126. setTimeout(function(){
  127. t.t(widget.focusCalled);
  128. t.t(widget.focus2Called);
  129. deferred.callback(true);
  130. }, 0);
  131. return deferred;
  132. }
  133. ]
  134. );
  135. doh.run();
  136. });
  137. </script>
  138. <style type="text/css">
  139. @import "../themes/tundra/tundra.css";
  140. </style>
  141. </head>
  142. <body>
  143. <h1>_Templated test</h1>
  144. <div id="simpleWrapper"></div>
  145. <div id="variables1Wrapper"></div>
  146. <div id="variables2Wrapper"></div>
  147. <table><tbody id="trWrapper"></tbody></table>
  148. <div id="attachPointWrapper"></div>
  149. <div id="attributeMapWrapper"></div>
  150. <div id="attachEventWrapper"></div>
  151. </body>
  152. </html>