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:
@@ -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.
|
||||||
|
|||||||
@@ -85,15 +85,24 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
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() {
|
||||||
expect(7);
|
expect(7);
|
||||||
|
|
||||||
@@ -481,14 +490,37 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('Test svgedit.draw.randomizeIds()', function() {
|
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);
|
drawing = new svgedit.draw.Drawing(svg.cloneNode(true));
|
||||||
var 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());
|
ok(!!drawing.getNonce());
|
||||||
|
|
||||||
svgedit.draw.randomizeIds(false);
|
// Confirm new drawings in ALWAYS_RANDOMIZE mode have a nonce.
|
||||||
var drawing = new svgedit.draw.Drawing(svg);
|
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());
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user