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 <svg> element.

The default behaviour is as before. But, if you call svgCanvas.randomizeIds() or if setSvgString() detects an se:nonce attribute on the root <svg> 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
master
Jacques Distler 2010-02-27 22:55:53 +00:00
parent cf4e097316
commit 5f6830745b
2 changed files with 75 additions and 5 deletions

View File

@ -12,8 +12,13 @@ $(function() {
svgCanvas.addExtension("Arrows", function(S) { svgCanvas.addExtension("Arrows", function(S) {
var svgcontent = S.svgcontent, var svgcontent = S.svgcontent,
addElem = S.addSvgElementFromJson, addElem = S.addSvgElementFromJson,
nonce = S.nonce,
randomize_ids = S.randomize_ids,
selElems; selElems;
svgCanvas.bind('setarrownonce', setArrowNonce);
svgCanvas.bind('unsetsetarrownonce', unsetArrowNonce);
var lang_list = { var lang_list = {
"en":[ "en":[
{"id": "arrow_none", "textContent": "No arrow" } {"id": "arrow_none", "textContent": "No arrow" }
@ -23,10 +28,32 @@ $(function() {
] ]
}; };
var pathdata = { var prefix = 'se_arrow_';
fw: {d:"m0,0l10,5l-10,5l5,-5l-5,-5z", refx:8, id:"se_arrow_fw"}, if (randomize_ids) {
bk: {d:"m10,0l-10,5l10,5l-5,-5l5,-5z", refx:2, id:"se_arrow_bk"} 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) { function getLinked(elem, attr) {
var str = elem.getAttribute(attr); var str = elem.getAttribute(attr);
if(!str) return null; if(!str) return null;
@ -77,7 +104,7 @@ $(function() {
function addMarker(dir, type, id) { function addMarker(dir, type, id) {
// TODO: Make marker (or use?) per arrow type, since refX can be different // 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); var marker = S.getElem(id);
@ -174,7 +201,7 @@ $(function() {
var last_id = marker.id; var last_id = marker.id;
var dir = last_id.indexOf('_fw') !== -1?'fw':'bk'; 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); $(new_marker).children().attr('fill', color);
} }

View File

@ -943,6 +943,10 @@ function BatchCommand(text) {
$(svgroot).appendTo(container); $(svgroot).appendTo(container);
//nonce to uniquify id's
var nonce = Math.floor(Math.random()*100001);
var randomize_ids = false;
// map namespace URIs to prefixes // map namespace URIs to prefixes
var nsMap = {}; var nsMap = {};
nsMap[xlinkns] = 'xlink'; nsMap[xlinkns] = 'xlink';
@ -982,8 +986,10 @@ function BatchCommand(text) {
y: 480, y: 480,
overflow: 'visible', overflow: 'visible',
xmlns: svgns, xmlns: svgns,
"xmlns:se": se_ns,
"xmlns:xlink": xlinkns "xmlns:xlink": xlinkns
}).appendTo(svgroot); }).appendTo(svgroot);
if (randomize_ids) svgContent.setAttributeNS(se_ns, nonce);
var convertToNum, convertToUnit, setUnitAttr; var convertToNum, convertToUnit, setUnitAttr;
@ -1298,7 +1304,11 @@ function BatchCommand(text) {
// private functions // private functions
var getId = function() { var getId = function() {
if (events["getid"]) return call("getid", obj_num); if (events["getid"]) return call("getid", obj_num);
if (randomize_ids) {
return idprefix + nonce +'_' + obj_num;
} else {
return idprefix + obj_num; return idprefix + obj_num;
}
}; };
var getNextId = function() { var getNextId = function() {
@ -5535,6 +5545,27 @@ function BatchCommand(text) {
return svgCanvasToString(); 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 // Function: setSvgString
// This function sets the current drawing as the input SVG XML. // This function sets the current drawing as the input SVG XML.
// //
@ -5558,6 +5589,17 @@ function BatchCommand(text) {
// set new svg document // set new svg document
svgcontent = svgroot.appendChild(svgdoc.importNode(newDoc.documentElement, true)); 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 // change image href vals if possible
$(svgcontent).find('image').each(function() { $(svgcontent).find('image').each(function() {
var image = this; var image = this;
@ -8012,6 +8054,7 @@ function BatchCommand(text) {
var ext = ext_func($.extend(canvas.getPrivateMethods(), { var ext = ext_func($.extend(canvas.getPrivateMethods(), {
svgroot: svgroot, svgroot: svgroot,
svgcontent: svgcontent, svgcontent: svgcontent,
nonce: nonce,
selectorManager: selectorManager selectorManager: selectorManager
})); }));
extensions[name] = ext; extensions[name] = ext;