unabridged / motion

Reactive frontend UI components for Rails in pure Ruby
https://github.com/unabridged/motion
MIT License
697 stars 19 forks source link

Transpiled Event Serialization Issue #55

Closed bert-mccutchen closed 4 years ago

bert-mccutchen commented 4 years ago

The serializeEventDetails function attempts to perform destructuring assignment on event objects: https://github.com/unabridged/motion/blob/7464c9955d803af1dbb9062dc71028704d93b335/javascript/serializeEvent.js#L28

However, object destructuring ends up using hasOwnProperty when transpiled, as can be seen here:

function serializeEventDetails(event) {
  var details = {};

  var _iterator = _createForOfIteratorHelper(detailProperties),
      _step;

  try {
    for (_iterator.s(); !(_step = _iterator.n()).done;) {
      var property = _step.value;

      if (Object.prototype.hasOwnProperty.call(event, property)) {
        details[property] = event[property];
      }
    }
  } catch (err) {
    _iterator.e(err);
  } finally {
    _iterator.f();
  }

  return details;
}

This is an issue since hasOwnProperty will always return false on Event objects. See this StackOverflow for more details

const { key } = new KeyboardEvent('keydown', { key: 'test' })
key
// => 'test'

new KeyboardEvent('keydown', { key: 'test' }).hasOwnProperty('key')
// => false

I came across this while using keyboard events, it is not limited to only keyboard events.

latortuga commented 4 years ago

@bert-mccutchen this is such a thorough and detailed bug report, thanks so much for reporting this!