Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(514)

Unified Diff: sdk/lib/html/dart2js/html_dart2js.dart

Issue 11348111: Adding support for accessing attributes in alternate namespaces. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Incorporating review feedback. Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | sdk/lib/html/dartium/html_dartium.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/html/dart2js/html_dart2js.dart
diff --git a/sdk/lib/html/dart2js/html_dart2js.dart b/sdk/lib/html/dart2js/html_dart2js.dart
index f9891b3506d0585b0df86691cc3b894ecab0175a..674d2a8380bcd2ab4f7c573f453b22be659b1522 100644
--- a/sdk/lib/html/dart2js/html_dart2js.dart
+++ b/sdk/lib/html/dart2js/html_dart2js.dart
@@ -7401,171 +7401,6 @@ class _FrozenElementListIterator implements Iterator<Element> {
bool get hasNext => _index < _list.length;
}
-class _ElementAttributeMap implements Map<String, String> {
-
- final Element _element;
-
- _ElementAttributeMap(this._element);
-
- bool containsValue(String value) {
- final attributes = _element.$dom_attributes;
- for (int i = 0, len = attributes.length; i < len; i++) {
- if(value == attributes[i].value) {
- return true;
- }
- }
- return false;
- }
-
- bool containsKey(String key) {
- return _element.$dom_hasAttribute(key);
- }
-
- String operator [](String key) {
- return _element.$dom_getAttribute(key);
- }
-
- void operator []=(String key, value) {
- _element.$dom_setAttribute(key, '$value');
- }
-
- String putIfAbsent(String key, String ifAbsent()) {
- if (!containsKey(key)) {
- this[key] = ifAbsent();
- }
- return this[key];
- }
-
- String remove(String key) {
- String value = _element.$dom_getAttribute(key);
- _element.$dom_removeAttribute(key);
- return value;
- }
-
- void clear() {
- final attributes = _element.$dom_attributes;
- for (int i = attributes.length - 1; i >= 0; i--) {
- remove(attributes[i].name);
- }
- }
-
- void forEach(void f(String key, String value)) {
- final attributes = _element.$dom_attributes;
- for (int i = 0, len = attributes.length; i < len; i++) {
- final item = attributes[i];
- f(item.name, item.value);
- }
- }
-
- Collection<String> get keys {
- // TODO(jacobr): generate a lazy collection instead.
- final attributes = _element.$dom_attributes;
- final keys = new List<String>(attributes.length);
- for (int i = 0, len = attributes.length; i < len; i++) {
- keys[i] = attributes[i].name;
- }
- return keys;
- }
-
- Collection<String> get values {
- // TODO(jacobr): generate a lazy collection instead.
- final attributes = _element.$dom_attributes;
- final values = new List<String>(attributes.length);
- for (int i = 0, len = attributes.length; i < len; i++) {
- values[i] = attributes[i].value;
- }
- return values;
- }
-
- /**
- * The number of {key, value} pairs in the map.
- */
- int get length {
- return _element.$dom_attributes.length;
- }
-
- /**
- * Returns true if there is no {key, value} pair in the map.
- */
- bool get isEmpty {
- return length == 0;
- }
-}
-
-/**
- * Provides a Map abstraction on top of data-* attributes, similar to the
- * dataSet in the old DOM.
- */
-class _DataAttributeMap implements Map<String, String> {
-
- final Map<String, String> $dom_attributes;
-
- _DataAttributeMap(this.$dom_attributes);
-
- // interface Map
-
- // TODO: Use lazy iterator when it is available on Map.
- bool containsValue(String value) => values.some((v) => v == value);
-
- bool containsKey(String key) => $dom_attributes.containsKey(_attr(key));
-
- String operator [](String key) => $dom_attributes[_attr(key)];
-
- void operator []=(String key, value) {
- $dom_attributes[_attr(key)] = '$value';
- }
-
- String putIfAbsent(String key, String ifAbsent()) =>
- $dom_attributes.putIfAbsent(_attr(key), ifAbsent);
-
- String remove(String key) => $dom_attributes.remove(_attr(key));
-
- void clear() {
- // Needs to operate on a snapshot since we are mutating the collection.
- for (String key in keys) {
- remove(key);
- }
- }
-
- void forEach(void f(String key, String value)) {
- $dom_attributes.forEach((String key, String value) {
- if (_matches(key)) {
- f(_strip(key), value);
- }
- });
- }
-
- Collection<String> get keys {
- final keys = new List<String>();
- $dom_attributes.forEach((String key, String value) {
- if (_matches(key)) {
- keys.add(_strip(key));
- }
- });
- return keys;
- }
-
- Collection<String> get values {
- final values = new List<String>();
- $dom_attributes.forEach((String key, String value) {
- if (_matches(key)) {
- values.add(value);
- }
- });
- return values;
- }
-
- int get length => keys.length;
-
- // TODO: Use lazy iterator when it is available on Map.
- bool get isEmpty => length == 0;
-
- // Helpers.
- String _attr(String key) => 'data-$key';
- bool _matches(String key) => key.startsWith('data-');
- String _strip(String key) => key.substring(5);
-}
-
class _ElementCssClassSet extends CssClassSet {
final Element _element;
@@ -7678,6 +7513,14 @@ abstract class Element extends Node implements ElementTraversal native "*Element
}
}
+ /**
+ * Gets a map for manipulating the attributes of a particular namespace.
+ * This is primarily useful for SVG attributes such as xref:link.
+ */
+ Map<String, String> getNamespacedAttributes(String namespace) {
+ return new _NamespacedAttributeMap(this, namespace);
+ }
+
/** @domName Window.getComputedStyle */
Future<CSSStyleDeclaration> get computedStyle {
// TODO(jacobr): last param should be null, see b/5045788
@@ -7914,6 +7757,9 @@ abstract class Element extends Node implements ElementTraversal native "*Element
/// @domName Element.getAttribute; @docsEditable true
String $dom_getAttribute(String name) native "getAttribute";
+ /// @domName Element.getAttributeNS; @docsEditable true
+ String $dom_getAttributeNS(String namespaceURI, String localName) native "getAttributeNS";
+
/// @domName Element.getBoundingClientRect; @docsEditable true
ClientRect getBoundingClientRect() native;
@@ -7932,6 +7778,9 @@ abstract class Element extends Node implements ElementTraversal native "*Element
/// @domName Element.hasAttribute; @docsEditable true
bool $dom_hasAttribute(String name) native "hasAttribute";
+ /// @domName Element.hasAttributeNS; @docsEditable true
+ bool $dom_hasAttributeNS(String namespaceURI, String localName) native "hasAttributeNS";
+
/// @domName Element.querySelector; @docsEditable true
Element $dom_querySelector(String selectors) native "querySelector";
@@ -7942,6 +7791,9 @@ abstract class Element extends Node implements ElementTraversal native "*Element
/// @domName Element.removeAttribute; @docsEditable true
void $dom_removeAttribute(String name) native "removeAttribute";
+ /// @domName Element.removeAttributeNS; @docsEditable true
+ void $dom_removeAttributeNS(String namespaceURI, String localName) native "removeAttributeNS";
+
/// @domName Element.scrollByLines; @docsEditable true
void scrollByLines(int lines) native;
@@ -7954,6 +7806,9 @@ abstract class Element extends Node implements ElementTraversal native "*Element
/// @domName Element.setAttribute; @docsEditable true
void $dom_setAttribute(String name, String value) native "setAttribute";
+ /// @domName Element.setAttributeNS; @docsEditable true
+ void $dom_setAttributeNS(String namespaceURI, String qualifiedName, String value) native "setAttributeNS";
+
/// @domName Element.webkitMatchesSelector; @docsEditable true
bool matchesSelector(String selectors) native "webkitMatchesSelector";
@@ -13996,6 +13851,12 @@ class Node extends EventTarget native "*Node" {
/// @domName Node.lastChild; @docsEditable true
Node get $dom_lastChild => JS("Node", "#.lastChild", this);
+ /// @domName Node.localName; @docsEditable true
+ String get $dom_localName => JS("String", "#.localName", this);
+
+ /// @domName Node.namespaceURI; @docsEditable true
+ String get $dom_namespaceURI => JS("String", "#.namespaceURI", this);
+
/// @domName Node.nextSibling; @docsEditable true
Node get nextNode => JS("Node", "#.nextSibling", this);
@@ -22137,6 +21998,228 @@ class _XSLTProcessorFactoryProvider {
// BSD-style license that can be found in the LICENSE file.
+abstract class _AttributeMap implements Map<String, String> {
+
+ bool containsValue(String value) {
+ for (var v in this.values) {
+ if (value == v) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ String putIfAbsent(String key, String ifAbsent()) {
+ if (!containsKey(key)) {
+ this[key] = ifAbsent();
+ }
+ return this[key];
+ }
+
+ void clear() {
+ for (var key in keys) {
+ remove(key);
+ }
+ }
+
+ void forEach(void f(String key, String value)) {
+ for (var key in keys) {
+ var value = this[key];
+ f(key, value);
+ }
+ }
+
+ Collection<String> get keys {
+ // TODO: generate a lazy collection instead.
+ var attributes = _element.$dom_attributes;
+ var keys = new List<String>();
+ for (int i = 0, len = attributes.length; i < len; i++) {
+ if (_matches(attributes[i])) {
+ keys.add(attributes[i].$dom_localName);
+ }
+ }
+ return keys;
+ }
+
+ Collection<String> get values {
+ // TODO: generate a lazy collection instead.
+ var attributes = _element.$dom_attributes;
+ var values = new List<String>();
+ for (int i = 0, len = attributes.length; i < len; i++) {
+ if (_matches(attributes[i])) {
+ values.add(attributes[i].value);
+ }
+ }
+ return values;
+ }
+
+ /**
+ * Returns true if there is no {key, value} pair in the map.
+ */
+ bool get isEmpty {
+ return length == 0;
+ }
+
+ /**
+ * Checks to see if the node should be included in this map.
+ */
+ bool _matches(Node node);
+}
+
+/**
+ * Wrapper to expose Element.attributes as a typed map.
+ */
+class _ElementAttributeMap extends _AttributeMap {
+
+ final Element _element;
+
+ _ElementAttributeMap(this._element);
+
+ bool containsKey(String key) {
+ return _element.$dom_hasAttribute(key);
+ }
+
+ String operator [](String key) {
+ return _element.$dom_getAttribute(key);
+ }
+
+ void operator []=(String key, value) {
+ _element.$dom_setAttribute(key, '$value');
+ }
+
+ String remove(String key) {
+ String value = _element.$dom_getAttribute(key);
+ _element.$dom_removeAttribute(key);
+ return value;
+ }
+
+ /**
+ * The number of {key, value} pairs in the map.
+ */
+ int get length {
+ return keys.length;
+ }
+
+ bool _matches(Node node) => node.$dom_namespaceURI == null;
+}
+
+/**
+ * Wrapper to expose namespaced attributes as a typed map.
+ */
+class _NamespacedAttributeMap extends _AttributeMap {
+
+ final Element _element;
+ final String _namespace;
+
+ _NamespacedAttributeMap(this._element, this._namespace);
+
+ bool containsKey(String key) {
+ return _element.$dom_hasAttributeNS(_namespace, key);
+ }
+
+ String operator [](String key) {
+ return _element.$dom_getAttributeNS(_namespace, key);
+ }
+
+ void operator []=(String key, value) {
+ _element.$dom_setAttributeNS(_namespace, key, '$value');
+ }
+
+ String remove(String key) {
+ String value = this[key];
+ _element.$dom_removeAttributeNS(_namespace, key);
+ return value;
+ }
+
+ /**
+ * The number of {key, value} pairs in the map.
+ */
+ int get length {
+ return keys.length;
+ }
+
+ bool _matches(Node node) => node.$dom_namespaceURI == _namespace;
+}
+
+
+/**
+ * Provides a Map abstraction on top of data-* attributes, similar to the
+ * dataSet in the old DOM.
+ */
+class _DataAttributeMap implements Map<String, String> {
+
+ final Map<String, String> $dom_attributes;
+
+ _DataAttributeMap(this.$dom_attributes);
+
+ // interface Map
+
+ // TODO: Use lazy iterator when it is available on Map.
+ bool containsValue(String value) => values.some((v) => v == value);
+
+ bool containsKey(String key) => $dom_attributes.containsKey(_attr(key));
+
+ String operator [](String key) => $dom_attributes[_attr(key)];
+
+ void operator []=(String key, value) {
+ $dom_attributes[_attr(key)] = '$value';
+ }
+
+ String putIfAbsent(String key, String ifAbsent()) =>
+ $dom_attributes.putIfAbsent(_attr(key), ifAbsent);
+
+ String remove(String key) => $dom_attributes.remove(_attr(key));
+
+ void clear() {
+ // Needs to operate on a snapshot since we are mutating the collection.
+ for (String key in keys) {
+ remove(key);
+ }
+ }
+
+ void forEach(void f(String key, String value)) {
+ $dom_attributes.forEach((String key, String value) {
+ if (_matches(key)) {
+ f(_strip(key), value);
+ }
+ });
+ }
+
+ Collection<String> get keys {
+ final keys = new List<String>();
+ $dom_attributes.forEach((String key, String value) {
+ if (_matches(key)) {
+ keys.add(_strip(key));
+ }
+ });
+ return keys;
+ }
+
+ Collection<String> get values {
+ final values = new List<String>();
+ $dom_attributes.forEach((String key, String value) {
+ if (_matches(key)) {
+ values.add(value);
+ }
+ });
+ return values;
+ }
+
+ int get length => keys.length;
+
+ // TODO: Use lazy iterator when it is available on Map.
+ bool get isEmpty => length == 0;
+
+ // Helpers.
+ String _attr(String key) => 'data-$key';
+ bool _matches(String key) => key.startsWith('data-');
+ String _strip(String key) => key.substring(5);
+}
+// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+
/**
* An object representing the top-level context object for web scripting.
*
« no previous file with comments | « no previous file | sdk/lib/html/dartium/html_dartium.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698