OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file |
| 4 |
| 5 library ElementAddTest; |
| 6 import 'package:unittest/unittest.dart'; |
| 7 import 'package:unittest/html_config.dart'; |
| 8 import 'util.dart'; |
| 9 import 'dart:html'; |
| 10 |
| 11 main() { |
| 12 useHtmlConfiguration(); |
| 13 |
| 14 var isSpanElement = predicate((x) => x is SpanElement, 'is a SpanElemt'); |
| 15 var isDivElement = predicate((x) => x is DivElement, 'is a DivElement'); |
| 16 var isText = predicate((x) => x is Text, 'is a Text'); |
| 17 |
| 18 void expectNoSuchMethod(void fn()) => |
| 19 expect(fn, throwsNoSuchMethodError); |
| 20 |
| 21 group('append', () { |
| 22 test('htmlelement', () { |
| 23 var el = new DivElement(); |
| 24 el.append(new SpanElement()); |
| 25 expect(el.children.length, equals(1)); |
| 26 var span = el.children[0]; |
| 27 expect(span, isSpanElement); |
| 28 |
| 29 el.append(new DivElement()); |
| 30 expect(el.children.length, equals(2)); |
| 31 // Validate that the first item is still first. |
| 32 expect(el.children[0], equals(span)); |
| 33 expect(el.children[1], isDivElement); |
| 34 }); |
| 35 |
| 36 test('documentFragment', () { |
| 37 var fragment = new DocumentFragment(); |
| 38 fragment.append(new SpanElement()); |
| 39 expect(fragment.children.length, equals(1)); |
| 40 expect(fragment.children[0], isSpanElement); |
| 41 }); |
| 42 }); |
| 43 |
| 44 group('appendHtml', () { |
| 45 test('htmlelement', () { |
| 46 var el = new DivElement(); |
| 47 el.appendHtml('<span></span>'); |
| 48 expect(el.children.length, equals(1)); |
| 49 var span = el.children[0]; |
| 50 expect(span, isSpanElement); |
| 51 |
| 52 el.appendHtml('<div></div>'); |
| 53 expect(el.children.length, equals(2)); |
| 54 // Validate that the first item is still first. |
| 55 expect(el.children[0], equals(span)); |
| 56 expect(el.children[1], isDivElement); |
| 57 }); |
| 58 |
| 59 test('documentFragment', () { |
| 60 var fragment = new DocumentFragment(); |
| 61 fragment.appendHtml('<span>something</span>'); |
| 62 expect(fragment.children.length, equals(1)); |
| 63 expect(fragment.children[0], isSpanElement); |
| 64 }); |
| 65 |
| 66 test('html interpreted in correct context', () { |
| 67 // appendHtml, in order to sanitize, needs to create a document fragment, |
| 68 // but it needs to be created in the right context. If we try to append |
| 69 // table components then the document fragment needs the table context |
| 70 // or it will fail to create them. |
| 71 var el = new TableElement(); |
| 72 el.appendHtml('<tr><td>foo</td></tr>'); |
| 73 expect(el.children.length, 1); |
| 74 var section = el.children.first; |
| 75 expect(section is TableSectionElement, isTrue); |
| 76 var row = section.children.first; |
| 77 expect(row is TableRowElement, isTrue); |
| 78 var item = row.children.first; |
| 79 expect(item is TableCellElement, isTrue); |
| 80 expect(item.innerHtml, 'foo'); |
| 81 }); |
| 82 |
| 83 test("use body context for elements that are don't support it", () { |
| 84 // Some elements can't be used as context for createContextualFragment, |
| 85 // often because it doesn't make any sense. So adding children to a |
| 86 // <br> has no real effect on the page, but we can do it. But the |
| 87 // document fragment will have to be created in the body context. Verify |
| 88 // that this doesn't throw and that the children show up. |
| 89 var el = new BRElement(); |
| 90 el.appendHtml("<p>Stuff</p>"); |
| 91 expect(el.children.length, 1); |
| 92 expect(el.children[0].outerHtml, "<p>Stuff</p>"); |
| 93 }); |
| 94 }); |
| 95 |
| 96 group('appendText', () { |
| 97 test('htmlelement', () { |
| 98 var el = new DivElement(); |
| 99 el.appendText('foo'); |
| 100 // No children were created. |
| 101 expect(el.children.length, equals(0)); |
| 102 // One text node was added. |
| 103 expect(el.nodes.length, equals(1)); |
| 104 }); |
| 105 |
| 106 test('htmlelement', () { |
| 107 var el = new DivElement(); |
| 108 var twoNewLines = "\n\n"; |
| 109 el.appendText(twoNewLines); |
| 110 // No children were created. |
| 111 expect(el.children.length, equals(0)); |
| 112 // One text node was added. |
| 113 expect(el.nodes.length, equals(1)); |
| 114 expect(el.nodes[0], isText); |
| 115 expect(el.nodes[0].text, equals(twoNewLines)); |
| 116 expect(el.text, equals(twoNewLines)); |
| 117 }); |
| 118 |
| 119 test('documentFragment', () { |
| 120 var fragment = new DocumentFragment(); |
| 121 fragment.appendText('foo'); |
| 122 // No children were created. |
| 123 expect(fragment.children.length, equals(0)); |
| 124 // One text node was added. |
| 125 expect(fragment.nodes.length, equals(1)); |
| 126 }); |
| 127 }); |
| 128 |
| 129 group('insertAdjacentElement', () { |
| 130 test('beforebegin', () { |
| 131 var parent = new DivElement(); |
| 132 var child = new DivElement(); |
| 133 var newChild = new SpanElement(); |
| 134 parent.children.add(child); |
| 135 |
| 136 child.insertAdjacentElement('beforebegin', newChild); |
| 137 |
| 138 expect(parent.children.length, 2); |
| 139 expect(parent.children[0], isSpanElement); |
| 140 }); |
| 141 |
| 142 test('afterend', () { |
| 143 var parent = new DivElement(); |
| 144 var child = new DivElement(); |
| 145 var newChild = new SpanElement(); |
| 146 parent.children.add(child); |
| 147 |
| 148 child.insertAdjacentElement('afterend', newChild); |
| 149 |
| 150 expect(parent.children.length, 2); |
| 151 expect(parent.children[1], isSpanElement); |
| 152 }); |
| 153 |
| 154 test('afterbegin', () { |
| 155 var parent = new DivElement(); |
| 156 var child = new DivElement(); |
| 157 var newChild = new SpanElement(); |
| 158 parent.children.add(child); |
| 159 |
| 160 parent.insertAdjacentElement('afterbegin', newChild); |
| 161 |
| 162 expect(parent.children.length, 2); |
| 163 expect(parent.children[0], isSpanElement); |
| 164 }); |
| 165 |
| 166 test('beforeend', () { |
| 167 var parent = new DivElement(); |
| 168 var child = new DivElement(); |
| 169 var newChild = new SpanElement(); |
| 170 parent.children.add(child); |
| 171 |
| 172 parent.insertAdjacentElement('beforeend', newChild); |
| 173 |
| 174 expect(parent.children.length, 2); |
| 175 expect(parent.children[1], isSpanElement); |
| 176 }); |
| 177 }); |
| 178 |
| 179 group('insertAdjacentHtml', () { |
| 180 test('beforebegin', () { |
| 181 var parent = new DivElement(); |
| 182 var child = new DivElement(); |
| 183 parent.children.add(child); |
| 184 |
| 185 child.insertAdjacentHtml('beforebegin', '<span></span>'); |
| 186 |
| 187 expect(parent.children.length, 2); |
| 188 expect(parent.children[0], isSpanElement); |
| 189 }); |
| 190 |
| 191 test('afterend', () { |
| 192 var parent = new DivElement(); |
| 193 var child = new DivElement(); |
| 194 parent.children.add(child); |
| 195 |
| 196 child.insertAdjacentHtml('afterend', '<span></span>'); |
| 197 |
| 198 expect(parent.children.length, 2); |
| 199 expect(parent.children[1], isSpanElement); |
| 200 }); |
| 201 |
| 202 test('afterbegin', () { |
| 203 var parent = new DivElement(); |
| 204 var child = new DivElement(); |
| 205 parent.children.add(child); |
| 206 |
| 207 parent.insertAdjacentHtml('afterbegin', '<span></span>'); |
| 208 |
| 209 expect(parent.children.length, 2); |
| 210 expect(parent.children[0], isSpanElement); |
| 211 }); |
| 212 |
| 213 test('beforeend', () { |
| 214 var parent = new DivElement(); |
| 215 var child = new DivElement(); |
| 216 parent.children.add(child); |
| 217 |
| 218 parent.insertAdjacentHtml('beforeend', '<span></span>'); |
| 219 |
| 220 expect(parent.children.length, 2); |
| 221 expect(parent.children[1], isSpanElement); |
| 222 }); |
| 223 }); |
| 224 |
| 225 group('insertAdjacentText', () { |
| 226 test('beforebegin', () { |
| 227 var parent = new DivElement(); |
| 228 var child = new DivElement(); |
| 229 parent.children.add(child); |
| 230 |
| 231 child.insertAdjacentText('beforebegin', 'test'); |
| 232 |
| 233 expect(parent.nodes.length, 2); |
| 234 expect(parent.nodes[0], isText); |
| 235 }); |
| 236 |
| 237 test('afterend', () { |
| 238 var parent = new DivElement(); |
| 239 var child = new DivElement(); |
| 240 parent.children.add(child); |
| 241 |
| 242 child.insertAdjacentText('afterend', 'test'); |
| 243 |
| 244 expect(parent.nodes.length, 2); |
| 245 expect(parent.nodes[1], isText); |
| 246 }); |
| 247 |
| 248 test('afterbegin', () { |
| 249 var parent = new DivElement(); |
| 250 var child = new DivElement(); |
| 251 parent.children.add(child); |
| 252 |
| 253 parent.insertAdjacentText('afterbegin', 'test'); |
| 254 |
| 255 expect(parent.nodes.length, 2); |
| 256 expect(parent.nodes[0], isText); |
| 257 }); |
| 258 |
| 259 test('beforeend', () { |
| 260 var parent = new DivElement(); |
| 261 var child = new DivElement(); |
| 262 parent.children.add(child); |
| 263 |
| 264 parent.insertAdjacentText('beforeend', 'test'); |
| 265 |
| 266 expect(parent.nodes.length, 2); |
| 267 expect(parent.nodes[1], isText); |
| 268 }); |
| 269 }); |
| 270 } |
OLD | NEW |