fixed unit[s] typo in svgedit.units.convertUnit, added the corresponding test

git-svn-id: http://svg-edit.googlecode.com/svn/trunk@2382 eee81c28-f429-11dd-99c0-75d572ba1ddd
master
Bruno Heridet 2013-02-14 12:18:13 +00:00
parent 7f560c0ed1
commit 5e68aefef8
2 changed files with 94 additions and 82 deletions

View File

@ -18,10 +18,10 @@ if (!svgedit.units) {
svgedit.units = {}; svgedit.units = {};
} }
var w_attrs = ['x', 'x1', 'cx', 'rx', 'width']; var wAttrs = ['x', 'x1', 'cx', 'rx', 'width'];
var h_attrs = ['y', 'y1', 'cy', 'ry', 'height']; var hAttrs = ['y', 'y1', 'cy', 'ry', 'height'];
var unit_attrs = $.merge(['r','radius'], w_attrs); var unitAttrs = ['r','radius'].concat(wAttrs, hAttrs);
// unused
var unitNumMap = { var unitNumMap = {
'%': 2, '%': 2,
'em': 3, 'em': 3,
@ -34,15 +34,13 @@ var unitNumMap = {
'pc': 10 'pc': 10
}; };
$.merge(unit_attrs, h_attrs);
// Container of elements. // Container of elements.
var elementContainer_; var elementContainer_;
/** /**
* Stores mapping of unit type to user coordinates. * Stores mapping of unit type to user coordinates.
*/ */
var typeMap_ = {px: 1}; var typeMap_ = {};
/** /**
* ElementContainer interface * ElementContainer interface
@ -78,14 +76,17 @@ svgedit.units.init = function(elementContainer) {
document.body.removeChild(svg); document.body.removeChild(svg);
var inch = bb.x; var inch = bb.x;
typeMap_['em'] = bb.width; typeMap_ = {
typeMap_['ex'] = bb.height; 'em': bb.width,
typeMap_['in'] = inch; 'ex': bb.height,
typeMap_['cm'] = inch / 2.54; 'in': inch,
typeMap_['mm'] = inch / 25.4; 'cm': inch / 2.54,
typeMap_['pt'] = inch / 72; 'mm': inch / 25.4,
typeMap_['pc'] = inch / 6; 'pt': inch / 72,
typeMap_['%'] = 0; 'pc': inch / 6,
'px': 1,
'%': 0
};
}; };
// Group: Unit conversion functions // Group: Unit conversion functions
@ -110,7 +111,8 @@ svgedit.units.shortFloat = function(val) {
if (!isNaN(val)) { if (!isNaN(val)) {
// Note that + converts to Number // Note that + converts to Number
return +((+val).toFixed(digits)); return +((+val).toFixed(digits));
} else if($.isArray(val)) { }
if ($.isArray(val)) {
return svgedit.units.shortFloat(val[0]) + ',' + svgedit.units.shortFloat(val[1]); return svgedit.units.shortFloat(val[0]) + ',' + svgedit.units.shortFloat(val[1]);
} }
return parseFloat(val).toFixed(digits) - 0; return parseFloat(val).toFixed(digits) - 0;
@ -123,7 +125,7 @@ svgedit.units.convertUnit = function(val, unit) {
// baseVal.convertToSpecifiedUnits(unitNumMap[unit]); // baseVal.convertToSpecifiedUnits(unitNumMap[unit]);
// var val = baseVal.valueInSpecifiedUnits; // var val = baseVal.valueInSpecifiedUnits;
// baseVal.convertToSpecifiedUnits(1); // baseVal.convertToSpecifiedUnits(1);
return svgedit.unit.shortFloat(val / typeMap_[unit]); return svgedit.units.shortFloat(val / typeMap_[unit]);
}; };
// Function: svgedit.units.setUnitAttr // Function: svgedit.units.setUnitAttr
@ -136,7 +138,7 @@ svgedit.units.convertUnit = function(val, unit) {
svgedit.units.setUnitAttr = function(elem, attr, val) { svgedit.units.setUnitAttr = function(elem, attr, val) {
if (!isNaN(val)) { if (!isNaN(val)) {
// New value is a number, so check currently used unit // New value is a number, so check currently used unit
var old_val = elem.getAttribute(attr); // var old_val = elem.getAttribute(attr);
// Enable this for alternate mode // Enable this for alternate mode
// if (old_val !== null && (isNaN(old_val) || elementContainer_.getBaseUnit() !== 'px')) { // if (old_val !== null && (isNaN(old_val) || elementContainer_.getBaseUnit() !== 'px')) {
@ -146,9 +148,9 @@ svgedit.units.setUnitAttr = function(elem, attr, val) {
// var res = getResolution(); // var res = getResolution();
// unit = '%'; // unit = '%';
// val *= 100; // val *= 100;
// if(w_attrs.indexOf(attr) >= 0) { // if (wAttrs.indexOf(attr) >= 0) {
// val = val / res.w; // val = val / res.w;
// } else if(h_attrs.indexOf(attr) >= 0) { // } else if (hAttrs.indexOf(attr) >= 0) {
// val = val / res.h; // val = val / res.h;
// } else { // } else {
// return val / Math.sqrt((res.w*res.w) + (res.h*res.h))/Math.sqrt(2); // return val / Math.sqrt((res.w*res.w) + (res.h*res.h))/Math.sqrt(2);
@ -189,7 +191,8 @@ svgedit.units.convertAttrs = function(element) {
var unit = elementContainer_.getBaseUnit(); var unit = elementContainer_.getBaseUnit();
var attrs = attrsToConvert[elName]; var attrs = attrsToConvert[elName];
if (!attrs) return; if (!attrs) return;
var len = attrs.length
var len = attrs.length;
for (var i = 0; i < len; i++) { for (var i = 0; i < len; i++) {
var attr = attrs[i]; var attr = attrs[i];
var cur = element.getAttribute(attr); var cur = element.getAttribute(attr);
@ -220,9 +223,9 @@ svgedit.units.convertToNum = function(attr, val) {
var width = elementContainer_.getWidth(); var width = elementContainer_.getWidth();
var height = elementContainer_.getHeight(); var height = elementContainer_.getHeight();
if(w_attrs.indexOf(attr) >= 0) { if (wAttrs.indexOf(attr) >= 0) {
return num * width; return num * width;
} else if(h_attrs.indexOf(attr) >= 0) { } else if (hAttrs.indexOf(attr) >= 0) {
return num * height; return num * height;
} else { } else {
return num * Math.sqrt((width*width) + (height*height))/Math.sqrt(2); return num * Math.sqrt((width*width) + (height*height))/Math.sqrt(2);
@ -243,7 +246,7 @@ svgedit.units.convertToNum = function(attr, val) {
// val - String with the attribute value to check // val - String with the attribute value to check
svgedit.units.isValidUnit = function(attr, val, selectedElement) { svgedit.units.isValidUnit = function(attr, val, selectedElement) {
var valid = false; var valid = false;
if(unit_attrs.indexOf(attr) >= 0) { if (unitAttrs.indexOf(attr) >= 0) {
// True if it's just a number // True if it's just a number
if (!isNaN(val)) { if (!isNaN(val)) {
valid = true; valid = true;
@ -277,5 +280,4 @@ svgedit.units.isValidUnit = function(attr, val, selectedElement) {
return valid; return valid;
}; };
})(); })();

View File

@ -68,14 +68,25 @@
ok(isValidUnit("-0.ex")); ok(isValidUnit("-0.ex"));
ok(isValidUnit("40.123%")); ok(isValidUnit("40.123%"));
equals(isValidUnit("id","uniqueId",document.getElementById("uniqueId")), true); equals(isValidUnit("id","uniqueId",document.getElementById("uniqueId")), true);
equals(isValidUnit("id","newId",document.getElementById("uniqueId")), true); equals(isValidUnit("id","newId",document.getElementById("uniqueId")), true);
equals(isValidUnit("id","uniqueId"), false); equals(isValidUnit("id","uniqueId"), false);
equals(isValidUnit("id","uniqueId",document.getElementById("nonUniqueId")), false); equals(isValidUnit("id","uniqueId",document.getElementById("nonUniqueId")), false);
}); });
test('Test svgedit.units.convertUnit()', function() {
expect(4);
setUp();
ok(svgedit.units.convertUnit);
equals(typeof svgedit.units.convertUnit, typeof function(){});
// cm in default setup
equals(svgedit.units.convertUnit(42), 1.1113);
equals(svgedit.units.convertUnit(42, 'px'), 42);
}); });
</script> });
</script>svgedit.units.convertUnit
</head> </head>
<body> <body>
<h1 id='qunit-header'>Unit Tests for units.js</h1> <h1 id='qunit-header'>Unit Tests for units.js</h1>
@ -89,7 +100,6 @@
<div id='uniqueId' style='visibility:hidden'></div> <div id='uniqueId' style='visibility:hidden'></div>
<div id='nonUniqueId' style='visibility:hidden'></div> <div id='nonUniqueId' style='visibility:hidden'></div>
</div> </div>
</div> </div>
</body> </body>
</html> </html>