vendor.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2012 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Vendor prefix getters.
  16. */
  17. goog.provide('goog.dom.vendor');
  18. goog.require('goog.string');
  19. goog.require('goog.userAgent');
  20. /**
  21. * Returns the JS vendor prefix used in CSS properties. Different vendors
  22. * use different methods of changing the case of the property names.
  23. *
  24. * @return {?string} The JS vendor prefix or null if there is none.
  25. */
  26. goog.dom.vendor.getVendorJsPrefix = function() {
  27. if (goog.userAgent.WEBKIT) {
  28. return 'Webkit';
  29. } else if (goog.userAgent.GECKO) {
  30. return 'Moz';
  31. } else if (goog.userAgent.IE) {
  32. return 'ms';
  33. } else if (goog.userAgent.OPERA) {
  34. return 'O';
  35. }
  36. return null;
  37. };
  38. /**
  39. * Returns the vendor prefix used in CSS properties.
  40. *
  41. * @return {?string} The vendor prefix or null if there is none.
  42. */
  43. goog.dom.vendor.getVendorPrefix = function() {
  44. if (goog.userAgent.WEBKIT) {
  45. return '-webkit';
  46. } else if (goog.userAgent.GECKO) {
  47. return '-moz';
  48. } else if (goog.userAgent.IE) {
  49. return '-ms';
  50. } else if (goog.userAgent.OPERA) {
  51. return '-o';
  52. }
  53. return null;
  54. };
  55. /**
  56. * @param {string} propertyName A property name.
  57. * @param {!Object=} opt_object If provided, we verify if the property exists in
  58. * the object.
  59. * @return {?string} A vendor prefixed property name, or null if it does not
  60. * exist.
  61. */
  62. goog.dom.vendor.getPrefixedPropertyName = function(propertyName, opt_object) {
  63. // We first check for a non-prefixed property, if available.
  64. if (opt_object && propertyName in opt_object) {
  65. return propertyName;
  66. }
  67. var prefix = goog.dom.vendor.getVendorJsPrefix();
  68. if (prefix) {
  69. prefix = prefix.toLowerCase();
  70. var prefixedPropertyName = prefix + goog.string.toTitleCase(propertyName);
  71. return (!goog.isDef(opt_object) || prefixedPropertyName in opt_object) ?
  72. prefixedPropertyName :
  73. null;
  74. }
  75. return null;
  76. };
  77. /**
  78. * @param {string} eventType An event type.
  79. * @return {string} A lower-cased vendor prefixed event type.
  80. */
  81. goog.dom.vendor.getPrefixedEventType = function(eventType) {
  82. var prefix = goog.dom.vendor.getVendorJsPrefix() || '';
  83. return (prefix + eventType).toLowerCase();
  84. };