| 1 |
(function (assert) { |
| 2 |
|
| 3 |
let paper, |
| 4 |
url = 'http://raphaeljs.com'; |
| 5 |
|
| 6 |
QUnit.module('DOM', { |
| 7 |
beforeEach: function () { |
| 8 |
paper = new Raphael(document.getElementById('qunit-fixture'), 1000, 1000); |
| 9 |
}, |
| 10 |
afterEach: function () { |
| 11 |
paper.remove(); |
| 12 |
} |
| 13 |
}); |
| 14 |
|
| 15 |
const equalNodePosition = function (assert, node, expectedParent, expectedPreviousSibling, expectedNextSibling) { |
| 16 |
assert.equal(node.parentNode, expectedParent); |
| 17 |
assert.equal(node.previousSibling, expectedPreviousSibling); |
| 18 |
assert.equal(node.nextSibling, expectedNextSibling); |
| 19 |
}; |
| 20 |
|
| 21 |
const equalNodePositionWrapped = function (assert, node, anchor, expectedParent, expectedPreviousSibling, expectedNextSibling) { |
| 22 |
assert.equal(node.parentNode, anchor); |
| 23 |
equalNodePosition(assert, anchor, expectedParent, expectedPreviousSibling, expectedNextSibling); |
| 24 |
}; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
QUnit.test('insertBefore: no element', function (assert) { |
| 30 |
const el = paper.rect(); |
| 31 |
|
| 32 |
el.insertBefore(null); |
| 33 |
|
| 34 |
equalNodePosition(assert, el.node, paper.canvas, paper.defs, null); |
| 35 |
}); |
| 36 |
|
| 37 |
QUnit.test('insertBefore: first element', function (assert) { |
| 38 |
const x = paper.rect(); |
| 39 |
const el = paper.rect(); |
| 40 |
|
| 41 |
el.insertBefore(x); |
| 42 |
|
| 43 |
equalNodePosition(assert, el.node, paper.canvas, paper.defs, x.node); |
| 44 |
}); |
| 45 |
|
| 46 |
QUnit.test('insertBefore: middle element', function (assert) { |
| 47 |
const x = paper.rect(); |
| 48 |
const y = paper.rect(); |
| 49 |
const el = paper.rect(); |
| 50 |
|
| 51 |
el.insertBefore(y); |
| 52 |
|
| 53 |
equalNodePosition(assert, el.node, paper.canvas, x.node, y.node); |
| 54 |
}); |
| 55 |
|
| 56 |
QUnit.test('insertBefore: no element when wrapped in <a>', function (assert) { |
| 57 |
const el = paper.rect().attr('href', url), |
| 58 |
anchor = el.node.parentNode; |
| 59 |
|
| 60 |
el.insertBefore(null); |
| 61 |
|
| 62 |
equalNodePositionWrapped(assert, el.node, anchor, paper.canvas, paper.defs, null); |
| 63 |
}); |
| 64 |
|
| 65 |
QUnit.test('insertBefore: first element when wrapped in <a>', function (assert) { |
| 66 |
const x = paper.rect(); |
| 67 |
const el = paper.rect().attr('href', url), |
| 68 |
anchor = el.node.parentNode; |
| 69 |
|
| 70 |
el.insertBefore(x); |
| 71 |
|
| 72 |
equalNodePositionWrapped(assert, el.node, anchor, paper.canvas, paper.defs, x.node); |
| 73 |
}); |
| 74 |
|
| 75 |
QUnit.test('insertBefore: first element wrapped in <a> and wrapped in <a>', function (assert) { |
| 76 |
const x = paper.rect().attr('href', url), |
| 77 |
xAnchor = x.node.parentNode; |
| 78 |
const el = paper.rect().attr('href', url), |
| 79 |
anchor = el.node.parentNode; |
| 80 |
|
| 81 |
el.insertBefore(x); |
| 82 |
|
| 83 |
equalNodePositionWrapped(assert, el.node, anchor, paper.canvas, paper.defs, xAnchor); |
| 84 |
}); |
| 85 |
|
| 86 |
QUnit.test('insertBefore: middle element when wrapped in <a>', function (assert) { |
| 87 |
const x = paper.rect(); |
| 88 |
const y = paper.rect(); |
| 89 |
const el = paper.rect().attr('href', url), |
| 90 |
anchor = el.node.parentNode; |
| 91 |
|
| 92 |
el.insertBefore(y); |
| 93 |
|
| 94 |
equalNodePositionWrapped(assert, el.node, anchor, paper.canvas, x.node, y.node); |
| 95 |
}); |
| 96 |
|
| 97 |
QUnit.test('insertBefore: middle element wrapped in <a> and wrapped in <a>', function (assert) { |
| 98 |
const x = paper.rect().attr('href', url), |
| 99 |
xAnchor = x.node.parentNode; |
| 100 |
const y = paper.rect().attr('href', url), |
| 101 |
yAnchor = y.node.parentNode; |
| 102 |
const el = paper.rect().attr('href', url), |
| 103 |
anchor = el.node.parentNode; |
| 104 |
|
| 105 |
el.insertBefore(y); |
| 106 |
|
| 107 |
equalNodePositionWrapped(assert, el.node, anchor, paper.canvas, xAnchor, yAnchor); |
| 108 |
}); |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
QUnit.test('insertAfter: no element', function (assert) { |
| 118 |
const el = paper.rect(); |
| 119 |
|
| 120 |
el.insertAfter(null); |
| 121 |
|
| 122 |
equalNodePosition(assert, el.node, paper.canvas, paper.defs, null); |
| 123 |
}); |
| 124 |
|
| 125 |
QUnit.test('insertAfter: last element', function (assert) { |
| 126 |
const x = paper.rect(); |
| 127 |
const el = paper.rect(); |
| 128 |
|
| 129 |
el.insertAfter(x); |
| 130 |
|
| 131 |
equalNodePosition(assert, el.node, paper.canvas, x.node, null); |
| 132 |
}); |
| 133 |
|
| 134 |
QUnit.test('insertAfter: middle element', function (assert) { |
| 135 |
const x = paper.rect(); |
| 136 |
const y = paper.rect(); |
| 137 |
const el = paper.rect(); |
| 138 |
|
| 139 |
el.insertAfter(x); |
| 140 |
|
| 141 |
equalNodePosition(assert, el.node, paper.canvas, x.node, y.node); |
| 142 |
}); |
| 143 |
|
| 144 |
QUnit.test('insertAfter: no element when wrapped in <a>', function (assert) { |
| 145 |
const el = paper.rect().attr('href', url), |
| 146 |
anchor = el.node.parentNode; |
| 147 |
|
| 148 |
el.insertAfter(null); |
| 149 |
|
| 150 |
equalNodePositionWrapped(assert, el.node, anchor, paper.canvas, paper.defs, null); |
| 151 |
}); |
| 152 |
|
| 153 |
QUnit.test('insertAfter: last element when wrapped in <a>', function (assert) { |
| 154 |
const x = paper.rect(); |
| 155 |
const el = paper.rect().attr('href', url), |
| 156 |
anchor = el.node.parentNode; |
| 157 |
|
| 158 |
el.insertAfter(x); |
| 159 |
|
| 160 |
equalNodePositionWrapped(assert, el.node, anchor, paper.canvas, x.node, null); |
| 161 |
}); |
| 162 |
|
| 163 |
QUnit.test('insertAfter: last element wrapped in <a> and wrapped in <a>', function (assert) { |
| 164 |
const x = paper.rect().attr('href', url), |
| 165 |
xAnchor = x.node.parentNode; |
| 166 |
const el = paper.rect().attr('href', url), |
| 167 |
anchor = el.node.parentNode; |
| 168 |
|
| 169 |
el.insertAfter(x); |
| 170 |
|
| 171 |
equalNodePositionWrapped(assert, el.node, anchor, paper.canvas, xAnchor, null); |
| 172 |
}); |
| 173 |
|
| 174 |
QUnit.test('insertAfter: middle element when wrapped in <a>', function (assert) { |
| 175 |
const x = paper.rect(); |
| 176 |
const y = paper.rect(); |
| 177 |
const el = paper.rect().attr('href', url), |
| 178 |
anchor = el.node.parentNode; |
| 179 |
|
| 180 |
el.insertAfter(x); |
| 181 |
|
| 182 |
equalNodePositionWrapped(assert, el.node, anchor, paper.canvas, x.node, y.node); |
| 183 |
}); |
| 184 |
|
| 185 |
QUnit.test('insertAfter: middle element wrapped in <a> and wrapped in <a>', function (assert) { |
| 186 |
const x = paper.rect().attr('href', url), |
| 187 |
xAnchor = x.node.parentNode; |
| 188 |
const y = paper.rect().attr('href', url), |
| 189 |
yAnchor = y.node.parentNode; |
| 190 |
const el = paper.rect().attr('href', url), |
| 191 |
anchor = el.node.parentNode; |
| 192 |
|
| 193 |
el.insertAfter(x); |
| 194 |
|
| 195 |
equalNodePositionWrapped(assert, el.node, anchor, paper.canvas, xAnchor, yAnchor); |
| 196 |
}); |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
QUnit.test('remove: after added', function (assert) { |
| 206 |
const el = paper.rect(), |
| 207 |
node = el.node; |
| 208 |
|
| 209 |
el.remove(); |
| 210 |
|
| 211 |
assert.equal(el.node, null); |
| 212 |
assert.equal(node.parentNode, null); |
| 213 |
}); |
| 214 |
|
| 215 |
QUnit.test('remove: when wrapped in <a>', function (assert) { |
| 216 |
const el = paper.rect().attr('href', url), |
| 217 |
node = el.node, |
| 218 |
anchor = node.parentNode; |
| 219 |
|
| 220 |
el.remove(); |
| 221 |
|
| 222 |
assert.equal(el.node, null); |
| 223 |
assert.equal(node.parentNode, anchor); |
| 224 |
assert.equal(anchor.parentNode, null); |
| 225 |
}); |
| 226 |
|
| 227 |
QUnit.test('remove: when already removed', function (assert) { |
| 228 |
const el = paper.rect(), |
| 229 |
node = el.node; |
| 230 |
|
| 231 |
el.remove(); |
| 232 |
el.remove(); |
| 233 |
|
| 234 |
assert.equal(el.node, null); |
| 235 |
assert.equal(node.parentNode, null); |
| 236 |
}); |
| 237 |
|
| 238 |
QUnit.test('remove: when the canvas is removed', function (assert) { |
| 239 |
const el = paper.rect(), |
| 240 |
node = el.node; |
| 241 |
|
| 242 |
paper.remove(); |
| 243 |
el.remove(); |
| 244 |
|
| 245 |
assert.equal(el.node, null); |
| 246 |
assert.equal(node.parentNode, null); |
| 247 |
}); |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
QUnit.test('toFront: normal', function (assert) { |
| 253 |
const el = paper.rect(); |
| 254 |
const x = paper.rect(); |
| 255 |
|
| 256 |
el.toFront(); |
| 257 |
|
| 258 |
equalNodePosition(assert, el.node, paper.canvas, x.node, null); |
| 259 |
}); |
| 260 |
|
| 261 |
QUnit.test('toFront: when wrapped in <a>', function (assert) { |
| 262 |
const el = paper.rect().attr('href', url), |
| 263 |
anchor = el.node.parentNode; |
| 264 |
const x = paper.rect(); |
| 265 |
|
| 266 |
el.toFront(); |
| 267 |
|
| 268 |
equalNodePositionWrapped(assert, el.node, anchor, paper.canvas, x.node, null); |
| 269 |
}); |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
QUnit.test('toBack: normal', function (assert) { |
| 275 |
const x = paper.rect(); |
| 276 |
const el = paper.rect(); |
| 277 |
|
| 278 |
el.toBack(); |
| 279 |
|
| 280 |
equalNodePosition(assert, el.node, paper.canvas, null, paper.desc); |
| 281 |
equalNodePosition(assert, x.node, paper.canvas, paper.defs, null); |
| 282 |
}); |
| 283 |
|
| 284 |
QUnit.test('toBack: when wrapped in <a>', function (assert) { |
| 285 |
const x = paper.rect(); |
| 286 |
const el = paper.rect().attr('href', url), |
| 287 |
anchor = el.node.parentNode; |
| 288 |
|
| 289 |
el.toBack(); |
| 290 |
|
| 291 |
equalNodePositionWrapped(assert, el.node, anchor, paper.canvas, null, paper.desc); |
| 292 |
equalNodePosition(assert, x.node, paper.canvas, paper.defs, null); |
| 293 |
}); |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
})(); |