getStyleProperty.js.flow 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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)
  13. /*?string*/
  14. {
  15. return value == null ? value : String(value);
  16. }
  17. function getStyleProperty(
  18. /*DOMNode*/
  19. node,
  20. /*string*/
  21. name)
  22. /*?string*/
  23. {
  24. let computedStyle; // W3C Standard
  25. if (window.getComputedStyle) {
  26. // In certain cases such as within an iframe in FF3, this returns null.
  27. computedStyle = window.getComputedStyle(node, null);
  28. if (computedStyle) {
  29. return asString(computedStyle.getPropertyValue(hyphenate(name)));
  30. }
  31. } // Safari
  32. if (document.defaultView && document.defaultView.getComputedStyle) {
  33. computedStyle = document.defaultView.getComputedStyle(node, null); // A Safari bug causes this to return null for `display: none` elements.
  34. if (computedStyle) {
  35. return asString(computedStyle.getPropertyValue(hyphenate(name)));
  36. }
  37. if (name === 'display') {
  38. return 'none';
  39. }
  40. } // Internet Explorer
  41. if (node.currentStyle) {
  42. if (name === 'float') {
  43. return asString(node.currentStyle.cssFloat || node.currentStyle.styleFloat);
  44. }
  45. return asString(node.currentStyle[camelize(name)]);
  46. }
  47. return asString(node.style && node.style[camelize(name)]);
  48. }
  49. module.exports = getStyleProperty;