You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ComputedStyle.java 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.client;
  17. import com.google.gwt.core.client.JavaScriptObject;
  18. import com.google.gwt.dom.client.Element;
  19. public class ComputedStyle {
  20. protected final JavaScriptObject computedStyle;
  21. private final Element elem;
  22. /**
  23. * Gets this element's computed style object which can be used to gather
  24. * information about the current state of the rendered node.
  25. * <p>
  26. * Note that this method is expensive. Wherever possible, reuse the returned
  27. * object.
  28. *
  29. * @param elem
  30. * the element
  31. * @return the computed style
  32. */
  33. public ComputedStyle(Element elem) {
  34. computedStyle = getComputedStyle(elem);
  35. this.elem = elem;
  36. }
  37. private static native JavaScriptObject getComputedStyle(Element elem)
  38. /*-{
  39. if(elem.nodeType != 1) {
  40. return {};
  41. }
  42. if($wnd.document.defaultView && $wnd.document.defaultView.getComputedStyle) {
  43. return $wnd.document.defaultView.getComputedStyle(elem, null);
  44. }
  45. if(elem.currentStyle) {
  46. return elem.currentStyle;
  47. }
  48. }-*/;
  49. /**
  50. *
  51. * @param name
  52. * name of the CSS property in camelCase
  53. * @return the value of the property, normalized for across browsers (each
  54. * browser returns pixel values whenever possible).
  55. */
  56. public final native String getProperty(String name)
  57. /*-{
  58. var cs = this.@com.vaadin.client.ComputedStyle::computedStyle;
  59. var elem = this.@com.vaadin.client.ComputedStyle::elem;
  60. // Border values need to be checked separately. The width might have a
  61. // meaningful value even if the border style is "none". In that case the
  62. // value should be 0.
  63. if(name.indexOf("border") > -1 && name.indexOf("Width") > -1) {
  64. var borderStyleProp = name.substring(0,name.length-5) + "Style";
  65. if(cs.getPropertyValue)
  66. var borderStyle = cs.getPropertyValue(borderStyleProp);
  67. else // IE
  68. var borderStyle = cs[borderStyleProp];
  69. if(borderStyle == "none")
  70. return "0px";
  71. }
  72. if(cs.getPropertyValue) {
  73. // Convert name to dashed format
  74. name = name.replace(/([A-Z])/g, "-$1").toLowerCase();
  75. var ret = cs.getPropertyValue(name);
  76. } else {
  77. var ret = cs[name];
  78. var style = elem.style;
  79. // From the awesome hack by Dean Edwards
  80. // http://erik.eae.net/archives/2007/07/27/18.54.15/#comment-102291
  81. // If we're not dealing with a regular pixel number
  82. // but a number that has a weird ending, we need to convert it to pixels
  83. if ( !/^\d+(px)?$/i.test( ret ) && /^\d/.test( ret ) ) {
  84. // Remember the original values
  85. var left = style.left, rsLeft = elem.runtimeStyle.left;
  86. // Put in the new values to get a computed value out
  87. elem.runtimeStyle.left = cs.left;
  88. style.left = ret || 0;
  89. ret = style.pixelLeft + "px";
  90. // Revert the changed values
  91. style.left = left;
  92. elem.runtimeStyle.left = rsLeft;
  93. }
  94. }
  95. // Normalize margin values. This is not totally valid, but in most cases
  96. // it is what the user wants to know.
  97. if(name.indexOf("margin") > -1 && ret == "auto") {
  98. return "0px";
  99. }
  100. // Some browsers return undefined width and height values as "auto", so
  101. // we need to retrieve those ourselves.
  102. if (name == "width" && ret == "auto") {
  103. ret = elem.clientWidth + "px";
  104. } else if (name == "height" && ret == "auto") {
  105. ret = elem.clientHeight + "px";
  106. }
  107. return ret;
  108. }-*/;
  109. public final int getIntProperty(String name) {
  110. Profiler.enter("ComputedStyle.getIntProperty");
  111. String value = getProperty(name);
  112. int result = parseIntNative(value);
  113. Profiler.leave("ComputedStyle.getIntProperty");
  114. return result;
  115. }
  116. /**
  117. * Get current margin values from the DOM. The array order is the default
  118. * CSS order: top, right, bottom, left.
  119. */
  120. public final int[] getMargin() {
  121. int[] margin = { 0, 0, 0, 0 };
  122. margin[0] = getIntProperty("marginTop");
  123. margin[1] = getIntProperty("marginRight");
  124. margin[2] = getIntProperty("marginBottom");
  125. margin[3] = getIntProperty("marginLeft");
  126. return margin;
  127. }
  128. /**
  129. * Get current padding values from the DOM. The array order is the default
  130. * CSS order: top, right, bottom, left.
  131. */
  132. public final int[] getPadding() {
  133. int[] padding = { 0, 0, 0, 0 };
  134. padding[0] = getIntProperty("paddingTop");
  135. padding[1] = getIntProperty("paddingRight");
  136. padding[2] = getIntProperty("paddingBottom");
  137. padding[3] = getIntProperty("paddingLeft");
  138. return padding;
  139. }
  140. /**
  141. * Get current border values from the DOM. The array order is the default
  142. * CSS order: top, right, bottom, left.
  143. */
  144. public final int[] getBorder() {
  145. int[] border = { 0, 0, 0, 0 };
  146. border[0] = getIntProperty("borderTopWidth");
  147. border[1] = getIntProperty("borderRightWidth");
  148. border[2] = getIntProperty("borderBottomWidth");
  149. border[3] = getIntProperty("borderLeftWidth");
  150. return border;
  151. }
  152. /**
  153. * Takes a String value e.g. "12px" and parses that to Integer 12.
  154. *
  155. * @param String
  156. * a value starting with a number
  157. * @return Integer the value from the string before any non-numeric
  158. * characters. If the value cannot be parsed to a number, returns
  159. * <code>null</code>.
  160. *
  161. * @deprecated Since 7.1.4, the method {@link #parseIntNative(String)} is
  162. * used internally and this method does not belong in the public
  163. * API of {@link ComputedStyle}. {@link #parseInt(String)} might
  164. * be removed or moved to a utility class in future versions.
  165. */
  166. @Deprecated
  167. public static native Integer parseInt(final String value)
  168. /*-{
  169. var number = parseInt(value, 10);
  170. if (isNaN(number))
  171. return null;
  172. else
  173. // $entry not needed as function is not exported
  174. return @java.lang.Integer::valueOf(I)(number);
  175. }-*/;
  176. /**
  177. * Takes a String value e.g. "12px" and parses that to int 12.
  178. *
  179. * <p>
  180. * This method returns 0 for <code>NaN</code>.
  181. *
  182. * @param String
  183. * a value starting with a number
  184. * @return int the value from the string before any non-numeric characters.
  185. * If the value cannot be parsed to a number, returns 0.
  186. */
  187. private static native int parseIntNative(final String value)
  188. /*-{
  189. var number = parseInt(value, 10);
  190. if (isNaN(number))
  191. return 0;
  192. else
  193. return number;
  194. }-*/;
  195. }