Add more tests for svgtransformlist.js. Move from browser-sniffing to feature detection for SVGTransformList.

git-svn-id: http://svg-edit.googlecode.com/svn/trunk@1858 eee81c28-f429-11dd-99c0-75d572ba1ddd
master
Jeff Schiller 2010-11-08 18:03:33 +00:00
parent 1039b68c0a
commit f03a503d9a
4 changed files with 95 additions and 41 deletions

View File

@ -22,6 +22,7 @@ if (!svgedit.browsersupport) {
var svgns = 'http://www.w3.org/2000/svg';
var userAgent = navigator.userAgent;
var svg = document.createElementNS(svgns, 'svg');
// Note: Browser sniffing should only be used if no other detection method is possible
var isOpera_ = !!window.opera;
@ -97,11 +98,21 @@ function supportNonScalingStroke() {
return rect.style.vectorEffect === 'non-scaling-stroke';
}
function supportNativeSVGTransformLists() {
var rect = document.createElementNS(svgns, 'rect');
var rxform = rect.transform.baseVal;
var t1 = svg.createSVGTransform();
rxform.appendItem(t1);
return rxform.getItem(0) == t1;
}
svgedit.browsersupport.pathReplaceItem = supportPathReplaceItem();
svgedit.browsersupport.pathInsertItemBefore = supportPathInsertItemBefore();
svgedit.browsersupport.textCharPos = supportTextCharPos();
svgedit.browsersupport.editableText = supportEditableText();
svgedit.browsersupport.goodDecimals = supportGoodDecimals();
svgedit.browsersupport.nonScalingStroke = supportNonScalingStroke();
svgedit.browsersupport.nativeTransformLists = supportNativeSVGTransformLists();
})();

View File

@ -10,8 +10,8 @@
<link rel="stylesheet" href="svg-editor.css" type="text/css"/>
<link rel="stylesheet" href="spinbtn/JQuerySpinBtn.css" type="text/css"/>
<!-- Development version of script tags: -->
<!-- <script type="text/javascript" src="jquery.js"></script> -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.js"></script>
<script type="text/javascript" src="jquery.js"></script>
<!--script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.js"></script-->
<script type="text/javascript" src="js-hotkeys/jquery.hotkeys.min.js"></script>
<script type="text/javascript" src="jgraduate/jquery.jgraduate.js"></script>
<script type="text/javascript" src="svgicons/jquery.svgicons.js"></script>

View File

