Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

feat(eventHandler): Support snake-case event names instead of camelCase. #1477

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 5 additions & 15 deletions lib/core_dom/event_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,26 +78,16 @@ class EventHandler {
}

/**
* Converts event name into attribute. Event named 'someCustomEvent' needs to
* be transformed into on-some-custom-event.
* Converts event name into attribute name.
*/
static String eventNameToAttrName(String eventName) {
var part = eventName.replaceAllMapped(new RegExp("([A-Z])"), (Match match) {
return '-${match.group(0).toLowerCase()}';
});
return 'on-${part}';
}
static String eventNameToAttrName(String eventName) => 'on-$eventName';

/**
* Converts attribute into event name. Attribute 'on-some-custom-event'
* corresponds to event named 'someCustomEvent'.
* Converts attribute name into event name.
*/
static String attrNameToEventName(String attrName) {
var part = attrName.startsWith("on-") ? attrName.substring(3) : attrName;
part = part.replaceAllMapped(new RegExp(r'\-(\w)'), (Match match) {
return match.group(0).toUpperCase();
});
return part.replaceAll("-", "");
assert(attrName.startsWith('on-'));
return attrName.substring(3);
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/core_dom/event_handler_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ main() {
var e = compile(_,
'''<div on-my-new-event="invoked=true;"></div>''');

_.triggerEvent(e, 'myNewEvent');
_.triggerEvent(e, 'my-new-event');
expect(_.rootScope.context['invoked']).toEqual(true);
});

Expand Down