| 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 part of html; | 5 part of html; |
| 6 | 6 |
| 7 abstract class _AttributeMap implements Map<String, String> { | 7 abstract class _AttributeMap implements Map<String, String> { |
| 8 final Element _element; | 8 final Element _element; |
| 9 | 9 |
| 10 _AttributeMap(this._element); | 10 _AttributeMap(this._element); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 | 33 |
| 34 void forEach(void f(String key, String value)) { | 34 void forEach(void f(String key, String value)) { |
| 35 for (var key in keys) { | 35 for (var key in keys) { |
| 36 var value = this[key]; | 36 var value = this[key]; |
| 37 f(key, value); | 37 f(key, value); |
| 38 } | 38 } |
| 39 } | 39 } |
| 40 | 40 |
| 41 Iterable<String> get keys { | 41 Iterable<String> get keys { |
| 42 // TODO: generate a lazy collection instead. | 42 // TODO: generate a lazy collection instead. |
| 43 var attributes = _element.$dom_attributes; | 43 var attributes = _element._attributes; |
| 44 var keys = new List<String>(); | 44 var keys = new List<String>(); |
| 45 for (int i = 0, len = attributes.length; i < len; i++) { | 45 for (int i = 0, len = attributes.length; i < len; i++) { |
| 46 if (_matches(attributes[i])) { | 46 if (_matches(attributes[i])) { |
| 47 keys.add(attributes[i].name); | 47 keys.add(attributes[i].name); |
| 48 } | 48 } |
| 49 } | 49 } |
| 50 return keys; | 50 return keys; |
| 51 } | 51 } |
| 52 | 52 |
| 53 Iterable<String> get values { | 53 Iterable<String> get values { |
| 54 // TODO: generate a lazy collection instead. | 54 // TODO: generate a lazy collection instead. |
| 55 var attributes = _element.$dom_attributes; | 55 var attributes = _element._attributes; |
| 56 var values = new List<String>(); | 56 var values = new List<String>(); |
| 57 for (int i = 0, len = attributes.length; i < len; i++) { | 57 for (int i = 0, len = attributes.length; i < len; i++) { |
| 58 if (_matches(attributes[i])) { | 58 if (_matches(attributes[i])) { |
| 59 values.add(attributes[i].value); | 59 values.add(attributes[i].value); |
| 60 } | 60 } |
| 61 } | 61 } |
| 62 return values; | 62 return values; |
| 63 } | 63 } |
| 64 | 64 |
| 65 /** | 65 /** |
| (...skipping 15 matching lines...) Expand all Loading... |
| 81 } | 81 } |
| 82 | 82 |
| 83 /** | 83 /** |
| 84 * Wrapper to expose [Element.attributes] as a typed map. | 84 * Wrapper to expose [Element.attributes] as a typed map. |
| 85 */ | 85 */ |
| 86 class _ElementAttributeMap extends _AttributeMap { | 86 class _ElementAttributeMap extends _AttributeMap { |
| 87 | 87 |
| 88 _ElementAttributeMap(Element element): super(element); | 88 _ElementAttributeMap(Element element): super(element); |
| 89 | 89 |
| 90 bool containsKey(String key) { | 90 bool containsKey(String key) { |
| 91 return _element.$dom_hasAttribute(key); | 91 return _element._hasAttribute(key); |
| 92 } | 92 } |
| 93 | 93 |
| 94 String operator [](String key) { | 94 String operator [](String key) { |
| 95 return _element.$dom_getAttribute(key); | 95 return _element.$dom_getAttribute(key); |
| 96 } | 96 } |
| 97 | 97 |
| 98 void operator []=(String key, String value) { | 98 void operator []=(String key, String value) { |
| 99 _element.$dom_setAttribute(key, value); | 99 _element.$dom_setAttribute(key, value); |
| 100 } | 100 } |
| 101 | 101 |
| 102 String remove(String key) { | 102 String remove(String key) { |
| 103 String value = _element.$dom_getAttribute(key); | 103 String value = _element.$dom_getAttribute(key); |
| 104 _element.$dom_removeAttribute(key); | 104 _element._removeAttribute(key); |
| 105 return value; | 105 return value; |
| 106 } | 106 } |
| 107 | 107 |
| 108 /** | 108 /** |
| 109 * The number of {key, value} pairs in the map. | 109 * The number of {key, value} pairs in the map. |
| 110 */ | 110 */ |
| 111 int get length { | 111 int get length { |
| 112 return keys.length; | 112 return keys.length; |
| 113 } | 113 } |
| 114 | 114 |
| 115 bool _matches(Node node) => node.$dom_namespaceUri == null; | 115 bool _matches(Node node) => node._namespaceUri == null; |
| 116 } | 116 } |
| 117 | 117 |
| 118 /** | 118 /** |
| 119 * Wrapper to expose namespaced attributes as a typed map. | 119 * Wrapper to expose namespaced attributes as a typed map. |
| 120 */ | 120 */ |
| 121 class _NamespacedAttributeMap extends _AttributeMap { | 121 class _NamespacedAttributeMap extends _AttributeMap { |
| 122 | 122 |
| 123 final String _namespace; | 123 final String _namespace; |
| 124 | 124 |
| 125 _NamespacedAttributeMap(Element element, this._namespace): super(element); | 125 _NamespacedAttributeMap(Element element, this._namespace): super(element); |
| 126 | 126 |
| 127 bool containsKey(String key) { | 127 bool containsKey(String key) { |
| 128 return _element.$dom_hasAttributeNS(_namespace, key); | 128 return _element._hasAttributeNS(_namespace, key); |
| 129 } | 129 } |
| 130 | 130 |
| 131 String operator [](String key) { | 131 String operator [](String key) { |
| 132 return _element.$dom_getAttributeNS(_namespace, key); | 132 return _element.$dom_getAttributeNS(_namespace, key); |
| 133 } | 133 } |
| 134 | 134 |
| 135 void operator []=(String key, String value) { | 135 void operator []=(String key, String value) { |
| 136 _element.$dom_setAttributeNS(_namespace, key, value); | 136 _element.$dom_setAttributeNS(_namespace, key, value); |
| 137 } | 137 } |
| 138 | 138 |
| 139 String remove(String key) { | 139 String remove(String key) { |
| 140 String value = this[key]; | 140 String value = this[key]; |
| 141 _element.$dom_removeAttributeNS(_namespace, key); | 141 _element._removeAttributeNS(_namespace, key); |
| 142 return value; | 142 return value; |
| 143 } | 143 } |
| 144 | 144 |
| 145 /** | 145 /** |
| 146 * The number of {key, value} pairs in the map. | 146 * The number of {key, value} pairs in the map. |
| 147 */ | 147 */ |
| 148 int get length { | 148 int get length { |
| 149 return keys.length; | 149 return keys.length; |
| 150 } | 150 } |
| 151 | 151 |
| 152 bool _matches(Node node) => node.$dom_namespaceUri == _namespace; | 152 bool _matches(Node node) => node._namespaceUri == _namespace; |
| 153 } | 153 } |
| 154 | 154 |
| 155 | 155 |
| 156 /** | 156 /** |
| 157 * Provides a Map abstraction on top of data-* attributes, similar to the | 157 * Provides a Map abstraction on top of data-* attributes, similar to the |
| 158 * dataSet in the old DOM. | 158 * dataSet in the old DOM. |
| 159 */ | 159 */ |
| 160 class _DataAttributeMap implements Map<String, String> { | 160 class _DataAttributeMap implements Map<String, String> { |
| 161 | 161 |
| 162 final Map<String, String> $dom_attributes; | 162 final Map<String, String> _attributes; |
| 163 | 163 |
| 164 _DataAttributeMap(this.$dom_attributes); | 164 _DataAttributeMap(this._attributes); |
| 165 | 165 |
| 166 // interface Map | 166 // interface Map |
| 167 | 167 |
| 168 // TODO: Use lazy iterator when it is available on Map. | 168 // TODO: Use lazy iterator when it is available on Map. |
| 169 bool containsValue(String value) => values.any((v) => v == value); | 169 bool containsValue(String value) => values.any((v) => v == value); |
| 170 | 170 |
| 171 bool containsKey(String key) => $dom_attributes.containsKey(_attr(key)); | 171 bool containsKey(String key) => _attributes.containsKey(_attr(key)); |
| 172 | 172 |
| 173 String operator [](String key) => $dom_attributes[_attr(key)]; | 173 String operator [](String key) => _attributes[_attr(key)]; |
| 174 | 174 |
| 175 void operator []=(String key, String value) { | 175 void operator []=(String key, String value) { |
| 176 $dom_attributes[_attr(key)] = value; | 176 _attributes[_attr(key)] = value; |
| 177 } | 177 } |
| 178 | 178 |
| 179 String putIfAbsent(String key, String ifAbsent()) => | 179 String putIfAbsent(String key, String ifAbsent()) => |
| 180 $dom_attributes.putIfAbsent(_attr(key), ifAbsent); | 180 _attributes.putIfAbsent(_attr(key), ifAbsent); |
| 181 | 181 |
| 182 String remove(String key) => $dom_attributes.remove(_attr(key)); | 182 String remove(String key) => _attributes.remove(_attr(key)); |
| 183 | 183 |
| 184 void clear() { | 184 void clear() { |
| 185 // Needs to operate on a snapshot since we are mutating the collection. | 185 // Needs to operate on a snapshot since we are mutating the collection. |
| 186 for (String key in keys) { | 186 for (String key in keys) { |
| 187 remove(key); | 187 remove(key); |
| 188 } | 188 } |
| 189 } | 189 } |
| 190 | 190 |
| 191 void forEach(void f(String key, String value)) { | 191 void forEach(void f(String key, String value)) { |
| 192 $dom_attributes.forEach((String key, String value) { | 192 _attributes.forEach((String key, String value) { |
| 193 if (_matches(key)) { | 193 if (_matches(key)) { |
| 194 f(_strip(key), value); | 194 f(_strip(key), value); |
| 195 } | 195 } |
| 196 }); | 196 }); |
| 197 } | 197 } |
| 198 | 198 |
| 199 Iterable<String> get keys { | 199 Iterable<String> get keys { |
| 200 final keys = new List<String>(); | 200 final keys = new List<String>(); |
| 201 $dom_attributes.forEach((String key, String value) { | 201 _attributes.forEach((String key, String value) { |
| 202 if (_matches(key)) { | 202 if (_matches(key)) { |
| 203 keys.add(_strip(key)); | 203 keys.add(_strip(key)); |
| 204 } | 204 } |
| 205 }); | 205 }); |
| 206 return keys; | 206 return keys; |
| 207 } | 207 } |
| 208 | 208 |
| 209 Iterable<String> get values { | 209 Iterable<String> get values { |
| 210 final values = new List<String>(); | 210 final values = new List<String>(); |
| 211 $dom_attributes.forEach((String key, String value) { | 211 _attributes.forEach((String key, String value) { |
| 212 if (_matches(key)) { | 212 if (_matches(key)) { |
| 213 values.add(value); | 213 values.add(value); |
| 214 } | 214 } |
| 215 }); | 215 }); |
| 216 return values; | 216 return values; |
| 217 } | 217 } |
| 218 | 218 |
| 219 int get length => keys.length; | 219 int get length => keys.length; |
| 220 | 220 |
| 221 // TODO: Use lazy iterator when it is available on Map. | 221 // TODO: Use lazy iterator when it is available on Map. |
| 222 bool get isEmpty => length == 0; | 222 bool get isEmpty => length == 0; |
| 223 | 223 |
| 224 bool get isNotEmpty => !isEmpty; | 224 bool get isNotEmpty => !isEmpty; |
| 225 | 225 |
| 226 // Helpers. | 226 // Helpers. |
| 227 String _attr(String key) => 'data-$key'; | 227 String _attr(String key) => 'data-$key'; |
| 228 bool _matches(String key) => key.startsWith('data-'); | 228 bool _matches(String key) => key.startsWith('data-'); |
| 229 String _strip(String key) => key.substring(5); | 229 String _strip(String key) => key.substring(5); |
| 230 } | 230 } |
| OLD | NEW |