66570ea61d3e8df51d9f687d13e896d67aed2490.svn-base 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. if (!dojo._hasResource["dojox.dtl.tag.html"]) { // _hasResource checks added by
  2. // build. Do not use
  3. // _hasResource directly in your
  4. // code.
  5. dojo._hasResource["dojox.dtl.tag.html"] = true;
  6. dojo.provide("dojox.dtl.tag.html");
  7. dojo.require("dojox.dtl._base");
  8. dojox.dtl.tag.html.HtmlNode = function(name) {
  9. this.contents = new dojox.dtl.Filter(name);
  10. this._div = document.createElement("div");
  11. this._lasts = [];
  12. }
  13. dojo.extend(dojox.dtl.tag.html.HtmlNode, {
  14. render : function(context, buffer) {
  15. var text = this.contents.resolve(context);
  16. text = text.replace(/<(\/?script)/ig, '&lt;$1').replace(
  17. /\bon[a-z]+\s*=/ig, '');
  18. if (this._rendered && this._last != text) {
  19. buffer = this.unrender(context, buffer);
  20. }
  21. this._last = text;
  22. // This can get reset in the above tag
  23. if (!this._rendered) {
  24. this._rendered = true;
  25. var div = this._div;
  26. div.innerHTML = text;
  27. var children = div.childNodes;
  28. while (children.length) {
  29. var removed = div.removeChild(children[0]);
  30. this._lasts.push(removed);
  31. buffer = buffer.concat(removed);
  32. }
  33. }
  34. return buffer;
  35. },
  36. unrender : function(context, buffer) {
  37. if (this._rendered) {
  38. this._rendered = false;
  39. this._last = "";
  40. for (var i = 0, node; node = this._lasts[i++];) {
  41. buffer = buffer.remove(node);
  42. dojo._destroyElement(node);
  43. }
  44. this._lasts = [];
  45. }
  46. return buffer;
  47. },
  48. clone : function(buffer) {
  49. return new dojox.dtl.tag.html.HtmlNode(this.contents.contents);
  50. },
  51. toString : function() {
  52. return "dojox.dtl.tag.html.HtmlNode";
  53. }
  54. });
  55. dojox.dtl.tag.html.StyleNode = function(styles) {
  56. this.contents = {};
  57. this._styles = styles;
  58. for (var key in styles) {
  59. this.contents[key] = new dojox.dtl.Template(styles[key]);
  60. }
  61. }
  62. dojo.extend(dojox.dtl.tag.html.StyleNode, {
  63. render : function(context, buffer) {
  64. for (var key in this.contents) {
  65. dojo.style(buffer.getParent(), key, this.contents[key]
  66. .render(context));
  67. }
  68. return buffer;
  69. },
  70. unrender : function(context, buffer) {
  71. return buffer;
  72. },
  73. clone : function(buffer) {
  74. return new dojox.dtl.tag.html.HtmlNode(this._styles);
  75. },
  76. toString : function() {
  77. return "dojox.dtl.tag.html.StyleNode";
  78. }
  79. });
  80. dojox.dtl.tag.html.AttachNode = function(key) {
  81. this.contents = key;
  82. }
  83. dojo.extend(dojox.dtl.tag.html.AttachNode, {
  84. render : function(context, buffer) {
  85. if (!this._rendered) {
  86. this._rendered = true;
  87. context.getThis()[this.contents] = buffer.getParent();
  88. }
  89. return buffer;
  90. },
  91. unrender : function(context, buffer) {
  92. if (this._rendered) {
  93. this._rendered = false;
  94. if (context.getThis()[this.contents] === buffer
  95. .getParent()) {
  96. delete context.getThis()[this.contents];
  97. }
  98. }
  99. return buffer;
  100. },
  101. clone : function(buffer) {
  102. return new dojox.dtl.tag.html.HtmlNode(this._styles);
  103. },
  104. toString : function() {
  105. return "dojox.dtl.tag.html.AttachNode";
  106. }
  107. });
  108. dojox.dtl.tag.html.html = function(parser, text) {
  109. var parts = text.split(" ", 2);
  110. return new dojox.dtl.tag.html.HtmlNode(parts[1]);
  111. }
  112. dojox.dtl.tag.html.tstyle = function(parser, text) {
  113. var styles = {};
  114. text = text.replace(dojox.dtl.tag.html.tstyle._re, "");
  115. var rules = text.split(dojox.dtl.tag.html.tstyle._re1);
  116. for (var i = 0, rule; rule = rules[i]; i++) {
  117. var parts = rule.split(dojox.dtl.tag.html.tstyle._re2);
  118. var key = parts[0];
  119. var value = parts[1];
  120. if (value.indexOf("{{") == 0) {
  121. styles[key] = value;
  122. }
  123. }
  124. return new dojox.dtl.tag.html.StyleNode(styles);
  125. }
  126. dojo.mixin(dojox.dtl.tag.html.tstyle, {
  127. _re : /^tstyle\s+/,
  128. _re1 : /\s*;\s*/g,
  129. _re2 : /\s*:\s*/g
  130. });
  131. dojox.dtl.tag.html.attach = function(parser, text) {
  132. var parts = text.split(dojox.dtl.tag.html.attach._re);
  133. return new dojox.dtl.tag.html.AttachNode(parts[1]);
  134. }
  135. dojo.mixin(dojox.dtl.tag.html.attach, {
  136. _re : /\s+/g
  137. })
  138. }