OLD | NEW |
(Empty) | |
| 1 import 'dart:async'; |
| 2 |
| 3 void assertHasParentNode(Node n) { assert(n.parentNode != null); } |
| 4 void assertHasParentNodes(List<Node> list) { |
| 5 for (var n in list) { |
| 6 assertHasParentNode(n); |
| 7 } |
| 8 } |
| 9 |
| 10 class Node { |
| 11 |
| 12 ParentNode parentNode; |
| 13 Node nextSibling; |
| 14 Node previousSibling; |
| 15 Node(); |
| 16 |
| 17 void insertBefore(List<Node> nodes) { |
| 18 int count = nodes.length; |
| 19 while (count-- > 0) { |
| 20 parentNode._insertBefore(nodes[count], this); |
| 21 } |
| 22 |
| 23 assertHasParentNodes(nodes); |
| 24 } |
| 25 |
| 26 remove() { |
| 27 if (parentNode == null) { |
| 28 return; |
| 29 } |
| 30 |
| 31 if (nextSibling != null) { |
| 32 nextSibling.previousSibling = previousSibling; |
| 33 } else { |
| 34 parentNode.lastChild = previousSibling; |
| 35 } |
| 36 |
| 37 if (previousSibling != null) { |
| 38 previousSibling.nextSibling = nextSibling; |
| 39 } else { |
| 40 parentNode.firstChild = nextSibling; |
| 41 } |
| 42 |
| 43 parentNode = null; |
| 44 nextSibling = null; |
| 45 previousSibling = null; |
| 46 } |
| 47 } |
| 48 |
| 49 class Text extends Node { |
| 50 String data; |
| 51 Text(this.data) : super(); |
| 52 } |
| 53 |
| 54 class ParentNode extends Node { |
| 55 Node firstChild; |
| 56 Node lastChild; |
| 57 |
| 58 ParentNode() : super(); |
| 59 |
| 60 Node setChild(Node node) { |
| 61 firstChild = node; |
| 62 lastChild = node; |
| 63 node.parentNode = this; |
| 64 assertHasParentNode(node); |
| 65 return node; |
| 66 } |
| 67 |
| 68 Node _insertBefore(Node node, Node ref) { |
| 69 assert(ref == null || ref.parentNode == this); |
| 70 |
| 71 if (node.parentNode != null) { |
| 72 node.remove(); |
| 73 } |
| 74 |
| 75 node.parentNode = this; |
| 76 |
| 77 if (firstChild == null && lastChild == null) { |
| 78 firstChild = node; |
| 79 lastChild = node; |
| 80 } else if (ref == null) { |
| 81 node.previousSibling = lastChild; |
| 82 lastChild.nextSibling = node; |
| 83 lastChild = node; |
| 84 } else { |
| 85 if (ref == firstChild) { |
| 86 assert(ref.previousSibling == null); |
| 87 firstChild = node; |
| 88 } |
| 89 node.previousSibling = ref.previousSibling; |
| 90 ref.previousSibling = node; |
| 91 node.nextSibling = ref; |
| 92 } |
| 93 |
| 94 assertHasParentNode(node); |
| 95 return node; |
| 96 } |
| 97 |
| 98 Node appendChild(Node node) { |
| 99 return _insertBefore(node, null); |
| 100 } |
| 101 } |
| 102 |
| 103 class Element extends ParentNode { |
| 104 void addEventListener(String type, EventListener listener, [bool useCapture =
false]) {} |
| 105 void removeEventListener(String type, EventListener listener) {} |
| 106 void setAttribute(String name, [String value]) {} |
| 107 } |
| 108 |
| 109 class Document extends ParentNode { |
| 110 Document(); |
| 111 Element createElement(String tagName) { |
| 112 switch (tagName) { |
| 113 case 'img' : return new HTMLImageElement(); |
| 114 default : return new Element(); |
| 115 } |
| 116 } |
| 117 } |
| 118 |
| 119 class HTMLImageElement extends Element { |
| 120 Image(); |
| 121 String src; |
| 122 Object style = {}; |
| 123 } |
| 124 |
| 125 class Event { |
| 126 Event(); |
| 127 } |
| 128 |
| 129 typedef EventListener(Event event); |
| 130 |
| 131 void _callRAF(Function fn) { |
| 132 fn(new DateTime.now().millisecondsSinceEpoch.toDouble()); |
| 133 } |
| 134 |
| 135 class Window { |
| 136 int requestAnimationFrame(Function fn) { |
| 137 new Timer(const Duration(milliseconds: 16), () { |
| 138 _callRAF(fn); |
| 139 }); |
| 140 } |
| 141 |
| 142 void cancelAnimationFrame(int id) { |
| 143 } |
| 144 } |
| 145 |
| 146 Document document = new Document(); |
| 147 |
| 148 Window window = new Window(); |
OLD | NEW |