Move browser support checking into its own JS module

git-svn-id: http://svg-edit.googlecode.com/svn/trunk@1818 eee81c28-f429-11dd-99c0-75d572ba1ddd
master
Jeff Schiller 2010-10-26 07:25:21 +00:00
parent 89d5451cef
commit d8f744fd2f
3 changed files with 105 additions and 58 deletions

97
editor/browsersupport.js Normal file
View File

@ -0,0 +1,97 @@
/**
* Browser Support module for SVG-edit
*
* Licensed under the Apache License, Version 2
*
* Copyright(c) 2010 Jeff Schiller
* Copyright(c) 2010 Alexis Deveria
*/
// Dependencies:
// 1) jQuery (for $.alert())
(function() {
BrowserSupport = {};
var svgns = 'http://www.w3.org/2000/svg';
var userAgent = navigator.userAgent;
// Note: Browser sniffing should only be used if no other detection method is possible
BrowserSupport.isOpera = !!window.opera;
BrowserSupport.isWebkit = userAgent.indexOf("AppleWebKit") >= 0;
BrowserSupport.isGecko = userAgent.indexOf('Gecko/') >= 0;
// segList functions (for FF1.5 and 2.0)
function getPathReplaceItem() {
var path = document.createElementNS(svgns,'path');
path.setAttribute('d','M0,0 10,10');
var seglist = path.pathSegList;
var seg = path.createSVGPathSegLinetoAbs(5,5);
try {
seglist.replaceItem(seg, 0);
return true;
} catch(err) {}
return false;
}
function getPathInsertItemBefore() {
var path = document.createElementNS(svgns,'path');
path.setAttribute('d','M0,0 10,10');
var seglist = path.pathSegList;
var seg = path.createSVGPathSegLinetoAbs(5,5);
try {
seglist.insertItemBefore(seg, 0);
return true;
} catch(err) {}
return false;
}
// text character positioning
function getTextCharPos() {
var retValue = false;
var svgcontent = document.createElementNS(svgns, 'svg');
document.documentElement.appendChild(svgcontent);
try {
var text = document.createElementNS(svgns,'text');
text.textContent = 'a';
svgcontent.appendChild(text);
text.getStartPositionOfChar(0);
retValue = true;
} catch(err) {}
document.documentElement.removeChild(svgcontent);
return retValue;
}
function getEditableText() {
// TODO: Find better way to check support for this
return BrowserSupport.isOpera;
}
function getGoodDecimals() {
// Correct decimals on clone attributes (Opera < 10.5/win/non-en)
var rect = document.createElementNS(svgns,'rect');
rect.setAttribute('x',.1);
var crect = rect.cloneNode(false);
var retValue = (crect.getAttribute('x').indexOf(',') == -1);
if(!retValue) {
$.alert("NOTE: This version of Opera is known to contain bugs in SVG-edit.\n\
Please upgrade to the <a href='http://opera.com'>latest version</a> in which the problems have been fixed.");
}
return retValue;
}
function getNonScalingStroke() {
var rect = document.createElementNS(svgns,'rect');
rect.setAttribute('style','vector-effect:non-scaling-stroke');
return rect.style.vectorEffect === 'non-scaling-stroke';
}
BrowserSupport.pathReplaceItem = getPathReplaceItem();
BrowserSupport.pathInsertItemBefore = getPathInsertItemBefore();
BrowserSupport.textCharPos = getTextCharPos();
BrowserSupport.editableText = getEditableText();
BrowserSupport.goodDecimals = getGoodDecimals();
BrowserSupport.nonScalingStroke = getNonScalingStroke();
})();

View File

@ -18,6 +18,7 @@
<script type="text/javascript" src="jquerybbq/jquery.bbq.min.js"></script>
<script type="text/javascript" src="spinbtn/JQuerySpinBtn.js"></script>
<script type="text/javascript" src="contextmenu/jquery.contextMenu.js"></script>
<script type="text/javascript" src="browsersupport.js"></script>
<script type="text/javascript" src="svgutils.js"></script>
<script type="text/javascript" src="svgcanvas.js"></script>
<script type="text/javascript" src="svg-editor.js"></script>

View File

@ -89,14 +89,10 @@ if(window.opera) {
// config - An object that contains configuration data
$.SvgCanvas = function(container, config)
{
var userAgent = navigator.userAgent,
// Note: Browser sniffing should only be used if no other detection method is possible
isOpera = !!window.opera,
isWebkit = userAgent.indexOf("AppleWebKit") >= 0,
isGecko = userAgent.indexOf('Gecko/') >= 0,
// Object populated later with booleans indicating support for features
support = {},
var isOpera = BrowserSupport.isOpera,
isWebkit = BrowserSupport.isWebkit,
isGecko = BrowserSupport.isGecko,
support = BrowserSupport,
// this defines which elements and attributes that we support
svgWhiteList = {
@ -11309,54 +11305,11 @@ function disableAdvancedTextEdit() {
}
// Test support for features/bugs
(function() {
// segList functions (for FF1.5 and 2.0)
var path = document.createElementNS(svgns,'path');
path.setAttribute('d','M0,0 10,10');
var seglist = path.pathSegList;
var seg = path.createSVGPathSegLinetoAbs(5,5);
try {
seglist.replaceItem(seg, 0);
support.pathReplaceItem = true;
} catch(err) {
support.pathReplaceItem = false;
}
try {
seglist.insertItemBefore(seg, 0);
support.pathInsertItemBefore = true;
} catch(err) {
support.pathInsertItemBefore = false;
}
var text = document.createElementNS(svgns,'text');
text.textContent = 'a';
svgcontent.appendChild(text);
// text character positioning
try {
text.getStartPositionOfChar(0);
support.textCharPos = true;
} catch(err) {
support.textCharPos = false;
if (!BrowserSupport.textCharPos) {
disableAdvancedTextEdit();
}
svgcontent.removeChild(text);
// TODO: Find better way to check support for this
support.editableText = isOpera;
// Correct decimals on clone attributes (Opera < 10.5/win/non-en)
var rect = document.createElementNS(svgns,'rect');
rect.setAttribute('x',.1);
var crect = rect.cloneNode(false);
support.goodDecimals = (crect.getAttribute('x').indexOf(',') == -1);
if(!support.goodDecimals) {
$.alert("NOTE: This version of Opera is known to contain bugs in SVG-edit.\n\
Please upgrade to the <a href='http://opera.com'>latest version</a> in which the problems have been fixed.");
}
// Get correct em/ex values
var rect = document.createElementNS(svgns,'rect');
rect.setAttribute('width',"1em");
@ -11373,11 +11326,7 @@ function disableAdvancedTextEdit() {
unit_types.pt = inch / 72;
unit_types.pc = inch / 6;
unit_types['%'] = 0;
rect.setAttribute('style','vector-effect:non-scaling-stroke');
support.nonScalingStroke = (rect.style.vectorEffect === 'non-scaling-stroke');
svgcontent.removeChild(rect);
}());
})();
}