getStyleProperty.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. 'use strict';
  2. /**
  3. * Copyright (c) 2013-present, Facebook, Inc.
  4. *
  5. * This source code is licensed under the MIT license found in the
  6. * LICENSE file in the root directory of this source tree.
  7. *
  8. * @typechecks
  9. */
  10. var camelize = require('./camelize');
  11. var 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. var computedStyle = void 0;
  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;