[483] | 1 | define(['doh', 'dojo/_base/html', '../parser'], function(doh, html, dxparser){ |
---|
| 2 | |
---|
| 3 | doh.register("dojox.xml.tests.parser", |
---|
| 4 | [ |
---|
| 5 | function testParse(t){ |
---|
| 6 | var document = dxparser.parse(); |
---|
| 7 | t.assertTrue(document !== null); |
---|
| 8 | }, |
---|
| 9 | function testParseFromText(t){ |
---|
| 10 | var simpleXml = "<parentNode><childNode><grandchildNode/></childNode><childNode/></parentNode>"; |
---|
| 11 | var document = dxparser.parse(simpleXml, "text/xml"); |
---|
| 12 | |
---|
| 13 | var parent = document.firstChild; |
---|
| 14 | t.assertTrue(parent !== null); |
---|
| 15 | t.assertTrue(parent.tagName === "parentNode"); |
---|
| 16 | t.assertTrue(parent.childNodes.length == 2); |
---|
| 17 | |
---|
| 18 | var firstChild = parent.firstChild; |
---|
| 19 | t.assertTrue(firstChild !== null); |
---|
| 20 | t.assertTrue(firstChild.tagName === "childNode"); |
---|
| 21 | t.assertTrue(firstChild.childNodes.length == 1); |
---|
| 22 | |
---|
| 23 | var secondChild = firstChild.nextSibling; |
---|
| 24 | t.assertTrue(secondChild !== null); |
---|
| 25 | t.assertTrue(secondChild.tagName === "childNode"); |
---|
| 26 | |
---|
| 27 | var grandChild = firstChild.firstChild; |
---|
| 28 | t.assertTrue(grandChild !== null); |
---|
| 29 | t.assertTrue(grandChild.tagName === "grandchildNode"); |
---|
| 30 | |
---|
| 31 | }, |
---|
| 32 | function testParseEmptyString(t){ |
---|
| 33 | var simpleXml = ""; |
---|
| 34 | var document = dxparser.parse(simpleXml, "text/xml"); |
---|
| 35 | |
---|
| 36 | t.assertTrue(typeof document != "undefined"); |
---|
| 37 | |
---|
| 38 | var parent = document.firstChild; |
---|
| 39 | t.assertTrue(parent === null); |
---|
| 40 | }, |
---|
| 41 | function testParseEmpty(t){ |
---|
| 42 | var simpleXml; |
---|
| 43 | var document = dxparser.parse(); |
---|
| 44 | |
---|
| 45 | t.assertTrue(typeof document != "undefined"); |
---|
| 46 | |
---|
| 47 | var parent = document.firstChild; |
---|
| 48 | t.assertTrue(parent === null); |
---|
| 49 | }, |
---|
| 50 | function testReadTextContent(t){ |
---|
| 51 | var text = "This is a bunch of child text on the node"; |
---|
| 52 | var simpleXml = "<parentNode>" + text + "</parentNode>"; |
---|
| 53 | var document = dxparser.parse(simpleXml, "text/xml"); |
---|
| 54 | |
---|
| 55 | var topNode = document.firstChild; |
---|
| 56 | t.assertTrue(topNode !== null); |
---|
| 57 | t.assertTrue(topNode.tagName === "parentNode"); |
---|
| 58 | t.assertTrue(text === dxparser.textContent(topNode)); |
---|
| 59 | html.destroy(topNode); |
---|
| 60 | t.assertTrue(document.firstChild === null); |
---|
| 61 | }, |
---|
| 62 | function testSetTextContent(t){ |
---|
| 63 | var text = "This is a bunch of child text on the node"; |
---|
| 64 | var text2 = "This is the new text"; |
---|
| 65 | var simpleXml = "<parentNode>" + text + "</parentNode>"; |
---|
| 66 | var document = dxparser.parse(simpleXml, "text/xml"); |
---|
| 67 | |
---|
| 68 | var topNode = document.firstChild; |
---|
| 69 | t.assertTrue(topNode !== null); |
---|
| 70 | t.assertTrue(topNode.tagName === "parentNode"); |
---|
| 71 | t.assertTrue(text === dxparser.textContent(topNode)); |
---|
| 72 | dxparser.textContent(topNode, text2); |
---|
| 73 | t.assertTrue(text2 === dxparser.textContent(topNode)); |
---|
| 74 | html.destroy(topNode); |
---|
| 75 | t.assertTrue(document.firstChild === null); |
---|
| 76 | |
---|
| 77 | }, |
---|
| 78 | function testReplaceChildrenArray(t){ |
---|
| 79 | var simpleXml1 = "<parentNode><child1/><child2/><child3/></parentNode>"; |
---|
| 80 | var simpleXml2 = "<parentNode><child4/><child5/><child6/><child7/></parentNode>"; |
---|
| 81 | var doc1 = dxparser.parse(simpleXml1, "text/xml"); |
---|
| 82 | var doc2 = dxparser.parse(simpleXml2, "text/xml"); |
---|
| 83 | |
---|
| 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 | dxparser.removeChildren(topNode1); |
---|
| 91 | var newChildren=[]; |
---|
| 92 | for(var i=0;i<topNode2.childNodes.length;i++){ |
---|
| 93 | newChildren.push(topNode2.childNodes[i]); |
---|
| 94 | } |
---|
| 95 | dxparser.removeChildren(topNode2); |
---|
| 96 | dxparser.replaceChildren(topNode1,newChildren); |
---|
| 97 | t.assertEqual(4, topNode1.childNodes.length); |
---|
| 98 | t.assertEqual("child4", topNode1.firstChild.tagName); |
---|
| 99 | t.assertEqual("child7", topNode1.lastChild.tagName); |
---|
| 100 | |
---|
| 101 | }, |
---|
| 102 | function testReplaceChildrenSingle(t){ |
---|
| 103 | var simpleXml1 = "<parentNode><child1/><child2/><child3/></parentNode>"; |
---|
| 104 | var simpleXml2 = "<parentNode><child4/></parentNode>"; |
---|
| 105 | var doc1 = dxparser.parse(simpleXml1, "text/xml"); |
---|
| 106 | var doc2 = dxparser.parse(simpleXml2, "text/xml"); |
---|
| 107 | |
---|
| 108 | var topNode1 = doc1.firstChild; |
---|
| 109 | var topNode2 = doc2.firstChild; |
---|
| 110 | t.assertTrue(topNode1 !== null); |
---|
| 111 | t.assertTrue(topNode1.tagName === "parentNode"); |
---|
| 112 | t.assertTrue(topNode2 !== null); |
---|
| 113 | t.assertTrue(topNode2.tagName === "parentNode"); |
---|
| 114 | dxparser.removeChildren(topNode1); |
---|
| 115 | |
---|
| 116 | var newChildren = topNode2.firstChild; |
---|
| 117 | dxparser.removeChildren(topNode2); |
---|
| 118 | dxparser.replaceChildren(topNode1,newChildren); |
---|
| 119 | t.assertTrue(topNode1.childNodes.length === 1); |
---|
| 120 | t.assertTrue(topNode1.firstChild.tagName === "child4"); |
---|
| 121 | t.assertTrue(topNode1.lastChild.tagName === "child4"); |
---|
| 122 | }, |
---|
| 123 | function testRemoveChildren(t){ |
---|
| 124 | var simpleXml1 = "<parentNode><child1/><child2/><child3/></parentNode>"; |
---|
| 125 | var doc1 = dxparser.parse(simpleXml1, "text/xml"); |
---|
| 126 | |
---|
| 127 | var topNode1 = doc1.firstChild; |
---|
| 128 | t.assertTrue(topNode1 !== null); |
---|
| 129 | t.assertTrue(topNode1.tagName === "parentNode"); |
---|
| 130 | dxparser.removeChildren(topNode1); |
---|
| 131 | t.assertTrue(topNode1.childNodes.length === 0); |
---|
| 132 | t.assertTrue(topNode1.firstChild === null); |
---|
| 133 | }, |
---|
| 134 | function testInnerXML(t){ |
---|
| 135 | var simpleXml1 = "<parentNode><child1/><child2/><child3/></parentNode>"; |
---|
| 136 | var doc1 = dxparser.parse(simpleXml1, "text/xml"); |
---|
| 137 | |
---|
| 138 | var topNode1 = doc1.firstChild; |
---|
| 139 | t.assertTrue(topNode1 !== null); |
---|
| 140 | t.assertTrue(topNode1.tagName === "parentNode"); |
---|
| 141 | |
---|
| 142 | var innerXml = dxparser.innerXML(topNode1); |
---|
| 143 | t.assertTrue(simpleXml1 === innerXml); |
---|
| 144 | } |
---|
| 145 | ] |
---|
| 146 | ); |
---|
| 147 | |
---|
| 148 | }); |
---|