OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013, 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 template_binding.test.utils; |
| 6 |
| 7 import 'dart:async'; |
| 8 import 'dart:html'; |
| 9 import 'package:observe/observe.dart'; |
| 10 |
| 11 // Note: tests that import 'utils.dart' rely on the following line to make test |
| 12 // smaller for dart2js and prevent timeouts in the test bots. |
| 13 import 'package:observe/mirrors_used.dart'; |
| 14 import 'package:template_binding/template_binding.dart'; |
| 15 export 'package:observe/src/dirty_check.dart' show dirtyCheckZone; |
| 16 |
| 17 /// A small method to help readability. Used to cause the next "then" in a chain |
| 18 /// to happen in the next microtask: |
| 19 /// |
| 20 /// future.then(endOfMicrotask).then(...) |
| 21 endOfMicrotask(_) => new Future.value(); |
| 22 |
| 23 /// A small method to help readability. Used to cause the next "then" in a chain |
| 24 /// to happen in the next microtask, after a timer: |
| 25 /// |
| 26 /// future.then(nextMicrotask).then(...) |
| 27 nextMicrotask(_) => new Future(() {}); |
| 28 |
| 29 final bool parserHasNativeTemplate = () { |
| 30 var div = new DivElement()..innerHtml = '<table><template>'; |
| 31 return div.firstChild.firstChild != null && |
| 32 div.firstChild.firstChild.tagName == 'TEMPLATE'; |
| 33 }(); |
| 34 |
| 35 recursivelySetTemplateModel(element, model, [delegate]) { |
| 36 for (var node in element.queryAll('*')) { |
| 37 if (isSemanticTemplate(node)) { |
| 38 templateBind(node) |
| 39 ..bindingDelegate = delegate |
| 40 ..model = model; |
| 41 } |
| 42 } |
| 43 } |
| 44 |
| 45 dispatchEvent(type, target) { |
| 46 target.dispatchEvent(new Event(type, cancelable: false)); |
| 47 } |
| 48 |
| 49 class FooBarModel extends Observable { |
| 50 @observable var foo; |
| 51 @observable var bar; |
| 52 |
| 53 FooBarModel([this.foo, this.bar]); |
| 54 } |
| 55 |
| 56 @reflectable |
| 57 class FooBarNotifyModel extends ChangeNotifier implements FooBarModel { |
| 58 var _foo; |
| 59 var _bar; |
| 60 |
| 61 FooBarNotifyModel([this._foo, this._bar]); |
| 62 |
| 63 get foo => _foo; |
| 64 set foo(value) { |
| 65 _foo = notifyPropertyChange(#foo, _foo, value); |
| 66 } |
| 67 |
| 68 get bar => _bar; |
| 69 set bar(value) { |
| 70 _bar = notifyPropertyChange(#bar, _bar, value); |
| 71 } |
| 72 } |
| 73 |
| 74 DivElement testDiv; |
| 75 |
| 76 createTestHtml(s) { |
| 77 var div = new DivElement(); |
| 78 div.setInnerHtml(s, treeSanitizer: new NullTreeSanitizer()); |
| 79 testDiv.append(div); |
| 80 |
| 81 for (var node in div.querySelectorAll('*')) { |
| 82 if (isSemanticTemplate(node)) TemplateBindExtension.decorate(node); |
| 83 } |
| 84 |
| 85 return div; |
| 86 } |
| 87 |
| 88 createShadowTestHtml(s) { |
| 89 var div = new DivElement(); |
| 90 var root = div.createShadowRoot(); |
| 91 root.setInnerHtml(s, treeSanitizer: new NullTreeSanitizer()); |
| 92 testDiv.append(div); |
| 93 |
| 94 for (var node in root.querySelectorAll('*')) { |
| 95 if (isSemanticTemplate(node)) TemplateBindExtension.decorate(node); |
| 96 } |
| 97 |
| 98 return root; |
| 99 } |
| 100 |
| 101 /** |
| 102 * Sanitizer which does nothing. |
| 103 */ |
| 104 class NullTreeSanitizer implements NodeTreeSanitizer { |
| 105 void sanitizeTree(Node node) {} |
| 106 } |
| 107 |
| 108 clearAllTemplates(node) { |
| 109 if (isSemanticTemplate(node)) { |
| 110 templateBind(node).clear(); |
| 111 } |
| 112 for (var child = node.firstChild; child != null; child = child.nextNode) { |
| 113 clearAllTemplates(child); |
| 114 } |
| 115 } |
OLD | NEW |