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-75d572ba1dddmaster
parent
7f560c0ed1
commit
5e68aefef8
|
@ -18,10 +18,10 @@ if (!svgedit.units) {
|
|||
svgedit.units = {};
|
||||
}
|
||||
|
||||
var w_attrs = ['x', 'x1', 'cx', 'rx', 'width'];
|
||||
var h_attrs = ['y', 'y1', 'cy', 'ry', 'height'];
|
||||
var unit_attrs = $.merge(['r','radius'], w_attrs);
|
||||
|
||||
var wAttrs = ['x', 'x1', 'cx', 'rx', 'width'];
|
||||
var hAttrs = ['y', 'y1', 'cy', 'ry', 'height'];
|
||||
var unitAttrs = ['r','radius'].concat(wAttrs, hAttrs);
|
||||
// unused
|
||||
var unitNumMap = {
|
||||
'%': 2,
|
||||
'em': 3,
|
||||
|
@ -34,15 +34,13 @@ var unitNumMap = {
|
|||
'pc': 10
|
||||
};
|
||||
|
||||
$.merge(unit_attrs, h_attrs);
|
||||
|
||||
// Container of elements.
|
||||
var elementContainer_;
|
||||
|
||||
/**
|
||||
* Stores mapping of unit type to user coordinates.
|
||||
*/
|
||||
var typeMap_ = {px: 1};
|
||||
var typeMap_ = {};
|
||||
|
||||
/**
|
||||
* ElementContainer interface
|
||||
|
@ -78,14 +76,17 @@ svgedit.units.init = function(elementContainer) {
|
|||
document.body.removeChild(svg);
|
||||
|
||||
var inch = bb.x;
|
||||
typeMap_['em'] = bb.width;
|
||||
typeMap_['ex'] = bb.height;
|
||||
typeMap_['in'] = inch;
|
||||
typeMap_['cm'] = inch / 2.54;
|
||||
typeMap_['mm'] = inch / 25.4;
|
||||
typeMap_['pt'] = inch / 72;
|
||||
typeMap_['pc'] = inch / 6;
|
||||
typeMap_['%'] = 0;
|
||||
typeMap_ = {
|
||||
'em': bb.width,
|
||||
'ex': bb.height,
|
||||
'in': inch,
|
||||
'cm': inch / 2.54,
|
||||
'mm': inch / 25.4,
|
||||
'pt': inch / 72,
|
||||
'pc': inch / 6,
|
||||
'px': 1,
|
||||
'%': 0
|
||||
};
|
||||
};
|
||||
|
||||
// Group: Unit conversion functions
|
||||
|
@ -110,7 +111,8 @@ svgedit.units.shortFloat = function(val) {
|
|||
if (!isNaN(val)) {
|
||||
// Note that + converts to Number
|
||||
return +((+val).toFixed(digits));
|
||||
} else if($.isArray(val)) {
|
||||
}
|
||||
if ($.isArray(val)) {
|
||||
return svgedit.units.shortFloat(val[0]) + ',' + svgedit.units.shortFloat(val[1]);
|
||||
}
|
||||
return parseFloat(val).toFixed(digits) - 0;
|
||||
|
@ -123,7 +125,7 @@ svgedit.units.convertUnit = function(val, unit) {
|
|||
// baseVal.convertToSpecifiedUnits(unitNumMap[unit]);
|
||||
// var val = baseVal.valueInSpecifiedUnits;
|
||||
// baseVal.convertToSpecifiedUnits(1);
|
||||
return svgedit.unit.shortFloat(val / typeMap_[unit]);
|
||||
return svgedit.units.shortFloat(val / typeMap_[unit]);
|
||||
};
|
||||
|
||||
// Function: svgedit.units.setUnitAttr
|
||||
|
@ -136,7 +138,7 @@ svgedit.units.convertUnit = function(val, unit) {
|
|||
svgedit.units.setUnitAttr = function(elem, attr, val) {
|
||||
if (!isNaN(val)) {
|
||||
// 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
|
||||
// if (old_val !== null && (isNaN(old_val) || elementContainer_.getBaseUnit() !== 'px')) {
|
||||
|
@ -146,9 +148,9 @@ svgedit.units.setUnitAttr = function(elem, attr, val) {
|
|||
// var res = getResolution();
|
||||
// unit = '%';
|
||||
// val *= 100;
|
||||
// if(w_attrs.indexOf(attr) >= 0) {
|
||||
// if (wAttrs.indexOf(attr) >= 0) {
|
||||
// val = val / res.w;
|
||||
// } else if(h_attrs.indexOf(attr) >= 0) {
|
||||
// } else if (hAttrs.indexOf(attr) >= 0) {
|
||||
// val = val / res.h;
|
||||
// } else {
|
||||
// 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 attrs = attrsToConvert[elName];
|
||||
if (!attrs) return;
|
||||
var len = attrs.length
|
||||
|
||||
var len = attrs.length;
|
||||
for (var i = 0; i < len; i++) {
|
||||
var attr = attrs[i];
|
||||
var cur = element.getAttribute(attr);
|
||||
|
@ -220,9 +223,9 @@ svgedit.units.convertToNum = function(attr, val) {
|
|||
var width = elementContainer_.getWidth();
|
||||
var height = elementContainer_.getHeight();
|
||||
|
||||
if(w_attrs.indexOf(attr) >= 0) {
|
||||
if (wAttrs.indexOf(attr) >= 0) {
|
||||
return num * width;
|
||||
} else if(h_attrs.indexOf(attr) >= 0) {
|
||||
} else if (hAttrs.indexOf(attr) >= 0) {
|
||||
return num * height;
|
||||
} else {
|
||||
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
|
||||
svgedit.units.isValidUnit = function(attr, val, selectedElement) {
|
||||
var valid = false;
|
||||
if(unit_attrs.indexOf(attr) >= 0) {
|
||||
if (unitAttrs.indexOf(attr) >= 0) {
|
||||
// True if it's just a number
|
||||
if (!isNaN(val)) {
|
||||
valid = true;
|
||||
|
@ -277,5 +280,4 @@ svgedit.units.isValidUnit = function(attr, val, selectedElement) {
|
|||
return valid;
|
||||
};
|
||||
|
||||
|
||||
})();
|
|
@ -68,14 +68,25 @@
|
|||
ok(isValidUnit("-0.ex"));
|
||||
ok(isValidUnit("40.123%"));
|
||||
|
||||
|
||||
equals(isValidUnit("id","uniqueId",document.getElementById("uniqueId")), true);
|
||||
equals(isValidUnit("id","newId",document.getElementById("uniqueId")), true);
|
||||
equals(isValidUnit("id","uniqueId"), 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>
|
||||
<body>
|
||||
<h1 id='qunit-header'>Unit Tests for units.js</h1>
|
||||
|
@ -89,7 +100,6 @@
|
|||
<div id='uniqueId' style='visibility:hidden'></div>
|
||||
<div id='nonUniqueId' style='visibility:hidden'></div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
|
|
Loading…
Reference in New Issue