Debian Codegraph @

closure-library/closure/goog/testing/style/style.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 License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 /**
16 * @fileoverview Utilities for inspecting page layout. This is a port of
17 * http://go/layoutbot.java
18 * See {@link http://go/layouttesting}.
19 */
20
21 goog.setTestOnly('goog.testing.style');
22 goog.provide('goog.testing.style');
23
24 goog.require('goog.dom');
25 goog.require('goog.math.Rect');
26 goog.require('goog.style');
27
28
29 /**
30 * Determines whether the bounding rectangles of the given elements intersect.
31 * @param {Element} element The first element.
32 * @param {Element} otherElement The second element.
33 * @return {boolean} Whether the bounding rectangles of the given elements
34 * intersect.
35 */
36 goog.testing.style.intersects = function(element, otherElement) {
37 var elementRect = goog.style.getBounds(element);
38 var otherElementRect = goog.style.getBounds(otherElement);
39 return goog.math.Rect.intersects(elementRect, otherElementRect);
40 };
41
42
43 /**
44 * Determines whether the element has visible dimensions, i.e. x > 0 && y > 0.
45 * @param {Element} element The element to check.
46 * @return {boolean} Whether the element has visible dimensions.
47 */
48 goog.testing.style.hasVisibleDimensions = function(element) {
49 var elSize = goog.style.getSize(element);
50 var shortest = elSize.getShortest();
51 if (shortest <= 0) {
52 return false;
53 }
54
55 return true;
56 };
57
58
59 /**
60 * Determines whether the CSS style of the element renders it visible.
61 * @param {!Element} element The element to check.
62 * @return {boolean} Whether the CSS style of the element renders it visible.
63 */
64 goog.testing.style.isVisible = function(element) {
65 var visibilityStyle =
66 goog.testing.style.getAvailableStyle_(element, 'visibility');
67 var displayStyle = goog.testing.style.getAvailableStyle_(element, 'display');
68
69 return (visibilityStyle != 'hidden' && displayStyle != 'none');
70 };
71
72
73 /**
74 * Test whether the given element is on screen.
75 * @param {!Element} el The element to test.
76 * @return {boolean} Whether the element is on the screen.
77 */
78 goog.testing.style.isOnScreen = function(el) {
79 var doc = goog.dom.getDomHelper(el).getDocument();
80 var viewport = goog.style.getVisibleRectForElement(doc.body);
81 var viewportRect = goog.math.Rect.createFromBox(viewport);
82 return goog.dom.contains(doc, el) &&
83 goog.style.getBounds(el).intersects(viewportRect);
84 };
85
86
87 /**
88 * This is essentially goog.style.getStyle_. goog.style.getStyle_ is private
89 * and is not a recommended way for general purpose style extractor. For the
90 * purposes of layout testing, we only use this function for retrieving
91 * 'visiblity' and 'display' style.
92 * @param {!Element} element The element to retrieve the style from.
93 * @param {string} style Style property name.
94 * @return {string} Style value.
95 * @private
96 */
97 goog.testing.style.getAvailableStyle_ = function(element, style) {
98 return goog.style.getComputedStyle(element, style) ||
99 goog.style.getCascadedStyle(element, style) ||
100 goog.style.getStyle(element, style);
101 };