Rewrite browsersupport JS module to only expose functions. Added detection for Selectors and XPath support. Updated svgcanvas.js to create its getElem() function based on browsersupport.

git-svn-id: http://svg-edit.googlecode.com/svn/trunk@1909 eee81c28-f429-11dd-99c0-75d572ba1ddd
master
Jeff Schiller 2011-01-13 07:38:27 +00:00
parent e557f346e9
commit 282f81f27a
3 changed files with 62 additions and 44 deletions

View File

@ -29,12 +29,16 @@ var isOpera_ = !!window.opera;
var isWebkit_ = userAgent.indexOf("AppleWebKit") >= 0; var isWebkit_ = userAgent.indexOf("AppleWebKit") >= 0;
var isGecko_ = userAgent.indexOf('Gecko/') >= 0; var isGecko_ = userAgent.indexOf('Gecko/') >= 0;
svgedit.browsersupport.isOpera = function() { return isOpera_; } var supportsSelectors_ = (function() {
svgedit.browsersupport.isWebkit = function() { return isWebkit_; } return !!svg.querySelector;
svgedit.browsersupport.isGecko = function() { return isGecko_; } })();
var supportsXpath_ = (function() {
return !!document.evaluate;
})();
// segList functions (for FF1.5 and 2.0) // segList functions (for FF1.5 and 2.0)
function supportPathReplaceItem() { var supportsPathReplaceItem_ = (function() {
var path = document.createElementNS(svgns, 'path'); var path = document.createElementNS(svgns, 'path');
path.setAttribute('d','M0,0 10,10'); path.setAttribute('d','M0,0 10,10');
var seglist = path.pathSegList; var seglist = path.pathSegList;
@ -44,9 +48,9 @@ function supportPathReplaceItem() {
return true; return true;
} catch(err) {} } catch(err) {}
return false; return false;
} })();
function supportPathInsertItemBefore() { var supportsPathInsertItemBefore_ = (function() {
var path = document.createElementNS(svgns,'path'); var path = document.createElementNS(svgns,'path');
path.setAttribute('d','M0,0 10,10'); path.setAttribute('d','M0,0 10,10');
var seglist = path.pathSegList; var seglist = path.pathSegList;
@ -56,10 +60,10 @@ function supportPathInsertItemBefore() {
return true; return true;
} catch(err) {} } catch(err) {}
return false; return false;
} })();
// text character positioning // text character positioning
function supportTextCharPos() { var supportsTextCharPos_ = (function() {
var retValue = false; var retValue = false;
var svgcontent = document.createElementNS(svgns, 'svg'); var svgcontent = document.createElementNS(svgns, 'svg');
document.documentElement.appendChild(svgcontent); document.documentElement.appendChild(svgcontent);
@ -72,14 +76,14 @@ function supportTextCharPos() {
} catch(err) {} } catch(err) {}
document.documentElement.removeChild(svgcontent); document.documentElement.removeChild(svgcontent);
return retValue; return retValue;
} })();
function supportEditableText() { var supportsEditableText_ = (function() {
// TODO: Find better way to check support for this // TODO: Find better way to check support for this
return svgedit.browsersupport.isOpera(); return isOpera_;
} })();
function supportGoodDecimals() { var supportsGoodDecimals_ = (function() {
// Correct decimals on clone attributes (Opera < 10.5/win/non-en) // Correct decimals on clone attributes (Opera < 10.5/win/non-en)
var rect = document.createElementNS(svgns, 'rect'); var rect = document.createElementNS(svgns, 'rect');
rect.setAttribute('x',.1); rect.setAttribute('x',.1);
@ -90,29 +94,38 @@ function supportGoodDecimals() {
Please upgrade to the <a href='http://opera.com'>latest version</a> in which the problems have been fixed."); Please upgrade to the <a href='http://opera.com'>latest version</a> in which the problems have been fixed.");
} }
return retValue; return retValue;
} })();
function supportNonScalingStroke() { var supportsNonScalingStroke_ = (function() {
var rect = document.createElementNS(svgns, 'rect'); var rect = document.createElementNS(svgns, 'rect');
rect.setAttribute('style','vector-effect:non-scaling-stroke'); rect.setAttribute('style','vector-effect:non-scaling-stroke');
return rect.style.vectorEffect === 'non-scaling-stroke'; return rect.style.vectorEffect === 'non-scaling-stroke';
} })();
function supportNativeSVGTransformLists() { var supportsNativeSVGTransformLists_ = (function() {
var rect = document.createElementNS(svgns, 'rect'); var rect = document.createElementNS(svgns, 'rect');
var rxform = rect.transform.baseVal; var rxform = rect.transform.baseVal;
var t1 = svg.createSVGTransform(); var t1 = svg.createSVGTransform();
rxform.appendItem(t1); rxform.appendItem(t1);
return rxform.getItem(0) == t1; return rxform.getItem(0) == t1;
} })();
svgedit.browsersupport.pathReplaceItem = supportPathReplaceItem(); // Public API
svgedit.browsersupport.pathInsertItemBefore = supportPathInsertItemBefore();
svgedit.browsersupport.textCharPos = supportTextCharPos(); svgedit.browsersupport.isOpera = function() { return isOpera_; }
svgedit.browsersupport.editableText = supportEditableText(); svgedit.browsersupport.isWebkit = function() { return isWebkit_; }
svgedit.browsersupport.goodDecimals = supportGoodDecimals(); svgedit.browsersupport.isGecko = function() { return isGecko_; }
svgedit.browsersupport.nonScalingStroke = supportNonScalingStroke();
svgedit.browsersupport.nativeTransformLists = supportNativeSVGTransformLists(); svgedit.browsersupport.supportsSelectors = function() { return supportsSelectors_; }
svgedit.browsersupport.supportsXpath = function() { return supportsXpath_; }
svgedit.browsersupport.supportsPathReplaceItem = function() { return supportsPathReplaceItem_; }
svgedit.browsersupport.supportsPathInsertItemBefore = function() { return supportsPathInsertItemBefore_; }
svgedit.browsersupport.supportsTextCharPos = function() { return supportsTextCharPos_; }
svgedit.browsersupport.supportsEditableText = function() { return supportsEditableText_; }
svgedit.browsersupport.supportsGoodDecimals = function() { return supportsGoodDecimals_; }
svgedit.browsersupport.supportsNonScalingStroke = function() { return supportsNonScalingStroke_; }
svgedit.browsersupport.supportsNativeTransformLists = function() { return supportsNativeSVGTransformLists_; }
})(); })();

