NodeList.html 19.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
	"http://www.w3.org/TR/html4/strict.dtd">
<!--
	we use a strict-mode DTD to ensure that the box model is the same for these
	basic tests
-->
<html>
	<head>
		<style type="text/css">
			@import "../../resources/dojo.css";
			html, body {
				padding: 0px;
				margin: 0px;
				border: 0px;
			}

			#sq100 {
				background-color: black;
				color: white;
				position: absolute;
				left: 100px;
				top: 100px;
				width: 100px;
				height: 100px;
				border: 0px;
				padding: 0px;
				margin: 0px;
				overflow: hidden;
			}

		</style>
		<title>testing dojo.NodeList</title>
		<script type="text/javascript" src="../../dojo.js"></script>
		<script type="text/javascript">
		require(["dojo", "doh", "dojo/query", "dojo/on", "dojo/string", "dojo/parser", "dojo/domReady!"], function(dojo, doh, query, listen){
			function registerForNodeList(name, query, NodeList){
				var tn;
				var c = dojo.byId("c1");
				var t = dojo.byId("t");
				var s = dojo.byId("sq100");
				var fourElementNL = new NodeList(c, t, c, t);

				function verify(/*dojo.NodeList*/nl, /*Array*/ids){
					for(var i = 0, node; node = nl[i]; i++){
						doh.is(ids[i], node.id);
					}
					//Make sure lengths are equal.
					doh.is(ids.length, i);
				}

				doh.register(name,
					[
						// constructor tests
						function ctor(){
							var nl = new NodeList();
							nl.push(c);
							doh.is(1, nl.length);
						},
						function ctorArgs(){
							var nl = new NodeList(4);
							nl.push(c);
							doh.is(5, nl.length);
						},
						function ctorArgs2(){
							var nl = new NodeList(c, t);
							doh.is(2, nl.length);
							doh.is(c, nl[0]);
							doh.is(t, nl[1]);
						},
						// iteration and array tests
						function forEach(){
							var lastItem;
							var nl = new NodeList(c, t);
							nl.forEach(function(i){ lastItem = i; });
							doh.is(t, lastItem);

							var r = nl.forEach(function(i, idx, arr){
								doh.t(arr.constructor == dojo.NodeList);
								doh.is(2, arr.length);
							});
							doh.t(r.constructor == dojo.NodeList);
							doh.is(r, nl);
						},

						function indexOf(){
							doh.is(0, fourElementNL.indexOf(c));
							doh.is(1, fourElementNL.indexOf(t));
							doh.is(-1, fourElementNL.indexOf(null));
						},

						function lastIndexOf(){
							doh.is(2, fourElementNL.lastIndexOf(c));
							doh.is(3, fourElementNL.lastIndexOf(t));
							doh.is(-1, fourElementNL.lastIndexOf(null));
						},

						function every(){
							var ctr = 0;
							var ret = fourElementNL.every(function(){
								ctr++;
								return true;
							});
							doh.is(4, ctr);
							doh.t(ret);

							ctr = 0;
							var ret = fourElementNL.every(function(){
								ctr++;
								return false;
							});
							doh.is(1, ctr);
							doh.f(ret);
						},

						function some(){
							var ret = fourElementNL.some(function(){
								return true;
							});
							doh.t(ret);

							var ret = fourElementNL.some(function(i){
								return (i.id == "t");
							});
							doh.t(ret);
						},

						function map(){
							var ret = fourElementNL.map(function(){
								return true;
							});

							doh.is(ret, [true, true, true, true]);

							verify(ret.end(), ["c1", "t", "c1", "t"]);

							var cnt = 0;
							var ret = fourElementNL.map(function(){
								return cnt++;
							});
							// doh.is(ret, [0, 1, 2, 3]);

							doh.t(ret.constructor == dojo.NodeList);

							// make sure that map() returns a NodeList
							var sum = 0;
							fourElementNL.map(function(){ return 2; }).forEach( function(x){ sum += x; } );
							doh.is(sum, 8);
						},

						function slice(){
							var pnl = new NodeList(t, t, c);
							doh.is(2, pnl.slice(1).length);
							doh.is(3, pnl.length);
							doh.is(c, pnl.slice(-1)[0]);
							doh.is(2, pnl.slice(-2).length);
							verify(pnl.slice(1).end(), ["t", "t", "c1"]);
						},

						function splice(){
							var pnl = new NodeList(t, t, c);
							console.debug(pnl.splice(1));

							/*
							doh.is(2, pnl.splice(1).length);
							doh.is(1, pnl.length);
							pnl = new NodeList(t, t, c);
							doh.is(c, pnl.splice(-1)[0]);
							doh.is(2, pnl.length);
							pnl = new NodeList(t, t, c);
							doh.is(2, pnl.splice(-2).length);
							*/
						},

						function spliceInsert(){
							// insert 1
							var pnl = new NodeList(t, t, c);
							pnl.splice(0, 0, c);
							doh.is(4, pnl.length);
							doh.is(c, pnl[0]);

							// insert multiple
							pnl = new NodeList(t, t, c);
							pnl.splice(0, 0, c, s);
							doh.is(5, pnl.length);
							doh.is(c, pnl[0]);
							doh.is(s, pnl[1]);
							doh.is(t, pnl[2]);

							// insert multiple at offset
							pnl = new NodeList(t, t, c);
							pnl.splice(1, 0, c, s);
							doh.is(5, pnl.length);
							doh.is(t, pnl[0]);
							doh.is(c, pnl[1]);
							doh.is(s, pnl[2]);
							doh.is(t, pnl[3]);
						},

						function spliceDel(){
							// clobbery 1
							var pnl = new NodeList(c, t, s);
							pnl.splice(0, 1);
							doh.is(2, pnl.length);
							doh.is(t, pnl[0]);

							// clobber multiple
							pnl = new NodeList(c, t, s);
							pnl.splice(0, 2);
							doh.is(1, pnl.length);
							doh.is(s, pnl[0]);

							// ...at an offset
							pnl = new NodeList(c, t, s);
							pnl.splice(1, 1);
							doh.is(2, pnl.length);
							doh.is(c, pnl[0]);
							doh.is(s, pnl[1]);

						},

						function spliceInsertDel(){
							// clobbery 1
							var pnl = new NodeList(c, t, s);
							pnl.splice(1, 1, s);
							doh.is(3, pnl.length);
							doh.is(new NodeList(c, s, s), pnl);

							pnl = new NodeList(c, t, s);
							pnl.splice(1, 2, s);
							doh.is(2, pnl.length);
							doh.is(new NodeList(c, s), pnl);
						},

						// sub-search
						function queryTest(){
							var pnl = new NodeList(t);
							doh.is(c, pnl.query("span")[0]);
							if(name != "t"){
							// this gets messed up by new DOM nodes the second time around
								doh.is(t, query("body").query(":last-child")[0]);
								doh.is(c, query("body").query(":last-child")[1]);
								doh.is(1, pnl.query().length);
								verify(pnl.query("span").end(), ["t"]);
							}
						},

						function filter(){
							if(name != "t"){
							// this gets messed up by new DOM nodes the second time around
								doh.is(query("body :first-child").filter(":last-child")[0], c);
								doh.is(1, query("*").filter(function(n){ return (n.nodeName.toLowerCase() == "span"); }).length);
	
								var filterObj = {
									filterFunc: function(n){
										return (n.nodeName.toLowerCase() == "span");
									}
								};
								doh.is(1, query("*").filter(filterObj.filterFunc).length);
								doh.is(1, query("*").filter(filterObj.filterFunc, filterObj).length);
								verify((new NodeList(t)).filter("span").end(), ["t"]);
							}
						},

						// layout DOM functions
						function coords(){
							var tnl = new NodeList(dojo.byId('sq100'));
							doh.t(dojo.isArray(tnl));
							doh.is(100, tnl.coords()[0].w);
							doh.is(100, tnl.coords()[0].h);
							doh.is(100, tnl.position()[0].w);
							doh.is(100, tnl.position()[0].h);
							doh.is(document.body.getElementsByTagName("*").length, query("body *").coords().length);
							doh.is(document.body.getElementsByTagName("*").length, query("body *").position().length);
						},

						function styleGet(){
							// test getting
							var tnl = new NodeList(s);
							doh.is(1, tnl.style("opacity")[0]);
							tnl.push(t);
							dojo.style(t, "opacity", 0.5);
							doh.is(0.5, tnl.style("opacity").slice(-1)[0]);
							tnl.style("opacity", 1);
						},

						function styleSet(){
							// test setting
							var tnl = new NodeList(s, t);
							tnl.style("opacity", 0.5);
							doh.is(0.5, dojo.style(tnl[0], "opacity"));
							doh.is(0.5, dojo.style(tnl[1], "opacity"));
							// reset
							tnl.style("opacity", 1);
						},

						function style(){
							var tnl = new NodeList(s, t);
							tnl.style("opacity", 1);
							doh.is(1, tnl.style("opacity")[0]);
							dojo.style(t, "opacity", 0.5);
							doh.is(1.0, tnl.style("opacity")[0]);
							doh.is(0.5, tnl.style("opacity")[1]);
							// reset things
							tnl.style("opacity", 1);
						},

						function addRemoveClass(){
							var tnl = new NodeList(s, t);
							tnl.addClass("a");
							doh.is("a", s.className);
							doh.is("a", t.className);
							tnl.addClass("a b");
							doh.is("a b", s.className);
							doh.is("a b", t.className);
							tnl.addClass(["a", "c"]);
							doh.is("a b c", s.className);
							doh.is("a b c", t.className);
							tnl.removeClass();
							doh.is("", s.className);
							doh.is("", t.className);
							tnl.addClass("    a");
							doh.is("a", s.className);
							doh.is("a", t.className);
							tnl.addClass(" a  b ");
							doh.is("a b", s.className);
							doh.is("a b", t.className);
							tnl.addClass(" c  b a ");
							doh.is("a b c", s.className);
							doh.is("a b c", t.className);
							tnl.removeClass(" b");
							doh.is("a c", s.className);
							doh.is("a c", t.className);
							tnl.removeClass("a b ");
							doh.is("c", s.className);
							doh.is("c", t.className);
							tnl.removeClass(["a", "c"]);
							doh.is("", s.className);
							doh.is("", t.className);
							tnl.addClass("a b c");
							tnl.replaceClass("d e", "a b");
							doh.is("c d e", s.className, "class is c d e after replacing a b with d e");
							doh.is("c d e", t.className, "class is c d e after replacing a b with d e");
							tnl.replaceClass("f", "d");
							doh.is("c e f", s.className, "class is c e f after replacing d with f");
							doh.is("c e f", t.className, "class is c e f after replacing d with f");
							tnl.replaceClass("d");
							doh.is("d", s.className);
							doh.is("d", t.className);
							tnl.removeClass();
							doh.is("", s.className, "empty class");
							doh.is("", t.className, "empty class");
						},

						function concat(){
							if(name != "t"){
								// this isn't supported in the new query method
								var spans = query("span");
								var divs = query("div");
								var cat = spans.concat(divs);
								console.debug(cat);
								doh.t(cat.constructor == dojo.NodeList || cat.constructor == dojo.NodeList);
								doh.is((divs.length + spans.length), cat.length);
								verify(cat.end(), ["c1"]);
							}
						},

						function concat2(){
							var spans = query("span");
							var divs = query("div");
							doh.is(spans.concat([]).constructor, dojo.NodeList);
						},

						function concat3(){
							var spans = query("span");
							var divs = query("div");
							var cat = spans.concat(divs);

							doh.t(typeof NodeList != "undefined" && cat.constructor === dojo.NodeList
								|| cat.constructor === dojo.NodeList);
						},
						function concat4(){ 
							var res = (new dojo.NodeList()).concat([]); 
							doh.is(0, res.length); 
						}, 
						function place(){
							var ih = "<div><span></span></div><span class='thud'><b>blah</b></span>";

							var tn = document.createElement("div");
							tn.innerHTML = ih;
							dojo.body().appendChild(tn);
							var nl = query("b", tn).place(tn, "first");
							doh.t(nl.constructor == dojo.NodeList || nl.constructor == dojo.NodeList);
							doh.is(1, nl.length);
							doh.is("b", nl[0].nodeName.toLowerCase());
							doh.is(tn, nl[0].parentNode);
							doh.is(tn.firstChild, nl[0]);
						},

						function orphan(){
							var ih = "<div><span></span></div><span class='thud'><b>blah</b></span>";

							var tn = document.createElement("div");
							tn.innerHTML = ih;
							dojo.body().appendChild(tn);
							var nl = query("span", tn).orphan();
							doh.t(nl.constructor == dojo.NodeList || nl.constructor == dojo.NodeList);

							doh.is(2, nl.length);
							doh.is(1, tn.getElementsByTagName("*").length);

							tn.innerHTML = ih;
							var nl = query("*", tn).orphan("b");
							doh.is(1, nl.length);
							doh.is("blah", nl[0].innerHTML);
						},

						function adopt(){
							var div = query(dojo.create("div"));
							div.adopt(dojo.create("span"));
							div.adopt(dojo.create("em"), "first");
							doh.is(2, query("*", div[0]).length);
							doh.is("em", div[0].firstChild.tagName.toLowerCase());
							doh.is("span", div[0].lastChild.tagName.toLowerCase());
						},

						function addContent(){
							//text content
							var tn = document.createElement("div");
							var nl = query(tn).addContent("some text content");

							doh.is(1, nl[0].childNodes.length);
							doh.is("some text content", nl[0].firstChild.nodeValue);

							//move a node
							var mNode = document.createElement("span");
							mNode.id = "addContent1";
							mNode.innerHTML = "hello";
							dojo.body().appendChild(mNode);
							doh.t(dojo.byId("addContent1"));

							nl.addContent(mNode);
							doh.f(dojo.byId("addContent1"));
							doh.is("addContent1", nl[0].lastChild.id);

							//put in multiple content/clone node
							tn.innerHTML = '<select><option name="second"  value="second" selected>second</option></select>';
							nl = query("select", tn).addContent('<option name="first" value="first">first</option>', "first");
							nl.forEach(function(node){
								doh.is("first", node.options[0].value);
								doh.f(node.options[0].selected);
							});

							//Some divs to use for addContent template actions.
							var templs = dojo._toDom('<div class="multitemplate"></div><div class="multitemplate"></div>');
							dojo.body().appendChild(templs);
							templs = query(".multitemplate");

							//templateFunc test
							templs.addContent({
								template: '<b>[name]</b>',
								templateFunc: function(str, obj){ return str.replace(/\[name\]/g, obj.name); },
								name: "bar"
							});

							var bolds = templs.query("b");
							doh.is(2, bolds.length);
							bolds.forEach(function(node){
								doh.is("bar", node.innerHTML);
							});

							//template with dojo.string.substitute used.
							templs.addContent({
								template: "<p>${name}</p>",
								name: "baz"
							});

							var ps = templs.query("p");
							doh.is(2, ps.length);
							ps.forEach(function(node){
								doh.is("baz", node.innerHTML);
							});

							//Try a dojo.declared thing.
							dojo.declare("dojo.tests.Mini", null, {
								constructor: function(args, node){
									dojo.mixin(this, args);
									node.innerHTML = this.name;
									this.domNode = node;
								},
								name: ""
							});

							templs.addContent({
								template: '<i dojoType="dojo.tests.Mini" name="cool"></i>',
								parse: true
							});


							var declaredNodes = templs.query("[dojoType]");

							doh.is(2, declaredNodes.length);
							dojo.forEach(declaredNodes, function(node){
								doh.is("cool", node.innerHTML);
							});

							//Get rid of the junk used for template testing.
							templs.orphan();
						},

						function connect(){
							var ih = "<div><span></span></div><span class='thud'><button>blah</button></span>";

							tn = document.createElement("div");
							tn.innerHTML = ih;
							dojo.body().appendChild(tn);

							var ctr = 0;
							var nl = query("button", tn).connect("onclick", function(){
								ctr++;
							});
							nl[0].click();
							doh.is(1, ctr);
							nl[0].click();
							nl[0].click();
							doh.is(3, ctr);
						},
						function on(){
							var ctr = 0;
							dojo.body().appendChild(tn);
							var nl = query("button", tn);
							var handle = nl.on("click", function(){
								ctr++;
							});
							nl[0].click();
							doh.is(1, ctr);
							var inButton = nl[0].appendChild(document.createElement("span"));
							listen.emit(nl[0], "click", {
							});
							listen.emit(inButton, "click", {
								bubbles: true
							});
							listen.emit(inButton, "click", {
								bubbles: false
							});
							doh.is(3, ctr);
							handle.remove();
							listen.emit(nl[0], "click", {
							});
							doh.is(3, ctr);
						},
						function onDelegate(){
							var ctr = 0;
							dojo.body().appendChild(tn);
							var nl = query(".thud", tn);
							var bl = query("button", tn);
							var handle = nl.on("button:click", function(){
								doh.is(this, bl[0]);
								ctr++;
							});
							doh.is(0, ctr);
							listen.emit(nl[0], "click", {
							});
							listen.emit(bl[0], "click", {
								bubbles: true
							});
							doh.is(1, ctr);
							handle.remove();
							listen.emit(bl[0], "click", {
								bubbles: true
							});
							doh.is(1, ctr);
							// listen and on should behave the same
							query(tn).on(".thud:click, .thud button:custom", function(){
								ctr++;
							});
							listen.emit(bl[0], "click", {
								bubbles: true
							});
							doh.is(2, ctr);
							listen.emit(bl[0], "click", {
								bubbles: false
							});
							doh.is(2, ctr);
							listen.emit(bl[0], "custom", {
								bubbles: true
							});
							doh.is(3, ctr);
							listen.emit(bl[0], "mouseout", {
								bubbles: true
							});
							doh.is(3, ctr);
							bl[0].click();
							doh.is(4, ctr);
						},

						function at(){
							var divs = query("body div");
							var at0 = divs.at(0);
							doh.is(divs[0], at0[0]);
							if(name != "t"){
								// this gets messed up by new DOM nodes the second time around
								
								var at1 = divs.at(1,3,5);
								doh.is(divs[1], at1[0]);
								doh.is(divs[3], at1[1]);
								doh.is(divs[5], at1[2]);
	
								var at2 = divs.at(3,6,9);
								doh.is(at2.length, 2);
	
								var at3 = divs.at(3,6).at(1);
								doh.is(divs[6], at3[0]);
	
								var ending = divs.at(0).end();
								verify([ending[0], ending[1]], ["sq100", "t"]);
								
								var at4 = divs.at(-1);
								doh.is(divs[divs.length - 1], at4[0]);
								
								var at5 = divs.at(1, -1);
								doh.is(at5[0], divs[1]);
								doh.is(at5[1], divs[divs.length - 1]);
							}
							
						},

						function attr(){
							var divs = query("div");
							var ids = divs.attr("id");
						},

						function _adaptAsForEach(t){
							var passes = false;
							var count = 0;
							var i = {
								setTrue: function(node){
									count++;
									passes = true;
								}
							};
							dojo.NodeList.prototype.setTrue = dojo.NodeList._adaptAsForEach(i.setTrue, i);
							var divs = query("div").setTrue();
							doh.t(passes);
							doh.is(count, divs.length);
						},

						function instantiate(){
							//Insert some divs to use for test
							dojo.place('<p id="thinger">Hi</p><p id="thinger2">Hi</p>', dojo.body());

							var test = 0;
							dojo.declare("tests._base.NodeList.some.Thing", null, {
								foo:"baz",
								constructor: function(props, node){
									dojo.mixin(this, props);
									doh.is("bar", this.foo, test++);
								}
							});

							query("#thinger").instantiate(tests._base.NodeList.some.Thing, {
								foo:"bar"
							});

							query("#thinger2").instantiate("tests._base.NodeList.some.Thing", {
								foo:"bar"
							});

							doh.is(2, test);

							//clean up the divs inserted for the test.
							query("#thinger, #thinger2").orphan();
						},
						
						function removeAttr(){
							// buildup
							dojo.place('<p id="attr" title="Foobar">Hi</p>', dojo.body());
							
							var n = query("#attr");
							
							doh.t(dojo.hasAttr(n[0], "title"));
							
							var t = n.attr("title");
							doh.is(t, "Foobar");
							
							n.removeAttr("title");
							
							t = dojo.hasAttr(n[0], "title");
							doh.f(t);
							
							// cleanup
							n.orphan();
						}
					]
				);
			}
			registerForNodeList("t-backcompat", dojo.query, function(){
				return dojo.NodeList.apply(dojo.NodeList, arguments);
			});
			registerForNodeList("t", query, dojo.NodeList);
			
			doh.run();
		});
		</script>
	</head>
	<body>
		<h1>testing dojo.NodeList</h1>
		<div id="sq100">
			100px square, abs
		</div>
		<div id="t">
			<span id="c1">c1</span>
		</div>
	</body>
</html>