![]() |
API 0.9.5
|
00001 /* 00002 * CPCompatibility.j 00003 * AppKit 00004 * 00005 * Created by Francisco Tolmasky. 00006 * Copyright 2008, 280 North, Inc. 00007 * 00008 * This library is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU Lesser General Public 00010 * License as published by the Free Software Foundation; either 00011 * version 2.1 of the License, or (at your option) any later version. 00012 * 00013 * This library is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 * Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License along with this library; if not, write to the Free Software 00020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00021 */ 00022 00023 00024 00025 // Browser Engines 00026 CPUnknownBrowserEngine = 0; 00027 CPGeckoBrowserEngine = 1; 00028 CPInternetExplorerBrowserEngine = 2; 00029 CPKHTMLBrowserEngine = 3; 00030 CPOperaBrowserEngine = 4; 00031 CPWebKitBrowserEngine = 5; 00032 00033 // Operating Systems 00034 CPMacOperatingSystem = 0; 00035 CPWindowsOperatingSystem = 1; 00036 CPOtherOperatingSystem = 2; 00037 00038 // Features 00039 CPCSSRGBAFeature = 1 << 5; 00040 00041 CPHTMLCanvasFeature = 1 << 6; 00042 CPHTMLContentEditableFeature = 1 << 7; 00043 CPHTMLDragAndDropFeature = 1 << 8; 00044 00045 CPJavaScriptInnerTextFeature = 1 << 9; 00046 CPJavaScriptTextContentFeature = 1 << 10; 00047 CPJavaScriptClipboardEventsFeature = 1 << 11; 00048 CPJavaScriptClipboardAccessFeature = 1 << 12; 00049 CPJavaScriptCanvasDrawFeature = 1 << 13; 00050 CPJavaScriptCanvasTransformFeature = 1 << 14; 00051 00052 CPVMLFeature = 1 << 15; 00053 00054 CPJavaScriptRemedialKeySupport = 1 << 16; 00055 CPJavaScriptShadowFeature = 1 << 20; 00056 00057 CPJavaScriptNegativeMouseWheelValues = 1 << 22; 00058 CPJavaScriptMouseWheelValues_8_15 = 1 << 23; 00059 00060 CPOpacityRequiresFilterFeature = 1 << 24; 00061 00062 //Internet explorer does not allow dynamically changing the type of an input element 00063 CPInputTypeCanBeChangedFeature = 1 << 25; 00064 CPHTML5DragAndDropSourceYOffBy1 = 1 << 26; 00065 00066 CPSOPDisabledFromFileURLs = 1 << 27; 00067 00068 var USER_AGENT = "", 00069 PLATFORM_ENGINE = CPUnknownBrowserEngine, 00070 PLATFORM_FEATURES = 0; 00071 00072 // default these features to true 00073 00074 PLATFORM_FEATURES |= CPInputTypeCanBeChangedFeature; 00075 00076 if (typeof window !== "undefined" && typeof window.navigator !== "undefined") 00077 USER_AGENT = window.navigator.userAgent; 00078 00079 // Opera 00080 if (typeof window !== "undefined" && window.opera) 00081 { 00082 PLATFORM_ENGINE = CPOperaBrowserEngine; 00083 00084 PLATFORM_FEATURES |= CPJavaScriptCanvasDrawFeature; 00085 } 00086 00087 // Internet Explorer 00088 else if (typeof window !== "undefined" && window.attachEvent) // Must follow Opera check. 00089 { 00090 PLATFORM_ENGINE = CPInternetExplorerBrowserEngine; 00091 00092 // Features we can only be sure of with IE (no known independent tests) 00093 PLATFORM_FEATURES |= CPVMLFeature; 00094 PLATFORM_FEATURES |= CPJavaScriptRemedialKeySupport; 00095 PLATFORM_FEATURES |= CPJavaScriptShadowFeature; 00096 00097 PLATFORM_FEATURES |= CPOpacityRequiresFilterFeature; 00098 00099 PLATFORM_FEATURES &= ~CPInputTypeCanBeChangedFeature; 00100 } 00101 00102 // WebKit 00103 else if (USER_AGENT.indexOf("AppleWebKit/") != -1) 00104 { 00105 PLATFORM_ENGINE = CPWebKitBrowserEngine; 00106 00107 // Features we can only be sure of with WebKit (no known independent tests) 00108 PLATFORM_FEATURES |= CPCSSRGBAFeature; 00109 PLATFORM_FEATURES |= CPHTMLContentEditableFeature; 00110 00111 if (USER_AGENT.indexOf("Chrome") === -1) 00112 PLATFORM_FEATURES |= CPHTMLDragAndDropFeature; 00113 00114 PLATFORM_FEATURES |= CPJavaScriptClipboardEventsFeature; 00115 PLATFORM_FEATURES |= CPJavaScriptClipboardAccessFeature; 00116 PLATFORM_FEATURES |= CPJavaScriptShadowFeature; 00117 00118 var versionStart = USER_AGENT.indexOf("AppleWebKit/") + "AppleWebKit/".length, 00119 versionEnd = USER_AGENT.indexOf(" ", versionStart), 00120 versionString = USER_AGENT.substring(versionStart, versionEnd), 00121 versionDivision = versionString.indexOf('.'), 00122 majorVersion = parseInt(versionString.substring(0, versionDivision)), 00123 minorVersion = parseInt(versionString.substr(versionDivision + 1)); 00124 00125 if ((USER_AGENT.indexOf("Safari") !== CPNotFound && (majorVersion > 525 || (majorVersion === 525 && minorVersion > 14))) || USER_AGENT.indexOf("Chrome") !== CPNotFound) 00126 PLATFORM_FEATURES |= CPJavaScriptRemedialKeySupport; 00127 00128 // FIXME this is a terrible hack to get around this bug: 00129 // https://bugs.webkit.org/show_bug.cgi?id=21548 00130 if (![CPPlatform isBrowser]) 00131 PLATFORM_FEATURES |= CPJavaScriptRemedialKeySupport; 00132 00133 if (majorVersion < 532 || (majorVersion === 532 && minorVersion < 6)) 00134 PLATFORM_FEATURES |= CPHTML5DragAndDropSourceYOffBy1; 00135 00136 if (USER_AGENT.indexOf("Chrome") === CPNotFound) 00137 PLATFORM_FEATURES |= CPSOPDisabledFromFileURLs; 00138 } 00139 00140 // KHTML 00141 else if (USER_AGENT.indexOf("KHTML") != -1) // Must follow WebKit check. 00142 { 00143 PLATFORM_ENGINE = CPKHTMLBrowserEngine; 00144 } 00145 00146 // Gecko 00147 else if (USER_AGENT.indexOf("Gecko") !== -1) // Must follow KHTML check. 00148 { 00149 PLATFORM_ENGINE = CPGeckoBrowserEngine; 00150 00151 PLATFORM_FEATURES |= CPJavaScriptCanvasDrawFeature; 00152 00153 var index = USER_AGENT.indexOf("Firefox"), 00154 version = (index === -1) ? 2.0 : parseFloat(USER_AGENT.substring(index + "Firefox".length + 1)); 00155 00156 if (version >= 3.0) 00157 PLATFORM_FEATURES |= CPCSSRGBAFeature; 00158 00159 if (version < 3.0) 00160 PLATFORM_FEATURES |= CPJavaScriptMouseWheelValues_8_15; 00161 } 00162 00163 // Feature Specific Checks 00164 if (typeof document != "undefined") 00165 { 00166 var canvasElement = document.createElement("canvas"); 00167 // Detect Canvas Support 00168 if (canvasElement && canvasElement.getContext) 00169 { 00170 PLATFORM_FEATURES |= CPHTMLCanvasFeature; 00171 00172 // Detect Canvas setTransform/transform support 00173 var context = document.createElement("canvas").getContext("2d"); 00174 00175 if (context && context.setTransform && context.transform) 00176 PLATFORM_FEATURES |= CPJavaScriptCanvasTransformFeature; 00177 } 00178 00179 var DOMElement = document.createElement("div"); 00180 00181 // Detect whether we have innerText or textContent (or neither) 00182 if (DOMElement.innerText != undefined) 00183 PLATFORM_FEATURES |= CPJavaScriptInnerTextFeature; 00184 else if (DOMElement.textContent != undefined) 00185 PLATFORM_FEATURES |= CPJavaScriptTextContentFeature; 00186 } 00187 00188 function CPFeatureIsCompatible(aFeature) 00189 { 00190 return PLATFORM_FEATURES & aFeature; 00191 } 00192 00193 function CPBrowserIsEngine(anEngine) 00194 { 00195 return PLATFORM_ENGINE === anEngine; 00196 } 00197 00198 function CPBrowserIsOperatingSystem(anOperatingSystem) 00199 { 00200 return OPERATING_SYSTEM === anOperatingSystem; 00201 } 00202 00203 OPERATING_SYSTEM = CPOtherOperatingSystem; 00204 00205 if (USER_AGENT.indexOf("Mac") !== -1) 00206 { 00207 OPERATING_SYSTEM = CPMacOperatingSystem; 00208 00209 CPPlatformActionKeyMask = CPCommandKeyMask; 00210 00211 CPUndoKeyEquivalent = @"z"; 00212 CPRedoKeyEquivalent = @"Z"; 00213 00214 CPUndoKeyEquivalentModifierMask = CPCommandKeyMask; 00215 CPRedoKeyEquivalentModifierMask = CPCommandKeyMask; 00216 } 00217 else 00218 { 00219 if (USER_AGENT.indexOf("Windows") !== -1) 00220 OPERATING_SYSTEM = CPWindowsOperatingSystem; 00221 00222 CPPlatformActionKeyMask = CPControlKeyMask; 00223 00224 CPUndoKeyEquivalent = @"z"; 00225 CPRedoKeyEquivalent = @"y"; 00226 00227 CPUndoKeyEquivalentModifierMask = CPControlKeyMask; 00228 CPRedoKeyEquivalentModifierMask = CPControlKeyMask; 00229 }