Bulk performance improvements by using .indexOf over $.inArray and >= 0

git-svn-id: http://svg-edit.googlecode.com/svn/trunk@1769 eee81c28-f429-11dd-99c0-75d572ba1ddd
master
Alexis Deveria 2010-09-29 18:37:51 +00:00
parent 0dbe3a0b84
commit e26eb40fcc
2 changed files with 41 additions and 41 deletions

View File

@ -93,7 +93,7 @@
if(val) curPrefs[key] = val; if(val) curPrefs[key] = val;
key = 'svg-edit-'+key; key = 'svg-edit-'+key;
var host = location.hostname, var host = location.hostname,
onweb = host && host.indexOf('.') != -1, onweb = host && host.indexOf('.') >= 0,
store = (val != undefined), store = (val != undefined),
storage = false; storage = false;
// Some FF versions throw security errors here // Some FF versions throw security errors here
@ -452,7 +452,7 @@
"#aaaaff", "#d4aaff", "#ffaaff", "#ffaad4", "#aaaaff", "#d4aaff", "#ffaaff", "#ffaad4",
]; ];
isMac = (navigator.platform.indexOf("Mac") != -1); isMac = (navigator.platform.indexOf("Mac") >= 0);
modKey = (isMac ? "meta+" : "ctrl+"); // ⌘ modKey = (isMac ? "meta+" : "ctrl+"); // ⌘
path = svgCanvas.pathActions, path = svgCanvas.pathActions,
undoMgr = svgCanvas.undoMgr, undoMgr = svgCanvas.undoMgr,
@ -1282,7 +1282,7 @@
// updates the toolbar (colors, opacity, etc) based on the selected element // updates the toolbar (colors, opacity, etc) based on the selected element
// This function also updates the opacity and id elements that are in the context panel // This function also updates the opacity and id elements that are in the context panel
var updateToolbar = function() { var updateToolbar = function() {
if (selectedElement != null && $.inArray(selectedElement.tagName, ['use', 'image', 'foreignObject', 'g', 'a']) === -1) { if (selectedElement != null && ['use', 'image', 'foreignObject', 'g', 'a'].indexOf(selectedElement.tagName) === -1) {
// get opacity values // get opacity values
var fillOpacity = parseFloat(selectedElement.getAttribute("fill-opacity")); var fillOpacity = parseFloat(selectedElement.getAttribute("fill-opacity"));
@ -1445,12 +1445,12 @@
if(!is_node && currentMode != 'pathedit') { if(!is_node && currentMode != 'pathedit') {
$('#selected_panel').show(); $('#selected_panel').show();
// Elements in this array already have coord fields // Elements in this array already have coord fields
if($.inArray(elname, ['line', 'circle', 'ellipse']) != -1) { if(['line', 'circle', 'ellipse'].indexOf(elname) >= 0) {
$('#xy_panel').hide(); $('#xy_panel').hide();
} else { } else {
var x,y; var x,y;
// Get BBox vals for g, polyline and path // Get BBox vals for g, polyline and path
if($.inArray(elname, ['g', 'polyline', 'path']) != -1) { if(['g', 'polyline', 'path'].indexOf(elname) >= 0) {
var bb = svgCanvas.getStrokedBBox([elem]); var bb = svgCanvas.getStrokedBBox([elem]);
if(bb) { if(bb) {
x = bb.x; x = bb.x;
@ -1466,7 +1466,7 @@
} }
// Elements in this array cannot be converted to a path // Elements in this array cannot be converted to a path
var no_path = $.inArray(elname, ['image', 'text', 'path', 'g', 'use']) == -1; var no_path = ['image', 'text', 'path', 'g', 'use'].indexOf(elname) == -1;
$('#tool_topath').toggle(no_path); $('#tool_topath').toggle(no_path);
$('#tool_reorient').toggle(elname == 'path'); $('#tool_reorient').toggle(elname == 'path');
$('#tool_reorient').toggleClass('disabled', angle == 0); $('#tool_reorient').toggleClass('disabled', angle == 0);
@ -1659,7 +1659,7 @@
var changeStrokeWidth = function(ctl) { var changeStrokeWidth = function(ctl) {
var val = ctl.value; var val = ctl.value;
if(val == 0 && selectedElement && $.inArray(selectedElement.nodeName, ['line', 'polyline']) != -1) { if(val == 0 && selectedElement && ['line', 'polyline'].indexOf(selectedElement.nodeName) >= 0) {
val = ctl.value = 1; val = ctl.value = 1;
} }
svgCanvas.setStrokeWidth(val); svgCanvas.setStrokeWidth(val);

View File

@ -90,7 +90,7 @@ if(window.opera) {
$.SvgCanvas = function(container, config) $.SvgCanvas = function(container, config)
{ {
var isOpera = !!window.opera, var isOpera = !!window.opera,
isWebkit = navigator.userAgent.indexOf("AppleWebKit") != -1, isWebkit = navigator.userAgent.indexOf("AppleWebKit") >= 0,
// Object populated later with booleans indicating support for features // Object populated later with booleans indicating support for features
support = {}, support = {},
@ -406,7 +406,7 @@ var Utils = this.Utils = function() {
// Cross-browser compatible method of converting a string to an XML tree // Cross-browser compatible method of converting a string to an XML tree
// found this function here: http://groups.google.com/group/jquery-dev/browse_thread/thread/c6d11387c580a77f // found this function here: http://groups.google.com/group/jquery-dev/browse_thread/thread/c6d11387c580a77f
"text2xml": function(sXML) { "text2xml": function(sXML) {
if(sXML.indexOf('<svg:svg') !== -1) { if(sXML.indexOf('<svg:svg') >= 0) {
sXML = sXML.replace(/<(\/?)svg:/g, '<$1').replace('xmlns:svg', 'xmlns'); sXML = sXML.replace(/<(\/?)svg:/g, '<$1').replace('xmlns:svg', 'xmlns');
} }
@ -513,7 +513,7 @@ var svgWhiteListNS = {};
$.each(svgWhiteList, function(elt,atts){ $.each(svgWhiteList, function(elt,atts){
var attNS = {}; var attNS = {};
$.each(atts, function(i, att){ $.each(atts, function(i, att){
if (att.indexOf(':') != -1) { if (att.indexOf(':') >= 0) {
var v = att.split(':'); var v = att.split(':');
attNS[v[1]] = nsRevMap[v[0]]; attNS[v[1]] = nsRevMap[v[0]];
} else { } else {
@ -559,9 +559,9 @@ var convertToNum, convertToUnit, setUnitAttr;
var num = val.substr(0, val.length-1)/100; var num = val.substr(0, val.length-1)/100;
var res = getResolution(); var res = getResolution();
if($.inArray(attr, w_attrs) !== -1) { if(w_attrs.indexOf(attr) >= 0) {
return num * res.w; return num * res.w;
} else if($.inArray(attr, h_attrs) !== -1) { } else if(h_attrs.indexOf(attr) >= 0) {
return num * res.h; return num * res.h;
} else { } else {
return num * Math.sqrt((res.w*res.w) + (res.h*res.h))/Math.sqrt(2); return num * Math.sqrt((res.w*res.w) + (res.h*res.h))/Math.sqrt(2);
@ -594,9 +594,9 @@ var convertToNum, convertToUnit, setUnitAttr;
var res = getResolution(); var res = getResolution();
unit = '%'; unit = '%';
val *= 100; val *= 100;
if($.inArray(attr, w_attrs) !== -1) { if(w_attrs.indexOf(attr) >= 0) {
val = val / res.w; val = val / res.w;
} else if($.inArray(attr, h_attrs) !== -1) { } else if(h_attrs.indexOf(attr) >= 0) {
val = val / res.h; val = val / res.h;
} else { } else {
return val / Math.sqrt((res.w*res.w) + (res.h*res.h))/Math.sqrt(2); return val / Math.sqrt((res.w*res.w) + (res.h*res.h))/Math.sqrt(2);
@ -621,7 +621,7 @@ var convertToNum, convertToUnit, setUnitAttr;
// val - String with the attribute value to check // val - String with the attribute value to check
canvas.isValidUnit = function(attr, val) { canvas.isValidUnit = function(attr, val) {
var valid = false; var valid = false;
if($.inArray(attr, unit_attrs) != -1) { if(unit_attrs.indexOf(attr) >= 0) {
// True if it's just a number // True if it's just a number
if(!isNaN(val)) { if(!isNaN(val)) {
valid = true; valid = true;
@ -2133,7 +2133,7 @@ var getStrokedBBox = this.getStrokedBBox = function(elems) {
// Get the BBox from the raw path for these elements // Get the BBox from the raw path for these elements
var elemNames = ['ellipse','path','line','polyline','polygon']; var elemNames = ['ellipse','path','line','polyline','polygon'];
if($.inArray(elem.tagName, elemNames) != -1) { if(elemNames.indexOf(elem.tagName) >= 0) {
bb = good_bb = canvas.convertToPath(elem, true); bb = good_bb = canvas.convertToPath(elem, true);
} else if(elem.tagName == 'rect') { } else if(elem.tagName == 'rect') {
// Look for radius // Look for radius
@ -2514,7 +2514,7 @@ var sanitizeSvg = this.sanitizeSvg = function(node) {
while(p--) { while(p--) {
var nv = props[p].split(":"); var nv = props[p].split(":");
// now check that this attribute is supported // now check that this attribute is supported
if (allowedAttrs.indexOf(nv[0]) != -1) { if (allowedAttrs.indexOf(nv[0]) >= 0) {
node.setAttribute(nv[0],nv[1]); node.setAttribute(nv[0],nv[1]);
} }
} }
@ -2530,8 +2530,8 @@ var sanitizeSvg = this.sanitizeSvg = function(node) {
// (but not for links) // (but not for links)
var href = getHref(node); var href = getHref(node);
if(href && if(href &&
$.inArray(node.nodeName, ["filter", "linearGradient", "pattern", ["filter", "linearGradient", "pattern",
"radialGradient", "textPath", "use"]) != -1) "radialGradient", "textPath", "use"].indexOf(node.nodeName) >= 0)
{ {
// TODO: we simply check if the first character is a #, is this bullet-proof? // TODO: we simply check if the first character is a #, is this bullet-proof?
if (href[0] != "#") { if (href[0] != "#") {
@ -2638,7 +2638,7 @@ var getBBox = this.getBBox = function(elem) {
bb.x = ret.x + parseFloat(selected.getAttribute('x')||0); bb.x = ret.x + parseFloat(selected.getAttribute('x')||0);
bb.y = ret.y + parseFloat(selected.getAttribute('y')||0); bb.y = ret.y + parseFloat(selected.getAttribute('y')||0);
ret = bb; ret = bb;
} else if(~$.inArray(elname, visElems_arr)) { } else if(~visElems_arr.indexOf(elname)) {
try { ret = selected.getBBox();} try { ret = selected.getBBox();}
catch(e) { catch(e) {
// Check if element is child of a foreignObject // Check if element is child of a foreignObject
@ -3502,7 +3502,7 @@ var recalculateDimensions = this.recalculateDimensions = function(selected) {
if(child.getAttribute('clip-path')) { if(child.getAttribute('clip-path')) {
// tx, ty // tx, ty
var attr = child.getAttribute('clip-path'); var attr = child.getAttribute('clip-path');
if($.inArray(attr, clipPaths_done) === -1) { if(clipPaths_done.indexOf(attr) === -1) {
updateClipPath(attr, tx, ty); updateClipPath(attr, tx, ty);
clipPaths_done.push(attr); clipPaths_done.push(attr);
} }
@ -4256,7 +4256,7 @@ var getMouseTarget = this.getMouseTarget = function(evt) {
// for foreign content, go up until we find the foreignObject // for foreign content, go up until we find the foreignObject
// WebKit browsers set the mouse target to the svgcanvas div // WebKit browsers set the mouse target to the svgcanvas div
if ($.inArray(mouse_target.namespaceURI, [mathns, htmlns]) != -1 && if ([mathns, htmlns].indexOf(mouse_target.namespaceURI) >= 0 &&
mouse_target.id != "svgcanvas") mouse_target.id != "svgcanvas")
{ {
while (mouse_target.nodeName != "foreignObject") { while (mouse_target.nodeName != "foreignObject") {
@ -4267,7 +4267,7 @@ var getMouseTarget = this.getMouseTarget = function(evt) {
// Get the desired mouse_target with jQuery selector-fu // Get the desired mouse_target with jQuery selector-fu
// If it's root-like, select the root // If it's root-like, select the root
if($.inArray(mouse_target, [svgroot, container, svgcontent, current_layer]) !== -1) { if([svgroot, container, svgcontent, current_layer].indexOf(mouse_target) >= 0) {
return svgroot; return svgroot;
} }
@ -4348,7 +4348,7 @@ var getMouseTarget = this.getMouseTarget = function(evt) {
} }
// This would seem to be unnecessary... // This would seem to be unnecessary...
// if($.inArray(current_mode, ['select', 'resize']) == -1) { // if(['select', 'resize'].indexOf(current_mode) == -1) {
// setGradient(); // setGradient();
// } // }
@ -4813,13 +4813,13 @@ var getMouseTarget = this.getMouseTarget = function(evt) {
sy = height ? (height+dy)/height : 1, sy = height ? (height+dy)/height : 1,
sx = width ? (width+dx)/width : 1; sx = width ? (width+dx)/width : 1;
// if we are dragging on the north side, then adjust the scale factor and ty // if we are dragging on the north side, then adjust the scale factor and ty
if(current_resize_mode.indexOf("n") != -1) { if(current_resize_mode.indexOf("n") >= 0) {
sy = height ? (height-dy)/height : 1; sy = height ? (height-dy)/height : 1;
ty = height; ty = height;
} }
// if we dragging on the east side, then adjust the scale factor and tx // if we dragging on the east side, then adjust the scale factor and tx
if(current_resize_mode.indexOf("w") != -1) { if(current_resize_mode.indexOf("w") >= 0) {
sx = width ? (width-dx)/width : 1; sx = width ? (width-dx)/width : 1;
tx = width; tx = width;
} }
@ -6425,7 +6425,7 @@ var pathActions = this.pathActions = function() {
var index = indexes[i]; var index = indexes[i];
var seg = p.segs[index]; var seg = p.segs[index];
if(seg.ptgrip) { if(seg.ptgrip) {
if($.inArray(index, p.selected_pts) == -1 && index >= 0) { if(p.selected_pts.indexOf(index) == -1 && index >= 0) {
p.selected_pts.push(index); p.selected_pts.push(index);
} }
} }
@ -6449,7 +6449,7 @@ var pathActions = this.pathActions = function() {
} }
this.removePtFromSelection = function(index) { this.removePtFromSelection = function(index) {
var pos = $.inArray(index, p.selected_pts); var pos = p.selected_pts.indexOf(index);
if(pos == -1) { if(pos == -1) {
return; return;
} }
@ -7674,7 +7674,7 @@ var removeUnusedDefElems = this.removeUnusedDefElems = function() {
while (i--) { while (i--) {
var defelem = defelems[i]; var defelem = defelems[i];
var id = defelem.id; var id = defelem.id;
if($.inArray(id, defelem_uses) == -1) { if(defelem_uses.indexOf(id) == -1) {
// Not found, so remove // Not found, so remove
defelem.parentNode.removeChild(defelem); defelem.parentNode.removeChild(defelem);
numRemoved++; numRemoved++;
@ -7706,7 +7706,7 @@ var svgCanvasToString = this.svgCanvasToString = function() {
// Keep SVG-Edit comment on top // Keep SVG-Edit comment on top
$.each(svgcontent.childNodes, function(i, node) { $.each(svgcontent.childNodes, function(i, node) {
if(i && node.nodeType == 8 && node.data.indexOf('Created with') != -1) { if(i && node.nodeType == 8 && node.data.indexOf('Created with') >= 0) {
svgcontent.insertBefore(node, svgcontent.firstChild); svgcontent.insertBefore(node, svgcontent.firstChild);
} }
}); });
@ -7798,7 +7798,7 @@ var svgToString = this.svgToString = function(elem, indent) {
// only serialize attributes we don't use internally // only serialize attributes we don't use internally
if (attrVal != "" && if (attrVal != "" &&
$.inArray(attr.localName, ['width','height','xmlns','x','y','viewBox','id','overflow']) == -1) ['width','height','xmlns','x','y','viewBox','id','overflow'].indexOf(attr.localName) == -1)
{ {
if(!attr.namespaceURI || nsMap[attr.namespaceURI]) { if(!attr.namespaceURI || nsMap[attr.namespaceURI]) {
@ -7813,7 +7813,7 @@ var svgToString = this.svgToString = function(elem, indent) {
attr = attrs.item(i); attr = attrs.item(i);
var attrVal = toXml(attr.nodeValue); var attrVal = toXml(attr.nodeValue);
//remove bogus attributes added by Gecko //remove bogus attributes added by Gecko
if ($.inArray(attr.localName, ['-moz-math-font-style', '_moz-math-font-style']) !== -1) continue; if (['-moz-math-font-style', '_moz-math-font-style'].indexOf(attr.localName) >= 0) continue;
if (attrVal != "") { if (attrVal != "") {
if(attrVal.indexOf('pointer-events') === 0) continue; if(attrVal.indexOf('pointer-events') === 0) continue;
if(attr.localName === "class" && attrVal.indexOf('se_') === 0) continue; if(attr.localName === "class" && attrVal.indexOf('se_') === 0) continue;
@ -8054,8 +8054,8 @@ var uniquifyElems = this.uniquifyElems = function(g) {
var href = getHref(n); var href = getHref(n);
// TODO: what if an <image> or <a> element refers to an element internally? // TODO: what if an <image> or <a> element refers to an element internally?
if(href && if(href &&
$.inArray(n.nodeName, ["filter", "linearGradient", "pattern", ["filter", "linearGradient", "pattern",
"radialGradient", "textPath", "use"]) != -1) "radialGradient", "textPath", "use"].indexOf(n.nodeName) >= 0)
{ {
var refid = href.substr(1); var refid = href.substr(1);
if (!(refid in ids)) { if (!(refid in ids)) {
@ -8107,7 +8107,7 @@ var convertGradients = this.convertGradients = function(elem) {
if(!elems.length && isWebkit) { if(!elems.length && isWebkit) {
// Bug in webkit prevents regular *Gradient selector search // Bug in webkit prevents regular *Gradient selector search
elems = $(elem).find('*').filter(function() { elems = $(elem).find('*').filter(function() {
return (this.tagName.indexOf('Gradient') !== -1); return (this.tagName.indexOf('Gradient') >= 0);
}); });
} }
@ -8569,7 +8569,7 @@ var identifyLayers = function() {
// create a new layer and add all the orphans to it // create a new layer and add all the orphans to it
if (orphans.length > 0) { if (orphans.length > 0) {
var i = 1; var i = 1;
while ($.inArray(("Layer " + i), layernames) != -1) { i++; } while (layernames.indexOf(("Layer " + i)) >= 0) { i++; }
var newname = "Layer " + i; var newname = "Layer " + i;
current_layer = svgdoc.createElementNS(svgns, "g"); current_layer = svgdoc.createElementNS(svgns, "g");
var layer_title = svgdoc.createElementNS(svgns, "title"); var layer_title = svgdoc.createElementNS(svgns, "title");
@ -9649,7 +9649,7 @@ this.getStrokeWidth = function() {
// Parameters: // Parameters:
// val - A Float indicating the new stroke width value // val - A Float indicating the new stroke width value
this.setStrokeWidth = function(val) { this.setStrokeWidth = function(val) {
if(val == 0 && $.inArray(current_mode, ['line', 'path']) != -1) { if(val == 0 && ['line', 'path'].indexOf(current_mode) >= 0) {
canvas.setStrokeWidth(1); canvas.setStrokeWidth(1);
return; return;
} }
@ -10305,7 +10305,7 @@ var changeSelectedAttributeNoUndo = function(attr, newValue, elems) {
} }
// Set x,y vals on elements that don't have them // Set x,y vals on elements that don't have them
if((attr == 'x' || attr == 'y') && $.inArray(elem.tagName, ['g', 'polyline', 'path']) != -1) { if((attr == 'x' || attr == 'y') && ['g', 'polyline', 'path'].indexOf(elem.tagName) >= 0) {
var bbox = getStrokedBBox([elem]); var bbox = getStrokedBBox([elem]);
var diff_x = attr == 'x' ? newValue - bbox.x : 0; var diff_x = attr == 'x' ? newValue - bbox.x : 0;
var diff_y = attr == 'y' ? newValue - bbox.y : 0; var diff_y = attr == 'y' ? newValue - bbox.y : 0;
@ -10314,7 +10314,7 @@ var changeSelectedAttributeNoUndo = function(attr, newValue, elems) {
} }
// only allow the transform/opacity attribute to change on <g> elements, slightly hacky // only allow the transform/opacity attribute to change on <g> elements, slightly hacky
if (elem.tagName == "g" && $.inArray(attr, ['transform', 'opacity', 'filter']) !== -1); if (elem.tagName == "g" && ['transform', 'opacity', 'filter'].indexOf(attr) >= 0);
var oldval = attr == "#text" ? elem.textContent : elem.getAttribute(attr); var oldval = attr == "#text" ? elem.textContent : elem.getAttribute(attr);
if (oldval == null) oldval = ""; if (oldval == null) oldval = "";
if (oldval != String(newValue)) { if (oldval != String(newValue)) {
@ -10348,7 +10348,7 @@ var changeSelectedAttributeNoUndo = function(attr, newValue, elems) {
// Use the Firefox ffClone hack for text elements with gradients or // Use the Firefox ffClone hack for text elements with gradients or
// where other text attributes are changed. // where other text attributes are changed.
if(elem.nodeName == 'text') { if(elem.nodeName == 'text') {
if((newValue+'').indexOf('url') == 0 || $.inArray(attr, ['font-size','font-family','x','y']) != -1 && elem.textContent) { if((newValue+'').indexOf('url') == 0 || ['font-size','font-family','x','y'].indexOf(attr) >= 0 && elem.textContent) {
elem = ffClone(elem); elem = ffClone(elem);
} }
} }
@ -10356,7 +10356,7 @@ var changeSelectedAttributeNoUndo = function(attr, newValue, elems) {
// codedread: it is now possible for this function to be called with elements // codedread: it is now possible for this function to be called with elements
// that are not in the selectedElements array, we need to only request a // that are not in the selectedElements array, we need to only request a
// selector if the element is in that array // selector if the element is in that array
if ($.inArray(elem, selectedElements) != -1) { if (selectedElements.indexOf(elem) >= 0) {
setTimeout(function() { setTimeout(function() {
// Due to element replacement, this element may no longer // Due to element replacement, this element may no longer
// be part of the DOM // be part of the DOM