Debian Codegraph @

closure-library/closure/goog/editor/icontent_test.js

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