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
master
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 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
@ -50,12 +55,15 @@ svgedit.draw.Layer.prototype.getGroup = function() {
// Params:
// enableRandomization - flag indicating if documents should have randomized ids
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));
} 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');
// If already set in the DOM, use the nonce throughout the document
// else, if randomizeIds(true) has been called, create and set the nonce.
if (!!n) {
if (!!n && randomize_ids != RandomizeModes.NEVER_RANDOMIZE) {
this.nonce_ = n;
} else if (randomize_ids) {
this.nonce_ = Math.floor(Math.random() * 100001);
this.svgElem_.setAttributeNS(xmlns_ns, 'xmlns:se', se_ns);
this.svgElem_.setAttributeNS(se_ns, 'se:nonce', this.nonce_);
} else if (randomize_ids == RandomizeModes.ALWAYS_RANDOMIZE) {
this.setNonce(Math.floor(Math.random() * 100001));
}
};
@ -153,6 +159,12 @@ svgedit.draw.Drawing.prototype.setNonce = function(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.
* @return {String} The latest object Id.

View File

@ -85,15 +85,24 @@
});
test('Test nonce', function() {
expect(2);
expect(7);
var doc = new svgedit.draw.Drawing(svg);
equals(doc.getNonce(), "");
doc = new svgedit.draw.Drawing(svg_n);
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() {
expect(7);
@ -481,14 +490,37 @@
});
test('Test svgedit.draw.randomizeIds()', function() {
expect(2);
expect(9);
// Confirm in LET_DOCUMENT_DECIDE mode that the document decides
// if there is a nonce.
var drawing = new svgedit.draw.Drawing(svg_n.cloneNode(true));
ok(!!drawing.getNonce());
svgedit.draw.randomizeIds(true);
var drawing = new svgedit.draw.Drawing(svg.cloneNode(true));
drawing = new svgedit.draw.Drawing(svg.cloneNode(true));
ok(!drawing.getNonce());
// Confirm that a nonce is set once we're in ALWAYS_RANDOMIZE mode.
svgedit.draw.randomizeIds(true, drawing);
ok(!!drawing.getNonce());
svgedit.draw.randomizeIds(false);
var drawing = new svgedit.draw.Drawing(svg);
// 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());
});