From 3b1b46f874103ce38bcf4f6d4456519e5cbe05ba Mon Sep 17 00:00:00 2001 From: Jeff Schiller Date: Thu, 2 Dec 2010 17:14:24 +0000 Subject: [PATCH] Fix some bugs with svgutils.js. Add a couple tiny tests to selector_test.html git-svn-id: http://svg-edit.googlecode.com/svn/trunk@1880 eee81c28-f429-11dd-99c0-75d572ba1ddd --- editor/svgcanvas.js | 12 ++++++++---- editor/svgutils.js | 36 ++++++++++++++++++++++++++++-------- test/select_test.html | 14 +++++++++++++- 3 files changed, 49 insertions(+), 13 deletions(-) diff --git a/editor/svgcanvas.js b/editor/svgcanvas.js index b12cd988..3b3436d9 100644 --- a/editor/svgcanvas.js +++ b/editor/svgcanvas.js @@ -197,6 +197,10 @@ $.extend(all_properties.text, { // Current shape style properties var cur_shape = all_properties.shape; +// Array with all the currently selected elements +// default size of 1 until it needs to grow bigger +var selectedElements = new Array(1); + // Function: getElem // Get a DOM element by ID within the SVG root element. @@ -347,6 +351,10 @@ svgedit.units.init({ var convertToNum = canvas.convertToNum = svgedit.units.convertToNum; // import from svgutils.js +svgedit.utilities.init({ + getSelectedElements: function() { return selectedElements; }, + getSVGContent: function() { return svgcontent; } +}); var getUrlFromAttr = canvas.getUrlFromAttr = svgedit.utilities.getUrlFromAttr; var getHref = canvas.getHref = svgedit.utilities.getHref; var setHref = canvas.setHref = svgedit.utilities.setHref; @@ -541,10 +549,6 @@ var cur_text = all_properties.text, // Current general properties cur_properties = cur_shape, - // Array with all the currently selected elements - // default size of 1 until it needs to grow bigger - selectedElements = new Array(1), - // Array with selected elements' Bounding box object selectedBBoxes = new Array(1), diff --git a/editor/svgutils.js b/editor/svgutils.js index f4bdbe4f..9a0c42ad 100644 --- a/editor/svgutils.js +++ b/editor/svgutils.js @@ -23,17 +23,38 @@ if (!svgedit.utilities) { svgedit.utilities = {}; } +/** + * @interface + */ +svgedit.utilities.FooInterface = function() { +}; +svgedit.utilities.prototype.FooInterface.funky = function() {}; + +/** + * @constructor + * @implements {svgedit.utilities.FooInterface} + */ +svgedit.utilities.FooImplementor = function() { +}; + // Constants // String used to encode base64. -var KEYSTR = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; -var XLINKNS = "http://www.w3.org/1999/xlink"; +var KEYSTR = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; +var SVGNS = 'http://www.w3.org/2000/svg'; +var XLINKNS = 'http://www.w3.org/1999/xlink'; // Much faster than running getBBox() every time var visElems = 'a,circle,ellipse,foreignObject,g,image,line,path,polygon,polyline,rect,svg,text,tspan,use'; var visElems_arr = visElems.split(','); //var hidElems = 'clipPath,defs,desc,feGaussianBlur,filter,linearGradient,marker,mask,metadata,pattern,radialGradient,stop,switch,symbol,title,textPath'; +var editorContext_ = null; + +svgedit.utilities.init = function(editorContext) { + editorContext_ = editorContext; +}; + // Function: svgedit.utilities.toXml // Converts characters in a string to XML-friendly entities. // @@ -61,7 +82,6 @@ svgedit.utilities.fromXml = function(str) { return $('

').html(str).text(); }; - // This code was written by Tyler Akins and has been placed in the // public domain. It would be nice if you left this header intact. // Base64 code from Tyler Akins -- http://rumkin.com @@ -304,14 +324,14 @@ svgedit.utilities.setHref = function(elem, val) { // Returns: // The document's element, create it first if necessary svgedit.utilities.findDefs = function(svgElement) { - var svgElement = svgDoc.documentElement; - var defs = svgElement.getElementsByTagNameNS(svgns, "defs"); + var svgElement = editorContext_.getSVGContent().documentElement; + var defs = svgElement.getElementsByTagNameNS(SVGNS, "defs"); if (defs.length > 0) { defs = defs[0]; } else { // first child is a comment, so call nextSibling - defs = svgElement.insertBefore( svgElement.ownerDocument.createElementNS(svgns, "defs" ), svgElement.firstChild.nextSibling); + defs = svgElement.insertBefore( svgElement.ownerDocument.createElementNS(SVGNS, "defs" ), svgElement.firstChild.nextSibling); } return defs; }; @@ -406,7 +426,7 @@ svgedit.utilities.getPathBBox = function(path) { // Parameters: // elem - Optional DOM element to get the BBox for svgedit.utilities.getBBox = function(elem) { - var selected = elem || selectedElements[0]; + var selected = elem || editorContext_.geSelectedElements()[0]; if (elem.nodeType != 1) return null; var ret = null; var elname = selected.nodeName; @@ -459,7 +479,7 @@ svgedit.utilities.getBBox = function(elem) { // Returns: // Float with the angle in degrees or radians svgedit.utilities.getRotationAngle = function(elem, to_rad) { - var selected = elem || selectedElements[0]; + 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; // elements have no tlist diff --git a/test/select_test.html b/test/select_test.html index ae907b2d..b2baeb96 100644 --- a/test/select_test.html +++ b/test/select_test.html @@ -63,7 +63,10 @@ }) ); sandbox.appendChild(svgroot); + } + function setUpWithInit() { + setUp(); svgedit.select.init(mockConfig, mockFactory); } @@ -89,12 +92,20 @@ }); test('Test Selector DOM structure', function() { - expect(20); + expect(24); setUp(); ok(svgroot); ok(svgroot.hasChildNodes()); + + // Verify non-existence of Selector DOM nodes + equals(svgroot.childNodes.length, 1); + equals(svgroot.childNodes.item(0), svgcontent); + ok(!svgroot.querySelector('#selectorParentGroup')); + + svgedit.select.init(mockConfig, mockFactory); + equals(svgroot.childNodes.length, 3); // Verify existence of canvas background. @@ -108,6 +119,7 @@ // Verify existence of selectorParentGroup. var spg = svgroot.childNodes.item(2); ok(spg); + equals(svgroot.querySelector('#selectorParentGroup'), spg); equals(spg.id, 'selectorParentGroup'); equals(spg.tagName, 'g');