项目作者: mchalapuk

项目描述 :
Serialize DOM events with WSON.
高级语言: CoffeeScript
项目地址: git://github.com/mchalapuk/wson-event-connector.git
创建时间: 2016-07-06T16:22:01Z
项目社区:https://github.com/mchalapuk/wson-event-connector

开源协议:MIT License

下载


wson-event-connector

Build Status
Dependency Status
devDependency Status
NPM version

WSON is a human-readable data-interchange format with support for cyclic
structures. This module is an extension to wson that enables serializing
DOM events to strings and parsing those strings back to DOM events.

Possible Use Cases

  1. Record DOM events to later simulate a user during automated test
    (needs wson-dom-connector),
  2. Log DOM events just for debugging.

Installation

  1. npm install --save wson wson-event-connector

Usage

It can be used in a web browser via browserify

  1. var WSON = require("wson").Wson;
  2. var eventConnectors = require("wson-event-connector");
  3. var wson = new WSON({
  4. connectors: eventConnectors(window)
  5. });
  6. function logEvent(e) {
  7. console.log(wson.stringify(e));
  8. }
  9. var events = [ 'load', 'error', 'focus', 'blur', 'resize', 'scroll', 'unload' ];
  10. events.forEach(function(name) {
  11. window.addEventListener(name, logEvent);
  12. });

…or in node with any standard-compliant DOM implementation
(e.g. jsdom).

  1. var WSON = require("wson").Wson;
  2. var eventConnectors = require("wson-event-connector");
  3. var domConnectors = require("wson-dom-connector");
  4. var jsdom = require("jsdom");
  5. var _ = require("underscore");
  6. var window = jsdom.jsdom("<body></body>").defaultView;
  7. var wson = new WSON({
  8. connectors: _.extend(eventConnectors(window), domConnectors(window))
  9. });
  10. var body = window.document.body;
  11. body.addEventListener('click', function(event) {
  12. console.log(wson.stringify(event)));
  13. }
  14. body.dispatchEvent(new window.MouseEvent('click', {
  15. screenX: 300,
  16. screenY: 400,
  17. clientX: 20,
  18. clientY: 10,
  19. button: 1,
  20. buttons: 1,
  21. });
  22. // [:MouseEvent|click|#f|#f|#0|#0|#300|#400|#20|#10|#1|#1|#n|#n|[:HTMLBodyElement|/body`a1`e]]

Above example uses connectors from wson-dom-connector
module to serialize DOM nodes assigned to event properties.

Supported Events

Following events types are currently supported:

(Not Yet) Supported Events

Near future should bring support for following classes:

Feel free to message me if you desperately need one of above.

Pull requests are also very welcome!

CONTRIBUTING GUIDELINES:

Please do not submit pull requests implementing non-standard vendor-specific events.
For those, a separate module should be created (e.g. wson-mozilla-connector),
with this module as dependency (see API Reference).

Unsupported Events

Serialization of following event classes will not be implemented in this module:

Why are some properties not serialized?

Following properties are by default not serialized:

API Reference

This document describes API exported by this (wson-event-connector) module.
Please refer to wson’s documentation for description of wson’s API
and serialization algorithm.

All Connectors

  1. exports = function(window, additionalFields = []) { ... }

Creates WSON connectors for all event classes found in window namespace.
Created connectors will be extended to serialize fields passed
in additionalFields array. Function returns a map
(event class name => connector instance), which can be passed
as “connectors” option to WSON’s constructor (see example below).

  1. var WSON = require('wson').Wson;
  2. var eventConnectors = require('wson-event-connector');
  3. var wson = new WSON({ connectors: eventConnectors(window) });

Event Connector

  1. exports.Event = function(EventClass, additionalFields = []) { ... }

Constructs a connector which is able to serialize instances of EventClass.
Passed class must be derived from or equal window.Event.
Returned connector serializes fields in following order: Event.bubbles,
Event.cancelable, additionalFields…, Event.target.

Event.target is not settable from JavaScript. Web browsers assign
its value inside EventTarget.dispatchEvent(event).
Events returned from wson.parse(string) are not yet dispatched, hence they
do not have Event.target set. Instead, target is deserialized into
non-standard Event.parsedTarget property (see example below).

  1. var WSON = require('wson').Wson;
  2. var eventConnectors = require('wson-event-connector');
  3. var domConnectors = require('wson-dom-connector');
  4. var wson = new WSON({ connectors: {
  5. 'Event': eventConnectors.Event(window.Event),
  6. 'HTMLBodyElement': domConnectors(window).HTMLBodyElement,
  7. }});
  8. var event = wson.parse('[:Event|load|#f|#t|[:HTMLBodyElement|/body`a1`e]]');
  9. event.parsedTarget.dispatchEvent(event);

Init Based Connector

  1. exports.InitBased = function(Class, serializedFields) { ... }

Constructs a connector which is able to serialize instances of Class.
Class’ constructor must accept single argument, which is a map containing
initial values for properties of constructed object (init object pattern?).
Constructed connector serializes fields of names and in order as passed
in serializedFields array.

  1. var WSON = require('wson').Wson;
  2. var connectors = require('wson-event-connector');
  3. var wson = new WSON({ connectors: {
  4. 'Weather': new connectors.InitBased(Weather, [ 'temperature', 'pressure', 'humidity', 'sky' ])
  5. }});
  6. var weather = new Weather({
  7. temperature: '27C',
  8. pressure: '1000HpA',
  9. humidity: '75%',
  10. sky: 'clear'
  11. });
  12. console.log(wson.stringify(weather));
  13. // [:Weather|27C|1000Hpa|75%|clear]

License

Copyright © 2016 - 2019 Maciej Chałapuk.
Released under MIT license.