vendorPrefix.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for
  2. * full list of contributors). Published under the 2-clause BSD license.
  3. * See license.txt in the OpenLayers distribution or repository for the
  4. * full text of the license. */
  5. /**
  6. * @requires OpenLayers/SingleFile.js
  7. */
  8. OpenLayers.Util = OpenLayers.Util || {};
  9. /**
  10. * Namespace: OpenLayers.Util.vendorPrefix
  11. * A collection of utility functions to detect vendor prefixed features
  12. */
  13. OpenLayers.Util.vendorPrefix = (function() {
  14. "use strict";
  15. var VENDOR_PREFIXES = ["", "O", "ms", "Moz", "Webkit"],
  16. divStyle = document.createElement("div").style,
  17. cssCache = {},
  18. jsCache = {};
  19. /**
  20. * Function: domToCss
  21. * Converts a upper camel case DOM style property name to a CSS property
  22. * i.e. transformOrigin -> transform-origin
  23. * or WebkitTransformOrigin -> -webkit-transform-origin
  24. *
  25. * Parameters:
  26. * prefixedDom - {String} The property to convert
  27. *
  28. * Returns:
  29. * {String} The CSS property
  30. */
  31. function domToCss(prefixedDom) {
  32. if (!prefixedDom) { return null; }
  33. return prefixedDom.
  34. replace(/([A-Z])/g, function(c) { return "-" + c.toLowerCase(); }).
  35. replace(/^ms-/, "-ms-");
  36. }
  37. /**
  38. * APIMethod: css
  39. * Detect which property is used for a CSS property
  40. *
  41. * Parameters:
  42. * property - {String} The standard (unprefixed) CSS property name
  43. *
  44. * Returns:
  45. * {String} The standard CSS property, prefixed property or null if not
  46. * supported
  47. */
  48. function css(property) {
  49. if (cssCache[property] === undefined) {
  50. var domProperty = property.
  51. replace(/(-[\s\S])/g, function(c) { return c.charAt(1).toUpperCase(); });
  52. var prefixedDom = style(domProperty);
  53. cssCache[property] = domToCss(prefixedDom);
  54. }
  55. return cssCache[property];
  56. }
  57. /**
  58. * APIMethod: js
  59. * Detect which property is used for a JS property/method
  60. *
  61. * Parameters:
  62. * obj - {Object} The object to test on
  63. * property - {String} The standard (unprefixed) JS property name
  64. *
  65. * Returns:
  66. * {String} The standard JS property, prefixed property or null if not
  67. * supported
  68. */
  69. function js(obj, property) {
  70. if (jsCache[property] === undefined) {
  71. var tmpProp,
  72. i = 0,
  73. l = VENDOR_PREFIXES.length,
  74. prefix,
  75. isStyleObj = (typeof obj.cssText !== "undefined");
  76. jsCache[property] = null;
  77. for(; i<l; i++) {
  78. prefix = VENDOR_PREFIXES[i];
  79. if(prefix) {
  80. if (!isStyleObj) {
  81. // js prefix should be lower-case, while style
  82. // properties have upper case on first character
  83. prefix = prefix.toLowerCase();
  84. }
  85. tmpProp = prefix + property.charAt(0).toUpperCase() + property.slice(1);
  86. } else {
  87. tmpProp = property;
  88. }
  89. if(obj[tmpProp] !== undefined) {
  90. jsCache[property] = tmpProp;
  91. break;
  92. }
  93. }
  94. }
  95. return jsCache[property];
  96. }
  97. /**
  98. * APIMethod: style
  99. * Detect which property is used for a DOM style property
  100. *
  101. * Parameters:
  102. * property - {String} The standard (unprefixed) style property name
  103. *
  104. * Returns:
  105. * {String} The standard style property, prefixed property or null if not
  106. * supported
  107. */
  108. function style(property) {
  109. return js(divStyle, property);
  110. }
  111. return {
  112. css: css,
  113. js: js,
  114. style: style,
  115. // used for testing
  116. cssCache: cssCache,
  117. jsCache: jsCache
  118. };
  119. }());