OLD | NEW |
1 <sky> | 1 import "../resources/dom_utils.dart"; |
2 <import src="../resources/dom-utils.sky" as="DomUtils" /> | |
3 <script> | |
4 import "../resources/third_party/unittest/unittest.dart"; | 2 import "../resources/third_party/unittest/unittest.dart"; |
5 import "../resources/unit.dart"; | 3 import "../resources/unit.dart"; |
6 | 4 |
7 import "dart:sky"; | 5 import "dart:sky"; |
8 | 6 |
9 void main() { | 7 void main() { |
10 initUnit(); | 8 initUnit(); |
11 | 9 |
12 var doc; | 10 var doc; |
13 | 11 |
14 var childElementCount = DomUtils.childElementCount; | |
15 var childNodeCount = DomUtils.childNodeCount; | |
16 | |
17 setUp(() { | 12 setUp(() { |
18 doc = new Document(); | 13 doc = new Document(); |
19 }); | 14 }); |
20 | 15 |
21 test("should allow replacing the document element", () { | 16 test("should allow replacing the document element", () { |
22 var oldChild = doc.appendChild(doc.createElement("div")); | 17 var oldChild = doc.appendChild(doc.createElement("div")); |
23 expect(childElementCount(doc), equals(1)); | 18 expect(childElementCount(doc), equals(1)); |
24 var newChild = doc.createElement("div"); | 19 var newChild = doc.createElement("div"); |
25 oldChild.replaceWith([newChild]); | 20 oldChild.replaceWith([newChild]); |
26 expect(childElementCount(doc), equals(1)); | 21 expect(childElementCount(doc), equals(1)); |
27 expect(newChild.parentNode, equals(doc)); | 22 expect(newChild.parentNode, equals(doc)); |
28 expect(oldChild.parentNode, isNull); | 23 expect(oldChild.parentNode, isNull); |
29 }); | 24 }); |
30 | 25 |
31 test("should allow replacing a text child with an element", () { | 26 test("should allow replacing a text child with an element", () { |
32 var oldChild = doc.appendChild(new Text("text here")); | 27 var oldChild = doc.appendChild(doc.createText("text here")); |
33 expect(childElementCount(doc), equals(0)); | 28 expect(childElementCount(doc), equals(0)); |
34 expect(childNodeCount(doc), equals(1)); | 29 expect(childNodeCount(doc), equals(1)); |
35 var newChild = doc.createElement("div"); | 30 var newChild = doc.createElement("div"); |
36 oldChild.replaceWith([newChild]); | 31 oldChild.replaceWith([newChild]); |
37 expect(childElementCount(doc), equals(1)); | 32 expect(childElementCount(doc), equals(1)); |
38 expect(childNodeCount(doc), equals(1)); | 33 expect(childNodeCount(doc), equals(1)); |
39 expect(newChild.parentNode, equals(doc)); | 34 expect(newChild.parentNode, equals(doc)); |
40 expect(oldChild.parentNode, isNull); | 35 expect(oldChild.parentNode, isNull); |
41 }); | 36 }); |
42 | 37 |
43 test("should allow replacing the document element with text", () { | 38 test("should allow replacing the document element with text", () { |
44 var oldChild = doc.appendChild(doc.createElement("div")); | 39 var oldChild = doc.appendChild(doc.createElement("div")); |
45 expect(childElementCount(doc), equals(1)); | 40 expect(childElementCount(doc), equals(1)); |
46 var newChild = new Text(" text "); | 41 var newChild = doc.createText(" text "); |
47 oldChild.replaceWith([newChild]); | 42 oldChild.replaceWith([newChild]); |
48 expect(childElementCount(doc), equals(0)); | 43 expect(childElementCount(doc), equals(0)); |
49 expect(childNodeCount(doc), equals(1)); | 44 expect(childNodeCount(doc), equals(1)); |
50 expect(newChild.parentNode, equals(doc)); | 45 expect(newChild.parentNode, equals(doc)); |
51 expect(oldChild.parentNode, isNull); | 46 expect(oldChild.parentNode, isNull); |
52 }); | 47 }); |
53 | 48 |
54 test("should allow inserting text with a fragment", () { | 49 test("should allow inserting text with a fragment", () { |
55 var fragment = doc.createDocumentFragment(); | 50 var fragment = doc.createDocumentFragment(); |
56 fragment.appendChild(new Text(" text ")); | 51 fragment.appendChild(doc.createText(" text ")); |
57 fragment.appendChild(new Text(" text ")); | 52 fragment.appendChild(doc.createText(" text ")); |
58 expect(childNodeCount(doc), equals(0)); | 53 expect(childNodeCount(doc), equals(0)); |
59 doc.appendChild(fragment); | 54 doc.appendChild(fragment); |
60 expect(childElementCount(doc), equals(0)); | 55 expect(childElementCount(doc), equals(0)); |
61 expect(childNodeCount(doc), equals(2)); | 56 expect(childNodeCount(doc), equals(2)); |
62 }); | 57 }); |
63 | 58 |
64 test("should allow replacing the document element with a fragment", () { | 59 test("should allow replacing the document element with a fragment", () { |
65 var oldChild = doc.appendChild(doc.createElement("div")); | 60 var oldChild = doc.appendChild(doc.createElement("div")); |
66 expect(childElementCount(doc), equals(1)); | 61 expect(childElementCount(doc), equals(1)); |
67 var fragment = doc.createDocumentFragment(); | 62 var fragment = doc.createDocumentFragment(); |
68 fragment.appendChild(new Text(" text ")); | 63 fragment.appendChild(doc.createText(" text ")); |
69 var newChild = fragment.appendChild(doc.createElement("div")); | 64 var newChild = fragment.appendChild(doc.createElement("div")); |
70 fragment.appendChild(new Text(" ")); | 65 fragment.appendChild(doc.createText(" ")); |
71 oldChild.replaceWith([fragment]); | 66 oldChild.replaceWith([fragment]); |
72 expect(childElementCount(doc), equals(1)); | 67 expect(childElementCount(doc), equals(1)); |
73 expect(childNodeCount(doc), equals(3)); | 68 expect(childNodeCount(doc), equals(3)); |
74 expect(newChild.parentNode, equals(doc)); | 69 expect(newChild.parentNode, equals(doc)); |
75 expect(oldChild.parentNode, isNull); | 70 expect(oldChild.parentNode, isNull); |
76 }); | 71 }); |
77 | 72 |
78 test("should throw when inserting multiple elements", () { | 73 test("should throw when inserting multiple elements", () { |
79 doc.appendChild(doc.createElement("div")); | 74 doc.appendChild(doc.createElement("div")); |
80 var oldChild = doc.appendChild(new Text(" text ")); | 75 var oldChild = doc.appendChild(doc.createText(" text ")); |
81 expect(childElementCount(doc), equals(1)); | 76 expect(childElementCount(doc), equals(1)); |
82 var newChild = doc.createElement("div"); | 77 var newChild = doc.createElement("div"); |
83 // expect(() { | 78 // expect(() { |
84 // doc.replaceChild(newChild, 0); | 79 // doc.replaceChild(newChild, 0); |
85 // }, throws); | 80 // }, throws); |
86 // expect(() { | 81 // expect(() { |
87 // doc.insertBefore(newChild, oldChild); | 82 // doc.insertBefore(newChild, oldChild); |
88 // }, throws); | 83 // }, throws); |
89 }); | 84 }); |
90 | 85 |
91 test("should throw when inserting multiple elements with a fragment", () { | 86 test("should throw when inserting multiple elements with a fragment", () { |
92 var oldChild = doc.appendChild(doc.createElement("div")); | 87 var oldChild = doc.appendChild(doc.createElement("div")); |
93 expect(childElementCount(doc), equals(1)); | 88 expect(childElementCount(doc), equals(1)); |
94 var fragment = doc.createDocumentFragment(); | 89 var fragment = doc.createDocumentFragment(); |
95 fragment.appendChild(new Text(" text ")); | 90 fragment.appendChild(doc.createText(" text ")); |
96 fragment.appendChild(doc.createElement("div")); | 91 fragment.appendChild(doc.createElement("div")); |
97 fragment.appendChild(doc.createElement("div")); | 92 fragment.appendChild(doc.createElement("div")); |
98 fragment.appendChild(new Text(" ")); | 93 fragment.appendChild(doc.createText(" ")); |
99 // expect(() { | 94 // expect(() { |
100 // doc.replaceChild(fragment, 0); | 95 // doc.replaceChild(fragment, 0); |
101 // }, throws); | 96 // }, throws); |
102 // expect(() { | 97 // expect(() { |
103 // doc.insertBefore(fragment, oldChild); | 98 // doc.insertBefore(fragment, oldChild); |
104 // }, throws); | 99 // }, throws); |
105 }); | 100 }); |
106 } | 101 } |
107 </script> | |
108 </sky> | |
OLD | NEW |