dom.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. if (!dojo._hasResource["dojox.data.tests.dom"]) { // _hasResource checks added
  2. // by build. Do not use
  3. // _hasResource directly in
  4. // your code.
  5. dojo._hasResource["dojox.data.tests.dom"] = true;
  6. dojo.provide("dojox.data.tests.dom");
  7. dojo.require("dojox.data.dom");
  8. tests.register("dojox.data.tests.dom", [function testCreateDocument(t) {
  9. var document = dojox.data.dom.createDocument();
  10. t.assertTrue(document !== null);
  11. }, function testCreateDocumentFromText(t) {
  12. var simpleXml = "<parentNode><childNode><grandchildNode/></childNode><childNode/></parentNode>";
  13. var document = dojox.data.dom.createDocument(simpleXml,
  14. "text/xml");
  15. var parent = document.firstChild;
  16. t.assertTrue(parent !== null);
  17. t.assertTrue(parent.tagName === "parentNode");
  18. t.assertTrue(parent.childNodes.length == 2);
  19. var firstChild = parent.firstChild;
  20. t.assertTrue(firstChild !== null);
  21. t.assertTrue(firstChild.tagName === "childNode");
  22. t.assertTrue(firstChild.childNodes.length == 1);
  23. var secondChild = firstChild.nextSibling;
  24. t.assertTrue(secondChild !== null);
  25. t.assertTrue(secondChild.tagName === "childNode");
  26. var grandChild = firstChild.firstChild;
  27. t.assertTrue(grandChild !== null);
  28. t.assertTrue(grandChild.tagName === "grandchildNode");
  29. }, function testReadTextContent(t) {
  30. var text = "This is a bunch of child text on the node";
  31. var simpleXml = "<parentNode>" + text + "</parentNode>";
  32. var document = dojox.data.dom.createDocument(simpleXml,
  33. "text/xml");
  34. var topNode = document.firstChild;
  35. t.assertTrue(topNode !== null);
  36. t.assertTrue(topNode.tagName === "parentNode");
  37. t.assertTrue(text === dojox.data.dom.textContent(topNode));
  38. dojo._destroyElement(topNode);
  39. t.assertTrue(document.firstChild === null);
  40. }, function testSetTextContent(t) {
  41. var text = "This is a bunch of child text on the node";
  42. var text2 = "This is the new text";
  43. var simpleXml = "<parentNode>" + text + "</parentNode>";
  44. var document = dojox.data.dom.createDocument(simpleXml,
  45. "text/xml");
  46. var topNode = document.firstChild;
  47. t.assertTrue(topNode !== null);
  48. t.assertTrue(topNode.tagName === "parentNode");
  49. t.assertTrue(text === dojox.data.dom.textContent(topNode));
  50. dojox.data.dom.textContent(topNode, text2);
  51. t.assertTrue(text2 === dojox.data.dom.textContent(topNode));
  52. dojo._destroyElement(topNode);
  53. t.assertTrue(document.firstChild === null);
  54. }, function testReplaceChildrenArray(t) {
  55. var simpleXml1 = "<parentNode><child1/><child2/><child3/></parentNode>";
  56. var simpleXml2 = "<parentNode><child4/><child5/><child6/><child7/></parentNode>";
  57. var doc1 = dojox.data.dom
  58. .createDocument(simpleXml1, "text/xml");
  59. var doc2 = dojox.data.dom
  60. .createDocument(simpleXml2, "text/xml");
  61. var topNode1 = doc1.firstChild;
  62. var topNode2 = doc2.firstChild;
  63. t.assertTrue(topNode1 !== null);
  64. t.assertTrue(topNode1.tagName === "parentNode");
  65. t.assertTrue(topNode2 !== null);
  66. t.assertTrue(topNode2.tagName === "parentNode");
  67. dojox.data.dom.removeChildren(topNode1);
  68. var newChildren = [];
  69. for (var i = 0; i < topNode2.childNodes.length; i++) {
  70. newChildren.push(topNode2.childNodes[i]);
  71. }
  72. dojox.data.dom.removeChildren(topNode2);
  73. dojox.data.dom.replaceChildren(topNode1, newChildren);
  74. t.assertTrue(topNode1.childNodes.length === 4);
  75. t.assertTrue(topNode1.firstChild.tagName === "child4");
  76. t.assertTrue(topNode1.lastChild.tagName === "child7");
  77. }, function testReplaceChildrenSingle(t) {
  78. var simpleXml1 = "<parentNode><child1/><child2/><child3/></parentNode>";
  79. var simpleXml2 = "<parentNode><child4/></parentNode>";
  80. var doc1 = dojox.data.dom
  81. .createDocument(simpleXml1, "text/xml");
  82. var doc2 = dojox.data.dom
  83. .createDocument(simpleXml2, "text/xml");
  84. var topNode1 = doc1.firstChild;
  85. var topNode2 = doc2.firstChild;
  86. t.assertTrue(topNode1 !== null);
  87. t.assertTrue(topNode1.tagName === "parentNode");
  88. t.assertTrue(topNode2 !== null);
  89. t.assertTrue(topNode2.tagName === "parentNode");
  90. dojox.data.dom.removeChildren(topNode1);
  91. var newChildren = topNode2.firstChild;
  92. dojox.data.dom.removeChildren(topNode2);
  93. dojox.data.dom.replaceChildren(topNode1, newChildren);
  94. t.assertTrue(topNode1.childNodes.length === 1);
  95. t.assertTrue(topNode1.firstChild.tagName === "child4");
  96. t.assertTrue(topNode1.lastChild.tagName === "child4");
  97. }, function testRemoveChildren(t) {
  98. var simpleXml1 = "<parentNode><child1/><child2/><child3/></parentNode>";
  99. var doc1 = dojox.data.dom
  100. .createDocument(simpleXml1, "text/xml");
  101. var topNode1 = doc1.firstChild;
  102. t.assertTrue(topNode1 !== null);
  103. t.assertTrue(topNode1.tagName === "parentNode");
  104. dojox.data.dom.removeChildren(topNode1);
  105. t.assertTrue(topNode1.childNodes.length === 0);
  106. t.assertTrue(topNode1.firstChild === null);
  107. }, function testInnerXML(t) {
  108. var simpleXml1 = "<parentNode><child1/><child2/><child3/></parentNode>";
  109. var doc1 = dojox.data.dom
  110. .createDocument(simpleXml1, "text/xml");
  111. var topNode1 = doc1.firstChild;
  112. t.assertTrue(topNode1 !== null);
  113. t.assertTrue(topNode1.tagName === "parentNode");
  114. var innerXml = dojox.data.dom.innerXML(topNode1);
  115. t.assertTrue(simpleXml1 === innerXml);
  116. }]);
  117. }