style.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. * @fileoverview Utilities for inspecting page layout. This is a port of
  16. * http://go/layoutbot.java
  17. * See {@link http://go/layouttesting}.
  18. */
  19. goog.setTestOnly('goog.testing.style');
  20. goog.provide('goog.testing.style');
  21. goog.require('goog.dom');
  22. goog.require('goog.math.Rect');
  23. goog.require('goog.style');
  24. /**
  25. * Determines whether the bounding rectangles of the given elements intersect.
  26. * @param {Element} element The first element.
  27. * @param {Element} otherElement The second element.
  28. * @return {boolean} Whether the bounding rectangles of the given elements
  29. * intersect.
  30. */
  31. goog.testing.style.intersects = function(element, otherElement) {
  32. var elementRect = goog.style.getBounds(element);
  33. var otherElementRect = goog.style.getBounds(otherElement);
  34. return goog.math.Rect.intersects(elementRect, otherElementRect);
  35. };
  36. /**
  37. * Determines whether the element has visible dimensions, i.e. x > 0 && y > 0.
  38. * @param {Element} element The element to check.
  39. * @return {boolean} Whether the element has visible dimensions.
  40. */
  41. goog.testing.style.hasVisibleDimensions = function(element) {
  42. var elSize = goog.style.getSize(element);
  43. var shortest = elSize.getShortest();
  44. if (shortest <= 0) {
  45. return false;
  46. }
  47. return true;
  48. };
  49. /**
  50. * Determines whether the CSS style of the element renders it visible.
  51. * @param {!Element} element The element to check.
  52. * @return {boolean} Whether the CSS style of the element renders it visible.
  53. */
  54. goog.testing.style.isVisible = function(element) {
  55. var visibilityStyle =
  56. goog.testing.style.getAvailableStyle_(element, 'visibility');
  57. var displayStyle = goog.testing.style.getAvailableStyle_(element, 'display');
  58. return (visibilityStyle != 'hidden' && displayStyle != 'none');
  59. };
  60. /**
  61. * Test whether the given element is on screen.
  62. * @param {!Element} el The element to test.
  63. * @return {boolean} Whether the element is on the screen.
  64. */
  65. goog.testing.style.isOnScreen = function(el) {
  66. var doc = goog.dom.getDomHelper(el).getDocument();
  67. var viewport = goog.style.getVisibleRectForElement(doc.body);
  68. var viewportRect = goog.math.Rect.createFromBox(viewport);
  69. return goog.dom.contains(doc, el) &&
  70. goog.style.getBounds(el).intersects(viewportRect);
  71. };
  72. /**
  73. * This is essentially goog.style.getStyle_. goog.style.getStyle_ is private
  74. * and is not a recommended way for general purpose style extractor. For the
  75. * purposes of layout testing, we only use this function for retrieving
  76. * 'visiblity' and 'display' style.
  77. * @param {!Element} element The element to retrieve the style from.
  78. * @param {string} style Style property name.
  79. * @return {string} Style value.
  80. * @private
  81. */
  82. goog.testing.style.getAvailableStyle_ = function(element, style) {
  83. return goog.style.getComputedStyle(element, style) ||
  84. goog.style.getCascadedStyle(element, style) ||
  85. goog.style.getStyle(element, style);
  86. };