123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- (function() {
- var paper,
- url = 'http://raphaeljs.com';
- module('DOM', {
- setup: function() {
- paper = new Raphael(document.getElementById('qunit-fixture'), 1000, 1000);
- },
- teardown: function() {
- paper.remove();
- }
- });
- var equalNodePosition = function(node, expectedParent, expectedPreviousSibling, expectedNextSibling) {
- equal(node.parentNode, expectedParent);
- equal(node.previousSibling, expectedPreviousSibling);
- equal(node.nextSibling, expectedNextSibling);
- };
- var equalNodePositionWrapped = function(node, anchor, expectedParent, expectedPreviousSibling, expectedNextSibling) {
- equal(node.parentNode, anchor);
- equalNodePosition(anchor, expectedParent, expectedPreviousSibling, expectedNextSibling);
- };
- // Element#insertBefore
- // --------------------
- test('insertBefore: no element', function() {
- var el = paper.rect();
- el.insertBefore(null);
- equalNodePosition(el.node, paper.canvas, paper.defs, null);
- });
- test('insertBefore: first element', function() {
- var x = paper.rect();
- var el = paper.rect();
- el.insertBefore(x);
- equalNodePosition(el.node, paper.canvas, paper.defs, x.node);
- });
- test('insertBefore: middle element', function() {
- var x = paper.rect();
- var y = paper.rect();
- var el = paper.rect();
- el.insertBefore(y);
- equalNodePosition(el.node, paper.canvas, x.node, y.node);
- });
- test('insertBefore: no element when wrapped in <a>', function() {
- var el = paper.rect().attr('href', url),
- anchor = el.node.parentNode;
- el.insertBefore(null);
- equalNodePositionWrapped(el.node, anchor, paper.canvas, paper.defs, null);
- });
- test('insertBefore: first element when wrapped in <a>', function() {
- var x = paper.rect();
- var el = paper.rect().attr('href', url),
- anchor = el.node.parentNode;
- el.insertBefore(x);
- equalNodePositionWrapped(el.node, anchor, paper.canvas, paper.defs, x.node);
- });
- test('insertBefore: first element wrapped in <a> and wrapped in <a>', function() {
- var x = paper.rect().attr('href', url),
- xAnchor = x.node.parentNode;
- var el = paper.rect().attr('href', url),
- anchor = el.node.parentNode;
- el.insertBefore(x);
- equalNodePositionWrapped(el.node, anchor, paper.canvas, paper.defs, xAnchor);
- });
- test('insertBefore: middle element when wrapped in <a>', function() {
- var x = paper.rect();
- var y = paper.rect();
- var el = paper.rect().attr('href', url),
- anchor = el.node.parentNode;
- el.insertBefore(y);
- equalNodePositionWrapped(el.node, anchor, paper.canvas, x.node, y.node);
- });
- test('insertBefore: middle element wrapped in <a> and wrapped in <a>', function() {
- var x = paper.rect().attr('href', url),
- xAnchor = x.node.parentNode;
- var y = paper.rect().attr('href', url),
- yAnchor = y.node.parentNode;
- var el = paper.rect().attr('href', url),
- anchor = el.node.parentNode;
- el.insertBefore(y);
- equalNodePositionWrapped(el.node, anchor, paper.canvas, xAnchor, yAnchor);
- });
- // TODO...
- // insertBefore: with set
- // insertBefore: with nested set.
- // Element#insertAfter
- // -------------------
- test('insertAfter: no element', function() {
- var el = paper.rect();
- el.insertAfter(null);
- equalNodePosition(el.node, paper.canvas, paper.defs, null);
- });
- test('insertAfter: last element', function() {
- var x = paper.rect();
- var el = paper.rect();
- el.insertAfter(x);
- equalNodePosition(el.node, paper.canvas, x.node, null);
- });
- test('insertAfter: middle element', function() {
- var x = paper.rect();
- var y = paper.rect();
- var el = paper.rect();
- el.insertAfter(x);
- equalNodePosition(el.node, paper.canvas, x.node, y.node);
- });
- test('insertAfter: no element when wrapped in <a>', function() {
- var el = paper.rect().attr('href', url),
- anchor = el.node.parentNode;
- el.insertAfter(null);
- equalNodePositionWrapped(el.node, anchor, paper.canvas, paper.defs, null);
- });
- test('insertAfter: last element when wrapped in <a>', function() {
- var x = paper.rect();
- var el = paper.rect().attr('href', url),
- anchor = el.node.parentNode;
- el.insertAfter(x);
- equalNodePositionWrapped(el.node, anchor, paper.canvas, x.node, null);
- });
- test('insertAfter: last element wrapped in <a> and wrapped in <a>', function() {
- var x = paper.rect().attr('href', url),
- xAnchor = x.node.parentNode;
- var el = paper.rect().attr('href', url),
- anchor = el.node.parentNode;
- el.insertAfter(x);
- equalNodePositionWrapped(el.node, anchor, paper.canvas, xAnchor, null);
- });
- test('insertAfter: middle element when wrapped in <a>', function() {
- var x = paper.rect();
- var y = paper.rect();
- var el = paper.rect().attr('href', url),
- anchor = el.node.parentNode;
- el.insertAfter(x);
- equalNodePositionWrapped(el.node, anchor, paper.canvas, x.node, y.node);
- });
- test('insertAfter: middle element wrapped in <a> and wrapped in <a>', function() {
- var x = paper.rect().attr('href', url),
- xAnchor = x.node.parentNode;
- var y = paper.rect().attr('href', url),
- yAnchor = y.node.parentNode;
- var el = paper.rect().attr('href', url),
- anchor = el.node.parentNode;
- el.insertAfter(x);
- equalNodePositionWrapped(el.node, anchor, paper.canvas, xAnchor, yAnchor);
- });
- // TODO...
- // insertAfter: with set
- // insertAfter: with nested set.
- // Element#remove
- // --------------
- test('remove: after added', function() {
- var el = paper.rect(),
- node = el.node;
- el.remove();
- equal(el.node, null);
- equal(node.parentNode, null);
- });
- test('remove: when wrapped in <a>', function() {
- var el = paper.rect().attr('href', url),
- node = el.node,
- anchor = node.parentNode;
- el.remove();
- equal(el.node, null);
- equal(node.parentNode, anchor);
- equal(anchor.parentNode, null);
- });
- test('remove: when already removed', function() {
- var el = paper.rect(),
- node = el.node;
- el.remove();
- el.remove();
- equal(el.node, null);
- equal(node.parentNode, null);
- });
- test('remove: when the canvas is removed', function() {
- var el = paper.rect(),
- node = el.node;
- paper.remove();
- el.remove();
- equal(el.node, null);
- equal(node.parentNode, null);
- });
- // Element#toFront
- // --------------
- test('toFront: normal', function() {
- var el = paper.rect();
- var x = paper.rect();
- el.toFront();
- equalNodePosition(el.node, paper.canvas, x.node, null);
- });
- test('toFront: when wrapped in <a>', function() {
- var el = paper.rect().attr('href', url),
- anchor = el.node.parentNode;
- var x = paper.rect();
- el.toFront();
- equalNodePositionWrapped(el.node, anchor, paper.canvas, x.node, null);
- });
- // Element#toBack
- // --------------
- test('toBack: normal', function() {
- var x = paper.rect();
- var el = paper.rect();
- el.toBack();
- equalNodePosition(el.node, paper.canvas, null, paper.desc);
- equalNodePosition(x.node, paper.canvas, paper.defs, null);
- });
- test('toBack: when wrapped in <a>', function() {
- var x = paper.rect();
- var el = paper.rect().attr('href', url),
- anchor = el.node.parentNode;
- el.toBack();
- equalNodePositionWrapped(el.node, anchor, paper.canvas, null, paper.desc);
- equalNodePosition(x.node, paper.canvas, paper.defs, null);
- });
- })();
|