Add third mode of id randomization and some tests

git-svn-id: http://svg-edit.googlecode.com/svn/trunk@1974 eee81c28-f429-11dd-99c0-75d572ba1ddd
This commit is contained in:
Jeff Schiller
2011-02-03 16:38:15 +00:00
parent e3a8fb92b2
commit 76a2ce76d5
2 changed files with 60 additions and 16 deletions

View File

@@ -23,7 +23,12 @@ var svg_ns = "http://www.w3.org/2000/svg";
var se_ns = "http://svg-edit.googlecode.com"; var se_ns = "http://svg-edit.googlecode.com";
var xmlns_ns = "http://www.w3.org/2000/xmlns/"; var xmlns_ns = "http://www.w3.org/2000/xmlns/";
var randomize_ids = false; var RandomizeModes = {
LET_DOCUMENT_DECIDE: 0,
ALWAYS_RANDOMIZE: 1,
NEVER_RANDOMIZE: 2
};
var randomize_ids = RandomizeModes.LET_DOCUMENT_DECIDE;
/** /**
* This class encapsulates the concept of a layer in the drawing * This class encapsulates the concept of a layer in the drawing
@@ -50,12 +55,15 @@ svgedit.draw.Layer.prototype.getGroup = function() {
// Params: // Params:
// enableRandomization - flag indicating if documents should have randomized ids // enableRandomization - flag indicating if documents should have randomized ids
svgedit.draw.randomizeIds = function(enableRandomization, current_drawing) { svgedit.draw.randomizeIds = function(enableRandomization, current_drawing) {
randomize_ids = enableRandomization; randomize_ids = enableRandomization == false ?
RandomizeModes.NEVER_RANDOMIZE :
RandomizeModes.ALWAYS_RANDOMIZE;
if (enableRandomization && !current_drawing.getNonce()) { if (randomize_ids == RandomizeModes.ALWAYS_RANDOMIZE && !current_drawing.getNonce()) {
current_drawing.setNonce(Math.floor(Math.random() * 100001)); current_drawing.setNonce(Math.floor(Math.random() * 100001));
} else if (randomize_ids == RandomizeModes.NEVER_RANDOMIZE && current_drawing.getNonce()) {
current_drawing.clearNonce();
} }
}; };
/** /**
@@ -120,12 +128,10 @@ svgedit.draw.Drawing = function(svgElem, opt_idPrefix) {
var n = this.svgElem_.getAttributeNS(se_ns, 'nonce'); var n = this.svgElem_.getAttributeNS(se_ns, 'nonce');
// If already set in the DOM, use the nonce throughout the document // If already set in the DOM, use the nonce throughout the document
// else, if randomizeIds(true) has been called, create and set the nonce. // else, if randomizeIds(true) has been called, create and set the nonce.
if (!!n) { if (!!n && randomize_ids != RandomizeModes.NEVER_RANDOMIZE) {
this.nonce_ = n; this.nonce_ = n;
} else if (randomize_ids) { } else if (randomize_ids == RandomizeModes.ALWAYS_RANDOMIZE) {
this.nonce_ = Math.floor(Math.random() * 100001); this.setNonce(Math.floor(Math.random() * 100001));
this.svgElem_.setAttributeNS(xmlns_ns, 'xmlns:se', se_ns);
this.svgElem_.setAttributeNS(se_ns, 'se:nonce', this.nonce_);
} }
}; };
@@ -153,6 +159,12 @@ svgedit.draw.Drawing.prototype.setNonce = function(n) {
this.nonce_ = n; this.nonce_ = n;
}; };
svgedit.draw.Drawing.prototype.clearNonce = function() {
// We deliberately leave any se:nonce attributes alone,
// we just don't use it to randomize ids.
this.nonce_ = "";
};
/** /**
* Returns the latest object id as a string. * Returns the latest object id as a string.
* @return {String} The latest object Id. * @return {String} The latest object Id.

View File

@@ -85,13 +85,22 @@
}); });
test('Test nonce', function() { test('Test nonce', function() {
expect(2); expect(7);
var doc = new svgedit.draw.Drawing(svg); var doc = new svgedit.draw.Drawing(svg);
equals(doc.getNonce(), ""); equals(doc.getNonce(), "");
doc = new svgedit.draw.Drawing(svg_n); doc = new svgedit.draw.Drawing(svg_n);
equals(doc.getNonce(), NONCE); equals(doc.getNonce(), NONCE);
equals(doc.getSvgElem().getAttributeNS(SENS, 'nonce'), NONCE);
doc.clearNonce();
ok(!doc.getNonce());
ok(!doc.getSvgElem().getAttributeNS(SENS, 'se:nonce'));
doc.setNonce(NONCE);
equals(doc.getNonce(), NONCE);
equals(doc.getSvgElem().getAttributeNS(SENS, 'nonce'), NONCE);
}); });
test('Test getId() and getNextId() without nonce', function() { test('Test getId() and getNextId() without nonce', function() {
@@ -481,14 +490,37 @@
}); });
test('Test svgedit.draw.randomizeIds()', function() { test('Test svgedit.draw.randomizeIds()', function() {
expect(2); expect(9);
svgedit.draw.randomizeIds(true); // Confirm in LET_DOCUMENT_DECIDE mode that the document decides
var drawing = new svgedit.draw.Drawing(svg.cloneNode(true)); // if there is a nonce.
var drawing = new svgedit.draw.Drawing(svg_n.cloneNode(true));
ok(!!drawing.getNonce()); ok(!!drawing.getNonce());
svgedit.draw.randomizeIds(false); drawing = new svgedit.draw.Drawing(svg.cloneNode(true));
var drawing = new svgedit.draw.Drawing(svg); ok(!drawing.getNonce());
// Confirm that a nonce is set once we're in ALWAYS_RANDOMIZE mode.
svgedit.draw.randomizeIds(true, drawing);
ok(!!drawing.getNonce());
// Confirm new drawings in ALWAYS_RANDOMIZE mode have a nonce.
drawing = new svgedit.draw.Drawing(svg.cloneNode(true));
ok(!!drawing.getNonce());
drawing.clearNonce();
ok(!drawing.getNonce());
// Confirm new drawings in NEVER_RANDOMIZE mode do not have a nonce
// but that their se:nonce attribute is left alone.
svgedit.draw.randomizeIds(false, drawing);
ok(!drawing.getNonce());
ok(!!drawing.getSvgElem().getAttributeNS(SENS, 'nonce'));
drawing = new svgedit.draw.Drawing(svg.cloneNode(true));
ok(!drawing.getNonce());
drawing = new svgedit.draw.Drawing(svg_n.cloneNode(true));
ok(!drawing.getNonce()); ok(!drawing.getNonce());
}); });