OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library XMLElementTest; | 5 library XMLElementTest; |
6 import '../../pkg/unittest/lib/unittest.dart'; | 6 import '../../pkg/unittest/lib/unittest.dart'; |
7 import '../../pkg/unittest/lib/html_config.dart'; | 7 import '../../pkg/unittest/lib/html_config.dart'; |
8 import 'dart:html'; | 8 import 'dart:html'; |
9 | 9 |
10 main() { | 10 main() { |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 // FilteredElementList is tested more thoroughly in DocumentFragmentTests. | 56 // FilteredElementList is tested more thoroughly in DocumentFragmentTests. |
57 group('children', () { | 57 group('children', () { |
58 test('filters out non-element nodes', () { | 58 test('filters out non-element nodes', () { |
59 final el = new XMLElement.xml("<xml>1<a/><b/>2<c/>3<d/></xml>"); | 59 final el = new XMLElement.xml("<xml>1<a/><b/>2<c/>3<d/></xml>"); |
60 expect(el.children.map((e) => e.tagName), ["a", "b", "c", "d"]); | 60 expect(el.children.map((e) => e.tagName), ["a", "b", "c", "d"]); |
61 }); | 61 }); |
62 | 62 |
63 test('overwrites nodes when set', () { | 63 test('overwrites nodes when set', () { |
64 final el = new XMLElement.xml("<xml>1<a/><b/>2<c/>3<d/></xml>"); | 64 final el = new XMLElement.xml("<xml>1<a/><b/>2<c/>3<d/></xml>"); |
65 el.children = [new XMLElement.tag('x'), new XMLElement.tag('y')]; | 65 el.children = [new XMLElement.tag('x'), new XMLElement.tag('y')]; |
66 expect(el.outerHTML, "<xml><x></x><y></y></xml>"); | 66 expect(el.outerHtml, "<xml><x></x><y></y></xml>"); |
67 }); | 67 }); |
68 }); | 68 }); |
69 | 69 |
70 group('classes', () { | 70 group('classes', () { |
71 XMLElement makeElementWithClasses() => | 71 XMLElement makeElementWithClasses() => |
72 new XMLElement.xml("<xml class='foo bar baz'></xml>"); | 72 new XMLElement.xml("<xml class='foo bar baz'></xml>"); |
73 | 73 |
74 Set<String> makeClassSet() => makeElementWithClasses().classes; | 74 Set<String> makeClassSet() => makeElementWithClasses().classes; |
75 | 75 |
76 test('affects the "class" attribute', () { | 76 test('affects the "class" attribute', () { |
(...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
381 }); | 381 }); |
382 | 382 |
383 test('set', () { | 383 test('set', () { |
384 final el = makeElement(); | 384 final el = makeElement(); |
385 el.dir = 'foo'; | 385 el.dir = 'foo'; |
386 expect(el.attributes['dir'], 'foo'); | 386 expect(el.attributes['dir'], 'foo'); |
387 }); | 387 }); |
388 }); | 388 }); |
389 }); | 389 }); |
390 | 390 |
391 test('set innerHTML', () { | 391 test('set innerHtml', () { |
392 final el = makeElement(); | 392 final el = makeElement(); |
393 el.innerHTML = "<foo>Bar<baz/></foo>"; | 393 el.innerHtml = "<foo>Bar<baz/></foo>"; |
394 expect(el.nodes.length, 1); | 394 expect(el.nodes.length, 1); |
395 final node = el.nodes[0]; | 395 final node = el.nodes[0]; |
396 expect(node, isXMLElement); | 396 expect(node, isXMLElement); |
397 expect(node.tagName, 'foo'); | 397 expect(node.tagName, 'foo'); |
398 expect(node.nodes[0].text, 'Bar'); | 398 expect(node.nodes[0].text, 'Bar'); |
399 expect(node.nodes[1].tagName, 'baz'); | 399 expect(node.nodes[1].tagName, 'baz'); |
400 }); | 400 }); |
401 | 401 |
402 test('get innerHTML/outerHTML', () { | 402 test('get innerHtml/outerHtml', () { |
403 final el = makeElement(); | 403 final el = makeElement(); |
404 expect(el.innerHTML, "<foo></foo><bar></bar>"); | 404 expect(el.innerHtml, "<foo></foo><bar></bar>"); |
405 el.nodes.clear(); | 405 el.nodes.clear(); |
406 el.nodes.addAll([new Text("foo"), new XMLElement.xml("<a>bar</a>")]); | 406 el.nodes.addAll([new Text("foo"), new XMLElement.xml("<a>bar</a>")]); |
407 expect(el.innerHTML, "foo<a>bar</a>"); | 407 expect(el.innerHtml, "foo<a>bar</a>"); |
408 expect(el.outerHTML, "<xml>foo<a>bar</a></xml>"); | 408 expect(el.outerHtml, "<xml>foo<a>bar</a></xml>"); |
409 }); | 409 }); |
410 | 410 |
411 test('query', () { | 411 test('query', () { |
412 final el = makeElement(); | 412 final el = makeElement(); |
413 expect(el.query('foo').tagName, 'foo'); | 413 expect(el.query('foo').tagName, 'foo'); |
414 expect(el.query('baz'), isNull); | 414 expect(el.query('baz'), isNull); |
415 }); | 415 }); |
416 | 416 |
417 test('queryAll', () { | 417 test('queryAll', () { |
418 final el = new XMLElement.xml( | 418 final el = new XMLElement.xml( |
419 "<xml><foo id='f1' /><bar><foo id='f2' /></bar></xml>"); | 419 "<xml><foo id='f1' /><bar><foo id='f2' /></bar></xml>"); |
420 expect(el.queryAll('foo').map((e) => e.id), ['f1', 'f2']); | 420 expect(el.queryAll('foo').map((e) => e.id), ['f1', 'f2']); |
421 expect(el.queryAll('baz'), []); | 421 expect(el.queryAll('baz'), []); |
422 }); | 422 }); |
423 | 423 |
424 // TODO(nweiz): re-enable this when matchesSelector works cross-browser. | 424 // TODO(nweiz): re-enable this when matchesSelector works cross-browser. |
425 // | 425 // |
426 // test('matchesSelector', () { | 426 // test('matchesSelector', () { |
427 // final el = makeElement(); | 427 // final el = makeElement(); |
428 // expect(el.matchesSelector('*'), isTrue); | 428 // expect(el.matchesSelector('*'), isTrue); |
429 // expect(el.matchesSelector('xml'), isTrue); | 429 // expect(el.matchesSelector('xml'), isTrue); |
430 // expect(el.matchesSelector('html'), isFalse); | 430 // expect(el.matchesSelector('html'), isFalse); |
431 // }); | 431 // }); |
432 | 432 |
433 group('insertAdjacentElement', () { | 433 group('insertAdjacentElement', () { |
434 test('beforeBegin with no parent does nothing', () { | 434 test('beforeBegin with no parent does nothing', () { |
435 final el = makeElement(); | 435 final el = makeElement(); |
436 expect(el.insertAdjacentElement("beforeBegin", new XMLElement.tag("b")), | 436 expect(el.insertAdjacentElement("beforeBegin", new XMLElement.tag("b")), |
437 isNull); | 437 isNull); |
438 expect(el.innerHTML, "<foo></foo><bar></bar>"); | 438 expect(el.innerHtml, "<foo></foo><bar></bar>"); |
439 }); | 439 }); |
440 | 440 |
441 test('afterEnd with no parent does nothing', () { | 441 test('afterEnd with no parent does nothing', () { |
442 final el = makeElement(); | 442 final el = makeElement(); |
443 expect( | 443 expect( |
444 el.insertAdjacentElement("afterEnd", new XMLElement.tag("b")), isNull); | 444 el.insertAdjacentElement("afterEnd", new XMLElement.tag("b")), isNull); |
445 expect(el.innerHTML, "<foo></foo><bar></bar>"); | 445 expect(el.innerHtml, "<foo></foo><bar></bar>"); |
446 }); | 446 }); |
447 | 447 |
448 test('beforeBegin with parent inserts the element', () { | 448 test('beforeBegin with parent inserts the element', () { |
449 final el = makeElementWithParent(); | 449 final el = makeElementWithParent(); |
450 final newEl = new XMLElement.tag("b"); | 450 final newEl = new XMLElement.tag("b"); |
451 expect(el.insertAdjacentElement("beforeBegin", newEl), newEl); | 451 expect(el.insertAdjacentElement("beforeBegin", newEl), newEl); |
452 expect(el.innerHTML, "<foo></foo><bar></bar>"); | 452 expect(el.innerHtml, "<foo></foo><bar></bar>"); |
453 expect(el.parent.innerHTML, | 453 expect(el.parent.innerHtml, |
454 "<before></before><b></b><xml><foo></foo><bar></bar>" | 454 "<before></before><b></b><xml><foo></foo><bar></bar>" |
455 "</xml><after></after>"); | 455 "</xml><after></after>"); |
456 }); | 456 }); |
457 | 457 |
458 test('afterEnd with parent inserts the element', () { | 458 test('afterEnd with parent inserts the element', () { |
459 final el = makeElementWithParent(); | 459 final el = makeElementWithParent(); |
460 final newEl = new XMLElement.tag("b"); | 460 final newEl = new XMLElement.tag("b"); |
461 expect(el.insertAdjacentElement("afterEnd", newEl), newEl); | 461 expect(el.insertAdjacentElement("afterEnd", newEl), newEl); |
462 expect(el.innerHTML, "<foo></foo><bar></bar>"); | 462 expect(el.innerHtml, "<foo></foo><bar></bar>"); |
463 expect(el.parent.innerHTML, | 463 expect(el.parent.innerHtml, |
464 "<before></before><xml><foo></foo><bar></bar></xml><b>" | 464 "<before></before><xml><foo></foo><bar></bar></xml><b>" |
465 "</b><after></after>"); | 465 "</b><after></after>"); |
466 }); | 466 }); |
467 | 467 |
468 test('afterBegin inserts the element', () { | 468 test('afterBegin inserts the element', () { |
469 final el = makeElement(); | 469 final el = makeElement(); |
470 final newEl = new XMLElement.tag("b"); | 470 final newEl = new XMLElement.tag("b"); |
471 expect(el.insertAdjacentElement("afterBegin", newEl), newEl); | 471 expect(el.insertAdjacentElement("afterBegin", newEl), newEl); |
472 expect(el.innerHTML, "<b></b><foo></foo><bar></bar>"); | 472 expect(el.innerHtml, "<b></b><foo></foo><bar></bar>"); |
473 }); | 473 }); |
474 | 474 |
475 test('beforeEnd inserts the element', () { | 475 test('beforeEnd inserts the element', () { |
476 final el = makeElement(); | 476 final el = makeElement(); |
477 final newEl = new XMLElement.tag("b"); | 477 final newEl = new XMLElement.tag("b"); |
478 expect(el.insertAdjacentElement("beforeEnd", newEl), newEl); | 478 expect(el.insertAdjacentElement("beforeEnd", newEl), newEl); |
479 expect(el.innerHTML, "<foo></foo><bar></bar><b></b>"); | 479 expect(el.innerHtml, "<foo></foo><bar></bar><b></b>"); |
480 }); | 480 }); |
481 }); | 481 }); |
482 | 482 |
483 group('insertAdjacentText', () { | 483 group('insertAdjacentText', () { |
484 test('beforeBegin with no parent does nothing', () { | 484 test('beforeBegin with no parent does nothing', () { |
485 final el = makeElement(); | 485 final el = makeElement(); |
486 el.insertAdjacentText("beforeBegin", "foo"); | 486 el.insertAdjacentText("beforeBegin", "foo"); |
487 expect(el.innerHTML, "<foo></foo><bar></bar>"); | 487 expect(el.innerHtml, "<foo></foo><bar></bar>"); |
488 }); | 488 }); |
489 | 489 |
490 test('afterEnd with no parent does nothing', () { | 490 test('afterEnd with no parent does nothing', () { |
491 final el = makeElement(); | 491 final el = makeElement(); |
492 el.insertAdjacentText("afterEnd", "foo"); | 492 el.insertAdjacentText("afterEnd", "foo"); |
493 expect(el.innerHTML, "<foo></foo><bar></bar>"); | 493 expect(el.innerHtml, "<foo></foo><bar></bar>"); |
494 }); | 494 }); |
495 | 495 |
496 test('beforeBegin with parent inserts the text', () { | 496 test('beforeBegin with parent inserts the text', () { |
497 final el = makeElementWithParent(); | 497 final el = makeElementWithParent(); |
498 el.insertAdjacentText("beforeBegin", "foo"); | 498 el.insertAdjacentText("beforeBegin", "foo"); |
499 expect(el.innerHTML, "<foo></foo><bar></bar>"); | 499 expect(el.innerHtml, "<foo></foo><bar></bar>"); |
500 expect(el.parent.innerHTML, | 500 expect(el.parent.innerHtml, |
501 "<before></before>foo<xml><foo></foo><bar></bar></xml>" | 501 "<before></before>foo<xml><foo></foo><bar></bar></xml>" |
502 "<after></after>"); | 502 "<after></after>"); |
503 }); | 503 }); |
504 | 504 |
505 test('afterEnd with parent inserts the text', () { | 505 test('afterEnd with parent inserts the text', () { |
506 final el = makeElementWithParent(); | 506 final el = makeElementWithParent(); |
507 el.insertAdjacentText("afterEnd", "foo"); | 507 el.insertAdjacentText("afterEnd", "foo"); |
508 expect(el.innerHTML, "<foo></foo><bar></bar>"); | 508 expect(el.innerHtml, "<foo></foo><bar></bar>"); |
509 expect(el.parent.innerHTML, | 509 expect(el.parent.innerHtml, |
510 "<before></before><xml><foo></foo><bar></bar></xml>foo" | 510 "<before></before><xml><foo></foo><bar></bar></xml>foo" |
511 "<after></after>"); | 511 "<after></after>"); |
512 }); | 512 }); |
513 | 513 |
514 test('afterBegin inserts the text', () { | 514 test('afterBegin inserts the text', () { |
515 final el = makeElement(); | 515 final el = makeElement(); |
516 el.insertAdjacentText("afterBegin", "foo"); | 516 el.insertAdjacentText("afterBegin", "foo"); |
517 expect(el.innerHTML, "foo<foo></foo><bar></bar>"); | 517 expect(el.innerHtml, "foo<foo></foo><bar></bar>"); |
518 }); | 518 }); |
519 | 519 |
520 test('beforeEnd inserts the text', () { | 520 test('beforeEnd inserts the text', () { |
521 final el = makeElement(); | 521 final el = makeElement(); |
522 el.insertAdjacentText("beforeEnd", "foo"); | 522 el.insertAdjacentText("beforeEnd", "foo"); |
523 expect(el.innerHTML, "<foo></foo><bar></bar>foo"); | 523 expect(el.innerHtml, "<foo></foo><bar></bar>foo"); |
524 }); | 524 }); |
525 }); | 525 }); |
526 | 526 |
527 group('insertAdjacentHTML', () { | 527 group('insertAdjacentHtml', () { |
528 test('beforeBegin with no parent does nothing', () { | 528 test('beforeBegin with no parent does nothing', () { |
529 final el = makeElement(); | 529 final el = makeElement(); |
530 el.insertAdjacentHTML("beforeBegin", "foo<b/>"); | 530 el.insertAdjacentHtml("beforeBegin", "foo<b/>"); |
531 expect(el.innerHTML, "<foo></foo><bar></bar>"); | 531 expect(el.innerHtml, "<foo></foo><bar></bar>"); |
532 }); | 532 }); |
533 | 533 |
534 test('afterEnd with no parent does nothing', () { | 534 test('afterEnd with no parent does nothing', () { |
535 final el = makeElement(); | 535 final el = makeElement(); |
536 el.insertAdjacentHTML("afterEnd", "<b/>foo"); | 536 el.insertAdjacentHtml("afterEnd", "<b/>foo"); |
537 expect(el.innerHTML, "<foo></foo><bar></bar>"); | 537 expect(el.innerHtml, "<foo></foo><bar></bar>"); |
538 }); | 538 }); |
539 | 539 |
540 test('beforeBegin with parent inserts the HTML', () { | 540 test('beforeBegin with parent inserts the HTML', () { |
541 final el = makeElementWithParent(); | 541 final el = makeElementWithParent(); |
542 el.insertAdjacentHTML("beforeBegin", "foo<b/>"); | 542 el.insertAdjacentHtml("beforeBegin", "foo<b/>"); |
543 expect(el.innerHTML, "<foo></foo><bar></bar>"); | 543 expect(el.innerHtml, "<foo></foo><bar></bar>"); |
544 expect(el.parent.innerHTML, | 544 expect(el.parent.innerHtml, |
545 "<before></before>foo<b></b><xml><foo></foo><bar></bar>" | 545 "<before></before>foo<b></b><xml><foo></foo><bar></bar>" |
546 "</xml><after></after>"); | 546 "</xml><after></after>"); |
547 }); | 547 }); |
548 | 548 |
549 test('afterEnd with parent inserts the HTML', () { | 549 test('afterEnd with parent inserts the HTML', () { |
550 final el = makeElementWithParent(); | 550 final el = makeElementWithParent(); |
551 el.insertAdjacentHTML("afterEnd", "foo<b/>"); | 551 el.insertAdjacentHtml("afterEnd", "foo<b/>"); |
552 expect(el.innerHTML, "<foo></foo><bar></bar>"); | 552 expect(el.innerHtml, "<foo></foo><bar></bar>"); |
553 expect(el.parent.innerHTML, | 553 expect(el.parent.innerHtml, |
554 "<before></before><xml><foo></foo><bar></bar></xml>foo" | 554 "<before></before><xml><foo></foo><bar></bar></xml>foo" |
555 "<b></b><after></after>"); | 555 "<b></b><after></after>"); |
556 }); | 556 }); |
557 | 557 |
558 test('afterBegin inserts the HTML', () { | 558 test('afterBegin inserts the HTML', () { |
559 final el = makeElement(); | 559 final el = makeElement(); |
560 el.insertAdjacentHTML("afterBegin", "foo<b/>"); | 560 el.insertAdjacentHtml("afterBegin", "foo<b/>"); |
561 expect(el.innerHTML, "foo<b></b><foo></foo><bar></bar>"); | 561 expect(el.innerHtml, "foo<b></b><foo></foo><bar></bar>"); |
562 }); | 562 }); |
563 | 563 |
564 test('beforeEnd inserts the HTML', () { | 564 test('beforeEnd inserts the HTML', () { |
565 final el = makeElement(); | 565 final el = makeElement(); |
566 el.insertAdjacentHTML("beforeEnd", "<b/>foo"); | 566 el.insertAdjacentHtml("beforeEnd", "<b/>foo"); |
567 expect(el.innerHTML, "<foo></foo><bar></bar><b></b>foo"); | 567 expect(el.innerHtml, "<foo></foo><bar></bar><b></b>foo"); |
568 }); | 568 }); |
569 }); | 569 }); |
570 | 570 |
571 test('default rect values', () { | 571 test('default rect values', () { |
572 makeElement().rect.then( | 572 makeElement().rect.then( |
573 expectAsync1(ElementRect rect) { | 573 expectAsync1(ElementRect rect) { |
574 expectEmptyRect(rect.client); | 574 expectEmptyRect(rect.client); |
575 expectEmptyRect(rect.offset); | 575 expectEmptyRect(rect.offset); |
576 expectEmptyRect(rect.scroll); | 576 expectEmptyRect(rect.scroll); |
577 expectEmptyRect(rect.bounding); | 577 expectEmptyRect(rect.bounding); |
578 expect(rect.clientRects.isEmpty, isTrue); | 578 expect(rect.clientRects.isEmpty, isTrue); |
579 })); | 579 })); |
580 }); | 580 }); |
581 } | 581 } |
OLD | NEW |