getStyleProperty.js.flow 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /**
  2. * Copyright (c) 2013-present, Facebook, Inc.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @providesModule getStyleProperty
  8. * @typechecks
  9. */
  10. const camelize = require('./camelize');
  11. const hyphenate = require('./hyphenate');
  12. function asString(value) /*?string*/{
  13. return value == null ? value : String(value);
  14. }
  15. function getStyleProperty( /*DOMNode*/node, /*string*/name) /*?string*/{
  16. let computedStyle;
  17. // W3C Standard
  18. if (window.getComputedStyle) {
  19. // In certain cases such as within an iframe in FF3, this returns null.
  20. computedStyle = window.getComputedStyle(node, null);
  21. if (computedStyle) {
  22. return asString(computedStyle.getPropertyValue(hyphenate(name)));
  23. }
  24. }
  25. // Safari
  26. if (document.defaultView && document.defaultView.getComputedStyle) {
  27. computedStyle = document.defaultView.getComputedStyle(node, null);
  28. // A Safari bug causes this to return null for `display: none` elements.
  29. if (computedStyle) {
  30. return asString(computedStyle.getPropertyValue(hyphenate(name)));
  31. }
  32. if (name === 'display') {
  33. return 'none';
  34. }
  35. }
  36. // Internet Explorer
  37. if (node.currentStyle) {
  38. if (name === 'float') {
  39. return asString(node.currentStyle.cssFloat || node.currentStyle.styleFloat);
  40. }
  41. return asString(node.currentStyle[camelize(name)]);
  42. }
  43. return asString(node.style && node.style[camelize(name)]);
  44. }
  45. module.exports = getStyleProperty;