OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011, 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 DocumentFragmentTest; |
| 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 isAnchorElement = |
| 15 predicate((x) => x is AnchorElement, 'is an AnchorElement'); |
| 16 |
| 17 List<String> _nodeStrings(Iterable<Node> input) { |
| 18 var out = new List<String>(); |
| 19 for (Node n in input) { |
| 20 if (n is Element) { |
| 21 Element e = n; |
| 22 out.add(e.tagName); |
| 23 } else { |
| 24 out.add(n.text); |
| 25 } |
| 26 } |
| 27 return out; |
| 28 }; |
| 29 |
| 30 assertConstError(void fn()) { |
| 31 try { |
| 32 fn(); |
| 33 } catch (e) { |
| 34 if (e is UnsupportedError) { |
| 35 return; |
| 36 } |
| 37 } |
| 38 expect(true, isFalse, reason: 'Expected immutability error'); |
| 39 }; |
| 40 |
| 41 void expectEmptyStyleDeclaration(CssStyleDeclaration style) { |
| 42 expect(style.cssText, equals('')); |
| 43 expect(style.getPropertyPriority('color'), equals('')); |
| 44 expect(style.item(0), equals('')); |
| 45 expect(style.length, isZero); |
| 46 // TODO(jacobr): these checks throw UnimplementedErrors in dartium. |
| 47 // expect(style.parentRule, isNull); |
| 48 // expect(style.getPropertyCssValue('color'), isNull); |
| 49 // expect(style.getPropertyShorthand('color'), isNull); |
| 50 // expect(style.isPropertyImplicit('color'), isFalse); |
| 51 |
| 52 // Ideally these would throw errors, but it's not possible to create a class |
| 53 // that'll intercept these calls without implementing the entire |
| 54 // CssStyleDeclaration interface, so we'll settle for them being no-ops. |
| 55 style.cssText = '* {color: blue}'; |
| 56 style.removeProperty('color'); |
| 57 style.setProperty('color', 'blue'); |
| 58 } |
| 59 |
| 60 group('constructors', () { |
| 61 test('0-argument makes an empty fragment', () { |
| 62 final fragment = new DocumentFragment(); |
| 63 expect(fragment.children, equals([])); |
| 64 }); |
| 65 |
| 66 test('.html parses input as HTML', () { |
| 67 final fragment = new DocumentFragment.html('<a>foo</a>'); |
| 68 expect(fragment.children[0], isAnchorElement); |
| 69 }); |
| 70 |
| 71 // test('.svg parses input as SVG', () { |
| 72 // final fragment = new DocumentFragment.svg('<a>foo</a>'); |
| 73 // expect(fragment.children[0] is SVGAElement, isTrue); |
| 74 // }); |
| 75 |
| 76 // TODO(nweiz): enable this once XML is ported. |
| 77 // test('.xml parses input as XML', () { |
| 78 // final fragment = new DocumentFragment.xml('<a>foo</a>'); |
| 79 // expect(fragment.children[0] is XMLElement, isTrue); |
| 80 // }); |
| 81 }); |
| 82 |
| 83 group('children', () { |
| 84 var fragment; |
| 85 var children; |
| 86 |
| 87 init() { |
| 88 fragment = new DocumentFragment(); |
| 89 children = fragment.children; |
| 90 fragment.nodes.addAll( |
| 91 [new Text("1"), new Element.tag("A"), new Element.tag("B"), |
| 92 new Text("2"), new Element.tag("I"), new Text("3"), |
| 93 new Element.tag("U")]); |
| 94 }; |
| 95 |
| 96 test('is initially empty', () { |
| 97 children = new DocumentFragment().children; |
| 98 expect(children, equals([])); |
| 99 expect(children.isEmpty, isTrue); |
| 100 }); |
| 101 |
| 102 test('filters out non-element nodes', () { |
| 103 init(); |
| 104 expect(_nodeStrings(fragment.nodes), |
| 105 orderedEquals(["1", "A", "B", "2", "I", "3", "U"])); |
| 106 expect(_nodeStrings(children), |
| 107 orderedEquals(["A", "B", "I", "U"])); |
| 108 }); |
| 109 |
| 110 test('only indexes children, not other nodes', () { |
| 111 init(); |
| 112 children[1] = new Element.tag("BR"); |
| 113 expect(_nodeStrings(fragment.nodes), |
| 114 orderedEquals(["1", "A", "BR", "2", "I", "3", "U"])); |
| 115 expect(_nodeStrings(children), |
| 116 orderedEquals(["A", "BR", "I", "U"])); |
| 117 }); |
| 118 |
| 119 test('adds to both children and nodes', () { |
| 120 init(); |
| 121 children.add(new Element.tag("UL")); |
| 122 expect(_nodeStrings(fragment.nodes), |
| 123 orderedEquals(["1", "A", "B", "2", "I", "3", "U", "UL"])); |
| 124 expect(_nodeStrings(children), |
| 125 orderedEquals(["A", "B", "I", "U", "UL"])); |
| 126 }); |
| 127 |
| 128 test('removes only children, from both children and nodes', () { |
| 129 init(); |
| 130 expect(children.removeLast().tagName, equals('U')); |
| 131 expect(_nodeStrings(fragment.nodes), |
| 132 orderedEquals(["1", "A", "B", "2", "I", "3"])); |
| 133 expect(_nodeStrings(children), |
| 134 orderedEquals(["A", "B", "I"])); |
| 135 |
| 136 expect(children.removeLast().tagName, "I"); |
| 137 expect(_nodeStrings(fragment.nodes), |
| 138 equals(["1", "A", "B", "2", "3"])); |
| 139 expect(_nodeStrings(children), equals(["A", "B"])); |
| 140 }); |
| 141 |
| 142 test('accessors are wrapped', () { |
| 143 init(); |
| 144 expect(children[0].tagName, "A"); |
| 145 expect(_nodeStrings(children.where((e) => e.tagName == "I")), ["I"]); |
| 146 expect(children.every((e) => e is Element), isTrue); |
| 147 expect(children.any((e) => e.tagName == "U"), isTrue); |
| 148 expect(children.isEmpty, isFalse); |
| 149 expect(children.length, 4); |
| 150 expect(children[2].tagName, "I"); |
| 151 expect(children.last.tagName, "U"); |
| 152 }); |
| 153 |
| 154 test('setting children overwrites nodes as well', () { |
| 155 init(); |
| 156 fragment.children = [new Element.tag("DIV"), new Element.tag("HEAD")]; |
| 157 expect(_nodeStrings(fragment.nodes), equals(["DIV", "HEAD"])); |
| 158 }); |
| 159 }); |
| 160 |
| 161 test('setting innerHtml works', () { |
| 162 var fragment = new DocumentFragment(); |
| 163 fragment.append(new Text("foo")); |
| 164 fragment.innerHtml = "<a>bar</a>baz"; |
| 165 expect(_nodeStrings(fragment.nodes), equals(["A", "baz"])); |
| 166 }); |
| 167 |
| 168 test('getting innerHtml works', () { |
| 169 var fragment = new DocumentFragment(); |
| 170 fragment.nodes.addAll([new Text("foo"), new Element.html("<A>bar</A>")]); |
| 171 expect(fragment.innerHtml, "foo<a>bar</a>"); |
| 172 }); |
| 173 |
| 174 test('query searches the fragment', () { |
| 175 var fragment = new DocumentFragment.html( |
| 176 "<div class='foo'><a>foo</a><b>bar</b></div>"); |
| 177 expect(fragment.query(".foo a").tagName, "A"); |
| 178 expect(_nodeStrings(fragment.queryAll(".foo *")), equals(["A", "B"])); |
| 179 }); |
| 180 } |
OLD | NEW |