icontent_test.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // Copyright 2007 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. goog.provide('goog.editor.icontentTest');
  15. goog.setTestOnly('goog.editor.icontentTest');
  16. goog.require('goog.dom');
  17. goog.require('goog.dom.TagName');
  18. goog.require('goog.editor.BrowserFeature');
  19. goog.require('goog.editor.icontent');
  20. goog.require('goog.editor.icontent.FieldFormatInfo');
  21. goog.require('goog.editor.icontent.FieldStyleInfo');
  22. goog.require('goog.testing.PropertyReplacer');
  23. goog.require('goog.testing.jsunit');
  24. goog.require('goog.userAgent');
  25. var wrapperDiv;
  26. var realIframe;
  27. var realIframeDoc;
  28. var propertyReplacer;
  29. function setUp() {
  30. wrapperDiv = goog.dom.createDom(
  31. goog.dom.TagName.DIV, null,
  32. realIframe = goog.dom.createDom(goog.dom.TagName.IFRAME));
  33. goog.dom.appendChild(document.body, wrapperDiv);
  34. realIframeDoc = realIframe.contentWindow.document;
  35. propertyReplacer = new goog.testing.PropertyReplacer();
  36. }
  37. function tearDown() {
  38. goog.dom.removeNode(wrapperDiv);
  39. propertyReplacer.reset();
  40. }
  41. function testWriteHttpsInitialIframeContent() {
  42. // This is not a particularly useful test; it's just a sanity check to make
  43. // sure nothing explodes
  44. var info =
  45. new goog.editor.icontent.FieldFormatInfo('id', false, false, false);
  46. var doc = createMockDocument();
  47. goog.editor.icontent.writeHttpsInitialIframe(info, doc, 'some html');
  48. assertBodyCorrect(doc.body, 'id', 'some html');
  49. }
  50. function testWriteHttpsInitialIframeContentRtl() {
  51. var info = new goog.editor.icontent.FieldFormatInfo('id', false, false, true);
  52. var doc = createMockDocument();
  53. goog.editor.icontent.writeHttpsInitialIframe(info, doc, 'some html');
  54. assertBodyCorrect(doc.body, 'id', 'some html', true);
  55. }
  56. function testWriteInitialIframeContentBlendedStandardsGrowing() {
  57. if (goog.editor.BrowserFeature.HAS_CONTENT_EDITABLE) {
  58. return; // only executes when using an iframe
  59. }
  60. var info = new goog.editor.icontent.FieldFormatInfo('id', true, true, false);
  61. var styleInfo = new goog.editor.icontent.FieldStyleInfo(
  62. wrapperDiv, '.MyClass { position: absolute; top: 500px; }');
  63. var doc = realIframeDoc;
  64. var html = '<div class="MyClass">Some Html</div>';
  65. goog.editor.icontent.writeNormalInitialBlendedIframe(
  66. info, html, styleInfo, realIframe);
  67. assertBodyCorrect(doc.body, 'id', html);
  68. assertEquals('CSS1Compat', doc.compatMode); // standards
  69. assertEquals('auto', doc.documentElement.style.height); // growing
  70. assertEquals('100%', doc.body.style.height); // standards
  71. assertEquals('hidden', doc.body.style.overflowY); // growing
  72. assertEquals('', realIframe.style.position); // no padding on wrapper
  73. assertEquals(500, doc.body.firstChild.offsetTop);
  74. assert(
  75. goog.dom.getElementsByTagName(goog.dom.TagName.STYLE, doc)[0]
  76. .innerHTML.indexOf('-moz-force-broken-image-icon') !=
  77. -1); // standards
  78. }
  79. function testWriteInitialIframeContentBlendedQuirksFixedRtl() {
  80. if (goog.editor.BrowserFeature.HAS_CONTENT_EDITABLE) {
  81. return; // only executes when using an iframe
  82. }
  83. var info = new goog.editor.icontent.FieldFormatInfo('id', false, true, true);
  84. var styleInfo = new goog.editor.icontent.FieldStyleInfo(wrapperDiv, '');
  85. wrapperDiv.style.padding = '2px 5px';
  86. var doc = realIframeDoc;
  87. var html = 'Some Html';
  88. goog.editor.icontent.writeNormalInitialBlendedIframe(
  89. info, html, styleInfo, realIframe);
  90. assertBodyCorrect(doc.body, 'id', html, true);
  91. assertEquals('BackCompat', doc.compatMode); // quirks
  92. assertEquals('100%', doc.documentElement.style.height); // fixed height
  93. assertEquals('auto', doc.body.style.height); // quirks
  94. assertEquals('auto', doc.body.style.overflow); // fixed height
  95. assertEquals('-2px', realIframe.style.marginTop);
  96. assertEquals('-5px', realIframe.style.marginLeft);
  97. assert(
  98. goog.dom.getElementsByTagName(goog.dom.TagName.STYLE, doc)[0]
  99. .innerHTML.indexOf('-moz-force-broken-image-icon') == -1); // quirks
  100. }
  101. function testWhiteboxStandardsFixedRtl() {
  102. var info = new goog.editor.icontent.FieldFormatInfo('id', true, false, true);
  103. var styleInfo = null;
  104. var doc = realIframeDoc;
  105. var html = 'Some Html';
  106. goog.editor.icontent.writeNormalInitialBlendedIframe(
  107. info, html, styleInfo, realIframe);
  108. assertBodyCorrect(doc.body, 'id', html, true);
  109. // TODO(nicksantos): on Safari, there's a bug where all written iframes
  110. // are CSS1Compat. It's fixed in the nightlies as of 3/31/08, so remove
  111. // this guard when the latest version of Safari is on the farm.
  112. if (!goog.userAgent.WEBKIT) {
  113. assertEquals(
  114. 'BackCompat', doc.compatMode); // always use quirks in whitebox
  115. }
  116. }
  117. function testGetInitialIframeContent() {
  118. var info = new goog.editor.icontent.FieldFormatInfo('id', true, false, false);
  119. var styleInfo = null;
  120. var html = 'Some Html';
  121. propertyReplacer.set(
  122. goog.editor.BrowserFeature, 'HAS_CONTENT_EDITABLE', false);
  123. var htmlOut =
  124. goog.editor.icontent.getInitialIframeContent_(info, html, styleInfo);
  125. assertEquals(/contentEditable/i.test(htmlOut), false);
  126. propertyReplacer.set(
  127. goog.editor.BrowserFeature, 'HAS_CONTENT_EDITABLE', true);
  128. htmlOut =
  129. goog.editor.icontent.getInitialIframeContent_(info, html, styleInfo);
  130. assertEquals(/<body[^>]+?contentEditable/i.test(htmlOut), true);
  131. assertEquals(/<html[^>]+?style="[^>"]*min-width:\s*0/i.test(htmlOut), true);
  132. assertEquals(/<body[^>]+?style="[^>"]*min-width:\s*0/i.test(htmlOut), true);
  133. }
  134. function testIframeMinWidthOverride() {
  135. if (goog.editor.BrowserFeature.HAS_CONTENT_EDITABLE) {
  136. return; // only executes when using an iframe
  137. }
  138. var info = new goog.editor.icontent.FieldFormatInfo('id', true, true, false);
  139. var styleInfo = new goog.editor.icontent.FieldStyleInfo(
  140. wrapperDiv, '.MyClass { position: absolute; top: 500px; }');
  141. var doc = realIframeDoc;
  142. var html = '<div class="MyClass">Some Html</div>';
  143. goog.editor.icontent.writeNormalInitialBlendedIframe(
  144. info, html, styleInfo, realIframe);
  145. // Make sure that the minimum width isn't being inherited from the parent
  146. // document's style.
  147. assertTrue(doc.body.offsetWidth < 700);
  148. }
  149. function testBlendedStandardsGrowingMatchesComparisonDiv() {
  150. // TODO(nicksantos): If we ever move
  151. // TR_EditableUtil.prototype.makeIframeField_
  152. // into goog.editor.icontent (and I think we should), we could actually run
  153. // functional tests to ensure that the iframed field matches the dimensions
  154. // of the equivalent uneditable div. Functional tests would help a lot here.
  155. }
  156. /**
  157. * Check a given body for the most basic properties that all iframes must have.
  158. *
  159. * @param {Element} body The actual body element
  160. * @param {string} id The expected id
  161. * @param {string} bodyHTML The expected innerHTML
  162. * @param {boolean=} opt_rtl If true, expect RTL directionality
  163. */
  164. function assertBodyCorrect(body, id, bodyHTML, opt_rtl) {
  165. assertEquals(bodyHTML, body.innerHTML);
  166. // We can't just check
  167. // assert(HAS_CONTENTE_EDITABLE, !!body.contentEditable) since in
  168. // FF 3 we don't currently use contentEditable, but body.contentEditable
  169. // = 'inherit' and !!'inherit' = true.
  170. if (goog.editor.BrowserFeature.HAS_CONTENT_EDITABLE) {
  171. assertEquals('true', String(body.contentEditable));
  172. } else {
  173. assertNotEquals('true', String(body.contentEditable));
  174. }
  175. assertContains('editable', body.className.match(/\S+/g));
  176. assertEquals('true', String(body.getAttribute('g_editable')));
  177. assertEquals(
  178. 'true',
  179. // IE has bugs with getAttribute('hideFocus'), and
  180. // Webkit has bugs with normal .hideFocus access.
  181. String(
  182. goog.userAgent.IE ? body.hideFocus : body.getAttribute('hideFocus')));
  183. assertEquals(id, body.id);
  184. }
  185. /**
  186. * @return {Object} A mock document
  187. */
  188. function createMockDocument() {
  189. return {
  190. body: {
  191. setAttribute: function(key, val) { this[key] = val; },
  192. getAttribute: function(key) { return this[key]; },
  193. style: {direction: ''}
  194. }
  195. };
  196. }