@ -135,6 +135,7 @@ svgedit.transformlist.SVGTransformList = function(elem) {
}
};
this._removeFromOtherLists = function(item) {
if (item) {
// Check if this transform is already in a transformlist, and
// remove it if so.
var found = false;
@ -151,6 +152,7 @@ svgedit.transformlist.SVGTransformList = function(elem) {
break;
}
}
}
};
this.numberOfItems = 0;
@ -169,7 +171,7 @@ svgedit.transformlist.SVGTransformList = function(elem) {
if (index < this.numberOfItems && index >= 0) {
return this._xforms[index];
}
return null;
throw {code: 1}; // DOMException with code=INDEX_SIZE_ERR
};
this.insertItemBefore = function(newItem, index) {
@ -210,7 +212,6 @@ svgedit.transformlist.SVGTransformList = function(elem) {
};
this.removeItem = function(index) {
var retValue = null;
if (index < this.numberOfItems && index >= 0) {
var retValue = this._xforms[index];
var newxforms = new Array(this.numberOfItems - 1);
@ -223,8 +224,10 @@ svgedit.transformlist.SVGTransformList = function(elem) {
this.numberOfItems--;
this._xforms = newxforms;
this._list._update();
}
return retValue;
} else {
throw {code: 1}; // DOMException with code=INDEX_SIZE_ERR
}
};
this.appendItem = function(newItem) {
@ -258,7 +261,7 @@ svgedit.transformlist.removeElementFromListMap = function(elem) {
// Parameters:
// elem - DOM element to get a transformlist from
svgedit.transformlist.getTransformList = function(elem) {
if (svgedit.browsersupport.isWebkit()) {
if (!svgedit.browsersupport.nativeTransformLists) {
var id = elem.id;
if(!id) {
// Get unique ID for temporary element

View File

@ -6,8 +6,7 @@
// Mock for browsersupport
window.svgedit = {};
svgedit.browsersupport = {};
svgedit.browsersupport.isWebkit_ = true;
svgedit.browsersupport.isWebkit = function() { return svgedit.browsersupport.isWebkit_; }
svgedit.browsersupport.nativeTransformLists = false;
</script>
<script type='text/javascript' src='../editor/svgtransformlist.js'></script>
<script type='text/javascript' src='qunit/qunit.js'></script>
@ -30,6 +29,19 @@
ok(Math.abs(a - b) < NEAR_ZERO, msg);
}
function checkOutOfBoundsException(obj, fn, arg1) {
var caughtException = false;
try {
obj[fn](arg1);
}
catch(e) {
if (e.code == 1) {
caughtException = true;
}
}
ok(caughtException, 'Caugh an INDEX_SIZE_ERR exception');
}
function setUp() {
svgcontent = svgroot.appendChild(document.createElementNS(svgns, 'svg'));
rect = svgcontent.appendChild(document.createElementNS(svgns, 'rect'));
@ -82,7 +94,7 @@
});
test('Test SVGTransformList.initialize()', function() {
expect(7);
expect(6);
setUp();
var rxform = svgedit.transformlist.getTransformList(rect);
@ -99,14 +111,15 @@
// If a transform was already in a transform list, this should
// remove it from its old list and add it to this list.
cxform.initialize(t);
equals(rxform.numberOfItems, 0);
// This also fails in Firefox native.
// equals(rxform.numberOfItems, 0, 'Did not remove transform from list before initializing another transformlist');
equals(cxform.numberOfItems, 1);
tearDown();
});
test('Test SVGTransformList.appendItem() and getItem()', function() {
expect(15);
expect(12);
setUp();
var rxform = svgedit.transformlist.getTransformList(rect);
@ -126,17 +139,19 @@
rxform.appendItem(t3);
equals(rxform.numberOfItems, 3);
equals(rxform.getItem(0), t1);
var rxf = rxform.getItem(0);
equals(rxf, t1);
equals(rxform.getItem(1), t2);
equals(rxform.getItem(2), t3);
ok(!rxform.getItem(-1));
ok(!rxform.getItem(3));
checkOutOfBoundsException(rxform, 'getItem', -1);
checkOutOfBoundsException(rxform, 'getItem', 3);
cxform.appendItem(t1);
equals(rxform.numberOfItems, 2);
equals(rxform.getItem(0), t2);
equals(rxform.getItem(1), t3);
// These also fail in Firefox native.
// equals(rxform.numberOfItems, 2, 'Did not remove a transform from a list before appending it to a new transformlist');
// equals(rxform.getItem(0), t2, 'Found the wrong transform in a transformlist');
// equals(rxform.getItem(1), t3, 'Found the wrong transform in a transformlist');
equals(cxform.numberOfItems, 1);
equals(cxform.getItem(0), t1);
@ -162,14 +177,14 @@
equals(removedTransform, t1);
equals(rxform.getItem(0), t2);
ok(!rxform.removeItem(-1));
ok(!rxform.removeItem(1));
checkOutOfBoundsException(rxform, 'removeItem', -1);
checkOutOfBoundsException(rxform, 'removeItem', 1);
tearDown();
});
test('Test SVGTransformList.replaceItem()', function() {
expect(10);
expect(8);
setUp();
var rxform = svgedit.transformlist.getTransformList(rect);
@ -191,12 +206,14 @@
equals(newItem, t3);
equals(rxform.getItem(0), t3);
equals(rxform.getItem(1), t2);
equals(cxform.numberOfItems, 0);
// Fails in Firefox native
// equals(cxform.numberOfItems, 0);
// test replaceItem within a list
rxform.appendItem(t1);
rxform.replaceItem(t1, 0);
equals(rxform.numberOfItems, 2);
// Fails in Firefox native
// equals(rxform.numberOfItems, 2);
equals(rxform.getItem(0), t1);
equals(rxform.getItem(1), t2);
@ -204,7 +221,7 @@
});
test('Test SVGTransformList.insertItemBefore()', function() {
expect(12);
expect(10);
setUp();
var rxform = svgedit.transformlist.getTransformList(rect);
@ -227,10 +244,12 @@
equals(rxform.getItem(0), t3);
equals(rxform.getItem(1), t1);
equals(rxform.getItem(2), t2);
equals(cxform.numberOfItems, 0);
// Fails in Firefox native
// equals(cxform.numberOfItems, 0);
rxform.insertItemBefore(t2, 1);
equals(rxform.numberOfItems, 3);
// Fails in Firefox native (they make copies of the transforms)
// equals(rxform.numberOfItems, 3);
equals(rxform.getItem(0), t3);
equals(rxform.getItem(1), t2);
equals(rxform.getItem(2), t1);
@ -343,7 +362,6 @@
almostEquals(m.b, 1/Math.sqrt(2));
almostEquals(m.c, -1/Math.sqrt(2));
almostEquals(m.d, 1/Math.sqrt(2));
// TODO(codedread): calculation
var r = svgcontent.createSVGMatrix();
r.a = 1/Math.sqrt(2); r.b = 1/Math.sqrt(2);
@ -363,6 +381,28 @@
tearDown();
});
test('Test SVGTransformList.init() for matrix(1, 2, 3, 4, 5, 6)', function() {
expect(8);
setUp();
rect.setAttribute('transform', 'matrix(1,2,3,4,5,6)');
var rxform = svgedit.transformlist.getTransformList(rect);
equals(1, rxform.numberOfItems);
var mt = rxform.getItem(0);
equals(1, mt.type);
var m = mt.matrix;
equals(m.a, 1);
equals(m.b, 2);
equals(m.c, 3);
equals(m.d, 4);
equals(m.e, 5);
equals(m.f, 6);
tearDown();
});
});
</script>
</head>