Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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 /** | |
| 6 * Lazy implementation of the child nodes of an element that does not request | |
| 7 * the actual child nodes of an element until strictly necessary greatly | |
| 8 * improving performance for the typical cases where it is not required. | |
| 9 */ | |
| 10 class _ChildNodeListLazy implements NodeList { | |
| 11 final _NodeImpl _this; | |
| 12 | |
| 13 _ChildNodeListLazy(this._this); | |
| 14 | |
| 15 | |
| 16 $if FROG | |
| 17 _NodeImpl get first() native "return this._this.firstChild;"; | |
| 18 _NodeImpl last() native "return this._this.lastChild;"; | |
| 19 $else | |
| 20 _NodeImpl get first() => _this.$dom_firstChild; | |
| 21 _NodeImpl last() => _this.$dom_lastChild; | |
| 22 $endif | |
| 23 | |
| 24 void add(_NodeImpl value) { | |
| 25 _this.$dom_appendChild(value); | |
| 26 } | |
| 27 | |
| 28 void addLast(_NodeImpl value) { | |
| 29 _this.$dom_appendChild(value); | |
| 30 } | |
| 31 | |
| 32 | |
| 33 void addAll(Collection<_NodeImpl> collection) { | |
| 34 for (_NodeImpl node in collection) { | |
| 35 _this.$dom_appendChild(node); | |
| 36 } | |
| 37 } | |
| 38 | |
| 39 _NodeImpl removeLast() { | |
| 40 final last = last(); | |
| 41 if (last != null) { | |
| 42 _this.$dom_removeChild(last); | |
| 43 } | |
| 44 return last; | |
| 45 } | |
| 46 | |
| 47 void clear() { | |
| 48 _this.text = ''; | |
| 49 } | |
| 50 | |
| 51 void operator []=(int index, _NodeImpl value) { | |
| 52 _this.$dom_replaceChild(value, this[index]); | |
| 53 } | |
| 54 | |
| 55 Iterator<Node> iterator() => _this.$dom_childNodes.iterator(); | |
| 56 | |
| 57 // TODO(jacobr): We can implement these methods much more efficiently by | |
| 58 // looking up the nodeList only once instead of once per iteration. | |
| 59 void forEach(void f(Node element)) => _Collections.forEach(this, f); | |
| 60 | |
| 61 Collection map(f(Node element)) => _Collections.map(this, [], f); | |
| 62 | |
| 63 Collection<Node> filter(bool f(Node element)) => | |
| 64 new _NodeListWrapper(_Collections.filter(this, <Node>[], f)); | |
| 65 | |
| 66 bool every(bool f(Node element)) => _Collections.every(this, f); | |
| 67 | |
| 68 bool some(bool f(Node element)) => _Collections.some(this, f); | |
| 69 | |
| 70 bool isEmpty() => this.length == 0; | |
| 71 | |
| 72 // From List<Node>: | |
| 73 | |
| 74 // TODO(jacobr): this could be implemented for child node lists. | |
| 75 // The exception we throw here is misleading. | |
| 76 void sort(int compare(Node a, Node b)) { | |
| 77 throw new UnsupportedOperationException("Cannot sort immutable List."); | |
| 78 } | |
| 79 | |
| 80 int indexOf(Node element, [int start = 0]) => | |
| 81 _Lists.indexOf(this, element, start, this.length); | |
| 82 | |
| 83 int lastIndexOf(Node element, [int start = 0]) => | |
| 84 _Lists.lastIndexOf(this, element, start); | |
| 85 | |
| 86 // FIXME: implement thesee. | |
| 87 void setRange(int start, int length, List<Node> from, [int startFrom]) { | |
| 88 throw new UnsupportedOperationException("Cannot setRange on immutable List." ); | |
|
nweiz
2012/03/28 01:16:17
Line lengths.
Jacob
2012/03/28 17:52:54
Done.
| |
| 89 } | |
| 90 void removeRange(int start, int length) { | |
| 91 throw new UnsupportedOperationException("Cannot removeRange on immutable Lis t."); | |
| 92 } | |
| 93 void insertRange(int start, int length, [Node initialValue]) { | |
| 94 throw new UnsupportedOperationException("Cannot insertRange on immutable Lis t."); | |
| 95 } | |
| 96 NodeList getRange(int start, int length) => | |
| 97 new _NodeListWrapper(_Lists.getRange(this, start, length, <Node>[])); | |
| 98 | |
| 99 // -- end List<Node> mixins. | |
| 100 | |
| 101 // TODO(jacobr): benchmark whether this is more efficient or whether caching | |
| 102 // a local copy of $dom_childNodes is more efficient. | |
| 103 int get length() => _this.$dom_childNodes.length; | |
| 104 | |
| 105 _NodeImpl operator[](int index) => _this.$dom_childNodes[index]; | |
| 106 } | |
| 107 | |
| 5 class $CLASSNAME$EXTENDS$IMPLEMENTS$NATIVESPEC { | 108 class $CLASSNAME$EXTENDS$IMPLEMENTS$NATIVESPEC { |
| 6 _NodeListImpl get nodes() { | 109 _ChildNodeListLazy get nodes() { |
| 7 final list = _childNodes; | 110 return new _ChildNodeListLazy(this); |
| 8 list._parent = this; | |
| 9 return list; | |
| 10 } | 111 } |
| 11 | 112 |
| 12 void set nodes(Collection<Node> value) { | 113 void set nodes(Collection<Node> value) { |
| 13 // Copy list first since we don't want liveness during iteration. | 114 // Copy list first since we don't want liveness during iteration. |
| 14 // TODO(jacobr): there is a better way to do this. | 115 // TODO(jacobr): there is a better way to do this. |
| 15 List copy = new List.from(value); | 116 List copy = new List.from(value); |
| 16 text = ''; | 117 text = ''; |
| 17 for (Node node in copy) { | 118 for (Node node in copy) { |
| 18 _appendChild(node); | 119 $dom_appendChild(node); |
| 19 } | 120 } |
| 20 } | 121 } |
| 21 | 122 |
| 22 // TODO(jacobr): should we throw an exception if parent is already null? | 123 // TODO(jacobr): should we throw an exception if parent is already null? |
| 23 _NodeImpl remove() { | 124 _NodeImpl remove() { |
| 24 if (this.parent != null) { | 125 if (this.parent != null) { |
| 25 final _NodeImpl parent = this.parent; | 126 final _NodeImpl parent = this.parent; |
| 26 parent._removeChild(this); | 127 parent.$dom_removeChild(this); |
| 27 } | 128 } |
| 28 return this; | 129 return this; |
| 29 } | 130 } |
| 30 | 131 |
| 31 _NodeImpl replaceWith(Node otherNode) { | 132 _NodeImpl replaceWith(Node otherNode) { |
| 32 try { | 133 try { |
| 33 final _NodeImpl parent = this.parent; | 134 final _NodeImpl parent = this.parent; |
| 34 parent._replaceChild(otherNode, this); | 135 parent.$dom_replaceChild(otherNode, this); |
| 35 } catch(var e) { | 136 } catch(var e) { |
| 36 | 137 |
| 37 }; | 138 }; |
| 38 return this; | 139 return this; |
| 39 } | 140 } |
| 40 | 141 |
| 41 $!MEMBERS | 142 $!MEMBERS |
| 42 } | 143 } |
| OLD | NEW |