From d8f744fd2f01c198e21e644f196817d2701c6086 Mon Sep 17 00:00:00 2001 From: Jeff Schiller Date: Tue, 26 Oct 2010 07:25:21 +0000 Subject: [PATCH] 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 --- editor/browsersupport.js | 97 ++++++++++++++++++++++++++++++++++++++++ editor/svg-editor.html | 1 + editor/svgcanvas.js | 65 +++------------------------ 3 files changed, 105 insertions(+), 58 deletions(-) create mode 100644 editor/browsersupport.js diff --git a/editor/browsersupport.js b/editor/browsersupport.js new file mode 100644 index 00000000..e073435c --- /dev/null +++ b/editor/browsersupport.js @@ -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 latest version 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(); + +})(); \ No newline at end of file diff --git a/editor/svg-editor.html b/editor/svg-editor.html index 1718515f..0fee7a9e 100644 --- a/editor/svg-editor.html +++ b/editor/svg-editor.html @@ -18,6 +18,7 @@ + diff --git a/editor/svgcanvas.js b/editor/svgcanvas.js index 27b351a2..59300e25 100644 --- a/editor/svgcanvas.js +++ b/editor/svgcanvas.js @@ -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 latest version 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); -}()); +})(); }