View File

@ -218,22 +218,27 @@ var selectedElements = new Array(1);
// //
// Parameters: // Parameters:
// id - String with the element's new ID // id - String with the element's new ID
var getElem = function(id) { var getElem = null;
if(svgroot.querySelector) { if (svgedit.browsersupport.supportsSelectors()) {
getElem = function(id) {
// querySelector lookup // querySelector lookup
return svgroot.querySelector('#'+id); return svgroot.querySelector('#'+id);
} else if(svgdoc.evaluate) { };
} else if (svgedit.browsersupport.supportsXpath()) {
getElem = function(id) {
// xpath lookup // xpath lookup
return svgdoc.evaluate('svg:svg[@id="svgroot"]//svg:*[@id="'+id+'"]', container, function() { return "http://www.w3.org/2000/svg"; }, 9, null).singleNodeValue; return svgdoc.evaluate('svg:svg[@id="svgroot"]//svg:*[@id="'+id+'"]',
} else { container, function() { return "http://www.w3.org/2000/svg"; },
9, null).singleNodeValue;
};
} else {
getElem = function(id) {
// jQuery lookup: twice as slow as xpath in FF // jQuery lookup: twice as slow as xpath in FF
return $(svgroot).find('[id=' + id + ']')[0]; return $(svgroot).find('[id=' + id + ']')[0];
} };
}
canvas.getElem = getElem;
// getElementById lookup: includes icons, not good
// return svgdoc.getElementById(id);
};
// Function: assignAttributes // Function: assignAttributes
// Assigns multiple attributes to an element. // Assigns multiple attributes to an element.
// //
@ -2657,7 +2662,7 @@ var getMouseTarget = this.getMouseTarget = function(evt) {
tlist.appendItem(svgroot.createSVGTransform()); tlist.appendItem(svgroot.createSVGTransform());
tlist.appendItem(svgroot.createSVGTransform()); tlist.appendItem(svgroot.createSVGTransform());
if(svgedit.browsersupport.nonScalingStroke) { if(svgedit.browsersupport.supportsNonScalingStroke()) {
mouse_target.style.vectorEffect = 'non-scaling-stroke'; mouse_target.style.vectorEffect = 'non-scaling-stroke';
var all = mouse_target.getElementsByTagName('*'), len = all.length; var all = mouse_target.getElementsByTagName('*'), len = all.length;
for(var i = 0; i < all.length; i++) { for(var i = 0; i < all.length; i++) {
@ -3318,7 +3323,7 @@ var getMouseTarget = this.getMouseTarget = function(evt) {
} // no change in mouse position } // no change in mouse position
// Remove non-scaling stroke // Remove non-scaling stroke
if(svgedit.browsersupport.nonScalingStroke) { if(svgedit.browsersupport.supportsNonScalingStroke()) {
var elem = selectedElements[0]; var elem = selectedElements[0];
elem.removeAttribute('style'); elem.removeAttribute('style');
svgedit.utilities.walkTree(elem, function(elem) { svgedit.utilities.walkTree(elem, function(elem) {
@ -3898,7 +3903,7 @@ var textActions = canvas.textActions = function() {
$(curtext).css('cursor', 'text'); $(curtext).css('cursor', 'text');
// if(svgedit.browsersupport.editableText) { // if(svgedit.browsersupport.supportsEditableText()) {
// curtext.setAttribute('editable', 'simple'); // curtext.setAttribute('editable', 'simple');
// return; // return;
// } // }
@ -3938,7 +3943,7 @@ var textActions = canvas.textActions = function() {
curtext = false; curtext = false;
// if(svgedit.browsersupport.editableText) { // if(svgedit.browsersupport.supportsEditableText()) {
// curtext.removeAttribute('editable'); // curtext.removeAttribute('editable');
// } // }
}, },
@ -3954,7 +3959,7 @@ var textActions = canvas.textActions = function() {
init: function(inputElem) { init: function(inputElem) {
if(!curtext) return; if(!curtext) return;
// if(svgedit.browsersupport.editableText) { // if(svgedit.browsersupport.supportsEditableText()) {
// curtext.select(); // curtext.select();
// return; // return;
// } // }
@ -4042,7 +4047,7 @@ var pathActions = canvas.pathActions = function() {
// Support insertItemBefore on paths for FF2 // Support insertItemBefore on paths for FF2
var list = elem.pathSegList; var list = elem.pathSegList;
if(svgedit.browsersupport.pathInsertItemBefore) { if(svgedit.browsersupport.supportsPathInsertItemBefore()) {
list.insertItemBefore(newseg, index); list.insertItemBefore(newseg, index);
return; return;
} }
@ -4889,7 +4894,7 @@ var pathActions = canvas.pathActions = function() {
var func = 'createSVGPathSeg' + pathFuncs[type]; var func = 'createSVGPathSeg' + pathFuncs[type];
var seg = path[func].apply(path, pts); var seg = path[func].apply(path, pts);
if(svgedit.browsersupport.pathReplaceItem) { if(svgedit.browsersupport.supportsPathReplaceItem()) {
path.pathSegList.replaceItem(seg, index); path.pathSegList.replaceItem(seg, index);
} else { } else {
var segList = path.pathSegList; var segList = path.pathSegList;
@ -9835,7 +9840,7 @@ this.getPrivateMethods = function() {
canvas.textActions = textActions; canvas.textActions = textActions;
} }
if (!svgedit.browsersupport.textCharPos) { if (!svgedit.browsersupport.supportsTextCharPos()) {
disableAdvancedTextEdit(); disableAdvancedTextEdit();
} }
})(); })();

View File

@ -261,7 +261,7 @@ svgedit.transformlist.removeElementFromListMap = function(elem) {
// Parameters: // Parameters:
// elem - DOM element to get a transformlist from // elem - DOM element to get a transformlist from
svgedit.transformlist.getTransformList = function(elem) { svgedit.transformlist.getTransformList = function(elem) {
if (!svgedit.browsersupport.nativeTransformLists) { if (!svgedit.browsersupport.supportsNativeTransformLists()) {
var id = elem.id; var id = elem.id;
if(!id) { if(!id) {
// Get unique ID for temporary element // Get unique ID for temporary element