Debian Codegraph @

closure-library/closure/goog/cssom/iframe/style_test.js

raw
1 // Copyright 2010 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.cssom.iframe.styleTest');
16 goog.setTestOnly('goog.cssom.iframe.styleTest');
17
18 goog.require('goog.cssom');
19 goog.require('goog.cssom.iframe.style');
20 goog.require('goog.dom');
21 goog.require('goog.dom.DomHelper');
22 goog.require('goog.dom.TagName');
23 goog.require('goog.testing.jsunit');
24 goog.require('goog.userAgent');
25
26 // unit tests
27 var propertiesToTest = [
28 'color', 'font-family', 'font-style', 'font-size', 'font-variant',
29 'border-top-style', 'border-top-width', 'border-top-color',
30 'background-color', 'margin-bottom'
31 ];
32
33 function crawlDom(startNode, func) {
34 if (startNode.nodeType != 1) {
35 return;
36 }
37 func(startNode);
38 for (var i = 0; i < startNode.childNodes.length; i++) {
39 crawlDom(startNode.childNodes[i], func);
40 }
41 }
42
43 function getCurrentCssProperties(node, propList) {
44 var props = {};
45 if (node.nodeType != 1) {
46 return;
47 }
48 for (var i = 0; i < propList.length; i++) {
49 var prop = propList[i];
50 if (node.currentStyle) { // IE
51 var propCamelCase = '';
52 var propParts = prop.split('-');
53 for (var j = 0; j < propParts.length; j++) {
54 propCamelCase += propParts[j].charAt(0).toUpperCase() +
55 propParts[j].substring(1, propParts[j].length);
56 }
57 props[prop] = node.currentStyle[propCamelCase];
58 } else { // standards-compliant browsers
59 props[prop] = node.ownerDocument.defaultView.getComputedStyle(node, '')
60 .getPropertyValue(prop);
61 }
62 }
63 return props;
64 }
65
66 function CssPropertyCollector() {
67 var propsList = [];
68 this.propsList = propsList;
69
70 this.collectProps = function(node) {
71 var nodeProps = getCurrentCssProperties(node, propertiesToTest);
72 if (nodeProps) {
73 propsList.push([nodeProps, node]);
74 }
75 };
76 }
77
78 function recursivelyListCssProperties(el) {
79 var collector = new CssPropertyCollector();
80 crawlDom(el, collector.collectProps);
81 return collector.propsList;
82 }
83
84 function testMatchCssSelector() {
85 var container = goog.dom.createElement(goog.dom.TagName.DIV);
86 container.className = 'container';
87 var el = goog.dom.createElement(goog.dom.TagName.DIV);
88 x = el;
89 el.id = 'mydiv';
90 el.className = 'colorful foo';
91 // set some arbirtrary content
92 el.innerHTML = '<div><ul><li>One</li><li>Two</li></ul></div>';
93 container.appendChild(el);
94 document.body.appendChild(container);
95
96 var elementAncestry = new goog.cssom.iframe.style.NodeAncestry_(el);
97 assertEquals(5, elementAncestry.nodes.length);
98
99 // list of input/output results. Output is the index of the selector
100 // that we expect to match - for example, in 'body div div.colorful',
101 // 'div.colorful' has an index of 2.
102 var expectedResults = [
103 ['body div', [4, 1]], ['h1', null], ['body div h1', [4, 1]],
104 ['body div.colorful h1', [4, 1]], ['body div div', [4, 2]],
105 ['body div div div', [4, 2]], ['body div div.somethingelse div', [4, 1]],
106 ['body div.somethingelse div', [2, 0]], ['div.container', [3, 0]],
107 ['div.container div', [4, 1]], ['#mydiv', [4, 0]], ['div#mydiv', [4, 0]],
108 ['div.colorful', [4, 0]], ['div#mydiv .colorful', [4, 0]],
109 ['.colorful', [4, 0]], ['body * div', [4, 2]], ['body * *', [4, 2]]
110 ];
111 for (var i = 0; i < expectedResults.length; i++) {
112 var input = expectedResults[i][0];
113 var expectedResult = expectedResults[i][1];
114 var selector = new goog.cssom.iframe.style.CssSelector_(input);
115 var result = selector.matchElementAncestry(elementAncestry);
116 if (expectedResult == null) {
117 assertEquals('Expected null result', expectedResult, result);
118 } else {
119 assertEquals(
120 'Expected element index for ' + input, expectedResult[0],
121 result.elementIndex);
122 assertEquals(
123 'Expected selector part index for ' + input, expectedResult[1],
124 result.selectorPartIndex);
125 }
126 }
127 document.body.removeChild(container);
128 }
129
130 function makeIframeDocument(iframe) {
131 var doc = goog.dom.getFrameContentDocument(iframe);
132 doc.open();
133 doc.write('<html><head>');
134 doc.write('<style>html,body { background-color: transparent; }</style>');
135 doc.write('</head><body></body></html>');
136 doc.close();
137 return doc;
138 }
139
140 function testCopyCss() {
141 for (var i = 1; i <= 4; i++) {
142 var sourceElement = document.getElementById('source' + i);
143 var newFrame = goog.dom.createElement(goog.dom.TagName.IFRAME);
144 newFrame.allowTransparency = true;
145 sourceElement.parentNode.insertBefore(newFrame, sourceElement.nextSibling);
146 var doc = makeIframeDocument(newFrame);
147 goog.cssom.addCssText(
148 goog.cssom.iframe.style.getElementContext(sourceElement),
149 new goog.dom.DomHelper(doc));
150 doc.body.innerHTML = sourceElement.innerHTML;
151
152 var oldProps = recursivelyListCssProperties(sourceElement);
153 var newProps = recursivelyListCssProperties(doc.body);
154
155 assertEquals(oldProps.length, newProps.length);
156 for (var j = 0; j < oldProps.length; j++) {
157 for (var k = 0; k < propertiesToTest.length; k++) {
158 assertEquals(
159 'testing property ' + propertiesToTest[k],
160 oldProps[j][0][propertiesToTest[k]],
161 newProps[j][0][propertiesToTest[k]]);
162 }
163 }
164 }
165 }
166
167 function normalizeCssText(cssText) {
168 // Normalize cssText for testing purposes.
169 return cssText.replace(/\s/g, '').toLowerCase();
170 }
171
172 function testAImportantInFF2() {
173 var testDiv = document.getElementById('source1');
174 var cssText =
175 normalizeCssText(goog.cssom.iframe.style.getElementContext(testDiv));
176 var color = standardizeCSSValue('color', 'red');
177 var NORMAL_RULE = 'a{color:' + color;
178 var FF_2_RULE = 'a{color:' + color + '!important';
179 if (goog.userAgent.GECKO && !goog.userAgent.isVersionOrHigher('1.9a')) {
180 assertContains(FF_2_RULE, cssText);
181 } else {
182 assertContains(NORMAL_RULE, cssText);
183 assertNotContains(FF_2_RULE, cssText);
184 }
185 }
186
187 function testCopyBackgroundContext() {
188 var testDiv = document.getElementById('backgroundTest');
189 var cssText = goog.cssom.iframe.style.getElementContext(testDiv, null, true);
190 var iframe = goog.dom.createElement(goog.dom.TagName.IFRAME);
191 var ancestor = document.getElementById('backgroundTest-ancestor-1');
192 ancestor.parentNode.insertBefore(iframe, ancestor.nextSibling);
193 iframe.style.width = '100%';
194 iframe.style.height = '100px';
195 iframe.style.borderWidth = '0px';
196 var doc = makeIframeDocument(iframe);
197 goog.cssom.addCssText(cssText, new goog.dom.DomHelper(doc));
198 doc.body.innerHTML = testDiv.innerHTML;
199 var normalizedCssText = normalizeCssText(cssText);
200 assertTrue(
201 'Background color should be copied from parent element',
202 /body{[^{]*background-color:(?:rgb\(128,0,128\)|#800080)/.test(
203 normalizedCssText));
204 assertTrue(
205 'Background image should be copied from ancestor element',
206 /body{[^{]*background-image:url\(/.test(normalizedCssText));
207 // Background-position can't be calculated in FF2, due to this bug:
208 // http://bugzilla.mozilla.org/show_bug.cgi?id=316981
209 if (!(goog.userAgent.GECKO && !goog.userAgent.isVersionOrHigher('1.9'))) {
210 // Expected x position is:
211 // originalBackgroundPositionX - elementOffsetLeft
212 // 40px - (1px + 8px) == 31px
213 // Expected y position is:
214 // originalBackgroundPositionY - elementOffsetLeft
215 // 70px - (1px + 10px + 5px) == 54px;
216 assertTrue(
217 'Background image position should be adjusted correctly',
218 /body{[^{]*background-position:31px54px/.test(normalizedCssText));
219 }
220 }
221
222 function testCopyBackgroundContextFromIframe() {
223 var testDiv = document.getElementById('backgroundTest');
224 var iframe = goog.dom.createElement(goog.dom.TagName.IFRAME);
225 iframe.allowTransparency = true;
226 iframe.style.position = 'absolute';
227 iframe.style.top = '5px';
228 iframe.style.left = '5px';
229 iframe.style.borderWidth = '2px';
230 iframe.style.borderStyle = 'solid';
231 testDiv.appendChild(iframe);
232 var doc = makeIframeDocument(iframe);
233 doc.body.backgroundColor = 'transparent';
234 doc.body.style.margin = '0';
235 doc.body.style.padding = '0';
236 doc.body.innerHTML = '<p style="margin: 0">I am transparent!</p>';
237 var normalizedCssText = normalizeCssText(
238 goog.cssom.iframe.style.getElementContext(
239 doc.body.firstChild, null, true));
240 // Background properties should get copied through from the parent
241 // document since the iframe is transparent
242 assertTrue(
243 'Background color should be copied from parent element',
244 /body{[^{]*background-color:(?:rgb\(128,0,128\)|#800080)/.test(
245 normalizedCssText));
246 assertTrue(
247 'Background image should be copied from ancestor element',
248 /body{[^{]*background-image:url\(/.test(normalizedCssText));
249 // Background-position can't be calculated in FF2, due to this bug:
250 // http://bugzilla.mozilla.org/show_bug.cgi?id=316981
251 if (!(goog.userAgent.GECKO && !goog.userAgent.isVersionOrHigher('1.9'))) {
252 // Image offset should have been calculated to be the same as the
253 // above example, but adding iframe offset and borderWidth.
254 // Expected x position is:
255 // originalBackgroundPositionX - elementOffsetLeft
256 // 40px - (1px + 8px + 5px + 2px) == 24px
257 // Expected y position is:
258 // originalBackgroundPositionY - elementOffsetLeft
259 // 70px - (1px + 10px + 5px + 5px + 2px) == 47px;
260 assertTrue(
261 'Background image position should be adjusted correctly',
262 !!/body{[^{]*background-position:24px47px/.exec(normalizedCssText));
263 }
264 iframe.parentNode.removeChild(iframe);
265 }
266
267 function testCopyFontFaceRules() {
268 var isFontFaceCssomSupported = goog.userAgent.WEBKIT ||
269 goog.userAgent.OPERA ||
270 (goog.userAgent.GECKO && goog.userAgent.isVersionOrHigher('1.9.1'));
271 // We cannot use goog.testing.ExpectedFailures since it dynamically
272 // brings in CSS which causes the background context tests to fail
273 // in IE6.
274 if (isFontFaceCssomSupported) {
275 var cssText = goog.cssom.iframe.style.getElementContext(
276 document.getElementById('cavalier'));
277 assertTrue(
278 'The font face rule should have been copied correctly',
279 /@font-face/.test(cssText));
280 }
281 }