| OLD | NEW | 
|---|
| 1 // Copyright (c) 2011, the Dart project authors.  Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. | 
| 4 | 4 | 
| 5 library DocumentFragmentTest; | 5 library DocumentFragmentTest; | 
| 6 import '../../pkg/unittest/lib/unittest.dart'; | 6 import '../../pkg/unittest/lib/unittest.dart'; | 
| 7 import '../../pkg/unittest/lib/html_config.dart'; | 7 import '../../pkg/unittest/lib/html_config.dart'; | 
| 8 import 'dart:html'; | 8 import 'dart:html'; | 
| 9 part 'util.dart'; | 9 part 'util.dart'; | 
| 10 | 10 | 
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 53     // that'll intercept these calls without implementing the entire | 53     // that'll intercept these calls without implementing the entire | 
| 54     // CSSStyleDeclaration interface, so we'll settle for them being no-ops. | 54     // CSSStyleDeclaration interface, so we'll settle for them being no-ops. | 
| 55     style.cssText = '* {color: blue}'; | 55     style.cssText = '* {color: blue}'; | 
| 56     style.removeProperty('color'); | 56     style.removeProperty('color'); | 
| 57     style.setProperty('color', 'blue'); | 57     style.setProperty('color', 'blue'); | 
| 58   } | 58   } | 
| 59 | 59 | 
| 60   group('constructors', () { | 60   group('constructors', () { | 
| 61     test('0-argument makes an empty fragment', () { | 61     test('0-argument makes an empty fragment', () { | 
| 62       final fragment = new DocumentFragment(); | 62       final fragment = new DocumentFragment(); | 
| 63       expect(fragment.elements, equals([])); | 63       expect(fragment.children, equals([])); | 
| 64     }); | 64     }); | 
| 65 | 65 | 
| 66     test('.html parses input as HTML', () { | 66     test('.html parses input as HTML', () { | 
| 67       final fragment = new DocumentFragment.html('<a>foo</a>'); | 67       final fragment = new DocumentFragment.html('<a>foo</a>'); | 
| 68       expect(fragment.elements[0], isAnchorElement); | 68       expect(fragment.children[0], isAnchorElement); | 
| 69     }); | 69     }); | 
| 70 | 70 | 
| 71     // test('.svg parses input as SVG', () { | 71     // test('.svg parses input as SVG', () { | 
| 72     //   final fragment = new DocumentFragment.svg('<a>foo</a>'); | 72     //   final fragment = new DocumentFragment.svg('<a>foo</a>'); | 
| 73     //   expect(fragment.elements[0] is SVGAElement, isTrue); | 73     //   expect(fragment.children[0] is SVGAElement, isTrue); | 
| 74     // }); | 74     // }); | 
| 75 | 75 | 
| 76     // TODO(nweiz): enable this once XML is ported. | 76     // TODO(nweiz): enable this once XML is ported. | 
| 77     // test('.xml parses input as XML', () { | 77     // test('.xml parses input as XML', () { | 
| 78     //   final fragment = new DocumentFragment.xml('<a>foo</a>'); | 78     //   final fragment = new DocumentFragment.xml('<a>foo</a>'); | 
| 79     //   expect(fragment.elements[0] is XMLElement, isTrue); | 79     //   expect(fragment.children[0] is XMLElement, isTrue); | 
| 80     // }); | 80     // }); | 
| 81   }); | 81   }); | 
| 82 | 82 | 
| 83   test('Unsupported operations throw errors', () { | 83   test('Unsupported operations throw errors', () { | 
| 84     var emptyFragment = new DocumentFragment(); | 84     var emptyFragment = new DocumentFragment(); | 
| 85     expectUnsupported(() => emptyFragment.attributes = {}); | 85     expectUnsupported(() => emptyFragment.attributes = {}); | 
| 86     expectUnsupported(() => emptyFragment.classes = []); | 86     expectUnsupported(() => emptyFragment.classes = []); | 
| 87     expectUnsupported(() => emptyFragment.classes.add('foo')); | 87     expectUnsupported(() => emptyFragment.classes.add('foo')); | 
| 88     expectUnsupported(() => emptyFragment.dataAttributes = {}); | 88     expectUnsupported(() => emptyFragment.dataAttributes = {}); | 
| 89     expectUnsupported(() => emptyFragment.contentEditable = "true"); | 89     expectUnsupported(() => emptyFragment.contentEditable = "true"); | 
| 90     expectUnsupported(() => emptyFragment.dir); | 90     expectUnsupported(() => emptyFragment.dir); | 
| 91     expectUnsupported(() => emptyFragment.dir = "ltr"); | 91     expectUnsupported(() => emptyFragment.dir = "ltr"); | 
| 92     expectUnsupported(() => emptyFragment.draggable = true); | 92     expectUnsupported(() => emptyFragment.draggable = true); | 
| 93     expectUnsupported(() => emptyFragment.hidden = true); | 93     expectUnsupported(() => emptyFragment.hidden = true); | 
| 94     expectUnsupported(() => emptyFragment.id = "foo"); | 94     expectUnsupported(() => emptyFragment.id = "foo"); | 
| 95     expectUnsupported(() => emptyFragment.lang); | 95     expectUnsupported(() => emptyFragment.lang); | 
| 96     expectUnsupported(() => emptyFragment.lang = "en"); | 96     expectUnsupported(() => emptyFragment.lang = "en"); | 
| 97     expectUnsupported(() => emptyFragment.scrollLeft = 10); | 97     expectUnsupported(() => emptyFragment.scrollLeft = 10); | 
| 98     expectUnsupported(() => emptyFragment.scrollTop = 10); | 98     expectUnsupported(() => emptyFragment.scrollTop = 10); | 
| 99     expectUnsupported(() => emptyFragment.spellcheck = true); | 99     expectUnsupported(() => emptyFragment.spellcheck = true); | 
| 100     expectUnsupported(() => emptyFragment.translate = true); | 100     expectUnsupported(() => emptyFragment.translate = true); | 
| 101     expectUnsupported(() => emptyFragment.tabIndex = 5); | 101     expectUnsupported(() => emptyFragment.tabIndex = 5); | 
| 102     expectUnsupported(() => emptyFragment.title = "foo"); | 102     expectUnsupported(() => emptyFragment.title = "foo"); | 
| 103     expectUnsupported(() => emptyFragment.webkitdropzone = "foo"); | 103     expectUnsupported(() => emptyFragment.webkitdropzone = "foo"); | 
| 104     expectUnsupported(() => emptyFragment.webkitRegionOverflow = "foo"); | 104     expectUnsupported(() => emptyFragment.webkitRegionOverflow = "foo"); | 
| 105   }); | 105   }); | 
| 106 | 106 | 
| 107   group('elements', () { | 107   group('children', () { | 
| 108     var fragment; | 108     var fragment; | 
| 109     var elements; | 109     var children; | 
| 110 | 110 | 
| 111     init() { | 111     init() { | 
| 112       fragment = new DocumentFragment(); | 112       fragment = new DocumentFragment(); | 
| 113       elements = fragment.elements; | 113       children = fragment.children; | 
| 114       fragment.nodes.addAll( | 114       fragment.nodes.addAll( | 
| 115         [new Text("1"), new Element.tag("A"), new Element.tag("B"), | 115         [new Text("1"), new Element.tag("A"), new Element.tag("B"), | 
| 116          new Text("2"), new Element.tag("I"), new Text("3"), | 116          new Text("2"), new Element.tag("I"), new Text("3"), | 
| 117          new Element.tag("U")]); | 117          new Element.tag("U")]); | 
| 118     }; | 118     }; | 
| 119 | 119 | 
| 120     test('is initially empty', () { | 120     test('is initially empty', () { | 
| 121       elements = new DocumentFragment().elements; | 121       children = new DocumentFragment().children; | 
| 122       expect(elements, equals([])); | 122       expect(children, equals([])); | 
| 123       expect(elements.isEmpty, isTrue); | 123       expect(children.isEmpty, isTrue); | 
| 124     }); | 124     }); | 
| 125 | 125 | 
| 126     test('filters out non-element nodes', () { | 126     test('filters out non-element nodes', () { | 
| 127       init(); | 127       init(); | 
| 128       expect(_nodeStrings(fragment.nodes), | 128       expect(_nodeStrings(fragment.nodes), | 
| 129           orderedEquals(["1", "A", "B", "2", "I", "3", "U"])); | 129           orderedEquals(["1", "A", "B", "2", "I", "3", "U"])); | 
| 130       expect(_nodeStrings(elements), | 130       expect(_nodeStrings(children), | 
| 131           orderedEquals(["A", "B", "I", "U"])); | 131           orderedEquals(["A", "B", "I", "U"])); | 
| 132     }); | 132     }); | 
| 133 | 133 | 
| 134     test('only indexes elements, not other nodes', () { | 134     test('only indexes children, not other nodes', () { | 
| 135       init(); | 135       init(); | 
| 136       elements[1] = new Element.tag("BR"); | 136       children[1] = new Element.tag("BR"); | 
| 137       expect(_nodeStrings(fragment.nodes), | 137       expect(_nodeStrings(fragment.nodes), | 
| 138           orderedEquals(["1", "A", "BR", "2", "I", "3", "U"])); | 138           orderedEquals(["1", "A", "BR", "2", "I", "3", "U"])); | 
| 139       expect(_nodeStrings(elements), | 139       expect(_nodeStrings(children), | 
| 140           orderedEquals(["A", "BR", "I", "U"])); | 140           orderedEquals(["A", "BR", "I", "U"])); | 
| 141     }); | 141     }); | 
| 142 | 142 | 
| 143     test('adds to both elements and nodes', () { | 143     test('adds to both children and nodes', () { | 
| 144       init(); | 144       init(); | 
| 145       elements.add(new Element.tag("UL")); | 145       children.add(new Element.tag("UL")); | 
| 146       expect(_nodeStrings(fragment.nodes), | 146       expect(_nodeStrings(fragment.nodes), | 
| 147           orderedEquals(["1", "A", "B", "2", "I", "3", "U", "UL"])); | 147           orderedEquals(["1", "A", "B", "2", "I", "3", "U", "UL"])); | 
| 148       expect(_nodeStrings(elements), | 148       expect(_nodeStrings(children), | 
| 149           orderedEquals(["A", "B", "I", "U", "UL"])); | 149           orderedEquals(["A", "B", "I", "U", "UL"])); | 
| 150     }); | 150     }); | 
| 151 | 151 | 
| 152     test('removes only elements, from both elements and nodes', () { | 152     test('removes only children, from both children and nodes', () { | 
| 153       init(); | 153       init(); | 
| 154       expect(elements.removeLast().tagName, equals('U')); | 154       expect(children.removeLast().tagName, equals('U')); | 
| 155       expect(_nodeStrings(fragment.nodes), | 155       expect(_nodeStrings(fragment.nodes), | 
| 156           orderedEquals(["1", "A", "B", "2", "I", "3"])); | 156           orderedEquals(["1", "A", "B", "2", "I", "3"])); | 
| 157       expect(_nodeStrings(elements), | 157       expect(_nodeStrings(children), | 
| 158           orderedEquals(["A", "B", "I"])); | 158           orderedEquals(["A", "B", "I"])); | 
| 159 | 159 | 
| 160       expect(elements.removeLast().tagName, "I"); | 160       expect(children.removeLast().tagName, "I"); | 
| 161       expect(_nodeStrings(fragment.nodes), | 161       expect(_nodeStrings(fragment.nodes), | 
| 162           equals(["1", "A", "B", "2", "3"])); | 162           equals(["1", "A", "B", "2", "3"])); | 
| 163       expect(_nodeStrings(elements), equals(["A", "B"])); | 163       expect(_nodeStrings(children), equals(["A", "B"])); | 
| 164     }); | 164     }); | 
| 165 | 165 | 
| 166     test('accessors are wrapped', () { | 166     test('accessors are wrapped', () { | 
| 167       init(); | 167       init(); | 
| 168       expect(elements[0].tagName, "A"); | 168       expect(children[0].tagName, "A"); | 
| 169       expect(_nodeStrings(elements.filter((e) => e.tagName == "I")), ["I"]); | 169       expect(_nodeStrings(children.filter((e) => e.tagName == "I")), ["I"]); | 
| 170       expect(elements.every((e) => e is Element), isTrue); | 170       expect(children.every((e) => e is Element), isTrue); | 
| 171       expect(elements.some((e) => e.tagName == "U"), isTrue); | 171       expect(children.some((e) => e.tagName == "U"), isTrue); | 
| 172       expect(elements.isEmpty, isFalse); | 172       expect(children.isEmpty, isFalse); | 
| 173       expect(elements.length, 4); | 173       expect(children.length, 4); | 
| 174       expect(elements[2].tagName, "I"); | 174       expect(children[2].tagName, "I"); | 
| 175       expect(elements.last.tagName, "U"); | 175       expect(children.last.tagName, "U"); | 
| 176     }); | 176     }); | 
| 177 | 177 | 
| 178     test('setting elements overwrites nodes as well', () { | 178     test('setting children overwrites nodes as well', () { | 
| 179       init(); | 179       init(); | 
| 180       fragment.elements = [new Element.tag("DIV"), new Element.tag("HEAD")]; | 180       fragment.children = [new Element.tag("DIV"), new Element.tag("HEAD")]; | 
| 181       expect(_nodeStrings(fragment.nodes), equals(["DIV", "HEAD"])); | 181       expect(_nodeStrings(fragment.nodes), equals(["DIV", "HEAD"])); | 
| 182     }); | 182     }); | 
| 183   }); | 183   }); | 
| 184 | 184 | 
| 185   test('setting innerHTML works', () { | 185   test('setting innerHTML works', () { | 
| 186     var fragment = new DocumentFragment(); | 186     var fragment = new DocumentFragment(); | 
| 187     fragment.nodes.add(new Text("foo")); | 187     fragment.nodes.add(new Text("foo")); | 
| 188     fragment.innerHTML = "<a>bar</a>baz"; | 188     fragment.innerHTML = "<a>bar</a>baz"; | 
| 189     expect(_nodeStrings(fragment.nodes), equals(["A", "baz"])); | 189     expect(_nodeStrings(fragment.nodes), equals(["A", "baz"])); | 
| 190   }); | 190   }); | 
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 344   //   // assertConstError(() => fragment.classes.add('foo')); | 344   //   // assertConstError(() => fragment.classes.add('foo')); | 
| 345   // }); | 345   // }); | 
| 346 | 346 | 
| 347   test('query searches the fragment', () { | 347   test('query searches the fragment', () { | 
| 348     var fragment = new DocumentFragment.html( | 348     var fragment = new DocumentFragment.html( | 
| 349       "<div class='foo'><a>foo</a><b>bar</b></div>"); | 349       "<div class='foo'><a>foo</a><b>bar</b></div>"); | 
| 350     expect(fragment.query(".foo a").tagName, "A"); | 350     expect(fragment.query(".foo a").tagName, "A"); | 
| 351     expect(_nodeStrings(fragment.queryAll(".foo *")), equals(["A", "B"])); | 351     expect(_nodeStrings(fragment.queryAll(".foo *")), equals(["A", "B"])); | 
| 352   }); | 352   }); | 
| 353 } | 353 } | 
| OLD | NEW | 
|---|