From d12694f9fa0f08defc593e0950d18d2f363a3f4d Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Fri, 31 Jan 2014 01:05:03 +0000 Subject: [PATCH] More JSLint git-svn-id: http://svg-edit.googlecode.com/svn/trunk@2642 eee81c28-f429-11dd-99c0-75d572ba1ddd --- editor/path.js | 87 +++++++++++++++++++++++++------------------ editor/recalculate.js | 32 +++++++++------- editor/sanitize.js | 16 ++++---- editor/select.js | 54 +++++++++++++++------------ editor/svgedit.js | 5 ++- 5 files changed, 111 insertions(+), 83 deletions(-) diff --git a/editor/path.js b/editor/path.js index d75bb714..ddfe948f 100644 --- a/editor/path.js +++ b/editor/path.js @@ -1,3 +1,5 @@ +/*globals $, svgedit, svgroot*/ +/*jslint vars: true, eqeq: true, continue: true*/ /** * Package: svgedit.path * @@ -13,7 +15,7 @@ // 3) math.js // 4) svgutils.js -(function() { +(function() {'use strict'; if (!svgedit.path) { svgedit.path = {}; @@ -75,12 +77,13 @@ svgedit.path.insertItemBefore = function(elem, newseg, index) { } var len = list.numberOfItems; var arr = []; - for (var i=0; i= len ? null : segs[i+1]; var prev_seg = (i-1) < 0 ? null : segs[i-1]; - + var start_seg; if (seg.type === 2) { if (prev_seg && prev_seg.type !== 1) { // New sub-path, last one is open, // so add a grip to last sub-path's first point - var start_seg = segs[start_i]; + start_seg = segs[start_i]; start_seg.next = segs[start_i+1]; start_seg.next.prev = start_seg; start_seg.addGrip(); @@ -608,7 +619,7 @@ svgedit.path.Path.prototype.init = function() { if (seg.type !== 1) { // Last seg, doesn't close so add a grip // to last sub-path's first point - var start_seg = segs[start_i]; + start_seg = segs[start_i]; start_seg.next = segs[start_i+1]; start_seg.next.prev = start_seg; start_seg.addGrip(); @@ -634,17 +645,18 @@ svgedit.path.Path.prototype.init = function() { }; svgedit.path.Path.prototype.eachSeg = function(fn) { + var i; var len = this.segs.length; - for (var i=0; i < len; i++) { + for (i = 0; i < len; i++) { var ret = fn.call(this.segs[i], i); - if (ret === false) break; + if (ret === false) {break;} } }; svgedit.path.Path.prototype.addSeg = function(index) { // Adds a new segment var seg = this.segs[index]; - if (!seg.prev) return; + if (!seg.prev) {return;} var prev = seg.prev; var newseg; @@ -683,9 +695,10 @@ svgedit.path.Path.prototype.deleteSeg = function(index) { seg.show(false); var next = seg.next; + var pt; if (seg.mate) { // Make the next point be the "M" point - var pt = [next.item.x, next.item.y]; + pt = [next.item.x, next.item.y]; svgedit.path.replacePathSeg(2, next.index, pt); // Reposition last node @@ -695,7 +708,7 @@ svgedit.path.Path.prototype.deleteSeg = function(index) { } else if (!seg.prev) { // First node of open path, make next point the M var item = seg.item; - var pt = [next.item.x, next.item.y]; + pt = [next.item.x, next.item.y]; svgedit.path.replacePathSeg(2, seg.next.index, pt); list.removeItem(index); } else { @@ -707,7 +720,7 @@ svgedit.path.Path.prototype.subpathIsClosed = function(index) { var closed = false; // Check if subpath is already open svgedit.path.path.eachSeg(function(i) { - if (i <= index) return true; + if (i <= index) {return true;} if (this.type === 2) { // Found M first, so open return false; @@ -782,7 +795,7 @@ svgedit.path.Path.prototype.setSegType = function(new_type) { // Selected seg var cur = this.segs[sel_pt]; var prev = cur.prev; - if (!prev) continue; + if (!prev) {continue;} if (!new_type) { // double-click, so just toggle text = 'Toggle Path Segment Type'; @@ -877,12 +890,14 @@ svgedit.path.Path.prototype.update = function() { svgedit.path.getPath_ = function(elem) { var p = pathData[elem.id]; - if (!p) p = pathData[elem.id] = new svgedit.path.Path(elem); + if (!p) { + p = pathData[elem.id] = new svgedit.path.Path(elem); + } return p; }; svgedit.path.removePath_ = function(id) { - if (id in pathData) delete pathData[id]; + if (id in pathData) {delete pathData[id];} }; var getRotVals = function(x, y) { @@ -919,7 +934,7 @@ var getRotVals = function(x, y) { svgedit.path.recalcRotatedPath = function() { var current_path = svgedit.path.path.elem; var angle = svgedit.utilities.getRotationAngle(current_path, true); - if (!angle) return; + if (!angle) {return;} // selectedBBoxes[0] = svgedit.path.path.oldbbox; var box = svgedit.utilities.getBBox(current_path), oldbox = svgedit.path.path.oldbbox,//selectedBBoxes[0], @@ -943,7 +958,7 @@ svgedit.path.recalcRotatedPath = function() { i -= 1; var seg = list.getItem(i), type = seg.pathSegType; - if (type == 1) continue; + if (type == 1) {continue;} var rvals = getRotVals(seg.x, seg.y), points = [rvals.x, rvals.y]; @@ -973,4 +988,4 @@ svgedit.path.clearData = function() { pathData = {}; }; -})(); +}()); diff --git a/editor/recalculate.js b/editor/recalculate.js index 2eb0cbcb..3585147c 100644 --- a/editor/recalculate.js +++ b/editor/recalculate.js @@ -1,3 +1,5 @@ +/*globals $*/ +/*jslint vars: true, eqeq: true, continue: true*/ /** * Recalculate. * @@ -63,7 +65,7 @@ svgedit.recalculate.updateClipPath = function(attr, tx, ty) { // Returns: // Undo command object with the resulting change svgedit.recalculate.recalculateDimensions = function(selected) { - if (selected == null) return null; + if (selected == null) {return null;} // Firefox Issue - 1081 if (selected.nodeName == "svg" && navigator.userAgent.indexOf("Firefox/20") >= 0) { @@ -72,10 +74,10 @@ svgedit.recalculate.recalculateDimensions = function(selected) { var svgroot = context_.getSVGRoot(); var tlist = svgedit.transformlist.getTransformList(selected); - + var k; // remove any unnecessary transforms if (tlist && tlist.numberOfItems > 0) { - var k = tlist.numberOfItems; + k = tlist.numberOfItems; while (k--) { var xform = tlist.getItem(k); if (xform.type === 0) { @@ -96,7 +98,7 @@ svgedit.recalculate.recalculateDimensions = function(selected) { } // End here if all it has is a rotation if (tlist.numberOfItems === 1 && - svgedit.utilities.getRotationAngle(selected)) return null; + svgedit.utilities.getRotationAngle(selected)) {return null;} } // if this element had no transforms, we are done @@ -109,7 +111,7 @@ svgedit.recalculate.recalculateDimensions = function(selected) { // TODO: Make this work for more than 2 if (tlist) { - var k = tlist.numberOfItems; + k = tlist.numberOfItems; var mxs = []; while (k--) { var xform = tlist.getItem(k); @@ -187,19 +189,20 @@ svgedit.recalculate.recalculateDimensions = function(selected) { case 'polygon': case 'polyline': initial = {}; - initial['points'] = selected.getAttribute('points'); + initial.points = selected.getAttribute('points'); var list = selected.points; var len = list.numberOfItems; - changes['points'] = new Array(len); - for (var i = 0; i < len; ++i) { + changes.points = new Array(len); + var i; + for (i = 0; i < len; ++i) { var pt = list.getItem(i); - changes['points'][i] = {x:pt.x, y:pt.y}; + changes.points[i] = {x:pt.x, y:pt.y}; } break; case 'path': initial = {}; - initial['d'] = selected.getAttribute('d'); - changes['d'] = selected.getAttribute('d'); + initial.d = selected.getAttribute('d'); + changes.d = selected.getAttribute('d'); break; } // switch on element type to get initial values @@ -246,7 +249,8 @@ svgedit.recalculate.recalculateDimensions = function(selected) { // FIXME: This blows up if the angle is exactly 0! var s = 2/a; } - for (var i = 0; i < tlist.numberOfItems; ++i) { + var i; + for (i = 0; i < tlist.numberOfItems; ++i) { var xform = tlist.getItem(i); if (xform.type == 4) { // extract old center through mystical arts @@ -288,7 +292,7 @@ svgedit.recalculate.recalculateDimensions = function(selected) { var childTlist = svgedit.transformlist.getTransformList(child); // some children might not have a transform (, , etc) - if (!childTlist) continue; + if (!childTlist) {continue;} var m = svgedit.math.transformListToTransform(childTlist).matrix; @@ -473,7 +477,7 @@ svgedit.recalculate.recalculateDimensions = function(selected) { context_.setStartTransform(child.getAttribute('transform')); var childTlist = svgedit.transformlist.getTransformList(child); - if (!childTlist) continue; + if (!childTlist) {continue;} var em = svgedit.math.matrixMultiply(m, svgedit.math.transformListToTransform(childTlist).matrix); var e2m = svgroot.createSVGTransform(); diff --git a/editor/sanitize.js b/editor/sanitize.js index 4839809c..3fe901e0 100644 --- a/editor/sanitize.js +++ b/editor/sanitize.js @@ -1,3 +1,5 @@ +/*globals $, svgedit*/ +/*jslint vars: true, eqeq: true*/ /** * Package: svgedit.sanitize * @@ -12,7 +14,7 @@ // 2) browser.js // 3) svgutils.js -(function() { +(function() {'use strict'; if (!svgedit.sanitize) { svgedit.sanitize = {}; @@ -138,12 +140,12 @@ svgedit.sanitize.sanitizeSvg = function(node) { var allowedAttrs = svgWhiteList_[node.nodeName]; var allowedAttrsNS = svgWhiteListNS_[node.nodeName]; - + var i; // if this element is supported, sanitize it if (typeof allowedAttrs !== 'undefined') { var seAttrs = []; - var i = node.attributes.length; + i = node.attributes.length; while (i--) { // if the attribute is not in our whitelist, then remove it // could use jQuery's inArray(), but I don't know if that's any better @@ -173,6 +175,7 @@ svgedit.sanitize.sanitizeSvg = function(node) { case 'patternTransform': var val = attr.nodeValue.replace(/(\d)-/g, '$1 -'); node.setAttribute(attrName, val); + break; } } @@ -202,8 +205,7 @@ svgedit.sanitize.sanitizeSvg = function(node) { var href = svgedit.utilities.getHref(node); if (href && ['filter', 'linearGradient', 'pattern', - 'radialGradient', 'textPath', 'use'].indexOf(node.nodeName) >= 0) - { + 'radialGradient', 'textPath', 'use'].indexOf(node.nodeName) >= 0) { // TODO: we simply check if the first character is a #, is this bullet-proof? if (href[0] != '#') { // remove the attribute (but keep the element) @@ -248,9 +250,9 @@ svgedit.sanitize.sanitizeSvg = function(node) { parent.removeChild(node); // call sanitizeSvg on each of those children - var i = children.length; + i = children.length; while (i--) { svgedit.sanitize.sanitizeSvg(children[i]); } } }; -})(); +}()); diff --git a/editor/select.js b/editor/select.js index 07309501..587d3198 100644 --- a/editor/select.js +++ b/editor/select.js @@ -1,3 +1,5 @@ +/*globals $, svgedit*/ +/*jslint vars: true, eqeq: true, forin: true*/ /** * Package: svedit.select * @@ -13,7 +15,7 @@ // 3) math.js // 4) svgutils.js -(function() { +(function() {'use strict'; if (!svgedit.select) { svgedit.select = {}; @@ -96,10 +98,11 @@ svgedit.select.Selector.prototype.reset = function(e) { // Parameters: // angle - Float indicating current rotation angle in degrees svgedit.select.Selector.prototype.updateGripCursors = function(angle) { - var dir_arr = []; - var steps = Math.round(angle / 45); - if (steps < 0) steps += 8; - for (var dir in selectorManager_.selectorGrips) { + var dir, + dir_arr = [], + steps = Math.round(angle / 45); + if (steps < 0) {steps += 8;} + for (dir in selectorManager_.selectorGrips) { dir_arr.push(dir); } while (steps > 0) { @@ -107,7 +110,7 @@ svgedit.select.Selector.prototype.updateGripCursors = function(angle) { steps--; } var i = 0; - for (var dir in selectorManager_.selectorGrips) { + for (dir in selectorManager_.selectorGrips) { selectorManager_.selectorGrips[dir].setAttribute('style', ('cursor:' + dir_arr[i] + '-resize')); i++; } @@ -169,8 +172,8 @@ svgedit.select.Selector.prototype.resize = function() { } // apply the transforms - var l = bbox.x, t = bbox.y, w = bbox.width, h = bbox.height, - bbox = {x:l, y:t, width:w, height:h}; + var l = bbox.x, t = bbox.y, w = bbox.width, h = bbox.height; + bbox = {x:l, y:t, width:w, height:h}; // we need to handle temporary transforms too // if skewed, get its transformed box, then find its axis-aligned bbox @@ -206,12 +209,12 @@ svgedit.select.Selector.prototype.resize = function() { maxx = tl.x, maxy = tl.y; - var Min = Math.min, Max = Math.max; + var min = Math.min, max = Math.max; - minx = Min(minx, Min(nbox.tr.x, Min(nbox.bl.x, nbox.br.x) ) ) - offset; - miny = Min(miny, Min(nbox.tr.y, Min(nbox.bl.y, nbox.br.y) ) ) - offset; - maxx = Max(maxx, Max(nbox.tr.x, Max(nbox.bl.x, nbox.br.x) ) ) + offset; - maxy = Max(maxy, Max(nbox.tr.y, Max(nbox.bl.y, nbox.br.y) ) ) + offset; + minx = min(minx, min(nbox.tr.x, min(nbox.bl.x, nbox.br.x) ) ) - offset; + miny = min(miny, min(nbox.tr.y, min(nbox.bl.y, nbox.br.y) ) ) - offset; + maxx = max(maxx, max(nbox.tr.x, max(nbox.bl.x, nbox.br.x) ) ) + offset; + maxy = max(maxy, max(nbox.tr.y, max(nbox.bl.y, nbox.br.y) ) ) + offset; nbax = minx; nbay = miny; @@ -241,8 +244,8 @@ svgedit.select.Selector.prototype.resize = function() { 'e': [nbax + nbaw, nbay + (nbah)/2], 's': [nbax + (nbaw)/2, nbay + nbah] }; - - for (var dir in this.gripCoords) { + var dir; + for (dir in this.gripCoords) { var coords = this.gripCoords[dir]; selectedGrips[dir].setAttribute('cx', coords[0]); selectedGrips[dir].setAttribute('cy', coords[1]); @@ -320,7 +323,8 @@ svgedit.select.SelectorManager.prototype.initGroup = function() { this.rubberBandBox = null; // add the corner grips - for (var dir in this.selectorGrips) { + var dir; + for (dir in this.selectorGrips) { var grip = svgFactory_.createSVGElement({ 'element': 'circle', 'attr': { @@ -369,7 +373,7 @@ svgedit.select.SelectorManager.prototype.initGroup = function() { ); $.data(this.rotateGrip, 'type', 'rotate'); - if ($('#canvasBackground').length) return; + if ($('#canvasBackground').length) {return;} var dims = config_.dimensions; var canvasbg = svgFactory_.createSVGElement({ @@ -412,14 +416,15 @@ svgedit.select.SelectorManager.prototype.initGroup = function() { // Parameters: // elem - DOM element to get the selector for svgedit.select.SelectorManager.prototype.requestSelector = function(elem) { - if (elem == null) return null; - var N = this.selectors.length; + if (elem == null) {return null;} + var i, + N = this.selectors.length; // If we've already acquired one for this element, return it. if (typeof(this.selectorMap[elem.id]) == 'object') { this.selectorMap[elem.id].locked = true; return this.selectorMap[elem.id]; } - for (var i = 0; i < N; ++i) { + for (i = 0; i < N; ++i) { if (this.selectors[i] && !this.selectors[i].locked) { this.selectors[i].locked = true; this.selectors[i].reset(elem); @@ -440,10 +445,11 @@ svgedit.select.SelectorManager.prototype.requestSelector = function(elem) { // Parameters: // elem - DOM element to remove the selector for svgedit.select.SelectorManager.prototype.releaseSelector = function(elem) { - if (elem == null) return; - var N = this.selectors.length, + if (elem == null) {return;} + var i, + N = this.selectors.length, sel = this.selectorMap[elem.id]; - for (var i = 0; i < N; ++i) { + for (i = 0; i < N; ++i) { if (this.selectors[i] && this.selectors[i] == sel) { if (sel.locked == false) { // TODO(codedread): Ensure this exists in this module. @@ -525,4 +531,4 @@ svgedit.select.getSelectorManager = function() { return selectorManager_; }; -})(); \ No newline at end of file +}()); \ No newline at end of file diff --git a/editor/svgedit.js b/editor/svgedit.js index 44d4352c..6149b2de 100644 --- a/editor/svgedit.js +++ b/editor/svgedit.js @@ -1,7 +1,8 @@ +/*globals $, svgedit*/ /** * * Licensed under the MIT License - * main object, loaded first so other modules have the garanty of its existence + * main object, loaded first so other modules have the guarantee of its existence */ svgedit = { @@ -18,7 +19,7 @@ svgedit = { }; // return the svgedit.NS with key values switched and lowercase -svgedit.getReverseNS = function() { +svgedit.getReverseNS = function() {'use strict'; var reverseNS = {}; $.each(this.NS, function(name, URI) { reverseNS[URI] = name.toLowerCase();