JSLint
git-svn-id: http://svg-edit.googlecode.com/svn/trunk@2644 eee81c28-f429-11dd-99c0-75d572ba1ddd
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
/*globals $, svgedit, unescape, DOMParser, ActiveXObject, getStrokedBBox*/
|
||||
/*jslint vars: true, eqeq: true, bitwise: true, continue: true, forin: true*/
|
||||
/**
|
||||
* Package: svgedit.utilities
|
||||
*
|
||||
@@ -13,7 +15,7 @@
|
||||
// 3) svgtransformlist.js
|
||||
// 4) units.js
|
||||
|
||||
(function() {
|
||||
(function() {'use strict';
|
||||
|
||||
if (!svgedit.utilities) {
|
||||
svgedit.utilities = {};
|
||||
@@ -74,7 +76,7 @@ svgedit.utilities.fromXml = function(str) {
|
||||
// Base64 code from Tyler Akins -- http://rumkin.com
|
||||
|
||||
// schiller: Removed string concatenation in favour of Array.join() optimization,
|
||||
// also precalculate the size of the array needed.
|
||||
// also precalculate the size of the array needed.
|
||||
|
||||
// Function: svgedit.utilities.encode64
|
||||
// Converts a string to base64
|
||||
@@ -158,32 +160,33 @@ svgedit.utilities.decode64 = function(input) {
|
||||
// Currently not being used, so commented out for now
|
||||
// based on http://phpjs.org/functions/utf8_encode:577
|
||||
// codedread:does not seem to work with webkit-based browsers on OSX
|
||||
// 'encodeUTF8': function(input) {
|
||||
// //return unescape(encodeURIComponent(input)); //may or may not work
|
||||
// var output = '';
|
||||
// for (var n = 0; n < input.length; n++){
|
||||
// var c = input.charCodeAt(n);
|
||||
// if (c < 128) {
|
||||
// output += input[n];
|
||||
// }
|
||||
// else if (c > 127) {
|
||||
// if (c < 2048){
|
||||
// output += String.fromCharCode((c >> 6) | 192);
|
||||
// }
|
||||
// else {
|
||||
// output += String.fromCharCode((c >> 12) | 224) + String.fromCharCode((c >> 6) & 63 | 128);
|
||||
// }
|
||||
// output += String.fromCharCode((c & 63) | 128);
|
||||
// }
|
||||
// }
|
||||
// return output;
|
||||
// },
|
||||
// 'encodeUTF8': function(input) {
|
||||
// //return unescape(encodeURIComponent(input)); //may or may not work
|
||||
// var output = '';
|
||||
// for (var n = 0; n < input.length; n++){
|
||||
// var c = input.charCodeAt(n);
|
||||
// if (c < 128) {
|
||||
// output += input[n];
|
||||
// }
|
||||
// else if (c > 127) {
|
||||
// if (c < 2048){
|
||||
// output += String.fromCharCode((c >> 6) | 192);
|
||||
// }
|
||||
// else {
|
||||
// output += String.fromCharCode((c >> 12) | 224) + String.fromCharCode((c >> 6) & 63 | 128);
|
||||
// }
|
||||
// output += String.fromCharCode((c & 63) | 128);
|
||||
// }
|
||||
// }
|
||||
// return output;
|
||||
// },
|
||||
|
||||
// Function: svgedit.utilities.convertToXMLReferences
|
||||
// Converts a string to use XML references
|
||||
svgedit.utilities.convertToXMLReferences = function(input) {
|
||||
var output = '';
|
||||
for (var n = 0; n < input.length; n++){
|
||||
var n,
|
||||
output = '';
|
||||
for (n = 0; n < input.length; n++){
|
||||
var c = input.charCodeAt(n);
|
||||
if (c < 128) {
|
||||
output += input[n];
|
||||
@@ -202,18 +205,22 @@ svgedit.utilities.text2xml = function(sXML) {
|
||||
sXML = sXML.replace(/<(\/?)svg:/g, '<$1').replace('xmlns:svg', 'xmlns');
|
||||
}
|
||||
|
||||
var out;
|
||||
var out, dXML;
|
||||
try{
|
||||
var dXML = (window.DOMParser)?new DOMParser():new ActiveXObject('Microsoft.XMLDOM');
|
||||
dXML = (window.DOMParser)?new DOMParser():new ActiveXObject('Microsoft.XMLDOM');
|
||||
dXML.async = false;
|
||||
} catch(e){
|
||||
throw new Error('XML Parser could not be instantiated');
|
||||
}
|
||||
try{
|
||||
if(dXML.loadXML) out = (dXML.loadXML(sXML)) ? dXML : false;
|
||||
else out = dXML.parseFromString(sXML, 'text/xml');
|
||||
if (dXML.loadXML) {
|
||||
out = (dXML.loadXML(sXML)) ? dXML : false;
|
||||
}
|
||||
else {
|
||||
out = dXML.parseFromString(sXML, 'text/xml');
|
||||
}
|
||||
}
|
||||
catch(e){ throw new Error('Error parsing XML string'); }
|
||||
catch(e2){ throw new Error('Error parsing XML string'); }
|
||||
return out;
|
||||
};
|
||||
|
||||
@@ -286,10 +293,10 @@ svgedit.utilities.getUrlFromAttr = function(attrVal) {
|
||||
return attrVal.substring(5, attrVal.indexOf('"',6));
|
||||
}
|
||||
// url('#somegrad')
|
||||
else if (attrVal.indexOf("url('") === 0) {
|
||||
if (attrVal.indexOf("url('") === 0) {
|
||||
return attrVal.substring(5, attrVal.indexOf("'",6));
|
||||
}
|
||||
else if (attrVal.indexOf("url(") === 0) {
|
||||
if (attrVal.indexOf("url(") === 0) {
|
||||
return attrVal.substring(4, attrVal.indexOf(')'));
|
||||
}
|
||||
}
|
||||
@@ -349,10 +356,11 @@ svgedit.utilities.getPathBBox = function(path) {
|
||||
var start = seglist.getItem(0);
|
||||
var P0 = [start.x, start.y];
|
||||
|
||||
for(var i=0; i < tot; i++) {
|
||||
var i;
|
||||
for (i = 0; i < tot; i++) {
|
||||
var seg = seglist.getItem(i);
|
||||
|
||||
if(typeof seg.x == 'undefined') continue;
|
||||
if(typeof seg.x == 'undefined') {continue;}
|
||||
|
||||
// Add actual points to limits
|
||||
bounds[0].push(P0[0]);
|
||||
@@ -363,7 +371,8 @@ svgedit.utilities.getPathBBox = function(path) {
|
||||
P2 = [seg.x2, seg.y2],
|
||||
P3 = [seg.x, seg.y];
|
||||
|
||||
for(var j=0; j < 2; j++) {
|
||||
var j;
|
||||
for (j = 0; j < 2; j++) {
|
||||
|
||||
var calc = function(t) {
|
||||
return Math.pow(1-t,3) * P0[j]
|
||||
@@ -376,22 +385,22 @@ svgedit.utilities.getPathBBox = function(path) {
|
||||
var a = -3 * P0[j] + 9 * P1[j] - 9 * P2[j] + 3 * P3[j];
|
||||
var c = 3 * P1[j] - 3 * P0[j];
|
||||
|
||||
if(a == 0) {
|
||||
if(b == 0) {
|
||||
if (a == 0) {
|
||||
if (b == 0) {
|
||||
continue;
|
||||
}
|
||||
var t = -c / b;
|
||||
if(0 < t && t < 1) {
|
||||
if (0 < t && t < 1) {
|
||||
bounds[j].push(calc(t));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
var b2ac = Math.pow(b,2) - 4 * c * a;
|
||||
if(b2ac < 0) continue;
|
||||
if (b2ac < 0) {continue;}
|
||||
var t1 = (-b + Math.sqrt(b2ac))/(2 * a);
|
||||
if(0 < t1 && t1 < 1) bounds[j].push(calc(t1));
|
||||
if (0 < t1 && t1 < 1) {bounds[j].push(calc(t1));}
|
||||
var t2 = (-b - Math.sqrt(b2ac))/(2 * a);
|
||||
if(0 < t2 && t2 < 1) bounds[j].push(calc(t2));
|
||||
if (0 < t2 && t2 < 1) {bounds[j].push(calc(t2));}
|
||||
}
|
||||
P0 = P3;
|
||||
} else {
|
||||
@@ -426,10 +435,10 @@ function groupBBFix(selected) {
|
||||
}
|
||||
var ref = $.data(selected, 'ref');
|
||||
var matched = null;
|
||||
var ret;
|
||||
var ret, copy;
|
||||
|
||||
if(ref) {
|
||||
var copy = $(ref).children().clone().attr('visibility', 'hidden');
|
||||
copy = $(ref).children().clone().attr('visibility', 'hidden');
|
||||
$(svgroot_).append(copy);
|
||||
matched = copy.filter('line, path');
|
||||
} else {
|
||||
@@ -446,7 +455,7 @@ function groupBBFix(selected) {
|
||||
});
|
||||
if(issue) {
|
||||
var elems = ref ? copy : $(selected).children();
|
||||
ret = getStrokedBBox(elems);
|
||||
ret = getStrokedBBox(elems); // getStrokedBBox defined in svgcanvas
|
||||
} else {
|
||||
ret = selected.getBBox();
|
||||
}
|
||||
@@ -467,7 +476,7 @@ function groupBBFix(selected) {
|
||||
// elem - Optional DOM element to get the BBox for
|
||||
svgedit.utilities.getBBox = function(elem) {
|
||||
var selected = elem || editorContext_.geSelectedElements()[0];
|
||||
if (elem.nodeType != 1) return null;
|
||||
if (elem.nodeType != 1) {return null;}
|
||||
var ret = null;
|
||||
var elname = selected.nodeName;
|
||||
|
||||
@@ -485,7 +494,7 @@ svgedit.utilities.getBBox = function(elem) {
|
||||
if(!svgedit.browser.supportsPathBBox()) {
|
||||
ret = svgedit.utilities.getPathBBox(selected);
|
||||
} else {
|
||||
try { ret = selected.getBBox();} catch(e){}
|
||||
try { ret = selected.getBBox();} catch(e2){}
|
||||
}
|
||||
break;
|
||||
case 'g':
|
||||
@@ -498,7 +507,7 @@ svgedit.utilities.getBBox = function(elem) {
|
||||
ret = groupBBFix(selected, true);
|
||||
}
|
||||
if(elname === 'use' || ( elname === 'foreignObject' && svgedit.browser.isWebkit() ) ) {
|
||||
if(!ret) ret = selected.getBBox();
|
||||
if(!ret) {ret = selected.getBBox();}
|
||||
// This is resolved in later versions of webkit, perhaps we should
|
||||
// have a featured detection for correct 'use' behavior?
|
||||
// ——————————
|
||||
@@ -512,13 +521,13 @@ svgedit.utilities.getBBox = function(elem) {
|
||||
//}
|
||||
} else if(~visElems_arr.indexOf(elname)) {
|
||||
try { ret = selected.getBBox();}
|
||||
catch(e) {
|
||||
catch(e3) {
|
||||
// Check if element is child of a foreignObject
|
||||
var fo = $(selected).closest('foreignObject');
|
||||
if(fo.length) {
|
||||
try {
|
||||
ret = fo[0].getBBox();
|
||||
} catch(e) {
|
||||
} catch(e4) {
|
||||
ret = null;
|
||||
}
|
||||
} else {
|
||||
@@ -548,9 +557,10 @@ svgedit.utilities.getRotationAngle = function(elem, to_rad) {
|
||||
var selected = elem || editorContext_.getSelectedElements()[0];
|
||||
// find the rotation transform (if any) and set it
|
||||
var tlist = svgedit.transformlist.getTransformList(selected);
|
||||
if(!tlist) return 0; // <svg> elements have no tlist
|
||||
if(!tlist) {return 0;} // <svg> elements have no tlist
|
||||
var N = tlist.numberOfItems;
|
||||
for (var i = 0; i < N; ++i) {
|
||||
var i;
|
||||
for (i = 0; i < N; ++i) {
|
||||
var xform = tlist.getItem(i);
|
||||
if (xform.type == 4) {
|
||||
return to_rad ? xform.angle * Math.PI / 180.0 : xform.angle;
|
||||
@@ -604,12 +614,13 @@ if (svgedit.browser.supportsSelectors()) {
|
||||
// suspendLength - Optional integer of milliseconds to suspend redraw
|
||||
// unitCheck - Boolean to indicate the need to use svgedit.units.setUnitAttr
|
||||
svgedit.utilities.assignAttributes = function(node, attrs, suspendLength, unitCheck) {
|
||||
if(!suspendLength) suspendLength = 0;
|
||||
if(!suspendLength) {suspendLength = 0;}
|
||||
// Opera has a problem with suspendRedraw() apparently
|
||||
var handle = null;
|
||||
if (!svgedit.browser.isOpera()) svgroot_.suspendRedraw(suspendLength);
|
||||
if (!svgedit.browser.isOpera()) {svgroot_.suspendRedraw(suspendLength);}
|
||||
|
||||
for (var i in attrs) {
|
||||
var i;
|
||||
for (i in attrs) {
|
||||
var ns = (i.substr(0,4) === 'xml:' ? NS.XML :
|
||||
i.substr(0,6) === 'xlink:' ? NS.XLINK : null);
|
||||
|
||||
@@ -621,7 +632,7 @@ svgedit.utilities.assignAttributes = function(node, attrs, suspendLength, unitCh
|
||||
svgedit.units.setUnitAttr(node, i, attrs[i]);
|
||||
}
|
||||
}
|
||||
if (!svgedit.browser.isOpera()) svgroot_.unsuspendRedraw(handle);
|
||||
if (!svgedit.browser.isOpera()) {svgroot_.unsuspendRedraw(handle);}
|
||||
};
|
||||
|
||||
// Function: cleanupElement
|
||||
@@ -645,7 +656,8 @@ svgedit.utilities.cleanupElement = function(element) {
|
||||
'ry':0
|
||||
};
|
||||
|
||||
for(var attr in defaults) {
|
||||
var attr;
|
||||
for (attr in defaults) {
|
||||
var val = defaults[attr];
|
||||
if(element.getAttribute(attr) == val) {
|
||||
element.removeAttribute(attr);
|
||||
@@ -668,4 +680,4 @@ svgedit.utilities.snapToGrid = function(value) {
|
||||
return value;
|
||||
};
|
||||
|
||||
})();
|
||||
}());
|
||||
|
||||
Reference in New Issue
Block a user