Debian Codegraph @

closure-library/closure/goog/style/style_test.js

raw
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 Licensegg 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 /**
16 * @fileoverview Shared unit tests for styles.
17 */
18
19 goog.provide('goog.style_test');
20
21 goog.require('goog.array');
22 goog.require('goog.color');
23 goog.require('goog.dom');
24 goog.require('goog.dom.TagName');
25 goog.require('goog.events.BrowserEvent');
26 goog.require('goog.html.testing');
27 goog.require('goog.labs.userAgent.util');
28 goog.require('goog.math.Box');
29 goog.require('goog.math.Coordinate');
30 goog.require('goog.math.Rect');
31 goog.require('goog.math.Size');
32 goog.require('goog.object');
33 goog.require('goog.style');
34 goog.require('goog.testing.ExpectedFailures');
35 goog.require('goog.testing.MockUserAgent');
36 goog.require('goog.testing.TestCase');
37 goog.require('goog.testing.asserts');
38 goog.require('goog.testing.jsunit');
39 goog.require('goog.userAgent');
40 goog.require('goog.userAgentTestUtil');
41 goog.require('goog.userAgentTestUtil.UserAgents');
42
43 goog.setTestOnly('goog.style_test');
44
45 // Delay running the tests after page load. This test has some asynchronous
46 // behavior that interacts with page load detection.
47 goog.testing.jsunit.AUTO_RUN_DELAY_IN_MS = 500;
48
49 // IE before version 6 will always be border box in compat mode.
50 var isBorderBox = goog.dom.isCss1CompatMode() ?
51 (goog.userAgent.IE && !goog.userAgent.isVersionOrHigher('6')) :
52 true;
53 var EPSILON = 2;
54 var expectedFailures;
55 var $ = goog.dom.getElement;
56 var mockUserAgent;
57
58 function setUpPage() {
59 expectedFailures = new goog.testing.ExpectedFailures();
60 // TODO(b/25875505): Fix unreported assertions (go/failonunreportedasserts).
61 goog.testing.TestCase.getActiveTestCase().failOnUnreportedAsserts = false;
62
63 var viewportSize = goog.dom.getViewportSize();
64 // When the window is too short or not wide enough, some tests, especially
65 // those for off-screen elements, fail. Oddly, the most reliable
66 // indicator is a width of zero (which is of course erroneous), since
67 // height sometimes includes a scroll bar. We can make no assumptions on
68 // window size on the Selenium farm.
69 if (goog.userAgent.IE && viewportSize.width < 300) {
70 // Move to origin, since IE won't resize outside the screen.
71 window.moveTo(0, 0);
72 window.resizeTo(640, 480);
73 }
74 }
75
76 function setUp() {
77 window.scrollTo(0, 0);
78 goog.userAgentTestUtil.reinitializeUserAgent();
79 mockUserAgent = new goog.testing.MockUserAgent();
80 mockUserAgent.install();
81 }
82
83 function tearDown() {
84 expectedFailures.handleTearDown();
85 var testVisibleDiv2 = goog.dom.getElement('test-visible2');
86 testVisibleDiv2.setAttribute('style', '');
87 goog.dom.removeChildren(testVisibleDiv2);
88 var testViewport = goog.dom.getElement('test-viewport');
89 testViewport.setAttribute('style', '');
90 goog.dom.removeChildren(testViewport);
91 goog.dispose(mockUserAgent);
92
93 // Prevent multiple vendor prefixed mock elements from poisoning the cache.
94 goog.style.styleNameCache_ = {};
95 }
96
97 function testSetStyle() {
98 var el = $('span1');
99 goog.style.setStyle(el, 'textDecoration', 'underline');
100 assertEquals('Should be underline', 'underline', el.style.textDecoration);
101 }
102
103 function testSetStyleMap() {
104 var el = $('span6');
105
106 var styles =
107 {'background-color': 'blue', 'font-size': '100px', textAlign: 'center'};
108
109 goog.style.setStyle(el, styles);
110
111 var answers = {
112 backgroundColor: 'blue',
113 fontSize: '100px',
114 textAlign: 'center'
115 };
116
117 goog.object.forEach(answers, function(value, style) {
118 assertEquals('Should be ' + value, value, el.style[style]);
119 });
120 }
121
122 function testSetStyleWithNonCamelizedString() {
123 var el = $('span5');
124 goog.style.setStyle(el, 'text-decoration', 'underline');
125 assertEquals('Should be underline', 'underline', el.style.textDecoration);
126 }
127
128 function testGetStyle() {
129 var el = goog.dom.getElement('styleTest3');
130 goog.style.setStyle(el, 'width', '80px');
131 goog.style.setStyle(el, 'textDecoration', 'underline');
132
133 assertEquals('80px', goog.style.getStyle(el, 'width'));
134 assertEquals('underline', goog.style.getStyle(el, 'textDecoration'));
135 assertEquals('underline', goog.style.getStyle(el, 'text-decoration'));
136 // Non set properties are always empty strings.
137 assertEquals('', goog.style.getStyle(el, 'border'));
138 }
139
140 function testGetStyleMsFilter() {
141 // Element with -ms-filter style set.
142 var e = goog.dom.getElement('msFilter');
143
144 if (goog.userAgent.IE && goog.userAgent.isDocumentModeOrHigher(8) &&
145 !goog.userAgent.isDocumentModeOrHigher(10)) {
146 // Only IE8/9 supports -ms-filter and returns it as value for the "filter"
147 // property. When in compatibility mode, -ms-filter is not supported
148 // and IE8 behaves as IE7 so the other case will apply.
149 assertEquals('alpha(opacity=0)', goog.style.getStyle(e, 'filter'));
150 } else {
151 // Any other browser does not support ms-filter so it returns empty string.
152 assertEquals('', goog.style.getStyle(e, 'filter'));
153 }
154 }
155
156 function testGetStyleFilter() {
157 // Element with filter style set.
158 var e = goog.dom.getElement('filter');
159
160 if (goog.userAgent.IE && !goog.userAgent.isDocumentModeOrHigher(10)) {
161 // Filter supported.
162 assertEquals('alpha(opacity=0)', goog.style.getStyle(e, 'filter'));
163 } else {
164 assertEquals('', goog.style.getStyle(e, 'filter'));
165 }
166 }
167
168 function testGetComputedStyleMsFilter() {
169 // Element with -ms-filter style set.
170 var e = goog.dom.getElement('msFilter');
171
172 if (goog.userAgent.IE && !goog.userAgent.isDocumentModeOrHigher(10)) {
173 if (goog.userAgent.isDocumentModeOrHigher(9)) {
174 // IE 9 returns the value.
175 assertEquals(
176 'alpha(opacity=0)', goog.style.getComputedStyle(e, 'filter'));
177 } else {
178 // Older IE always returns empty string for computed styles.
179 assertEquals('', goog.style.getComputedStyle(e, 'filter'));
180 }
181 } else {
182 // Non IE returns 'none' for filter as it is an SVG property
183 assertEquals('none', goog.style.getComputedStyle(e, 'filter'));
184 }
185 }
186
187 function testGetComputedStyleFilter() {
188 // Element with filter style set.
189 var e = goog.dom.getElement('filter');
190
191 if (goog.userAgent.IE && !goog.userAgent.isDocumentModeOrHigher(10)) {
192 if (goog.userAgent.isDocumentModeOrHigher(9)) {
193 // IE 9 returns the value.
194 assertEquals(
195 'alpha(opacity=0)', goog.style.getComputedStyle(e, 'filter'));
196 } else {
197 // Older IE always returns empty string for computed styles.
198 assertEquals('', goog.style.getComputedStyle(e, 'filter'));
199 }
200 } else {
201 // Non IE returns 'none' for filter as it is an SVG property
202 assertEquals('none', goog.style.getComputedStyle(e, 'filter'));
203 }
204 }
205
206 function testGetComputedBoxSizing() {
207 if (!goog.userAgent.IE || goog.userAgent.isVersionOrHigher(8)) {
208 var defaultBoxSizing =
209 goog.dom.isCss1CompatMode() ? 'content-box' : 'border-box';
210 var el = goog.dom.getElement('box-sizing-unset');
211 assertEquals(defaultBoxSizing, goog.style.getComputedBoxSizing(el));
212
213 el = goog.dom.getElement('box-sizing-border-box');
214 assertEquals('border-box', goog.style.getComputedBoxSizing(el));
215 } else {
216 // IE7 and below don't support box-sizing.
217 assertNull(
218 goog.style.getComputedBoxSizing(
219 goog.dom.getElement('box-sizing-border-box')));
220 }
221 }
222
223 function testGetComputedPosition() {
224 assertEquals(
225 'position not set', 'static',
226 goog.style.getComputedPosition($('position-unset')));
227 assertEquals(
228 'position:relative in style attribute', 'relative',
229 goog.style.getComputedPosition($('style-position-relative')));
230 if (goog.userAgent.IE && !goog.dom.isCss1CompatMode() &&
231 !goog.userAgent.isVersionOrHigher(10)) {
232 assertEquals(
233 'position:fixed in style attribute', 'static',
234 goog.style.getComputedPosition($('style-position-fixed')));
235 } else {
236 assertEquals(
237 'position:fixed in style attribute', 'fixed',
238 goog.style.getComputedPosition($('style-position-fixed')));
239 }
240 assertEquals(
241 'position:absolute in css', 'absolute',
242 goog.style.getComputedPosition($('css-position-absolute')));
243 }
244
245 function testGetComputedOverflowXAndY() {
246 assertEquals(
247 'overflow-x:scroll in style attribute', 'scroll',
248 goog.style.getComputedOverflowX($('style-overflow-scroll')));
249 assertEquals(
250 'overflow-y:scroll in style attribute', 'scroll',
251 goog.style.getComputedOverflowY($('style-overflow-scroll')));
252 assertEquals(
253 'overflow-x:hidden in css', 'hidden',
254 goog.style.getComputedOverflowX($('css-overflow-hidden')));
255 assertEquals(
256 'overflow-y:hidden in css', 'hidden',
257 goog.style.getComputedOverflowY($('css-overflow-hidden')));
258 }
259
260 function testGetComputedZIndex() {
261 assertEquals(
262 'z-index:200 in style attribute', '200',
263 '' + goog.style.getComputedZIndex($('style-z-index-200')));
264 assertEquals(
265 'z-index:200 in css', '200',
266 '' + goog.style.getComputedZIndex($('css-z-index-200')));
267 }
268
269 function testGetComputedTextAlign() {
270 assertEquals(
271 'text-align:right in style attribute', 'right',
272 goog.style.getComputedTextAlign($('style-text-align-right')));
273 assertEquals(
274 'text-align:right inherited from parent', 'right',
275 goog.style.getComputedTextAlign($('style-text-align-right-inner')));
276 assertEquals(
277 'text-align:center in css', 'center',
278 goog.style.getComputedTextAlign($('css-text-align-center')));
279 }
280
281 function testGetComputedCursor() {
282 assertEquals(
283 'cursor:move in style attribute', 'move',
284 goog.style.getComputedCursor($('style-cursor-move')));
285 assertEquals(
286 'cursor:move inherited from parent', 'move',
287 goog.style.getComputedCursor($('style-cursor-move-inner')));
288 assertEquals(
289 'cursor:poiner in css', 'pointer',
290 goog.style.getComputedCursor($('css-cursor-pointer')));
291 }
292
293 function testGetBackgroundColor() {
294 var dest = $('bgcolorDest');
295
296 for (var i = 0; $('bgcolorTest' + i); i++) {
297 var src = $('bgcolorTest' + i);
298 var bgColor = goog.style.getBackgroundColor(src);
299
300 dest.style.backgroundColor = bgColor;
301 assertEquals(
302 'Background colors should be equal', goog.style.getBackgroundColor(src),
303 goog.style.getBackgroundColor(dest));
304
305 try {
306 // goog.color.parse throws a generic exception if handed input it
307 // doesn't understand.
308 var c = goog.color.parse(bgColor);
309 assertEquals('rgb(255,0,0)', goog.color.hexToRgbStyle(c.hex));
310 } catch (e) {
311 // Internet Explorer is unable to parse colors correctly after test 4.
312 // Other browsers may vary, but all should be able to handle straight
313 // hex input.
314 assertFalse('Should be able to parse color "' + bgColor + '"', i < 5);
315 }
316 }
317 }
318
319 function testSetPosition() {
320 var el = $('testEl');
321
322 goog.style.setPosition(el, 100, 100);
323 assertEquals('100px', el.style.left);
324 assertEquals('100px', el.style.top);
325
326 goog.style.setPosition(el, '50px', '25px');
327 assertEquals('50px', el.style.left);
328 assertEquals('25px', el.style.top);
329
330 goog.style.setPosition(el, '10ex', '25px');
331 assertEquals('10ex', el.style.left);
332 assertEquals('25px', el.style.top);
333
334 goog.style.setPosition(el, '10%', '25%');
335 assertEquals('10%', el.style.left);
336 assertEquals('25%', el.style.top);
337
338 // ignores stupid units
339 goog.style.setPosition(el, 0, 0);
340 // TODO(user): IE errors if you set these values. Should we make setStyle
341 // catch these? Or leave it up to the app. Fixing the tests for now.
342 // goog.style.setPosition(el, '10rainbows', '25rainbows');
343 assertEquals('0px', el.style.left);
344 assertEquals('0px', el.style.top);
345
346
347 goog.style.setPosition(el, new goog.math.Coordinate(20, 40));
348 assertEquals('20px', el.style.left);
349 assertEquals('40px', el.style.top);
350 }
351
352 function testGetClientPositionAbsPositionElement() {
353 var div = goog.dom.createDom(goog.dom.TagName.DIV);
354 div.style.position = 'absolute';
355 div.style.left = '100px';
356 div.style.top = '200px';
357 document.body.appendChild(div);
358 var pos = goog.style.getClientPosition(div);
359 assertEquals(100, pos.x);
360 assertEquals(200, pos.y);
361 }
362
363 function testGetClientPositionNestedElements() {
364 var innerDiv = goog.dom.createDom(goog.dom.TagName.DIV);
365 innerDiv.style.position = 'relative';
366 innerDiv.style.left = '-10px';
367 innerDiv.style.top = '-10px';
368 var div = goog.dom.createDom(goog.dom.TagName.DIV);
369 div.style.position = 'absolute';
370 div.style.left = '150px';
371 div.style.top = '250px';
372 div.appendChild(innerDiv);
373 document.body.appendChild(div);
374 var pos = goog.style.getClientPosition(innerDiv);
375 assertEquals(140, pos.x);
376 assertEquals(240, pos.y);
377 }
378
379 function testGetClientPositionOfOffscreenElement() {
380 var div = goog.dom.createDom(goog.dom.TagName.DIV);
381 div.style.position = 'absolute';
382 div.style.left = '2000px';
383 div.style.top = '2000px';
384 div.style.width = '10px';
385 div.style.height = '10px';
386 document.body.appendChild(div);
387
388 try {
389 window.scroll(0, 0);
390 var pos = goog.style.getClientPosition(div);
391 assertEquals(2000, pos.x);
392 assertEquals(2000, pos.y);
393
394 // The following tests do not work in Gecko 1.8 and below, due to an
395 // obscure off-by-one bug in goog.style.getPageOffset. Same for IE.
396 if (!goog.userAgent.IE &&
397 !(goog.userAgent.GECKO && !goog.userAgent.isVersionOrHigher('1.9'))) {
398 window.scroll(1, 1);
399 var pos = goog.style.getClientPosition(div);
400 assertEquals(1999, pos.x);
401 assertRoughlyEquals(1999, pos.y, 0.5);
402
403 window.scroll(2, 2);
404 pos = goog.style.getClientPosition(div);
405 assertEquals(1998, pos.x);
406 assertRoughlyEquals(1998, pos.y, 0.5);
407
408 window.scroll(100, 100);
409 pos = goog.style.getClientPosition(div);
410 assertEquals(1900, pos.x);
411 assertRoughlyEquals(1900, pos.y, 0.5);
412 }
413 } finally {
414 window.scroll(0, 0);
415 document.body.removeChild(div);
416 }
417 }
418
419 function testGetClientPositionOfOrphanElement() {
420 var orphanElem = goog.dom.createElement(goog.dom.TagName.DIV);
421 var pos = goog.style.getClientPosition(orphanElem);
422 assertEquals(0, pos.x);
423 assertEquals(0, pos.y);
424 }
425
426 function testGetClientPositionEvent() {
427 var mockEvent = {};
428 mockEvent.clientX = 100;
429 mockEvent.clientY = 200;
430 var pos = goog.style.getClientPosition(mockEvent);
431 assertEquals(100, pos.x);
432 assertEquals(200, pos.y);
433 }
434
435 function testGetClientPositionTouchEvent() {
436 var mockTouchEvent = {};
437 mockTouchEvent.changedTouches = [{}];
438 mockTouchEvent.changedTouches[0].clientX = 100;
439 mockTouchEvent.changedTouches[0].clientY = 200;
440
441 var pos = goog.style.getClientPosition(mockTouchEvent);
442 assertEquals(100, pos.x);
443 assertEquals(200, pos.y);
444 }
445
446 function testGetClientPositionEmptyTouchList() {
447 var mockTouchEvent = {};
448
449 mockTouchEvent.touches = [];
450
451 mockTouchEvent.changedTouches = [{}];
452 mockTouchEvent.changedTouches[0].clientX = 100;
453 mockTouchEvent.changedTouches[0].clientY = 200;
454
455 var pos = goog.style.getClientPosition(mockTouchEvent);
456 assertEquals(100, pos.x);
457 assertEquals(200, pos.y);
458 }
459
460 function testGetClientPositionAbstractedTouchEvent() {
461 var mockTouchEvent = {};
462 mockTouchEvent.changedTouches = [{}];
463 mockTouchEvent.changedTouches[0].clientX = 100;
464 mockTouchEvent.changedTouches[0].clientY = 200;
465
466 var e = new goog.events.BrowserEvent(mockTouchEvent);
467
468 var pos = goog.style.getClientPosition(e);
469 assertEquals(100, pos.x);
470 assertEquals(200, pos.y);
471 }
472
473 function testGetPageOffsetAbsPositionedElement() {
474 var div = goog.dom.createDom(goog.dom.TagName.DIV);
475 div.style.position = 'absolute';
476 div.style.left = '100px';
477 div.style.top = '200px';
478 document.body.appendChild(div);
479 var pos = goog.style.getPageOffset(div);
480 assertEquals(100, pos.x);
481 assertEquals(200, pos.y);
482 }
483
484 function testGetPageOffsetNestedElements() {
485 var innerDiv = goog.dom.createDom(goog.dom.TagName.DIV);
486 innerDiv.style.position = 'relative';
487 innerDiv.style.left = '-10px';
488 innerDiv.style.top = '-10px';
489 var div = goog.dom.createDom(goog.dom.TagName.DIV);
490 div.style.position = 'absolute';
491 div.style.left = '150px';
492 div.style.top = '250px';
493 div.appendChild(innerDiv);
494 document.body.appendChild(div);
495 var pos = goog.style.getPageOffset(innerDiv);
496 assertRoughlyEquals(140, pos.x, 0.1);
497 assertRoughlyEquals(240, pos.y, 0.1);
498 }
499
500 function testGetPageOffsetWithBodyPadding() {
501 document.body.style.margin = '40px';
502 document.body.style.padding = '60px';
503 document.body.style.borderWidth = '70px';
504 try {
505 var div = goog.dom.createDom(goog.dom.TagName.DIV);
506 div.style.position = 'absolute';
507 div.style.left = '100px';
508 div.style.top = '200px';
509 // Margin will affect position, but padding and borders should not.
510 div.style.margin = '1px';
511 div.style.padding = '2px';
512 div.style.borderWidth = '3px';
513 document.body.appendChild(div);
514 var pos = goog.style.getPageOffset(div);
515 assertRoughlyEquals(101, pos.x, 0.1);
516 assertRoughlyEquals(201, pos.y, 0.1);
517 } finally {
518 document.body.removeChild(div);
519 document.body.style.margin = '';
520 document.body.style.padding = '';
521 document.body.style.borderWidth = '';
522 }
523 }
524
525 function testGetPageOffsetWithDocumentElementPadding() {
526 document.documentElement.style.margin = '40px';
527 document.documentElement.style.padding = '60px';
528 document.documentElement.style.borderWidth = '70px';
529 try {
530 var div = goog.dom.createDom(goog.dom.TagName.DIV);
531 div.style.position = 'absolute';
532 div.style.left = '100px';
533 div.style.top = '200px';
534 // Margin will affect position, but padding and borders should not.
535 div.style.margin = '1px';
536 div.style.padding = '2px';
537 div.style.borderWidth = '3px';
538 document.body.appendChild(div);
539 var pos = goog.style.getPageOffset(div);
540 // FF3 (but not beyond) gets confused by document margins.
541 if (goog.userAgent.GECKO && goog.userAgent.isVersionOrHigher('1.9') &&
542 !goog.userAgent.isVersionOrHigher('1.9.1')) {
543 assertEquals(141, pos.x);
544 assertEquals(241, pos.y);
545 } else {
546 assertRoughlyEquals(101, pos.x, 0.1);
547 assertRoughlyEquals(201, pos.y, 0.1);
548 }
549 } finally {
550 document.body.removeChild(div);
551 document.documentElement.style.margin = '';
552 document.documentElement.style.padding = '';
553 document.documentElement.style.borderWidth = '';
554 }
555 }
556
557 function testGetPageOffsetElementOffscreen() {
558 var div = goog.dom.createDom(goog.dom.TagName.DIV);
559 div.style.position = 'absolute';
560 div.style.left = '10000px';
561 div.style.top = '20000px';
562 document.body.appendChild(div);
563 window.scroll(0, 0);
564 try {
565 var pos = goog.style.getPageOffset(div);
566 assertEquals(10000, pos.x);
567 assertEquals(20000, pos.y);
568
569 // The following tests do not work in Gecko 1.8 and below, due to an
570 // obscure off-by-one bug in goog.style.getPageOffset. Same for IE.
571 if (!(goog.userAgent.IE) &&
572 !(goog.userAgent.GECKO && !goog.userAgent.isVersionOrHigher('1.9'))) {
573 window.scroll(1, 1);
574 pos = goog.style.getPageOffset(div);
575 assertEquals(10000, pos.x);
576 assertRoughlyEquals(20000, pos.y, 0.5);
577
578 window.scroll(1000, 2000);
579 pos = goog.style.getPageOffset(div);
580 assertEquals(10000, pos.x);
581 assertRoughlyEquals(20000, pos.y, 1);
582
583 window.scroll(10000, 20000);
584 pos = goog.style.getPageOffset(div);
585 assertEquals(10000, pos.x);
586 assertRoughlyEquals(20000, pos.y, 1);
587 }
588 }
589 // Undo changes.
590 finally {
591 document.body.removeChild(div);
592 window.scroll(0, 0);
593 }
594 }
595
596 function testGetPageOffsetFixedPositionElements() {
597 // Skip these tests in certain browsers.
598 // position:fixed is not supported in IE before version 7
599 if (!goog.userAgent.IE || !goog.userAgent.isVersionOrHigher('6')) {
600 // Test with a position fixed element
601 var div = goog.dom.createDom(goog.dom.TagName.DIV);
602 div.style.position = 'fixed';
603 div.style.top = '10px';
604 div.style.left = '10px';
605 document.body.appendChild(div);
606 var pos = goog.style.getPageOffset(div);
607 assertEquals(10, pos.x);
608 assertEquals(10, pos.y);
609
610 // Test with a position fixed element as parent
611 var innerDiv = goog.dom.createDom(goog.dom.TagName.DIV);
612 div = goog.dom.createDom(goog.dom.TagName.DIV);
613 div.style.position = 'fixed';
614 div.style.top = '10px';
615 div.style.left = '10px';
616 div.style.padding = '5px';
617 div.appendChild(innerDiv);
618 document.body.appendChild(div);
619 pos = goog.style.getPageOffset(innerDiv);
620 assertEquals(15, pos.x);
621 assertEquals(15, pos.y);
622 }
623 }
624
625 function testGetPositionTolerantToNoDocumentElementBorder() {
626 // In IE, removing the border on the document element undoes the normal
627 // 2-pixel offset. Ensure that we're correctly compensating for both cases.
628 try {
629 document.documentElement.style.borderWidth = '0';
630 var div = goog.dom.createDom(goog.dom.TagName.DIV);
631 div.style.position = 'absolute';
632 div.style.left = '100px';
633 div.style.top = '200px';
634 document.body.appendChild(div);
635
636 // Test all major positioning methods.
637 // Disabled for IE9 and below - IE8 returns dimensions multiplied by 100.
638 // IE9 is flaky. See b/22873770.
639 expectedFailures.expectFailureFor(
640 goog.userAgent.IE && !goog.userAgent.isVersionOrHigher(10));
641 try {
642 // Test all major positioning methods.
643 var pos = goog.style.getClientPosition(div);
644 assertEquals(100, pos.x);
645 assertRoughlyEquals(200, pos.y, .1);
646 var offset = goog.style.getPageOffset(div);
647 assertEquals(100, offset.x);
648 assertRoughlyEquals(200, offset.y, .1);
649 } catch (e) {
650 expectedFailures.handleException(e);
651 }
652 } finally {
653 document.documentElement.style.borderWidth = '';
654 }
655 }
656
657 function testSetSize() {
658 var el = $('testEl');
659
660 goog.style.setSize(el, 100, 100);
661 assertEquals('100px', el.style.width);
662 assertEquals('100px', el.style.height);
663
664 goog.style.setSize(el, '50px', '25px');
665 assertEquals('should be "50px"', '50px', el.style.width);
666 assertEquals('should be "25px"', '25px', el.style.height);
667
668 goog.style.setSize(el, '10ex', '25px');
669 assertEquals('10ex', el.style.width);
670 assertEquals('25px', el.style.height);
671
672 goog.style.setSize(el, '10%', '25%');
673 assertEquals('10%', el.style.width);
674 assertEquals('25%', el.style.height);
675
676 // ignores stupid units
677 goog.style.setSize(el, 0, 0);
678 // TODO(user): IE errors if you set these values. Should we make setStyle
679 // catch these? Or leave it up to the app. Fixing the tests for now.
680 // goog.style.setSize(el, '10rainbows', '25rainbows');
681 assertEquals('0px', el.style.width);
682 assertEquals('0px', el.style.height);
683
684 goog.style.setSize(el, new goog.math.Size(20, 40));
685 assertEquals('20px', el.style.width);
686 assertEquals('40px', el.style.height);
687 }
688
689 function testSetWidthAndHeight() {
690 var el = $('testEl');
691
692 // Replicate all of the setSize tests above.
693
694 goog.style.setWidth(el, 100);
695 goog.style.setHeight(el, 100);
696 assertEquals('100px', el.style.width);
697 assertEquals('100px', el.style.height);
698
699 goog.style.setWidth(el, '50px');
700 goog.style.setHeight(el, '25px');
701 assertEquals('should be "50px"', '50px', el.style.width);
702 assertEquals('should be "25px"', '25px', el.style.height);
703
704 goog.style.setWidth(el, '10ex');
705 goog.style.setHeight(el, '25px');
706 assertEquals('10ex', el.style.width);
707 assertEquals('25px', el.style.height);
708
709 goog.style.setWidth(el, '10%');
710 goog.style.setHeight(el, '25%');
711 assertEquals('10%', el.style.width);
712 assertEquals('25%', el.style.height);
713
714 goog.style.setWidth(el, 0);
715 goog.style.setHeight(el, 0);
716 assertEquals('0px', el.style.width);
717 assertEquals('0px', el.style.height);
718
719 goog.style.setWidth(el, 20);
720 goog.style.setHeight(el, 40);
721 assertEquals('20px', el.style.width);
722 assertEquals('40px', el.style.height);
723
724 // Additional tests testing each separately.
725 goog.style.setWidth(el, '');
726 goog.style.setHeight(el, '');
727 assertEquals('', el.style.width);
728 assertEquals('', el.style.height);
729
730 goog.style.setHeight(el, 20);
731 assertEquals('', el.style.width);
732 assertEquals('20px', el.style.height);
733
734 goog.style.setWidth(el, 40);
735 assertEquals('40px', el.style.width);
736 assertEquals('20px', el.style.height);
737 }
738
739 function testGetSize() {
740 var el = $('testEl');
741 goog.style.setSize(el, 100, 100);
742
743 var dims = goog.style.getSize(el);
744 assertEquals(100, dims.width);
745 assertEquals(100, dims.height);
746
747 goog.style.setStyle(el, 'display', 'none');
748 dims = goog.style.getSize(el);
749 assertEquals(100, dims.width);
750 assertEquals(100, dims.height);
751
752 el = $('testEl5');
753 goog.style.setSize(el, 100, 100);
754 dims = goog.style.getSize(el);
755 assertEquals(100, dims.width);
756 assertEquals(100, dims.height);
757
758 el = $('span0');
759 dims = goog.style.getSize(el);
760 assertNotEquals(0, dims.width);
761 assertNotEquals(0, dims.height);
762
763 el = $('table1');
764 dims = goog.style.getSize(el);
765 assertNotEquals(0, dims.width);
766 assertNotEquals(0, dims.height);
767
768 el = $('td1');
769 dims = goog.style.getSize(el);
770 assertNotEquals(0, dims.width);
771 assertNotEquals(0, dims.height);
772
773 el = $('li1');
774 dims = goog.style.getSize(el);
775 assertNotEquals(0, dims.width);
776 assertNotEquals(0, dims.height);
777
778 el = goog.dom.getElementsByTagNameAndClass(goog.dom.TagName.HTML)[0];
779 dims = goog.style.getSize(el);
780 assertNotEquals(0, dims.width);
781 assertNotEquals(0, dims.height);
782
783 el = goog.dom.getElementsByTagNameAndClass(goog.dom.TagName.BODY)[0];
784 dims = goog.style.getSize(el);
785 assertNotEquals(0, dims.width);
786 assertNotEquals(0, dims.height);
787 }
788
789 function testGetSizeSvgElements() {
790 var svgEl = document.createElementNS &&
791 document.createElementNS('http://www.w3.org/2000/svg', 'svg');
792 if (!svgEl || svgEl.getAttribute('transform') == '' ||
793 (goog.userAgent.WEBKIT && !goog.userAgent.isVersionOrHigher(534.8))) {
794 // SVG not supported, or getBoundingClientRect not supported on SVG
795 // elements.
796 return;
797 }
798
799 document.body.appendChild(svgEl);
800 var el = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
801 el.setAttribute('x', 10);
802 el.setAttribute('y', 10);
803 el.setAttribute('width', 32);
804 el.setAttribute('height', 21);
805 el.setAttribute('fill', '#000');
806
807 svgEl.appendChild(el);
808
809 // The bounding size in 1 larger than the SVG element in IE.
810 var expectedWidth = (goog.userAgent.EDGE_OR_IE) ? 33 : 32;
811 var expectedHeight = (goog.userAgent.EDGE_OR_IE) ? 22 : 21;
812
813 var dims = goog.style.getSize(el);
814 assertEquals(expectedWidth, dims.width);
815 assertRoughlyEquals(expectedHeight, dims.height, 0.01);
816
817 dims = goog.style.getSize(svgEl);
818 // The size of the <svg> will be the viewport size on all browsers. This used
819 // to not be true for Firefox, but they fixed the glitch in Firefox 33.
820 // https://bugzilla.mozilla.org/show_bug.cgi?id=530985
821 assertTrue(dims.width >= expectedWidth);
822 assertTrue(dims.height >= expectedHeight);
823
824 el.style.visibility = 'none';
825
826 dims = goog.style.getSize(el);
827 assertEquals(expectedWidth, dims.width);
828 assertRoughlyEquals(expectedHeight, dims.height, 0.01);
829
830 dims = goog.style.getSize(svgEl);
831 assertTrue(dims.width >= expectedWidth);
832 assertTrue(dims.height >= expectedHeight);
833 }
834
835 function testGetSizeSvgDocument() {
836 var svgEl = document.createElementNS &&
837 document.createElementNS('http://www.w3.org/2000/svg', 'svg');
838 if (!svgEl || svgEl.getAttribute('transform') == '' ||
839 (goog.userAgent.WEBKIT && !goog.userAgent.isVersionOrHigher(534.8))) {
840 // SVG not supported, or getBoundingClientRect not supported on SVG
841 // elements.
842 return;
843 }
844
845 var frame = goog.dom.getElement('svg-frame');
846 var doc = goog.dom.getFrameContentDocument(frame);
847 var rect = doc.getElementById('rect');
848 var dims = goog.style.getSize(rect);
849 if (goog.userAgent.GECKO && goog.userAgent.isVersionOrHigher(53)) {
850 // Firefox >= 53 auto-scales iframe SVG content to fit the frame
851 // b/38432885 | https://bugzilla.mozilla.org/show_bug.cgi?id=1366126
852 assertEquals(75, dims.width);
853 assertEquals(75, dims.height);
854 } else if (!goog.userAgent.EDGE_OR_IE) {
855 assertEquals(50, dims.width);
856 assertEquals(50, dims.height);
857 } else {
858 assertEquals(51, dims.width);
859 assertEquals(51, dims.height);
860 }
861 }
862
863 function testGetSizeInlineBlock() {
864 var el = $('height-test-inner');
865 var dims = goog.style.getSize(el);
866 assertNotEquals(0, dims.height);
867 }
868
869 function testGetSizeTransformedRotated() {
870 if (!hasWebkitTransform()) return;
871
872 var el = $('rotated');
873 goog.style.setSize(el, 300, 200);
874
875 var noRotateDims = goog.style.getTransformedSize(el);
876 assertEquals(300, noRotateDims.width);
877 assertEquals(200, noRotateDims.height);
878
879 el.style.webkitTransform = 'rotate(180deg)';
880 var rotate180Dims = goog.style.getTransformedSize(el);
881 assertEquals(300, rotate180Dims.width);
882 assertEquals(200, rotate180Dims.height);
883
884 el.style.webkitTransform = 'rotate(90deg)';
885 var rotate90ClockwiseDims = goog.style.getTransformedSize(el);
886 assertEquals(200, rotate90ClockwiseDims.width);
887 assertEquals(300, rotate90ClockwiseDims.height);
888
889 el.style.webkitTransform = 'rotate(-90deg)';
890 var rotate90CounterClockwiseDims = goog.style.getTransformedSize(el);
891 assertEquals(200, rotate90CounterClockwiseDims.width);
892 assertEquals(300, rotate90CounterClockwiseDims.height);
893 }
894
895 function testGetSizeTransformedScaled() {
896 if (!hasWebkitTransform()) return;
897
898 var el = $('scaled');
899 goog.style.setSize(el, 300, 200);
900
901 var noScaleDims = goog.style.getTransformedSize(el);
902 assertEquals(300, noScaleDims.width);
903 assertEquals(200, noScaleDims.height);
904
905 el.style.webkitTransform = 'scale(2, 0.5)';
906 var scaledDims = goog.style.getTransformedSize(el);
907 assertEquals(600, scaledDims.width);
908 assertEquals(100, scaledDims.height);
909 }
910
911 function hasWebkitTransform() {
912 return 'webkitTransform' in document.body.style;
913 }
914
915 function testGetSizeOfOrphanElement() {
916 var orphanElem = goog.dom.createElement(goog.dom.TagName.DIV);
917 var size = goog.style.getSize(orphanElem);
918 assertEquals(0, size.width);
919 assertEquals(0, size.height);
920 }
921
922 function testGetBounds() {
923 var el = $('testEl');
924
925 var dims = goog.style.getSize(el);
926 var pos = goog.style.getPageOffset(el);
927
928 var rect = goog.style.getBounds(el);
929
930 // Relies on getSize and getPageOffset being correct.
931 assertEquals(dims.width, rect.width);
932 assertEquals(dims.height, rect.height);
933 assertEquals(pos.x, rect.left);
934 assertEquals(pos.y, rect.top);
935 }
936
937 function testInstallSafeStyleSheet() {
938 var el = $('installTest0');
939 var originalBackground = goog.style.getBackgroundColor(el);
940
941 // Uses background-color because it's easy to get the computed value
942 var result = goog.style.installSafeStyleSheet(
943 goog.html.testing.newSafeStyleSheetForTest(
944 '#installTest0 { background-color: rgb(255, 192, 203); }'));
945
946 assertColorRgbEquals('rgb(255,192,203)', goog.style.getBackgroundColor(el));
947
948 goog.style.uninstallStyles(result);
949 assertEquals(originalBackground, goog.style.getBackgroundColor(el));
950 }
951
952 function testSetSafeStyleSheet() {
953 var el = $('installTest1');
954
955 // Change to pink
956 var ss = goog.style.installSafeStyleSheet(
957 goog.html.testing.newSafeStyleSheetForTest(
958 '#installTest1 { background-color: rgb(255, 192, 203); }'));
959
960 assertColorRgbEquals('rgb(255,192,203)', goog.style.getBackgroundColor(el));
961
962 // Now change to orange
963 goog.style.setSafeStyleSheet(ss,
964 goog.html.testing.newSafeStyleSheetForTest(
965 '#installTest1 { background-color: rgb(255, 255, 0); }'));
966 assertColorRgbEquals('rgb(255,255,0)', goog.style.getBackgroundColor(el));
967 }
968
969 function assertColorRgbEquals(expected, actual) {
970 assertEquals(
971 expected, goog.color.hexToRgbStyle(goog.color.parse(actual).hex));
972 }
973
974 function testIsRightToLeft() {
975 assertFalse(goog.style.isRightToLeft($('rtl1')));
976 assertTrue(goog.style.isRightToLeft($('rtl2')));
977 assertFalse(goog.style.isRightToLeft($('rtl3')));
978 assertFalse(goog.style.isRightToLeft($('rtl4')));
979 assertTrue(goog.style.isRightToLeft($('rtl5')));
980 assertFalse(goog.style.isRightToLeft($('rtl6')));
981 assertTrue(goog.style.isRightToLeft($('rtl7')));
982 assertFalse(goog.style.isRightToLeft($('rtl8')));
983 assertTrue(goog.style.isRightToLeft($('rtl9')));
984 assertFalse(goog.style.isRightToLeft($('rtl10')));
985 }
986
987 function testIsUnselectable() {
988 assertEquals(
989 goog.userAgent.GECKO, goog.style.isUnselectable($('unselectable-gecko')));
990 assertEquals(
991 goog.userAgent.IE || goog.userAgent.OPERA,
992 goog.style.isUnselectable($('unselectable-ie')));
993 // Note: Firefox can go either way here - newer versions see -webkit-*
994 // properties and automatically add Moz* to the style object.
995 if (!goog.userAgent.GECKO) {
996 assertEquals(
997 goog.userAgent.WEBKIT || goog.userAgent.EDGE,
998 goog.style.isUnselectable($('unselectable-webkit')));
999 }
1000 }
1001
1002 function testSetUnselectable() {
1003 var el = $('make-unselectable');
1004 assertFalse(goog.style.isUnselectable(el));
1005
1006 function assertDescendantsUnselectable(unselectable) {
1007 goog.array.forEach(el.getElementsByTagName('*'), function(descendant) {
1008 // Skip MathML or any other elements that do not have a style property.
1009 if (descendant.style) {
1010 assertEquals(unselectable, goog.style.isUnselectable(descendant));
1011 }
1012 });
1013 }
1014
1015 goog.style.setUnselectable(el, true);
1016 assertTrue(goog.style.isUnselectable(el));
1017 assertDescendantsUnselectable(true);
1018
1019 goog.style.setUnselectable(el, false);
1020 assertFalse(goog.style.isUnselectable(el));
1021 assertDescendantsUnselectable(false);
1022
1023 goog.style.setUnselectable(el, true, true);
1024 assertTrue(goog.style.isUnselectable(el));
1025 assertDescendantsUnselectable(false);
1026
1027 goog.style.setUnselectable(el, false, true);
1028 assertFalse(goog.style.isUnselectable(el));
1029 assertDescendantsUnselectable(false);
1030 }
1031
1032 function testPosWithAbsoluteAndScroll() {
1033 var el = $('pos-scroll-abs');
1034 var el1 = $('pos-scroll-abs-1');
1035 var el2 = $('pos-scroll-abs-2');
1036
1037 el1.scrollTop = 200;
1038 var pos = goog.style.getPageOffset(el2);
1039
1040 assertEquals(200, pos.x);
1041 // Don't bother with IE in quirks mode
1042 if (!goog.userAgent.IE || document.compatMode == 'CSS1Compat') {
1043 assertRoughlyEquals(300, pos.y, .1);
1044 }
1045 }
1046
1047 function testPosWithAbsoluteAndWindowScroll() {
1048 window.scrollBy(0, 200);
1049 var el = $('abs-upper-left');
1050 var pos = goog.style.getPageOffset(el);
1051 assertRoughlyEquals('Top should be about 0', 0, pos.y, 0.1);
1052 }
1053
1054 function testGetBorderBoxSize() {
1055 // Strict mode
1056 var getBorderBoxSize = goog.style.getBorderBoxSize;
1057
1058 var el = $('size-a');
1059 var rect = getBorderBoxSize(el);
1060 assertEquals('width:100px', 100, rect.width);
1061 assertEquals('height:100px', 100, rect.height);
1062
1063 // with border: 10px
1064 el = $('size-b');
1065 rect = getBorderBoxSize(el);
1066 assertEquals('width:100px;border:10px', isBorderBox ? 100 : 120, rect.width);
1067 assertEquals(
1068 'height:100px;border:10px', isBorderBox ? 100 : 120, rect.height);
1069
1070 // with border: 10px; padding: 10px
1071 el = $('size-c');
1072 rect = getBorderBoxSize(el);
1073 assertEquals(
1074 'width:100px;border:10px;padding:10px', isBorderBox ? 100 : 140,
1075 rect.width);
1076 assertEquals(
1077 'height:100px;border:10px;padding:10px', isBorderBox ? 100 : 140,
1078 rect.height);
1079
1080 // size, padding and borders are all in non pixel units
1081 // all we test here is that we get a number out
1082 el = $('size-d');
1083 rect = getBorderBoxSize(el);
1084 assertEquals('number', typeof rect.width);
1085 assertEquals('number', typeof rect.height);
1086 assertFalse(isNaN(rect.width));
1087 assertFalse(isNaN(rect.height));
1088 }
1089
1090 function testGetContentBoxSize() {
1091 // Strict mode
1092 var getContentBoxSize = goog.style.getContentBoxSize;
1093
1094 var el = $('size-a');
1095 var rect = getContentBoxSize(el);
1096 assertEquals('width:100px', 100, rect.width);
1097 assertEquals('height:100px', 100, rect.height);
1098
1099 // with border: 10px
1100 el = $('size-b');
1101 rect = getContentBoxSize(el);
1102 assertEquals('width:100px;border:10px', isBorderBox ? 80 : 100, rect.width);
1103 assertEquals('height:100px;border:10px', isBorderBox ? 80 : 100, rect.height);
1104
1105 // with border: 10px; padding: 10px
1106 el = $('size-c');
1107 rect = getContentBoxSize(el);
1108 assertEquals(
1109 'width:100px;border:10px;padding:10px', isBorderBox ? 60 : 100,
1110 rect.width);
1111 assertEquals(
1112 'height:100px;border:10px;padding:10px', isBorderBox ? 60 : 100,
1113 rect.height);
1114
1115 // size, padding and borders are all in non pixel units
1116 // all we test here is that we get a number out
1117 el = $('size-d');
1118 rect = getContentBoxSize(el);
1119 assertEquals('number', typeof rect.width);
1120 assertEquals('number', typeof rect.height);
1121 assertFalse(isNaN(rect.width));
1122 assertFalse(isNaN(rect.height));
1123
1124 // test whether getContentBoxSize works when width and height
1125 // aren't explicitly set, but the default of 'auto'.
1126 // 'size-f' has no margin, border, or padding, so offsetWidth/Height
1127 // should match the content box size
1128 el = $('size-f');
1129 rect = getContentBoxSize(el);
1130 assertEquals(el.offsetWidth, rect.width);
1131 assertEquals(el.offsetHeight, rect.height);
1132 }
1133
1134 function testSetBorderBoxSize() {
1135 // Strict mode
1136 var el = $('size-e');
1137 var Size = goog.math.Size;
1138 var setBorderBoxSize = goog.style.setBorderBoxSize;
1139
1140 // Clean up
1141 // style element has 100x100, no border and no padding
1142 el.style.padding = '';
1143 el.style.margin = '';
1144 el.style.borderWidth = '';
1145 el.style.width = '';
1146 el.style.height = '';
1147
1148 setBorderBoxSize(el, new Size(100, 100));
1149
1150 assertEquals(100, el.offsetWidth);
1151 assertEquals(100, el.offsetHeight);
1152
1153 el.style.borderWidth = '10px';
1154 setBorderBoxSize(el, new Size(100, 100));
1155
1156 assertEquals('width:100px;border:10px', 100, el.offsetWidth);
1157 assertEquals('height:100px;border:10px', 100, el.offsetHeight);
1158
1159 el.style.padding = '10px';
1160 setBorderBoxSize(el, new Size(100, 100));
1161 assertEquals(100, el.offsetWidth);
1162 assertEquals(100, el.offsetHeight);
1163
1164 el.style.borderWidth = '0';
1165 setBorderBoxSize(el, new Size(100, 100));
1166 assertEquals(100, el.offsetWidth);
1167 assertEquals(100, el.offsetHeight);
1168
1169 if (goog.userAgent.GECKO) {
1170 assertEquals('border-box', el.style.MozBoxSizing);
1171 } else if (goog.userAgent.WEBKIT) {
1172 assertEquals('border-box', el.style.WebkitBoxSizing);
1173 } else if (
1174 goog.userAgent.OPERA ||
1175 goog.userAgent.IE && goog.userAgent.isDocumentModeOrHigher(8)) {
1176 assertEquals('border-box', el.style.boxSizing);
1177 }
1178
1179 // Try a negative width/height.
1180 setBorderBoxSize(el, new Size(-10, -10));
1181
1182 // Setting the border box smaller than the borders will just give you
1183 // a content box of size 0.
1184 // NOTE(nicksantos): I'm not really sure why IE7 is special here.
1185 var isIeLt8Quirks = goog.userAgent.IE &&
1186 !goog.userAgent.isDocumentModeOrHigher(8) && !goog.dom.isCss1CompatMode();
1187 assertEquals(20, el.offsetWidth);
1188 assertEquals(isIeLt8Quirks ? 39 : 20, el.offsetHeight);
1189 }
1190
1191 function testSetContentBoxSize() {
1192 // Strict mode
1193 var el = $('size-e');
1194 var Size = goog.math.Size;
1195 var setContentBoxSize = goog.style.setContentBoxSize;
1196
1197 // Clean up
1198 // style element has 100x100, no border and no padding
1199 el.style.padding = '';
1200 el.style.margin = '';
1201 el.style.borderWidth = '';
1202 el.style.width = '';
1203 el.style.height = '';
1204
1205 setContentBoxSize(el, new Size(100, 100));
1206
1207 assertEquals(100, el.offsetWidth);
1208 assertEquals(100, el.offsetHeight);
1209
1210 el.style.borderWidth = '10px';
1211 setContentBoxSize(el, new Size(100, 100));
1212 assertEquals('width:100px;border-width:10px', 120, el.offsetWidth);
1213 assertEquals('height:100px;border-width:10px', 120, el.offsetHeight);
1214
1215 el.style.padding = '10px';
1216 setContentBoxSize(el, new Size(100, 100));
1217 assertEquals(
1218 'width:100px;border-width:10px;padding:10px', 140, el.offsetWidth);
1219 assertEquals(
1220 'height:100px;border-width:10px;padding:10px', 140, el.offsetHeight);
1221
1222 el.style.borderWidth = '0';
1223 setContentBoxSize(el, new Size(100, 100));
1224 assertEquals('width:100px;padding:10px', 120, el.offsetWidth);
1225 assertEquals('height:100px;padding:10px', 120, el.offsetHeight);
1226
1227 if (goog.userAgent.GECKO) {
1228 assertEquals('content-box', el.style.MozBoxSizing);
1229 } else if (goog.userAgent.WEBKIT) {
1230 assertEquals('content-box', el.style.WebkitBoxSizing);
1231 } else if (
1232 goog.userAgent.OPERA ||
1233 goog.userAgent.IE && goog.userAgent.isDocumentModeOrHigher(8)) {
1234 assertEquals('content-box', el.style.boxSizing);
1235 }
1236
1237 // Try a negative width/height.
1238 setContentBoxSize(el, new Size(-10, -10));
1239
1240 // NOTE(nicksantos): I'm not really sure why IE7 is special here.
1241 var isIeLt8Quirks = goog.userAgent.IE &&
1242 !goog.userAgent.isDocumentModeOrHigher('8') &&
1243 !goog.dom.isCss1CompatMode();
1244 assertEquals(20, el.offsetWidth);
1245 assertEquals(isIeLt8Quirks ? 39 : 20, el.offsetHeight);
1246 }
1247
1248 function testGetPaddingBox() {
1249 // Strict mode
1250 var el = $('size-e');
1251 var Size = goog.math.Size;
1252 var getPaddingBox = goog.style.getPaddingBox;
1253
1254 // Clean up
1255 // style element has 100x100, no border and no padding
1256 el.style.padding = '';
1257 el.style.margin = '';
1258 el.style.borderWidth = '';
1259 el.style.width = '';
1260 el.style.height = '';
1261
1262 el.style.padding = '10px';
1263 var rect = getPaddingBox(el);
1264 assertEquals(10, rect.left);
1265 assertEquals(10, rect.right);
1266 assertEquals(10, rect.top);
1267 assertEquals(10, rect.bottom);
1268
1269 el.style.padding = '0';
1270 rect = getPaddingBox(el);
1271 assertEquals(0, rect.left);
1272 assertEquals(0, rect.right);
1273 assertEquals(0, rect.top);
1274 assertEquals(0, rect.bottom);
1275
1276 el.style.padding = '1px 2px 3px 4px';
1277 rect = getPaddingBox(el);
1278 assertEquals(1, rect.top);
1279 assertEquals(2, rect.right);
1280 assertEquals(3, rect.bottom);
1281 assertEquals(4, rect.left);
1282
1283 el.style.padding = '1mm 2em 3ex 4%';
1284 rect = getPaddingBox(el);
1285 assertFalse(isNaN(rect.top));
1286 assertFalse(isNaN(rect.right));
1287 assertFalse(isNaN(rect.bottom));
1288 assertFalse(isNaN(rect.left));
1289 assertTrue(rect.top >= 0);
1290 assertTrue(rect.right >= 0);
1291 assertTrue(rect.bottom >= 0);
1292 assertTrue(rect.left >= 0);
1293 }
1294
1295 function testGetPaddingBoxUnattached() {
1296 var el = goog.dom.createElement(goog.dom.TagName.DIV);
1297 var box = goog.style.getPaddingBox(el);
1298 if (goog.userAgent.WEBKIT) {
1299 assertTrue(isNaN(box.top));
1300 assertTrue(isNaN(box.right));
1301 assertTrue(isNaN(box.bottom));
1302 assertTrue(isNaN(box.left));
1303 } else {
1304 assertObjectEquals(new goog.math.Box(0, 0, 0, 0), box);
1305 }
1306 }
1307
1308 function testGetMarginBox() {
1309 // Strict mode
1310 var el = $('size-e');
1311 var Size = goog.math.Size;
1312 var getMarginBox = goog.style.getMarginBox;
1313
1314 // Clean up
1315 // style element has 100x100, no border and no padding
1316 el.style.padding = '';
1317 el.style.margin = '';
1318 el.style.borderWidth = '';
1319 el.style.width = '';
1320 el.style.height = '';
1321
1322 el.style.margin = '10px';
1323 var rect = getMarginBox(el);
1324 assertEquals(10, rect.left);
1325 // In webkit the right margin is the calculated distance from right edge and
1326 // not the computed right margin so it is not reliable.
1327 // See https://bugs.webkit.org/show_bug.cgi?id=19828
1328 if (!goog.userAgent.WEBKIT) {
1329 assertEquals(10, rect.right);
1330 }
1331 assertEquals(10, rect.top);
1332 assertEquals(10, rect.bottom);
1333
1334 el.style.margin = '0';
1335 rect = getMarginBox(el);
1336 assertEquals(0, rect.left);
1337 // In webkit the right margin is the calculated distance from right edge and
1338 // not the computed right margin so it is not reliable.
1339 // See https://bugs.webkit.org/show_bug.cgi?id=19828
1340 if (!goog.userAgent.WEBKIT) {
1341 assertEquals(0, rect.right);
1342 }
1343 assertEquals(0, rect.top);
1344 assertEquals(0, rect.bottom);
1345
1346 el.style.margin = '1px 2px 3px 4px';
1347 rect = getMarginBox(el);
1348 assertEquals(1, rect.top);
1349 // In webkit the right margin is the calculated distance from right edge and
1350 // not the computed right margin so it is not reliable.
1351 // See https://bugs.webkit.org/show_bug.cgi?id=19828
1352 if (!goog.userAgent.WEBKIT) {
1353 assertEquals(2, rect.right);
1354 }
1355 assertEquals(3, rect.bottom);
1356 assertEquals(4, rect.left);
1357
1358 el.style.margin = '1mm 2em 3ex 4%';
1359 rect = getMarginBox(el);
1360 assertFalse(isNaN(rect.top));
1361 assertFalse(isNaN(rect.right));
1362 assertFalse(isNaN(rect.bottom));
1363 assertFalse(isNaN(rect.left));
1364 assertTrue(rect.top >= 0);
1365 // In webkit the right margin is the calculated distance from right edge and
1366 // not the computed right margin so it is not reliable.
1367 // See https://bugs.webkit.org/show_bug.cgi?id=19828
1368 if (!goog.userAgent.WEBKIT) {
1369 assertTrue(rect.right >= 0);
1370 }
1371 assertTrue(rect.bottom >= 0);
1372 assertTrue(rect.left >= 0);
1373 }
1374
1375 function testGetBorderBox() {
1376 // Strict mode
1377 var el = $('size-e');
1378 var Size = goog.math.Size;
1379 var getBorderBox = goog.style.getBorderBox;
1380
1381 // Clean up
1382 // style element has 100x100, no border and no padding
1383 el.style.padding = '';
1384 el.style.margin = '';
1385 el.style.borderWidth = '';
1386 el.style.width = '';
1387 el.style.height = '';
1388
1389 el.style.borderWidth = '10px';
1390 var rect = getBorderBox(el);
1391 assertEquals(10, rect.left);
1392 assertEquals(10, rect.right);
1393 assertEquals(10, rect.top);
1394 assertEquals(10, rect.bottom);
1395
1396 el.style.borderWidth = '0';
1397 rect = getBorderBox(el);
1398 assertEquals(0, rect.left);
1399 assertEquals(0, rect.right);
1400 assertEquals(0, rect.top);
1401 assertEquals(0, rect.bottom);
1402
1403 el.style.borderWidth = '1px 2px 3px 4px';
1404 rect = getBorderBox(el);
1405 assertEquals(1, rect.top);
1406 assertEquals(2, rect.right);
1407 assertEquals(3, rect.bottom);
1408 assertEquals(4, rect.left);
1409
1410 // % does not work for border widths in IE
1411 el.style.borderWidth = '1mm 2em 3ex 4pt';
1412 rect = getBorderBox(el);
1413 assertFalse(isNaN(rect.top));
1414 assertFalse(isNaN(rect.right));
1415 assertFalse(isNaN(rect.bottom));
1416 assertFalse(isNaN(rect.left));
1417 assertTrue(rect.top >= 0);
1418 assertTrue(rect.right >= 0);
1419 assertTrue(rect.bottom >= 0);
1420 assertTrue(rect.left >= 0);
1421
1422 el.style.borderWidth = 'thin medium thick 1px';
1423 rect = getBorderBox(el);
1424 assertFalse(isNaN(rect.top));
1425 assertFalse(isNaN(rect.right));
1426 assertFalse(isNaN(rect.bottom));
1427 assertFalse(isNaN(rect.left));
1428 assertTrue(rect.top >= 0);
1429 assertTrue(rect.right >= 0);
1430 assertTrue(rect.bottom >= 0);
1431 assertTrue(rect.left >= 0);
1432 }
1433
1434 function testGetFontFamily() {
1435 // I tried to use common fonts for these tests. It's possible the test fails
1436 // because the testing platform doesn't have one of these fonts installed:
1437 // Comic Sans MS or Century Schoolbook L
1438 // Times
1439 // Helvetica
1440
1441 var tmpFont = goog.style.getFontFamily($('font-tag'));
1442 assertTrue(
1443 'FontFamily should be detectable when set via <font face>',
1444 'Times' == tmpFont || 'Times New Roman' == tmpFont);
1445 tmpFont = goog.style.getFontFamily($('small-text'));
1446 assertTrue(
1447 'Multiword fonts should be reported with quotes stripped.',
1448 'Comic Sans MS' == tmpFont || 'Century Schoolbook L' == tmpFont);
1449 // Firefox fails this test & retuns a generic 'monospace' instead of the
1450 // actually displayed font (e.g., "Times New").
1451 // tmpFont = goog.style.getFontFamily($('pre-font'));
1452 // assertEquals('<pre> tags should use a fixed-width font',
1453 // 'Times New',
1454 // tmpFont);
1455 tmpFont = goog.style.getFontFamily($('inherit-font'));
1456 assertEquals(
1457 'Explicitly inherited fonts should be detectable', 'Helvetica', tmpFont);
1458 tmpFont = goog.style.getFontFamily($('times-font-family'));
1459 assertEquals(
1460 'Font-family set via style attribute should be detected', 'Times',
1461 tmpFont);
1462 tmpFont = goog.style.getFontFamily($('bold-font'));
1463 assertEquals(
1464 'Implicitly inherited font should be detected', 'Helvetica', tmpFont);
1465 tmpFont = goog.style.getFontFamily($('css-html-tag-redefinition'));
1466 assertEquals('HTML tag CSS rewrites should be detected', 'Times', tmpFont);
1467 tmpFont = goog.style.getFontFamily($('no-text-font-styles'));
1468 assertEquals(
1469 'Font family should exist even with no text', 'Helvetica', tmpFont);
1470 tmpFont = goog.style.getFontFamily($('icon-font'));
1471 assertNotEquals(
1472 'icon is a special font-family value', 'icon', tmpFont.toLowerCase());
1473 tmpFont = goog.style.getFontFamily($('font-style-badfont'));
1474 // Firefox fails this test and reports the specified "badFont", which is
1475 // obviously not displayed.
1476 // assertEquals('Invalid fonts should not be returned',
1477 // 'Helvetica',
1478 // tmpFont);
1479 tmpFont = goog.style.getFontFamily($('img-font-test'));
1480 assertTrue(
1481 'Even img tags should inherit the document body\'s font', tmpFont != '');
1482 tmpFont = goog.style.getFontFamily($('nested-font'));
1483 assertEquals(
1484 'An element with nested content should be unaffected.', 'Arial', tmpFont);
1485 // IE raises an 'Invalid Argument' error when using the moveToElementText
1486 // method from the TextRange object with an element that is not attached to
1487 // a document.
1488 var element = goog.dom.createDom(
1489 goog.dom.TagName.SPAN, {style: 'font-family:Times,sans-serif;'},
1490 'some text');
1491 tmpFont = goog.style.getFontFamily(element);
1492 assertEquals(
1493 'Font should be correctly retrieved for element not attached' +
1494 ' to a document',
1495 'Times', tmpFont);
1496 }
1497
1498 function testGetFontSize() {
1499 assertEquals(
1500 'Font size should be determined even without any text', 30,
1501 goog.style.getFontSize($('no-text-font-styles')));
1502 assertEquals(
1503 'A 5em font should be 5x larger than its parent.', 150,
1504 goog.style.getFontSize($('css-html-tag-redefinition')));
1505 assertTrue(
1506 'Setting font size=-1 should result in a positive font size.',
1507 goog.style.getFontSize($('font-tag')) > 0);
1508 assertEquals(
1509 'Inheriting a 50% font-size should have no additional effect',
1510 goog.style.getFontSize($('font-style-badfont')),
1511 goog.style.getFontSize($('inherit-50pct-font')));
1512 assertTrue(
1513 'In pretty much any display, 3in should be > 8px',
1514 goog.style.getFontSize($('times-font-family')) >
1515 goog.style.getFontSize($('no-text-font-styles')));
1516 assertTrue(
1517 'With no applied styles, font-size should still be defined.',
1518 goog.style.getFontSize($('no-font-style')) > 0);
1519 assertEquals(
1520 '50% of 30px is 15', 15, goog.style.getFontSize($('font-style-badfont')));
1521 assertTrue(
1522 'x-small text should be smaller than small text',
1523 goog.style.getFontSize($('x-small-text')) <
1524 goog.style.getFontSize($('small-text')));
1525 // IE fails this test, the decimal portion of px lengths isn't reported
1526 // by getCascadedStyle. Firefox passes, but only because it ignores the
1527 // decimals altogether.
1528 // assertEquals('12.5px should be the same as 0.5em nested in a 25px node.',
1529 // goog.style.getFontSize($('font-size-12-point-5-px')),
1530 // goog.style.getFontSize($('font-size-50-pct-of-25-px')));
1531
1532 assertEquals(
1533 'Font size should not doubly count em values', 2,
1534 goog.style.getFontSize($('em-font-size')));
1535 }
1536
1537 function testGetLengthUnits() {
1538 assertEquals('px', goog.style.getLengthUnits('15px'));
1539 assertEquals('%', goog.style.getLengthUnits('99%'));
1540 assertNull(goog.style.getLengthUnits(''));
1541 }
1542
1543 function testParseStyleAttribute() {
1544 var css = 'left: 0px; text-align: center';
1545 var expected = {'left': '0px', 'textAlign': 'center'};
1546
1547 assertObjectEquals(expected, goog.style.parseStyleAttribute(css));
1548 }
1549
1550 function testToStyleAttribute() {
1551 var object = {'left': '0px', 'textAlign': 'center'};
1552 var expected = 'left:0px;text-align:center;';
1553
1554 assertEquals(expected, goog.style.toStyleAttribute(object));
1555 }
1556
1557 function testStyleAttributePassthrough() {
1558 var object = {'left': '0px', 'textAlign': 'center'};
1559
1560 assertObjectEquals(
1561 object,
1562 goog.style.parseStyleAttribute(goog.style.toStyleAttribute(object)));
1563 }
1564
1565 function testGetFloat() {
1566 assertEquals('', goog.style.getFloat($('no-float')));
1567 assertEquals('none', goog.style.getFloat($('float-none')));
1568 assertEquals('left', goog.style.getFloat($('float-left')));
1569 }
1570
1571 function testSetFloat() {
1572 var el = $('float-test');
1573
1574 goog.style.setFloat(el, 'left');
1575 assertEquals('left', goog.style.getFloat(el));
1576
1577 goog.style.setFloat(el, 'right');
1578 assertEquals('right', goog.style.getFloat(el));
1579
1580 goog.style.setFloat(el, 'none');
1581 assertEquals('none', goog.style.getFloat(el));
1582
1583 goog.style.setFloat(el, '');
1584 assertEquals('', goog.style.getFloat(el));
1585 }
1586
1587 function testIsElementShown() {
1588 var el = $('testEl');
1589
1590 goog.style.setElementShown(el, false);
1591 assertFalse(goog.style.isElementShown(el));
1592
1593 goog.style.setElementShown(el, true);
1594 assertTrue(goog.style.isElementShown(el));
1595 }
1596
1597 function testGetOpacity() {
1598 var el1 = {style: {opacity: '0.3'}};
1599
1600 var el2 = {style: {MozOpacity: '0.1'}};
1601
1602 var el3 = {
1603 style: {filter: 'some:other,filter;alpha(opacity=25.5);alpha(more=100);'}
1604 };
1605
1606 assertEquals(0.3, goog.style.getOpacity(el1));
1607 assertEquals(0.1, goog.style.getOpacity(el2));
1608 assertEquals(0.255, goog.style.getOpacity(el3));
1609
1610 el1.style.opacity = '0';
1611 el2.style.MozOpacity = '0';
1612 el3.style.filter = 'some:other,filter;alpha(opacity=0);alpha(more=100);';
1613
1614 assertEquals(0, goog.style.getOpacity(el1));
1615 assertEquals(0, goog.style.getOpacity(el2));
1616 assertEquals(0, goog.style.getOpacity(el3));
1617
1618 el1.style.opacity = '';
1619 el2.style.MozOpacity = '';
1620 el3.style.filter = '';
1621
1622 assertEquals('', goog.style.getOpacity(el1));
1623 assertEquals('', goog.style.getOpacity(el2));
1624 assertEquals('', goog.style.getOpacity(el3));
1625
1626 var el4 = {style: {}};
1627
1628 assertEquals('', goog.style.getOpacity(el4));
1629 assertEquals('', goog.style.getOpacity($('test-opacity')));
1630 }
1631
1632 function testSetOpacity() {
1633 var el1 = {style: {opacity: '0.3'}};
1634 goog.style.setOpacity(el1, 0.8);
1635
1636 var el2 = {style: {MozOpacity: '0.1'}};
1637 goog.style.setOpacity(el2, 0.5);
1638
1639 var el3 = {style: {filter: 'alpha(opacity=25)'}};
1640 goog.style.setOpacity(el3, 0.1);
1641
1642 assertEquals(0.8, Number(el1.style.opacity));
1643 assertEquals(0.5, Number(el2.style.MozOpacity));
1644 assertEquals('alpha(opacity=10)', el3.style.filter);
1645
1646 goog.style.setOpacity(el1, 0);
1647 goog.style.setOpacity(el2, 0);
1648 goog.style.setOpacity(el3, 0);
1649
1650 assertEquals(0, Number(el1.style.opacity));
1651 assertEquals(0, Number(el2.style.MozOpacity));
1652 assertEquals('alpha(opacity=0)', el3.style.filter);
1653
1654 goog.style.setOpacity(el1, '');
1655 goog.style.setOpacity(el2, '');
1656 goog.style.setOpacity(el3, '');
1657
1658 assertEquals('', el1.style.opacity);
1659 assertEquals('', el2.style.MozOpacity);
1660 assertEquals('', el3.style.filter);
1661 }
1662
1663 function testFramedPageOffset() {
1664 // Set up a complicated iframe ancestor chain.
1665 var iframe = goog.dom.getElement('test-frame-offset');
1666 var iframeDoc = goog.dom.getFrameContentDocument(iframe);
1667 var iframeWindow = goog.dom.getWindow(iframeDoc);
1668
1669 var iframePos = 'style="display:block;position:absolute;' +
1670 'top:50px;left:50px;width:50px;height:50px;"';
1671 iframeDoc.write(
1672 '<iframe id="test-frame-offset-2" ' + iframePos + '></iframe>' +
1673 '<div id="test-element-2" ' +
1674 ' style="position:absolute;left:300px;top:300px">hi mom!</div>');
1675 iframeDoc.close();
1676 var iframe2 = iframeDoc.getElementById('test-frame-offset-2');
1677 var testElement2 = iframeDoc.getElementById('test-element-2');
1678 var iframeDoc2 = goog.dom.getFrameContentDocument(iframe2);
1679 var iframeWindow2 = goog.dom.getWindow(iframeDoc2);
1680
1681 iframeDoc2.write(
1682 '<div id="test-element-3" ' +
1683 ' style="position:absolute;left:500px;top:500px">hi mom!</div>');
1684 iframeDoc2.close();
1685 var testElement3 = iframeDoc2.getElementById('test-element-3');
1686
1687 assertCoordinateApprox(300, 300, 0, goog.style.getPageOffset(testElement2));
1688 assertCoordinateApprox(500, 500, 0, goog.style.getPageOffset(testElement3));
1689
1690 assertCoordinateApprox(
1691 350, 350, 0, goog.style.getFramedPageOffset(testElement2, window));
1692 assertCoordinateApprox(
1693 300, 300, 0, goog.style.getFramedPageOffset(testElement2, iframeWindow));
1694
1695 assertCoordinateApprox(
1696 600, 600, 0, goog.style.getFramedPageOffset(testElement3, window));
1697 assertCoordinateApprox(
1698 550, 550, 0, goog.style.getFramedPageOffset(testElement3, iframeWindow));
1699 assertCoordinateApprox(
1700 500, 500, 0, goog.style.getFramedPageOffset(testElement3, iframeWindow2));
1701
1702 // Scroll the iframes a bit.
1703 window.scrollBy(0, 5);
1704 iframeWindow.scrollBy(0, 11);
1705 iframeWindow2.scrollBy(0, 18);
1706
1707 // On Firefox 2, scrolling inner iframes causes off by one errors
1708 // in the page position, because we're using screen coords to compute them.
1709 assertCoordinateApprox(300, 300, 2, goog.style.getPageOffset(testElement2));
1710 assertCoordinateApprox(500, 500, 2, goog.style.getPageOffset(testElement3));
1711
1712 assertCoordinateApprox(
1713 350, 350 - 11, 2, goog.style.getFramedPageOffset(testElement2, window));
1714 assertCoordinateApprox(
1715 300, 300, 2, goog.style.getFramedPageOffset(testElement2, iframeWindow));
1716
1717 assertCoordinateApprox(
1718 600, 600 - 18 - 11, 2,
1719 goog.style.getFramedPageOffset(testElement3, window));
1720 assertCoordinateApprox(
1721 550, 550 - 18, 2,
1722 goog.style.getFramedPageOffset(testElement3, iframeWindow));
1723 assertCoordinateApprox(
1724 500, 500, 2, goog.style.getFramedPageOffset(testElement3, iframeWindow2));
1725
1726 // In IE, if the element is in a frame that's been removed from the DOM and
1727 // relativeWin is not that frame's contentWindow, the contentWindow's parent
1728 // reference points to itself. We want to guarantee that we don't fall into
1729 // an infinite loop.
1730 var iframeParent = iframe.parentElement;
1731 iframeParent.removeChild(iframe);
1732 // We don't check the value returned as it differs by browser. 0,0 for Chrome
1733 // and FF. IE returns 30000 or 30198 for x in IE8-9 and 300 in IE10-11
1734 goog.style.getFramedPageOffset(testElement2, window);
1735 }
1736
1737
1738 /**
1739 * Asserts that the coordinate is approximately equal to the given
1740 * x and y coordinates, give or take delta.
1741 */
1742 function assertCoordinateApprox(x, y, delta, coord) {
1743 assertTrue(
1744 'Expected x: ' + x + ', actual x: ' + coord.x,
1745 coord.x >= x - delta && coord.x <= x + delta);
1746 assertTrue(
1747 'Expected y: ' + y + ', actual y: ' + coord.y,
1748 coord.y >= y - delta && coord.y <= y + delta);
1749 }
1750
1751 function testTranslateRectForAnotherFrame() {
1752 var rect = new goog.math.Rect(1, 2, 3, 4);
1753 var thisDom = goog.dom.getDomHelper();
1754 goog.style.translateRectForAnotherFrame(rect, thisDom, thisDom);
1755 assertEquals(1, rect.left);
1756 assertEquals(2, rect.top);
1757 assertEquals(3, rect.width);
1758 assertEquals(4, rect.height);
1759
1760 var iframe = $('test-translate-frame-standard');
1761 var iframeDoc = goog.dom.getFrameContentDocument(iframe);
1762 var iframeDom = goog.dom.getDomHelper(iframeDoc);
1763 // Cannot rely on iframe starting at origin.
1764 iframeDom.getWindow().scrollTo(0, 0);
1765 // iframe is at (100, 150) and its body is not scrolled.
1766 rect = new goog.math.Rect(1, 2, 3, 4);
1767 goog.style.translateRectForAnotherFrame(rect, iframeDom, thisDom);
1768 assertEquals(1 + 100, rect.left);
1769 assertRoughlyEquals(2 + 150, rect.top, .1);
1770 assertEquals(3, rect.width);
1771 assertEquals(4, rect.height);
1772
1773 iframeDom.getWindow().scrollTo(11, 13);
1774 rect = new goog.math.Rect(1, 2, 3, 4);
1775 goog.style.translateRectForAnotherFrame(rect, iframeDom, thisDom);
1776 assertEquals(1 + 100 - 11, rect.left);
1777 assertRoughlyEquals(2 + 150 - 13, rect.top, .1);
1778 assertEquals(3, rect.width);
1779 assertEquals(4, rect.height);
1780
1781 iframe = $('test-translate-frame-quirk');
1782 iframeDoc = goog.dom.getFrameContentDocument(iframe);
1783 iframeDom = goog.dom.getDomHelper(iframeDoc);
1784 // Cannot rely on iframe starting at origin.
1785 iframeDom.getWindow().scrollTo(0, 0);
1786 // iframe is at (100, 350) and its body is not scrolled.
1787 rect = new goog.math.Rect(1, 2, 3, 4);
1788 goog.style.translateRectForAnotherFrame(rect, iframeDom, thisDom);
1789 assertEquals(1 + 100, rect.left);
1790 assertRoughlyEquals(2 + 350, rect.top, .1);
1791 assertEquals(3, rect.width);
1792 assertEquals(4, rect.height);
1793
1794 iframeDom.getWindow().scrollTo(11, 13);
1795 rect = new goog.math.Rect(1, 2, 3, 4);
1796 goog.style.translateRectForAnotherFrame(rect, iframeDom, thisDom);
1797 assertEquals(1 + 100 - 11, rect.left);
1798 assertRoughlyEquals(2 + 350 - 13, rect.top, .1);
1799 assertEquals(3, rect.width);
1800 assertEquals(4, rect.height);
1801 }
1802
1803 function testGetVisibleRectForElement() {
1804 var container = goog.dom.getElement('test-visible');
1805 var el = goog.dom.getElement('test-visible-el');
1806 var dom = goog.dom.getDomHelper(el);
1807 var winScroll = dom.getDocumentScroll();
1808 var winSize = dom.getViewportSize();
1809
1810 // Skip this test if the window size is small. Firefox3/Linux in Selenium
1811 // sometimes fails without this check.
1812 if (winSize.width < 20 || winSize.height < 20) {
1813 return;
1814 }
1815
1816 // Move the container element to the window's viewport.
1817 var h = winSize.height < 100 ? winSize.height / 2 : 100;
1818 goog.style.setSize(container, winSize.width / 2, h);
1819 goog.style.setPosition(container, 8, winScroll.y + winSize.height - h);
1820 var visible = goog.style.getVisibleRectForElement(el);
1821 var bounds = goog.style.getBounds(container);
1822 // VisibleRect == Bounds rect of the offsetParent
1823 assertNotNull(visible);
1824 assertEquals(bounds.left, visible.left);
1825 assertEquals(bounds.top, visible.top);
1826 assertEquals(bounds.left + bounds.width, visible.right);
1827 assertEquals(bounds.top + bounds.height, visible.bottom);
1828
1829 // Move a part of the container element to outside of the viewpoert.
1830 goog.style.setPosition(container, 8, winScroll.y + winSize.height - h / 2);
1831 visible = goog.style.getVisibleRectForElement(el);
1832 bounds = goog.style.getBounds(container);
1833 // Confirm VisibleRect == Intersection of the bounds rect of the
1834 // offsetParent and the viewport.
1835 assertNotNull(visible);
1836 assertEquals(bounds.left, visible.left);
1837 assertEquals(bounds.top, visible.top);
1838 assertEquals(bounds.left + bounds.width, visible.right);
1839 assertEquals(winScroll.y + winSize.height, visible.bottom);
1840
1841 // Move the container element to outside of the viewpoert.
1842 goog.style.setPosition(container, 8, winScroll.y + winSize.height * 2);
1843 visible = goog.style.getVisibleRectForElement(el);
1844 assertNull(visible);
1845
1846 // Test the case with body element of height 0
1847 var iframe = goog.dom.getElement('test-visible-frame');
1848 var iframeDoc = goog.dom.getFrameContentDocument(iframe);
1849 el = iframeDoc.getElementById('test-visible');
1850 visible = goog.style.getVisibleRectForElement(el);
1851
1852 var iframeViewportSize = goog.dom.getDomHelper(el).getViewportSize();
1853 // NOTE(chrishenry): For iframe, the clipping viewport is always the iframe
1854 // viewport, and not the actual browser viewport.
1855 assertNotNull(visible);
1856 assertEquals(0, visible.top);
1857 assertEquals(iframeViewportSize.height, visible.bottom);
1858 assertEquals(0, visible.left);
1859 assertEquals(iframeViewportSize.width, visible.right);
1860 }
1861
1862 function testGetVisibleRectForElementWithBodyScrolled() {
1863 var container = goog.dom.getElement('test-visible2');
1864 var dom = goog.dom.getDomHelper(container);
1865 var el = dom.createDom(goog.dom.TagName.DIV, undefined, 'Test');
1866 el.style.position = 'absolute';
1867 dom.append(container, el);
1868
1869 container.style.position = 'absolute';
1870 goog.style.setPosition(container, 20, 500);
1871 goog.style.setSize(container, 100, 150);
1872
1873 // Scroll body container such that container is exactly at top.
1874 window.scrollTo(0, 500);
1875 var visibleRect = goog.style.getVisibleRectForElement(el);
1876 assertNotNull(visibleRect);
1877 assertRoughlyEquals(500, visibleRect.top, EPSILON);
1878 assertRoughlyEquals(20, visibleRect.left, EPSILON);
1879 assertRoughlyEquals(650, visibleRect.bottom, EPSILON);
1880 assertRoughlyEquals(120, visibleRect.right, EPSILON);
1881
1882 // Top 100px is clipped by window viewport.
1883 window.scrollTo(0, 600);
1884 var visibleRect = goog.style.getVisibleRectForElement(el);
1885 assertNotNull(visibleRect);
1886 assertRoughlyEquals(600, visibleRect.top, EPSILON);
1887 assertRoughlyEquals(20, visibleRect.left, EPSILON);
1888 assertRoughlyEquals(650, visibleRect.bottom, EPSILON);
1889 assertRoughlyEquals(120, visibleRect.right, EPSILON);
1890
1891 var winSize = dom.getViewportSize();
1892
1893 // Left 50px is clipped by window viewport.
1894 // Right part is clipped by window viewport.
1895 goog.style.setSize(container, 10000, 150);
1896 window.scrollTo(70, 500);
1897 var visibleRect = goog.style.getVisibleRectForElement(el);
1898 assertNotNull(visibleRect);
1899 assertRoughlyEquals(500, visibleRect.top, EPSILON);
1900 assertRoughlyEquals(70, visibleRect.left, EPSILON);
1901 assertRoughlyEquals(650, visibleRect.bottom, EPSILON);
1902 assertRoughlyEquals(70 + winSize.width, visibleRect.right, EPSILON);
1903
1904 // Bottom part is clipped by window viewport.
1905 goog.style.setSize(container, 100, 2000);
1906 window.scrollTo(0, 500);
1907 var visibleRect = goog.style.getVisibleRectForElement(el);
1908 assertNotNull(visibleRect);
1909 assertRoughlyEquals(500, visibleRect.top, EPSILON);
1910 assertRoughlyEquals(20, visibleRect.left, EPSILON);
1911 assertRoughlyEquals(120, visibleRect.right, EPSILON);
1912 assertRoughlyEquals(500 + winSize.height, visibleRect.bottom, EPSILON);
1913
1914 goog.style.setPosition(container, 10000, 10000);
1915 assertNull(goog.style.getVisibleRectForElement(el));
1916 }
1917
1918 function testGetVisibleRectForElementWithNestedAreaAndNonOffsetAncestor() {
1919 // IE7 quirks mode somehow consider container2 below as offset parent
1920 // of the element, which is incorrect.
1921 if (goog.userAgent.IE && !goog.userAgent.isDocumentModeOrHigher(8) &&
1922 !goog.dom.isCss1CompatMode()) {
1923 return;
1924 }
1925
1926 var container = goog.dom.getElement('test-visible2');
1927 var dom = goog.dom.getDomHelper(container);
1928 var container2 = dom.createDom(goog.dom.TagName.DIV);
1929 var el = dom.createDom(goog.dom.TagName.DIV, undefined, 'Test');
1930 el.style.position = 'absolute';
1931 dom.append(container, container2);
1932 dom.append(container2, el);
1933
1934 container.style.position = 'absolute';
1935 goog.style.setPosition(container, 20, 500);
1936 goog.style.setSize(container, 100, 150);
1937
1938 // container2 is a scrollable container but is not an offsetParent of
1939 // the element. It is ignored in the computation.
1940 container2.style.overflow = 'hidden';
1941 container2.style.marginTop = '50px';
1942 container2.style.marginLeft = '100px';
1943 goog.style.setSize(container2, 150, 100);
1944
1945 // Scroll body container such that container is exactly at top.
1946 window.scrollTo(0, 500);
1947 var visibleRect = goog.style.getVisibleRectForElement(el);
1948 assertNotNull(visibleRect);
1949 assertRoughlyEquals(500, visibleRect.top, EPSILON);
1950 assertRoughlyEquals(20, visibleRect.left, EPSILON);
1951 assertRoughlyEquals(650, visibleRect.bottom, EPSILON);
1952 assertRoughlyEquals(120, visibleRect.right, EPSILON);
1953
1954 // Top 100px is clipped by window viewport.
1955 window.scrollTo(0, 600);
1956 var visibleRect = goog.style.getVisibleRectForElement(el);
1957 assertNotNull(visibleRect);
1958 assertRoughlyEquals(600, visibleRect.top, EPSILON);
1959 assertRoughlyEquals(20, visibleRect.left, EPSILON);
1960 assertRoughlyEquals(650, visibleRect.bottom, EPSILON);
1961 assertRoughlyEquals(120, visibleRect.right, EPSILON);
1962
1963 var winSize = dom.getViewportSize();
1964
1965 // Left 50px is clipped by window viewport.
1966 // Right part is clipped by window viewport.
1967 goog.style.setSize(container, 10000, 150);
1968 window.scrollTo(70, 500);
1969 var visibleRect = goog.style.getVisibleRectForElement(el);
1970 assertNotNull(visibleRect);
1971 assertRoughlyEquals(500, visibleRect.top, EPSILON);
1972 assertRoughlyEquals(70, visibleRect.left, EPSILON);
1973 assertRoughlyEquals(650, visibleRect.bottom, EPSILON);
1974 assertRoughlyEquals(70 + winSize.width, visibleRect.right, EPSILON);
1975
1976 // Bottom part is clipped by window viewport.
1977 goog.style.setSize(container, 100, 2000);
1978 window.scrollTo(0, 500);
1979 var visibleRect = goog.style.getVisibleRectForElement(el);
1980 assertNotNull(visibleRect);
1981 assertRoughlyEquals(500, visibleRect.top, EPSILON);
1982 assertRoughlyEquals(20, visibleRect.left, EPSILON);
1983 assertRoughlyEquals(120, visibleRect.right, EPSILON);
1984 assertRoughlyEquals(500 + winSize.height, visibleRect.bottom, EPSILON);
1985
1986 goog.style.setPosition(container, 10000, 10000);
1987 assertNull(goog.style.getVisibleRectForElement(el));
1988 }
1989
1990 function testGetVisibleRectForElementInsideNestedScrollableArea() {
1991 var container = goog.dom.getElement('test-visible2');
1992 var dom = goog.dom.getDomHelper(container);
1993 var container2 = dom.createDom(goog.dom.TagName.DIV);
1994 var el = dom.createDom(goog.dom.TagName.DIV, undefined, 'Test');
1995 el.style.position = 'absolute';
1996 dom.append(container, container2);
1997 dom.append(container2, el);
1998
1999 container.style.position = 'absolute';
2000 goog.style.setPosition(container, 100 /* left */, 500 /* top */);
2001 goog.style.setSize(container, 300 /* width */, 300 /* height */);
2002
2003 container2.style.overflow = 'hidden';
2004 container2.style.position = 'relative';
2005 goog.style.setPosition(container2, 100, 50);
2006 goog.style.setSize(container2, 150, 100);
2007
2008 // Scroll body container such that container is exactly at top.
2009 window.scrollTo(0, 500);
2010 var visibleRect = goog.style.getVisibleRectForElement(el);
2011 assertNotNull(visibleRect);
2012 assertRoughlyEquals(550, visibleRect.top, EPSILON);
2013 assertRoughlyEquals(200, visibleRect.left, EPSILON);
2014 assertRoughlyEquals(650, visibleRect.bottom, EPSILON);
2015 assertRoughlyEquals(350, visibleRect.right, EPSILON);
2016
2017 // Left 50px is clipped by container.
2018 goog.style.setPosition(container2, -50, 50);
2019 var visibleRect = goog.style.getVisibleRectForElement(el);
2020 assertNotNull(visibleRect);
2021 assertRoughlyEquals(550, visibleRect.top, EPSILON);
2022 assertRoughlyEquals(100, visibleRect.left, EPSILON);
2023 assertRoughlyEquals(650, visibleRect.bottom, EPSILON);
2024 assertRoughlyEquals(200, visibleRect.right, EPSILON);
2025
2026 // Right part is clipped by container.
2027 goog.style.setPosition(container2, 100, 50);
2028 goog.style.setWidth(container2, 1000, 100);
2029 var visibleRect = goog.style.getVisibleRectForElement(el);
2030 assertNotNull(visibleRect);
2031 assertRoughlyEquals(550, visibleRect.top, EPSILON);
2032 assertRoughlyEquals(200, visibleRect.left, EPSILON);
2033 assertRoughlyEquals(650, visibleRect.bottom, EPSILON);
2034 assertRoughlyEquals(400, visibleRect.right, EPSILON);
2035
2036 // Top 50px is clipped by container.
2037 goog.style.setStyle(container2, 'width', '150px');
2038 goog.style.setStyle(container2, 'top', '-50px');
2039 var visibleRect = goog.style.getVisibleRectForElement(el);
2040 assertNotNull(visibleRect);
2041 assertRoughlyEquals(500, visibleRect.top, EPSILON);
2042 assertRoughlyEquals(200, visibleRect.left, EPSILON);
2043 assertRoughlyEquals(550, visibleRect.bottom, EPSILON);
2044 assertRoughlyEquals(350, visibleRect.right, EPSILON);
2045
2046 // Bottom part is clipped by container.
2047 goog.style.setStyle(container2, 'top', '50px');
2048 goog.style.setStyle(container2, 'height', '1000px');
2049 var visibleRect = goog.style.getVisibleRectForElement(el);
2050 assertNotNull(visibleRect);
2051 assertRoughlyEquals(550, visibleRect.top, EPSILON);
2052 assertRoughlyEquals(200, visibleRect.left, EPSILON);
2053 assertRoughlyEquals(800, visibleRect.bottom, EPSILON);
2054 assertRoughlyEquals(350, visibleRect.right, EPSILON);
2055
2056 // Outside viewport.
2057 goog.style.setStyle(container2, 'top', '10000px');
2058 goog.style.setStyle(container2, 'left', '10000px');
2059 assertNull(goog.style.getVisibleRectForElement(el));
2060 }
2061
2062 function testScrollIntoContainerViewQuirks() {
2063 if (goog.dom.isCss1CompatMode()) return;
2064
2065 var container = goog.dom.getElement('scrollable-container');
2066
2067 // Scroll the minimum amount to make the elements visible.
2068 goog.style.scrollIntoContainerView(goog.dom.getElement('item7'), container);
2069 assertEquals('scroll to item7', 79, container.scrollTop);
2070 goog.style.scrollIntoContainerView(goog.dom.getElement('item8'), container);
2071 assertEquals('scroll to item8', 100, container.scrollTop);
2072 goog.style.scrollIntoContainerView(goog.dom.getElement('item7'), container);
2073 assertEquals('item7 still visible', 100, container.scrollTop);
2074 goog.style.scrollIntoContainerView(goog.dom.getElement('item1'), container);
2075 assertEquals('scroll to item1', 17, container.scrollTop);
2076
2077 // Center the element in the first argument.
2078 goog.style.scrollIntoContainerView(
2079 goog.dom.getElement('item1'), container, true);
2080 assertEquals('center item1', 0, container.scrollTop);
2081 goog.style.scrollIntoContainerView(
2082 goog.dom.getElement('item4'), container, true);
2083 assertEquals('center item4', 48, container.scrollTop);
2084
2085 // The element is higher than the container.
2086 goog.dom.getElement('item3').style.height = '140px';
2087 goog.style.scrollIntoContainerView(goog.dom.getElement('item3'), container);
2088 assertEquals('show item3 with increased height', 59, container.scrollTop);
2089 goog.style.scrollIntoContainerView(
2090 goog.dom.getElement('item3'), container, true);
2091 assertEquals('center item3 with increased height', 87, container.scrollTop);
2092 goog.dom.getElement('item3').style.height = '';
2093
2094 // Scroll to non-integer position.
2095 goog.dom.getElement('item4').style.height = '21px';
2096 goog.style.scrollIntoContainerView(
2097 goog.dom.getElement('item4'), container, true);
2098 assertEquals('scroll position is rounded down', 48, container.scrollTop);
2099 goog.dom.getElement('item4').style.height = '';
2100 }
2101
2102 function testScrollIntoContainerViewStandard() {
2103 if (!goog.dom.isCss1CompatMode()) return;
2104
2105 var container = goog.dom.getElement('scrollable-container');
2106
2107 // Scroll the minimum amount to make the elements visible.
2108 goog.style.scrollIntoContainerView(goog.dom.getElement('item7'), container);
2109 assertEquals('scroll to item7', 115, container.scrollTop);
2110 goog.style.scrollIntoContainerView(goog.dom.getElement('item8'), container);
2111 assertEquals('scroll to item8', 148, container.scrollTop);
2112 goog.style.scrollIntoContainerView(goog.dom.getElement('item7'), container);
2113 assertEquals('item7 still visible', 148, container.scrollTop);
2114 goog.style.scrollIntoContainerView(goog.dom.getElement('item1'), container);
2115 assertEquals('scroll to item1', 17, container.scrollTop);
2116
2117 // Center the element in the first argument.
2118 goog.style.scrollIntoContainerView(
2119 goog.dom.getElement('item1'), container, true);
2120 assertEquals('center item1', 0, container.scrollTop);
2121 goog.style.scrollIntoContainerView(
2122 goog.dom.getElement('item4'), container, true);
2123 assertEquals('center item4', 66, container.scrollTop);
2124
2125 // The element is higher than the container.
2126 goog.dom.getElement('item3').style.height = '140px';
2127 goog.style.scrollIntoContainerView(goog.dom.getElement('item3'), container);
2128 assertEquals('show item3 with increased height', 83, container.scrollTop);
2129 goog.style.scrollIntoContainerView(
2130 goog.dom.getElement('item3'), container, true);
2131 assertEquals('center item3 with increased height', 93, container.scrollTop);
2132 goog.dom.getElement('item3').style.height = '';
2133
2134 // Scroll to non-integer position.
2135 goog.dom.getElement('item4').style.height = '21px';
2136 goog.style.scrollIntoContainerView(
2137 goog.dom.getElement('item4'), container, true);
2138 assertEquals('scroll position is rounded down', 66, container.scrollTop);
2139 goog.dom.getElement('item4').style.height = '';
2140 }
2141
2142 function testScrollIntoContainerViewSvg() {
2143 if (!goog.dom.isCss1CompatMode()) {
2144 return;
2145 }
2146
2147 var svgEl = document.createElementNS &&
2148 document.createElementNS('http://www.w3.org/2000/svg', 'svg');
2149 if (!svgEl || svgEl.getAttribute('transform') == '' ||
2150 (goog.userAgent.WEBKIT && !goog.userAgent.isVersionOrHigher(534.8))) {
2151 // SVG not supported, or getBoundingClientRect not supported on SVG
2152 // elements.
2153 return;
2154 }
2155
2156 var assertEqualsForSvgPos = function(expected, actual) {
2157 if (goog.userAgent.EDGE_OR_IE) {
2158 // The bounding size is 1 larger than the SVG element in IE. The scrollTop
2159 // value maybe 1 less or 1 more than the expected value depending on the
2160 // scroll direction.
2161 assertRoughlyEquals(expected, actual, 1);
2162 } else {
2163 assertEquals(expected, actual);
2164 }
2165 };
2166
2167 var svgItem1 = goog.dom.getElement('svg-item1');
2168 var svgItem2 = goog.dom.getElement('svg-item2');
2169 var svgItem3 = goog.dom.getElement('svg-item3');
2170
2171 // Scroll the minimum amount to make the elements visible.
2172 var container = goog.dom.getElement('svg-container');
2173 goog.style.scrollIntoContainerView(svgItem1, container);
2174 assertEquals(0, container.scrollTop);
2175 goog.style.scrollIntoContainerView(svgItem2, container);
2176 assertEqualsForSvgPos(50, container.scrollTop);
2177 goog.style.scrollIntoContainerView(svgItem3, container);
2178 assertEqualsForSvgPos(150, container.scrollTop);
2179 goog.style.scrollIntoContainerView(svgItem2, container);
2180 assertEqualsForSvgPos(100, container.scrollTop);
2181
2182 // Center the element in the first argument.
2183 goog.style.scrollIntoContainerView(svgItem2, container, true);
2184 assertEqualsForSvgPos(75, container.scrollTop);
2185 goog.style.scrollIntoContainerView(svgItem3, container, true);
2186 assertEqualsForSvgPos(175, container.scrollTop);
2187
2188 // The element is higher than the container.
2189 svgItem3.setAttribute('height', 200);
2190 goog.style.scrollIntoContainerView(svgItem3, container);
2191 assertEqualsForSvgPos(200, container.scrollTop);
2192 goog.style.scrollIntoContainerView(svgItem3, container, true);
2193 assertEqualsForSvgPos(225, container.scrollTop);
2194
2195 // Scroll to non-integer position.
2196 svgItem3.setAttribute('height', 75);
2197 goog.style.scrollIntoContainerView(svgItem3, container, true);
2198 // Scroll position is rounded down from 162.5
2199 assertEqualsForSvgPos(162, container.scrollTop);
2200 svgItem3.setAttribute('height', 100);
2201 }
2202
2203 function testOffsetParent() {
2204 var parent = goog.dom.getElement('offset-parent');
2205 var child = goog.dom.getElement('offset-child');
2206 assertEquals(parent, goog.style.getOffsetParent(child));
2207 }
2208
2209 function testOverflowOffsetParent() {
2210 var parent = goog.dom.getElement('offset-parent-overflow');
2211 var child = goog.dom.getElement('offset-child-overflow');
2212 assertEquals(parent, goog.style.getOffsetParent(child));
2213 }
2214
2215 function testShadowDomOffsetParent() {
2216 // Ignore browsers that don't support shadowDOM.
2217 if (!document.createShadowRoot) {
2218 return;
2219 }
2220
2221 var parent = goog.dom.createDom(goog.dom.TagName.DIV);
2222 parent.style.position = 'relative';
2223 var host = goog.dom.createDom(goog.dom.TagName.DIV);
2224 goog.dom.appendChild(parent, host);
2225 var root = host.createShadowRoot();
2226 var child = goog.dom.createDom(goog.dom.TagName.DIV);
2227 goog.dom.appendChild(root, child);
2228
2229 assertEquals(parent, goog.style.getOffsetParent(child));
2230 }
2231
2232 function testGetViewportPageOffset() {
2233 expectedFailures.expectFailureFor(
2234 goog.userAgent.IE && !goog.userAgent.isVersionOrHigher(10),
2235 'Test has been flaky for ie9-win7 and ie8-winxp image. Disabling. ' +
2236 'See b/22873770.');
2237
2238 try {
2239 var testViewport = goog.dom.getElement('test-viewport');
2240 testViewport.style.height = '5000px';
2241 testViewport.style.width = '5000px';
2242 var offset = goog.style.getViewportPageOffset(document);
2243 assertEquals(0, offset.x);
2244 assertEquals(0, offset.y);
2245
2246 window.scrollTo(0, 100);
2247 offset = goog.style.getViewportPageOffset(document);
2248 assertEquals(0, offset.x);
2249 assertEquals(100, offset.y);
2250
2251 window.scrollTo(100, 0);
2252 offset = goog.style.getViewportPageOffset(document);
2253 assertEquals(100, offset.x);
2254 assertEquals(0, offset.y);
2255 } catch (e) {
2256 expectedFailures.handleException(e);
2257 }
2258 }
2259
2260 function testGetsTranslation() {
2261 var element = document.getElementById('translation');
2262
2263 if (goog.userAgent.IE) {
2264 if (!goog.userAgent.isDocumentModeOrHigher(9) ||
2265 (!goog.dom.isCss1CompatMode() &&
2266 !goog.userAgent.isDocumentModeOrHigher(10))) {
2267 // 'CSS transforms were introduced in IE9, but only in standards mode
2268 // later browsers support the translations in quirks mode.
2269 return;
2270 }
2271 }
2272
2273 // First check the element is actually translated, and we haven't missed
2274 // one of the vendor-specific transform properties
2275 var position = goog.style.getClientPosition(element);
2276 var translation = goog.style.getCssTranslation(element);
2277 var expectedTranslation = new goog.math.Coordinate(20, 30);
2278
2279 assertEquals(30, position.x);
2280 assertRoughlyEquals(40, position.y, .1);
2281 assertObjectEquals(expectedTranslation, translation);
2282 }
2283
2284
2285 /**
2286 * Test browser detection for a user agent configuration.
2287 * @param {Array<number>} expectedAgents Array of expected userAgents.
2288 * @param {string} uaString User agent string.
2289 * @param {string=} opt_product Navigator product string.
2290 * @param {string=} opt_vendor Navigator vendor string.
2291 */
2292 function assertUserAgent(expectedAgents, uaString, opt_product, opt_vendor) {
2293 var mockNavigator =
2294 {'userAgent': uaString, 'product': opt_product, 'vendor': opt_vendor};
2295
2296 mockUserAgent.setNavigator(mockNavigator);
2297 mockUserAgent.setUserAgentString(uaString);
2298
2299 // Force User-Agent lib to reread the global userAgent.
2300 goog.labs.userAgent.util.setUserAgent(null);
2301
2302 goog.userAgentTestUtil.reinitializeUserAgent();
2303 for (var ua in goog.userAgentTestUtil.UserAgents) {
2304 var isExpected = goog.array.contains(
2305 expectedAgents, goog.userAgentTestUtil.UserAgents[ua]);
2306 assertEquals(
2307 isExpected, goog.userAgentTestUtil.getUserAgentDetected(
2308 goog.userAgentTestUtil.UserAgents[ua]));
2309 }
2310 }
2311
2312
2313 /**
2314 * Test for the proper vendor style name for a CSS property
2315 * with a vendor prefix for Webkit.
2316 */
2317 function testGetVendorStyleNameWebkit() {
2318 var mockElement = {'style': {'WebkitTransformOrigin': ''}};
2319
2320 assertUserAgent([goog.userAgentTestUtil.UserAgents.WEBKIT], 'WebKit');
2321 assertEquals(
2322 '-webkit-transform-origin',
2323 goog.style.getVendorStyleName_(mockElement, 'transform-origin'));
2324 }
2325
2326
2327 /**
2328 * Test for the proper vendor style name for a CSS property
2329 * when it exists without a vendor prefix for Webkit.
2330 */
2331 function testGetVendorStyleNameWebkitNoPrefix() {
2332 var mockElement = {
2333 'style': {'WebkitTransformOrigin': '', 'transformOrigin': ''}
2334 };
2335
2336 assertUserAgent([goog.userAgentTestUtil.UserAgents.WEBKIT], 'WebKit');
2337 assertEquals(
2338 'transform-origin',
2339 goog.style.getVendorStyleName_(mockElement, 'transform-origin'));
2340 }
2341
2342
2343 /**
2344 * Test for the proper vendor style name for a CSS property
2345 * with a vendor prefix for Gecko.
2346 */
2347 function testGetVendorStyleNameGecko() {
2348 var mockElement = {'style': {'MozTransformOrigin': ''}};
2349
2350 assertUserAgent([goog.userAgentTestUtil.UserAgents.GECKO], 'Gecko', 'Gecko');
2351 assertEquals(
2352 '-moz-transform-origin',
2353 goog.style.getVendorStyleName_(mockElement, 'transform-origin'));
2354 }
2355
2356
2357 /**
2358 * Test for the proper vendor style name for a CSS property
2359 * when it exists without a vendor prefix for Gecko.
2360 */
2361 function testGetVendorStyleNameGeckoNoPrefix() {
2362 var mockElement = {
2363 'style': {'MozTransformOrigin': '', 'transformOrigin': ''}
2364 };
2365
2366 assertUserAgent([goog.userAgentTestUtil.UserAgents.GECKO], 'Gecko', 'Gecko');
2367 assertEquals(
2368 'transform-origin',
2369 goog.style.getVendorStyleName_(mockElement, 'transform-origin'));
2370 }
2371
2372
2373 /**
2374 * Test for the proper vendor style name for a CSS property
2375 * with a vendor prefix for IE.
2376 */
2377 function testGetVendorStyleNameIE() {
2378 var mockElement = {'style': {'msTransformOrigin': ''}};
2379
2380 assertUserAgent([goog.userAgentTestUtil.UserAgents.IE], 'MSIE');
2381 assertEquals(
2382 '-ms-transform-origin',
2383 goog.style.getVendorStyleName_(mockElement, 'transform-origin'));
2384 }
2385
2386
2387 /**
2388 * Test for the proper vendor style name for a CSS property
2389 * when it exists without a vendor prefix for IE.
2390 */
2391 function testGetVendorStyleNameIENoPrefix() {
2392 var mockElement = {'style': {'msTransformOrigin': '', 'transformOrigin': ''}};
2393
2394 assertUserAgent([goog.userAgentTestUtil.UserAgents.IE], 'MSIE');
2395 assertEquals(
2396 'transform-origin',
2397 goog.style.getVendorStyleName_(mockElement, 'transform-origin'));
2398 }
2399
2400
2401 /**
2402 * Test for the proper vendor style name for a CSS property
2403 * with a vendor prefix for Opera.
2404 */
2405 function testGetVendorStyleNameOpera() {
2406 var mockElement = {'style': {'OTransformOrigin': ''}};
2407
2408 assertUserAgent([goog.userAgentTestUtil.UserAgents.OPERA], 'Opera');
2409 assertEquals(
2410 '-o-transform-origin',
2411 goog.style.getVendorStyleName_(mockElement, 'transform-origin'));
2412 }
2413
2414
2415 /**
2416 * Test for the proper vendor style name for a CSS property
2417 * when it exists without a vendor prefix for Opera.
2418 */
2419 function testGetVendorStyleNameOperaNoPrefix() {
2420 var mockElement = {'style': {'OTransformOrigin': '', 'transformOrigin': ''}};
2421
2422 assertUserAgent([goog.userAgentTestUtil.UserAgents.OPERA], 'Opera');
2423 assertEquals(
2424 'transform-origin',
2425 goog.style.getVendorStyleName_(mockElement, 'transform-origin'));
2426 }
2427
2428
2429 /**
2430 * Test for the proper vendor style name for a CSS property
2431 * with a vendor prefix for Webkit.
2432 */
2433 function testGetVendorJsStyleNameWebkit() {
2434 var mockElement = {'style': {'WebkitTransformOrigin': ''}};
2435
2436 assertUserAgent([goog.userAgentTestUtil.UserAgents.WEBKIT], 'WebKit');
2437 assertEquals(
2438 'WebkitTransformOrigin',
2439 goog.style.getVendorJsStyleName_(mockElement, 'transform-origin'));
2440 }
2441
2442
2443 /**
2444 * Test for the proper vendor style name for a CSS property
2445 * when it exists without a vendor prefix for Webkit.
2446 */
2447 function testGetVendorJsStyleNameWebkitNoPrefix() {
2448 var mockElement = {
2449 'style': {'WebkitTransformOrigin': '', 'transformOrigin': ''}
2450 };
2451
2452 assertUserAgent([goog.userAgentTestUtil.UserAgents.WEBKIT], 'WebKit');
2453 assertEquals(
2454 'transformOrigin',
2455 goog.style.getVendorJsStyleName_(mockElement, 'transform-origin'));
2456 }
2457
2458
2459 /**
2460 * Test for the proper vendor style name for a CSS property
2461 * with a vendor prefix for Gecko.
2462 */
2463 function testGetVendorJsStyleNameGecko() {
2464 var mockElement = {'style': {'MozTransformOrigin': ''}};
2465
2466 assertUserAgent([goog.userAgentTestUtil.UserAgents.GECKO], 'Gecko', 'Gecko');
2467 assertEquals(
2468 'MozTransformOrigin',
2469 goog.style.getVendorJsStyleName_(mockElement, 'transform-origin'));
2470 }
2471
2472
2473 /**
2474 * Test for the proper vendor style name for a CSS property
2475 * when it exists without a vendor prefix for Gecko.
2476 */
2477 function testGetVendorJsStyleNameGeckoNoPrefix() {
2478 var mockElement = {
2479 'style': {'MozTransformOrigin': '', 'transformOrigin': ''}
2480 };
2481
2482 assertUserAgent([goog.userAgentTestUtil.UserAgents.GECKO], 'Gecko', 'Gecko');
2483 assertEquals(
2484 'transformOrigin',
2485 goog.style.getVendorJsStyleName_(mockElement, 'transform-origin'));
2486 }
2487
2488
2489 /**
2490 * Test for the proper vendor style name for a CSS property
2491 * with a vendor prefix for IE.
2492 */
2493 function testGetVendorJsStyleNameIE() {
2494 var mockElement = {'style': {'msTransformOrigin': ''}};
2495
2496 assertUserAgent([goog.userAgentTestUtil.UserAgents.IE], 'MSIE');
2497 assertEquals(
2498 'msTransformOrigin',
2499 goog.style.getVendorJsStyleName_(mockElement, 'transform-origin'));
2500 }
2501
2502
2503 /**
2504 * Test for the proper vendor style name for a CSS property
2505 * when it exists without a vendor prefix for IE.
2506 */
2507 function testGetVendorJsStyleNameIENoPrefix() {
2508 var mockElement = {'style': {'msTransformOrigin': '', 'transformOrigin': ''}};
2509
2510 assertUserAgent([goog.userAgentTestUtil.UserAgents.IE], 'MSIE');
2511 assertEquals(
2512 'transformOrigin',
2513 goog.style.getVendorJsStyleName_(mockElement, 'transform-origin'));
2514 }
2515
2516
2517 /**
2518 * Test for the proper vendor style name for a CSS property
2519 * with a vendor prefix for Opera.
2520 */
2521 function testGetVendorJsStyleNameOpera() {
2522 var mockElement = {'style': {'OTransformOrigin': ''}};
2523
2524 assertUserAgent([goog.userAgentTestUtil.UserAgents.OPERA], 'Opera');
2525 assertEquals(
2526 'OTransformOrigin',
2527 goog.style.getVendorJsStyleName_(mockElement, 'transform-origin'));
2528 }
2529
2530
2531 /**
2532 * Test for the proper vendor style name for a CSS property
2533 * when it exists without a vendor prefix for Opera.
2534 */
2535 function testGetVendorJsStyleNameOperaNoPrefix() {
2536 var mockElement = {'style': {'OTransformOrigin': '', 'transformOrigin': ''}};
2537
2538 assertUserAgent([goog.userAgentTestUtil.UserAgents.OPERA], 'Opera');
2539 assertEquals(
2540 'transformOrigin',
2541 goog.style.getVendorJsStyleName_(mockElement, 'transform-origin'));
2542 }
2543
2544
2545 /**
2546 * Test for the setting a style name for a CSS property
2547 * with a vendor prefix for Webkit.
2548 */
2549 function testSetVendorStyleWebkit() {
2550 var mockElement = {'style': {'WebkitTransform': ''}};
2551 var styleValue = 'translate3d(0,0,0)';
2552
2553 assertUserAgent([goog.userAgentTestUtil.UserAgents.WEBKIT], 'WebKit');
2554 goog.style.setStyle(mockElement, 'transform', styleValue);
2555 assertEquals(styleValue, mockElement.style.WebkitTransform);
2556 }
2557
2558
2559 /**
2560 * Test for the setting a style name for a CSS property
2561 * with a vendor prefix for Mozilla.
2562 */
2563 function testSetVendorStyleGecko() {
2564 var mockElement = {'style': {'MozTransform': ''}};
2565 var styleValue = 'translate3d(0,0,0)';
2566
2567 assertUserAgent([goog.userAgentTestUtil.UserAgents.GECKO], 'Gecko', 'Gecko');
2568 goog.style.setStyle(mockElement, 'transform', styleValue);
2569 assertEquals(styleValue, mockElement.style.MozTransform);
2570 }
2571
2572
2573 /**
2574 * Test for the setting a style name for a CSS property
2575 * with a vendor prefix for IE.
2576 */
2577 function testSetVendorStyleIE() {
2578 var mockElement = {'style': {'msTransform': ''}};
2579 var styleValue = 'translate3d(0,0,0)';
2580
2581 assertUserAgent([goog.userAgentTestUtil.UserAgents.IE], 'MSIE');
2582 goog.style.setStyle(mockElement, 'transform', styleValue);
2583 assertEquals(styleValue, mockElement.style.msTransform);
2584 }
2585
2586
2587 /**
2588 * Test for the setting a style name for a CSS property
2589 * with a vendor prefix for Opera.
2590 */
2591 function testSetVendorStyleOpera() {
2592 var mockElement = {'style': {'OTransform': ''}};
2593 var styleValue = 'translate3d(0,0,0)';
2594
2595 assertUserAgent([goog.userAgentTestUtil.UserAgents.OPERA], 'Opera');
2596 goog.style.setStyle(mockElement, 'transform', styleValue);
2597 assertEquals(styleValue, mockElement.style.OTransform);
2598 }
2599
2600
2601 /**
2602 * Test for the getting a style name for a CSS property
2603 * with a vendor prefix for Webkit.
2604 */
2605 function testGetVendorStyleWebkit() {
2606 var mockElement = {'style': {'WebkitTransform': ''}};
2607 var styleValue = 'translate3d(0,0,0)';
2608
2609 assertUserAgent([goog.userAgentTestUtil.UserAgents.WEBKIT], 'WebKit');
2610 goog.style.setStyle(mockElement, 'transform', styleValue);
2611 assertEquals(styleValue, goog.style.getStyle(mockElement, 'transform'));
2612 }
2613
2614
2615 /**
2616 * Test for the getting a style name for a CSS property
2617 * with a vendor prefix for Mozilla.
2618 */
2619 function testGetVendorStyleGecko() {
2620 var mockElement = {'style': {'MozTransform': ''}};
2621 var styleValue = 'translate3d(0,0,0)';
2622
2623 assertUserAgent([goog.userAgentTestUtil.UserAgents.GECKO], 'Gecko', 'Gecko');
2624 goog.style.setStyle(mockElement, 'transform', styleValue);
2625 assertEquals(styleValue, goog.style.getStyle(mockElement, 'transform'));
2626 }
2627
2628
2629 /**
2630 * Test for the getting a style name for a CSS property
2631 * with a vendor prefix for IE.
2632 */
2633 function testGetVendorStyleIE() {
2634 var mockElement = {'style': {'msTransform': ''}};
2635 var styleValue = 'translate3d(0,0,0)';
2636
2637 assertUserAgent([goog.userAgentTestUtil.UserAgents.IE], 'MSIE');
2638 goog.style.setStyle(mockElement, 'transform', styleValue);
2639 assertEquals(styleValue, goog.style.getStyle(mockElement, 'transform'));
2640 }
2641
2642
2643 /**
2644 * Test for the getting a style name for a CSS property
2645 * with a vendor prefix for Opera.
2646 */
2647 function testGetVendorStyleOpera() {
2648 var mockElement = {'style': {'OTransform': ''}};
2649 var styleValue = 'translate3d(0,0,0)';
2650
2651 assertUserAgent([goog.userAgentTestUtil.UserAgents.OPERA], 'Opera');
2652 goog.style.setStyle(mockElement, 'transform', styleValue);
2653 assertEquals(styleValue, goog.style.getStyle(mockElement, 'transform'));
2654 }
2655
2656 function testParseStyleAttributeWithColon() {
2657 // Regression test for https://github.com/google/closure-library/issues/127.
2658 var cssObj = goog.style.parseStyleAttribute(
2659 'left: 0px; text-align: center; background-image: ' +
2660 'url(http://www.google.ca/Test.gif); -ms-filter: ' +
2661 'progid:DXImageTransform.Microsoft.MotionBlur(strength=50), ' +
2662 'progid:DXImageTransform.Microsoft.BasicImage(mirror=1);');
2663 assertEquals('url(http://www.google.ca/Test.gif)', cssObj.backgroundImage);
2664 assertEquals(
2665 'progid:DXImageTransform.Microsoft.MotionBlur(strength=50), ' +
2666 'progid:DXImageTransform.Microsoft.BasicImage(mirror=1)',
2667 cssObj.MsFilter);
2668 }