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

Unified Diff: sdk/lib/html/dartium/html_dartium.dart

Issue 26722002: Add "matchingTarget" function to Event, which is the Element that matched the CSS value selector. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
Download patch
Index: sdk/lib/html/dartium/html_dartium.dart
diff --git a/sdk/lib/html/dartium/html_dartium.dart b/sdk/lib/html/dartium/html_dartium.dart
index 73f50e22c232442e2b8f42fb898e6b27e526d071..f091d9548d9af5c5958862529e89e8351eeb90a6 100644
--- a/sdk/lib/html/dartium/html_dartium.dart
+++ b/sdk/lib/html/dartium/html_dartium.dart
@@ -11476,7 +11476,36 @@ class Event extends NativeFieldWrapperClass1 {
return e;
}
- Event._private();
+ /**
+ * Caches the pointer to the element that matches CSS selector involved in
+ * the event.
+ */
+ Element _cachedMatchingTarget;
+
+ /** The CSS selector involved with event delegation. */
+ String _selector;
+
+ /**
+ * A pointer to the element whose CSS selector matched within which an event
+ * was fired. If this Event was not associated with any Event delegation,
+ * then this value will be null.
+ */
+ Element matchingTarget() {
+ if (_cachedMatchingTarget == null) {
+ var currentTarget = this.currentTarget;
+ var target = this.target;
+ var matchedTarget;
+ while (matchedTarget == null && target != currentTarget &&
+ target != null) {
+ if (_selector != null && target.matches(_selector)) {
+ matchedTarget = target;
+ }
+ target = target.parent;
+ }
+ _cachedMatchingTarget = matchedTarget;
+ }
+ return _cachedMatchingTarget;
+ }
@DomName('Event.AT_TARGET')
@DocsEditable()
@@ -31152,7 +31181,9 @@ class _ElementEventStreamImpl<T extends Event> extends _EventStream<T>
super(target, eventType, useCapture);
Stream<T> matches(String selector) =>
- this.where((event) => event.target.matchesWithAncestors(selector));
+ new _MatchingStream(
+ this.where((event) => event.target.matchesWithAncestors(selector)),
+ selector);
}
/**
@@ -31174,7 +31205,9 @@ class _ElementListEventStreamImpl<T extends Event> extends Stream<T>
}
Stream<T> matches(String selector) =>
- this.where((event) => event.target.matchesWithAncestors(selector));
+ new _MatchingStream(
+ this.where((event) => event.target.matchesWithAncestors(selector)),
+ selector);
// Delegate all regular Stream behavor to our wrapped Stream.
StreamSubscription<T> listen(void onData(T event),
@@ -31190,6 +31223,27 @@ class _ElementListEventStreamImpl<T extends Event> extends Stream<T>
}
/**
+ * A stream that keeps track of what CSS selectors it used to collect element
+ * events for event delegation.
+ */
+class _MatchingStream<T extends Event> extends Stream<T> {
+ Stream<T> _stream;
+ String selector;
+
+ _MatchingStream(Stream this._stream, String this.selector);
+
+ // Delegate all regular Stream behavor to our wrapped Stream.
+ StreamSubscription<T> listen(void onData(T event),
+ { void onError(error),
+ void onDone(),
+ bool cancelOnError}) =>
+ _stream.listen((e) { e._selector = selector; return onData(e); },
+ onError: onError,
+ onDone: onDone,
+ cancelOnError: cancelOnError);
+}
+
+/**
* A stream of custom events, which enables the user to "fire" (add) their own
* custom events to a stream.
*/

Powered by Google App Engine
This is Rietveld 408576698