soy_testhelper.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. // Copyright 2011 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Provides test helpers for Soy tests.
  16. * @author chrishenry@google.com (Chris Henry)
  17. */
  18. goog.provide('goog.soy.testHelper');
  19. goog.setTestOnly('goog.soy.testHelper');
  20. goog.require('goog.dom');
  21. goog.require('goog.dom.TagName');
  22. goog.require('goog.i18n.bidi.Dir');
  23. goog.require('goog.soy.data.SanitizedContent');
  24. goog.require('goog.soy.data.SanitizedContentKind');
  25. goog.require('goog.string');
  26. goog.require('goog.userAgent');
  27. /**
  28. * Instantiable subclass of SanitizedContent.
  29. *
  30. * This is a spoof for sanitized content that isn't robust enough to get
  31. * through Soy's escaping functions but is good enough for the checks here.
  32. *
  33. * @constructor
  34. * @param {string} content The text.
  35. * @param {goog.soy.data.SanitizedContentKind} kind The kind of safe content.
  36. * @extends {goog.soy.data.SanitizedContent}
  37. * @suppress {missingProvide}
  38. */
  39. function SanitizedContentSubclass(content, kind) {
  40. // IMPORTANT! No superclass chaining to avoid exception being thrown.
  41. this.content = content;
  42. this.contentKind = kind;
  43. }
  44. goog.inherits(SanitizedContentSubclass, goog.soy.data.SanitizedContent);
  45. /**
  46. * @param {string} content The text.
  47. * @param {goog.soy.data.SanitizedContentKind|string} kind The kind of safe
  48. * content.
  49. * @return {!SanitizedContentSubclass}
  50. */
  51. function makeSanitizedContent(content, kind) {
  52. return new SanitizedContentSubclass(
  53. content,
  54. /** @type {goog.soy.data.SanitizedContentKind} */ (kind));
  55. }
  56. //
  57. // Fake Soy-generated template functions.
  58. //
  59. var example = {};
  60. /**
  61. * @param {{name: string}} data
  62. * @param {null=} opt_sb
  63. * @param {?Object<string, *>=} opt_injectedData
  64. * @return {string}
  65. */
  66. example.textNodeTemplate = function(data, opt_sb, opt_injectedData) {
  67. assertNotNull(data);
  68. assertNotUndefined(data);
  69. return goog.string.htmlEscape(data.name);
  70. };
  71. /**
  72. * @param {{name: string}} data
  73. * @param {null=} opt_sb
  74. * @param {?Object<string, *>=} opt_injectedData
  75. * @return {string}
  76. */
  77. example.singleRootTemplate = function(data, opt_sb, opt_injectedData) {
  78. assertNotNull(data);
  79. assertNotUndefined(data);
  80. return '<span>' + goog.string.htmlEscape(data.name) + '</span>';
  81. };
  82. /**
  83. * @param {{name: string}} data
  84. * @param {null=} opt_sb
  85. * @param {?Object<string, *>=} opt_injectedData
  86. * @return {string}
  87. */
  88. example.multiRootTemplate = function(data, opt_sb, opt_injectedData) {
  89. assertNotNull(data);
  90. assertNotUndefined(data);
  91. return '<div>Hello</div><div>' + goog.string.htmlEscape(data.name) + '</div>';
  92. };
  93. /**
  94. * @param {{name: string}} data
  95. * @param {null=} opt_sb
  96. * @param {?Object<string, *>=} opt_injectedData
  97. * @return {string}
  98. */
  99. example.injectedDataTemplate = function(data, opt_sb, opt_injectedData) {
  100. assertNotNull(data);
  101. assertNotUndefined(data);
  102. return goog.string.htmlEscape(data.name) +
  103. goog.string.htmlEscape(opt_injectedData.name);
  104. };
  105. /**
  106. * @param {{name: string}} data
  107. * @param {null=} opt_sb
  108. * @param {Object<string, *>=} opt_injectedData
  109. * @return {string}
  110. */
  111. example.noDataTemplate = function(data, opt_sb, opt_injectedData) {
  112. assertNotNull(data);
  113. assertNotUndefined(data);
  114. return '<div>Hello</div>';
  115. };
  116. /**
  117. * @param {{name: string}} data
  118. * @param {null=} opt_sb
  119. * @param {Object<string, *>=} opt_injectedData
  120. * @return {!SanitizedContentSubclass}
  121. */
  122. example.sanitizedHtmlTemplate = function(data, opt_sb, opt_injectedData) {
  123. // Test the SanitizedContent constructor.
  124. var sanitized = makeSanitizedContent(
  125. 'Hello <b>World</b>', goog.soy.data.SanitizedContentKind.HTML);
  126. sanitized.contentDir = goog.i18n.bidi.Dir.LTR;
  127. return sanitized;
  128. };
  129. /**
  130. * @param {{name: string}} data
  131. * @param {null=} opt_sb
  132. * @param {Object<string, *>=} opt_injectedData
  133. * @return {!SanitizedContentSubclass}
  134. */
  135. example.sanitizedHtmlAttributesTemplate = function(
  136. data, opt_sb, opt_injectedData) {
  137. return makeSanitizedContent(
  138. 'foo="bar"', goog.soy.data.SanitizedContentKind.ATTRIBUTES);
  139. };
  140. /**
  141. * @param {{name: string}} data
  142. * @param {null=} opt_sb
  143. * @param {?Object<string, *>=} opt_injectedData
  144. * @return {!SanitizedContentSubclass}
  145. */
  146. example.sanitizedSmsUrlTemplate = function(data, opt_sb, opt_injectedData) {
  147. // Test the SanitizedContent constructor.
  148. var sanitized = makeSanitizedContent(
  149. 'sms:123456789', goog.soy.data.SanitizedContentKind.URI);
  150. return sanitized;
  151. };
  152. /**
  153. * @param {{name: string}} data
  154. * @param {null=} opt_sb
  155. * @param {?Object<string, *>=} opt_injectedData
  156. * @return {!SanitizedContentSubclass}
  157. */
  158. example.sanitizedHttpUrlTemplate = function(data, opt_sb, opt_injectedData) {
  159. // Test the SanitizedContent constructor.
  160. var sanitized = makeSanitizedContent(
  161. 'https://google.com/foo?n=917', goog.soy.data.SanitizedContentKind.URI);
  162. return sanitized;
  163. };
  164. /**
  165. * @param {{name: string}} data
  166. * @param {null=} opt_sb
  167. * @param {Object<string, *>=} opt_injectedData
  168. * @return {!SanitizedContentSubclass}
  169. */
  170. example.sanitizedCssTemplate = function(data, opt_sb, opt_injectedData) {
  171. return makeSanitizedContent(
  172. 'display:none', goog.soy.data.SanitizedContentKind.CSS);
  173. };
  174. /**
  175. * @param {{name: string}} data
  176. * @param {null=} opt_sb
  177. * @param {Object<string, *>=} opt_injectedData
  178. * @return {!SanitizedContentSubclass}
  179. */
  180. example.unsanitizedTextTemplate = function(data, opt_sb, opt_injectedData) {
  181. return makeSanitizedContent(
  182. 'I <3 Puppies & Kittens', goog.soy.data.SanitizedContentKind.TEXT);
  183. };
  184. /**
  185. * @param {{name: string}} data
  186. * @param {null=} opt_sb
  187. * @param {?Object<string, *>=} opt_injectedData
  188. * @return {!SanitizedContentSubclass}
  189. */
  190. example.sanitizedUriTemplate = function(data, opt_sb, opt_injectedData) {
  191. return makeSanitizedContent(
  192. 'https://example.com', goog.soy.data.SanitizedContentKind.URI);
  193. };
  194. /**
  195. * @param {{name: string}} data
  196. * @param {null=} opt_sb
  197. * @param {Object<string, *>=} opt_injectedData
  198. * @return {!SanitizedContentSubclass}
  199. */
  200. example.templateSpoofingSanitizedContentString = function(
  201. data, opt_sb, opt_injectedData) {
  202. return makeSanitizedContent(
  203. 'Hello World',
  204. // This is to ensure we're using triple-equals against a unique Javascript
  205. // object. For example, in Javascript, consider ({}) == '[Object object]'
  206. // is true.
  207. goog.soy.data.SanitizedContentKind.HTML.toString());
  208. };
  209. /**
  210. * @param {{name: string}} data
  211. * @param {null=} opt_sb
  212. * @param {Object<string, *>=} opt_injectedData
  213. * @return {string}
  214. */
  215. example.tableRowTemplate = function(data, opt_sb, opt_injectedData) {
  216. return '<tr><td></td></tr>';
  217. };
  218. /**
  219. * @param {{name: string}} data
  220. * @param {null=} opt_sb
  221. * @param {Object<string, *>=} opt_injectedData
  222. * @return {string}
  223. */
  224. example.colGroupTemplateCaps = function(data, opt_sb, opt_injectedData) {
  225. return '<COLGROUP></COLGROUP>';
  226. };
  227. //
  228. // Test helper functions.
  229. //
  230. /**
  231. * Retrieves the content of document fragment as HTML.
  232. * @param {Node} fragment The document fragment.
  233. * @return {string} Content of the document fragment as HTML.
  234. */
  235. function fragmentToHtml(fragment) {
  236. var testDiv = goog.dom.createElement(goog.dom.TagName.DIV);
  237. testDiv.appendChild(fragment);
  238. return elementToInnerHtml(testDiv);
  239. }
  240. /**
  241. * Retrieves the content of an element as HTML.
  242. * @param {Element} elem The element.
  243. * @return {string} Content of the element as HTML.
  244. */
  245. function elementToInnerHtml(elem) {
  246. var innerHtml = elem.innerHTML;
  247. if (goog.userAgent.IE) {
  248. innerHtml = innerHtml.replace(/DIV/g, 'div').replace(/\s/g, '');
  249. }
  250. return innerHtml;
  251. }