|
Line 0
a/LayoutTests/ietestcenter/TestSupport/FeatureDetection.js_sec1
|
|
|
1 |
// Internet Explorer Test Pages Copyright © 2012 Microsoft Corporation. All rights reserved. |
| 2 |
// |
| 3 |
// Redistribution and use in source and binary forms, with or without modification, |
| 4 |
// are permitted provided that the following conditions are met: |
| 5 |
// |
| 6 |
// Redistributions of source code must retain the above copyright notice, this list of |
| 7 |
// conditions and the following disclaimer. |
| 8 |
// |
| 9 |
// Redistributions in binary form must reproduce the above copyright notice, this list of |
| 10 |
// conditions and the following disclaimer in the documentation and/or other materials |
| 11 |
// provided with the distribution. |
| 12 |
// |
| 13 |
// Neither the name of the Microsoft Corporation nor the names of its contributors may be |
| 14 |
// used to endorse or promote products derived from this software without specific prior |
| 15 |
// written permission. |
| 16 |
// |
| 17 |
// THIS SOFTWARE IS PROVIDED BY MICROSOFT CORPORATION "AS IS" AND ANY EXPRESS OR IMPLIED |
| 18 |
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 19 |
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MICROSOFT CORPORATION |
| 20 |
// BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 21 |
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 22 |
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 23 |
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 |
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 25 |
// POSSIBILITY OF SUCH DAMAGE. |
| 26 |
|
| 27 |
|
| 28 |
// This function returns the value of an API feature if it is defined with one of |
| 29 |
// the known ventor prefixes. |
| 30 |
// |
| 31 |
// Parameters: |
| 32 |
// parent: the object containing the API feature |
| 33 |
// feature: the name of the API feature, this should be a string value |
| 34 |
// isAttribute: set this to true to indicate that this is a constant attribute |
| 35 |
// as opposed to a variable |
| 36 |
function BrowserHasFeature(parent, feature, isAttribute) |
| 37 |
{ |
| 38 |
if (parent[feature] !== undefined) |
| 39 |
{ |
| 40 |
//feature is defined without a vendor prefix, no further checks necessary |
| 41 |
return parent[feature]; |
| 42 |
} |
| 43 |
|
| 44 |
// the feature is not defined without a vendor prefix, so find the vendor prefix, if any, |
| 45 |
// that it is defined with |
| 46 |
var prefix = GetVendorPrefix(parent, feature, isAttribute); |
| 47 |
|
| 48 |
// if prefix is not undefined, then the feature has been found to exist with this prefix |
| 49 |
if (prefix !== undefined) |
| 50 |
{ |
| 51 |
var prefixedFeatureName = AppendPrefix(prefix, feature, isAttribute); |
| 52 |
return parent[prefixedFeatureName]; |
| 53 |
} |
| 54 |
|
| 55 |
//The feature does not exist. |
| 56 |
//Callers should check for !==undefined as the feature itself could return |
| 57 |
//a Bool which would fail a typical if(feature) check |
| 58 |
return undefined; |
| 59 |
} |
| 60 |
|
| 61 |
// This function returns the vendor prefix found if a certain feature is defined with it. |
| 62 |
// It takes the same parameters at BrowserHasFeature(). |
| 63 |
function GetVendorPrefix(parent, feature, isAttribute) |
| 64 |
{ |
| 65 |
//Known vendor prefixes |
| 66 |
var VendorPrefixes = ["moz", "ms", "o", "webkit"]; |
| 67 |
for (vendor in VendorPrefixes) |
| 68 |
{ |
| 69 |
//Build up the new feature name with the vendor prefix |
| 70 |
var prefixedFeatureName = AppendPrefix(VendorPrefixes[vendor], feature, isAttribute); |
| 71 |
if (parent[prefixedFeatureName] !== undefined) |
| 72 |
{ |
| 73 |
//Vendor prefix version exists, return a pointer to the feature |
| 74 |
return VendorPrefixes[vendor]; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
// if no version of the feature with a vendor prefix has been found, return undefined |
| 79 |
return undefined; |
| 80 |
} |
| 81 |
|
| 82 |
// This will properly capitalize the feature name and then return the feature name preceded |
| 83 |
// with the provided vendor prefix. If the prefix given is undefined, this function will |
| 84 |
// return the feature name given as is. The output of this function should not be used |
| 85 |
// as an indicator of whether or not a feature exists as it will return the same thing if |
| 86 |
// the inputted feature is undefined or is defined without a vendor prefix. It takes the |
| 87 |
// same parameters at BrowserHasFeature(). |
| 88 |
function AppendPrefix(prefix, feature, isAttribute) |
| 89 |
{ |
| 90 |
if (prefix !== undefined) |
| 91 |
{ |
| 92 |
if (isAttribute) |
| 93 |
{ |
| 94 |
// if a certain feature is an attribute, then it follows a different naming standard |
| 95 |
// where it must be completely capitalized and have words split with an underscore |
| 96 |
return prefix.toUpperCase() + "_" + feature.toUpperCase(); |
| 97 |
} |
| 98 |
else |
| 99 |
{ |
| 100 |
//Vendor prefixing should follow the standard convention: vendorprefixFeature |
| 101 |
//Note that the feature is capitalized after the vendor prefix |
| 102 |
//Therefore if the feature is window.feature, the vendor prefix version is: |
| 103 |
//window.[vp]Feature where vp is the vendor prefix: |
| 104 |
//window.msFeature, window.webkitFeature, window.mozFeature, window.oFeature |
| 105 |
var newFeature = feature[0].toUpperCase() + feature.substr(1, feature.length); |
| 106 |
|
| 107 |
//Build up the new feature name with the vendor prefix |
| 108 |
return prefix + newFeature; |
| 109 |
} |
| 110 |
} |
| 111 |
else |
| 112 |
{ |
| 113 |
return feature; |
| 114 |
} |
| 115 |
} |