From 5f6830745b5f044aa5d12f61b94a6d47b430f476 Mon Sep 17 00:00:00 2001 From: Jacques Distler Date: Sat, 27 Feb 2010 22:55:53 +0000 Subject: [PATCH] SVG-Edit-generated IDs can now sport a nonce (to uniquify them). The value of the nonce is recorded as an se:nonce attribute on the root element. The default behaviour is as before. But, if you call svgCanvas.randomizeIds() or if setSvgString() detects an se:nonce attribute on the root element, this will trigger the new behaviour (in the latter case, using the existing nonce). git-svn-id: http://svg-edit.googlecode.com/svn/trunk@1436 eee81c28-f429-11dd-99c0-75d572ba1ddd --- editor/extensions/ext-arrows.js | 37 ++++++++++++++++++++++++---- editor/svgcanvas.js | 43 +++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 5 deletions(-) diff --git a/editor/extensions/ext-arrows.js b/editor/extensions/ext-arrows.js index 1245eb92..477b0f35 100644 --- a/editor/extensions/ext-arrows.js +++ b/editor/extensions/ext-arrows.js @@ -12,8 +12,13 @@ $(function() { svgCanvas.addExtension("Arrows", function(S) { var svgcontent = S.svgcontent, addElem = S.addSvgElementFromJson, + nonce = S.nonce, + randomize_ids = S.randomize_ids, selElems; + svgCanvas.bind('setarrownonce', setArrowNonce); + svgCanvas.bind('unsetsetarrownonce', unsetArrowNonce); + var lang_list = { "en":[ {"id": "arrow_none", "textContent": "No arrow" } @@ -23,10 +28,32 @@ $(function() { ] }; - var pathdata = { - fw: {d:"m0,0l10,5l-10,5l5,-5l-5,-5z", refx:8, id:"se_arrow_fw"}, - bk: {d:"m10,0l-10,5l10,5l-5,-5l5,-5z", refx:2, id:"se_arrow_bk"} + var prefix = 'se_arrow_'; + if (randomize_ids) { + var arrowprefix = prefix + nonce + '_'; + } else { + var arrowprefix = prefix; } + + var pathdata = { + fw: {d:"m0,0l10,5l-10,5l5,-5l-5,-5z", refx:8, id: arrowprefix + 'fw'}, + bk: {d:"m10,0l-10,5l10,5l-5,-5l5,-5z", refx:2, id: arrowprefix + 'bk'} + } + + function setArrowNonce(window, n) { + randomize_ids = true; + arrowprefix = prefix + n + '_'; + pathdata.fw.id = arrowprefix + 'fw'; + pathdata.bk.id = arrowprefix + 'bk'; + } + + function unsetArrowNonce(window) { + randomize_ids = false; + arrowprefix = prefix; + pathdata.fw.id = arrowprefix + 'fw'; + pathdata.bk.id = arrowprefix + 'bk'; + } + function getLinked(elem, attr) { var str = elem.getAttribute(attr); if(!str) return null; @@ -77,7 +104,7 @@ $(function() { function addMarker(dir, type, id) { // TODO: Make marker (or use?) per arrow type, since refX can be different - id = id || 'se_arrow_' + dir; + id = id || arrowprefix + dir; var marker = S.getElem(id); @@ -174,7 +201,7 @@ $(function() { var last_id = marker.id; var dir = last_id.indexOf('_fw') !== -1?'fw':'bk'; - new_marker = addMarker(dir, type, 'se_arrow_' + dir + all_markers.length); + new_marker = addMarker(dir, type, arrowprefix + dir + all_markers.length); $(new_marker).children().attr('fill', color); } diff --git a/editor/svgcanvas.js b/editor/svgcanvas.js index 8920c88e..854aa3b2 100644 --- a/editor/svgcanvas.js +++ b/editor/svgcanvas.js @@ -943,6 +943,10 @@ function BatchCommand(text) { $(svgroot).appendTo(container); + //nonce to uniquify id's + var nonce = Math.floor(Math.random()*100001); + var randomize_ids = false; + // map namespace URIs to prefixes var nsMap = {}; nsMap[xlinkns] = 'xlink'; @@ -982,8 +986,10 @@ function BatchCommand(text) { y: 480, overflow: 'visible', xmlns: svgns, + "xmlns:se": se_ns, "xmlns:xlink": xlinkns }).appendTo(svgroot); + if (randomize_ids) svgContent.setAttributeNS(se_ns, nonce); var convertToNum, convertToUnit, setUnitAttr; @@ -1298,7 +1304,11 @@ function BatchCommand(text) { // private functions var getId = function() { if (events["getid"]) return call("getid", obj_num); + if (randomize_ids) { + return idprefix + nonce +'_' + obj_num; + } else { return idprefix + obj_num; + } }; var getNextId = function() { @@ -5535,6 +5545,27 @@ function BatchCommand(text) { return svgCanvasToString(); }; + //function randomizeIds + // This function determines whether to add a nonce to the prefix, when + // generating IDs in SVG-Edit + // + // Parameters: + // an opional boolean, which, if true, adds a nonce to the prefix. Thus + // svgCanvas.randomizeIds() <==> svgCanvas.randomizeIds(true) + // + // if you're controlling SVG-Edit externally, and want randomized IDs, call + // this BEFORE calling svgCanvas.setSvgString + // + this.randomizeIds = function() { + if (arguments.length > 0 && arguments[0] == false) { + randomize_ids = false; + if (extensions["Arrows"]) call("unsetarrownonce") ; + } else { + randomize_ids = true; + } + } + + // // Function: setSvgString // This function sets the current drawing as the input SVG XML. // @@ -5558,6 +5589,17 @@ function BatchCommand(text) { // set new svg document svgcontent = svgroot.appendChild(svgdoc.importNode(newDoc.documentElement, true)); + // retrieve or set the nonce + n = svgcontent.getAttributeNS(se_ns, 'nonce'); + if (n) { + randomize_ids = true; + nonce = n; + if (extensions["Arrows"]) call("setarrownonce", n) ; + } else if (randomize_ids) { + svgcontent.setAttributeNS(xmlnsns, 'xml:se', se_ns); + svgcontent.setAttributeNS(se_ns, 'se:nonce', nonce); + if (extensions["Arrows"]) call("setarrownonce", nonce) ; + } // change image href vals if possible $(svgcontent).find('image').each(function() { var image = this; @@ -8012,6 +8054,7 @@ function BatchCommand(text) { var ext = ext_func($.extend(canvas.getPrivateMethods(), { svgroot: svgroot, svgcontent: svgcontent, + nonce: nonce, selectorManager: selectorManager })); extensions[name] = ext;