lib/goog/dom/vendor.js

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/**
16 * @fileoverview Vendor prefix getters.
17 */
18
19goog.provide('goog.dom.vendor');
20
21goog.require('goog.string');
22goog.require('goog.userAgent');
23
24
25/**
26 * Returns the JS vendor prefix used in CSS properties. Different vendors
27 * use different methods of changing the case of the property names.
28 *
29 * @return {?string} The JS vendor prefix or null if there is none.
30 */
31goog.dom.vendor.getVendorJsPrefix = function() {
32 if (goog.userAgent.WEBKIT) {
33 return 'Webkit';
34 } else if (goog.userAgent.GECKO) {
35 return 'Moz';
36 } else if (goog.userAgent.IE) {
37 return 'ms';
38 } else if (goog.userAgent.OPERA) {
39 return 'O';
40 }
41
42 return null;
43};
44
45
46/**
47 * Returns the vendor prefix used in CSS properties.
48 *
49 * @return {?string} The vendor prefix or null if there is none.
50 */
51goog.dom.vendor.getVendorPrefix = function() {
52 if (goog.userAgent.WEBKIT) {
53 return '-webkit';
54 } else if (goog.userAgent.GECKO) {
55 return '-moz';
56 } else if (goog.userAgent.IE) {
57 return '-ms';
58 } else if (goog.userAgent.OPERA) {
59 return '-o';
60 }
61
62 return null;
63};
64
65
66/**
67 * @param {string} propertyName A property name.
68 * @param {!Object=} opt_object If provided, we verify if the property exists in
69 * the object.
70 * @return {?string} A vendor prefixed property name, or null if it does not
71 * exist.
72 */
73goog.dom.vendor.getPrefixedPropertyName = function(propertyName, opt_object) {
74 // We first check for a non-prefixed property, if available.
75 if (opt_object && propertyName in opt_object) {
76 return propertyName;
77 }
78 var prefix = goog.dom.vendor.getVendorJsPrefix();
79 if (prefix) {
80 prefix = prefix.toLowerCase();
81 var prefixedPropertyName = prefix + goog.string.toTitleCase(propertyName);
82 return (!goog.isDef(opt_object) || prefixedPropertyName in opt_object) ?
83 prefixedPropertyName : null;
84 }
85 return null;
86};
87
88
89/**
90 * @param {string} eventType An event type.
91 * @return {string} A lower-cased vendor prefixed event type.
92 */
93goog.dom.vendor.getPrefixedEventType = function(eventType) {
94 var prefix = goog.dom.vendor.getVendorJsPrefix() || '';
95 return (prefix + eventType).toLowerCase();
96};