Skip to content

Commit 4d92ae2

Browse files
author
Oliver Pulges
committed
Merge pull request #65 from Waxolunist/wysihtml5
--- I restructured the tests for events slightly. The tests fail at the moment. So maybe you merge them in and ignore them for the moment. There are multiple errors: * The event object is not an argument of the callback. * Firing a drop event results in a paste event. I fire the events twice. Once with happen.js and once with vanilla js to show that not happen.js is the cause of this behaviour. I also tried it with the QUnit 1.12 and the old method, but the result is the same. The reason why the old tests were not showing the wrong behaviour was that paste did fire twice and so the missing execution of the drop callback was not spotted.
2 parents 4d481d0 + 37f6fee commit 4d92ae2

File tree

2 files changed

+98
-21
lines changed

2 files changed

+98
-21
lines changed

src/views/composer.observe.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,29 +158,34 @@
158158
}
159159

160160
// --------- Focus & blur logic ---------
161-
dom.observe(focusBlurElement, "focus", function() {
162-
that.parent.fire("focus").fire("focus:composer");
161+
dom.observe(focusBlurElement, "focus", function(event) {
162+
that.parent.fire("focus", event).fire("focus:composer", event);
163163

164164
// Delay storing of state until all focus handler are fired
165165
// especially the one which resets the placeholder
166166
setTimeout(function() { state = that.getValue(false, false); }, 0);
167167
});
168168

169-
dom.observe(focusBlurElement, "blur", function() {
169+
dom.observe(focusBlurElement, "blur", function(event) {
170170
if (state !== that.getValue(false, false)) {
171-
that.parent.fire("change").fire("change:composer");
171+
//create change event if supported (all except IE8)
172+
var changeevent = event;
173+
if(typeof Object.create == 'function') {
174+
changeevent = Object.create(event, { type: { value: 'change' } });
175+
}
176+
that.parent.fire("change", changeevent).fire("change:composer", changeevent);
172177
}
173-
that.parent.fire("blur").fire("blur:composer");
178+
that.parent.fire("blur", event).fire("blur:composer", event);
174179
});
175180

176181
// --------- Drag & Drop logic ---------
177182
dom.observe(element, "dragenter", function() {
178183
that.parent.fire("unset_placeholder");
179184
});
180185

181-
dom.observe(element, pasteEvents, function() {
186+
dom.observe(element, pasteEvents, function(event) {
182187
setTimeout(function() {
183-
that.parent.fire("paste").fire("paste:composer");
188+
that.parent.fire(event.type, event).fire(event.type + ":composer", event);
184189
}, 0);
185190
});
186191

test/editor_contenteditablemode_test.js

Lines changed: 86 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ if (wysihtml5.browser.supported()) {
5858

5959
// EVENTS TESTS
6060
asyncTest("Check events", function() {
61-
expect(8);
61+
expect(17);
6262

6363
var that = this;
6464
var editor = new wysihtml5.Editor(this.editableArea);
@@ -70,28 +70,32 @@ if (wysihtml5.browser.supported()) {
7070
editor.on("load", function() {
7171
var composerElement = that.editableArea;
7272

73-
editor.on("focus", function() {
73+
editor.on("focus", function(event) {
7474
ok(true, "'focus' event correctly fired");
75+
ok(event, "event is defined");
76+
ok(event instanceof Event, "event is instance of 'Event'");
77+
ok(event && event.type === 'focus', "event is of type 'focus'");
7578
});
7679

77-
editor.on("blur", function() {
80+
editor.on("blur", function(event) {
7881
ok(true, "'blur' event correctly fired");
82+
ok(event, "event is defined");
83+
ok(event instanceof Event, "event is instance of 'Event'");
84+
ok(event && event.type === 'blur', "event is of type 'blur'");
7985
});
8086

81-
editor.on("change", function() {
87+
editor.on("change", function(event) {
8288
ok(true, "'change' event correctly fired");
89+
ok(event, "event is defined");
90+
ok(event instanceof Event, "event is instance of 'Event'");
91+
ok(event && event.type === 'change', "event is of type 'change'");
8392
});
8493

85-
editor.on("paste", function() {
86-
ok(true, "'paste' event correctly fired");
87-
});
88-
89-
editor.on("drop", function() {
90-
ok(true, "'drop' event correctly fired");
91-
});
9294

93-
editor.on("custom_event", function() {
95+
editor.on("custom_event", function(event) {
9496
ok(true, "'custom_event' correctly fired");
97+
ok(event, "event is defined");
98+
ok(event && event.type === 'custom_event', "event is of type 'custom_event'");
9599
});
96100

97101
happen.once(composerElement, {type: "focus"});
@@ -104,15 +108,83 @@ if (wysihtml5.browser.supported()) {
104108

105109
equal(wysihtml5.dom.getStyle("margin-top").from(composerElement), "5px", ":focus styles are correctly unset");
106110

111+
112+
editor.fire("custom_event", { type: 'custom_event' });
113+
114+
setTimeout(function() { start(); }, 100);
115+
});
116+
});
117+
118+
asyncTest("Check events paste", function() {
119+
expect(12);
120+
121+
var that = this;
122+
var editor = new wysihtml5.Editor(this.editableArea);
123+
124+
editor.on("load", function() {
125+
var composerElement = that.editableArea;
126+
127+
editor.on("paste", function(event) {
128+
ok(event, "event is defined");
129+
ok(event instanceof Event, "event is instance of 'Event'");
130+
ok(event && event.type === 'paste', "event is of type 'paste'");
131+
});
132+
133+
//Assure that the event on the dom element works as expected
134+
that.editableArea.addEventListener('paste', function (event) {
135+
ok(event, "event is defined");
136+
ok(event instanceof Event, "event is instance of 'Event'");
137+
ok(event && event.type === 'paste', "event is of type 'paste'");
138+
});
139+
107140
happen.once(composerElement, {type: "paste"});
108-
happen.once(composerElement, {type: "drop"});
141+
//Just to show that not happen.js is the source of error
142+
var event = new Event('paste');
143+
that.editableArea.dispatchEvent(event);
144+
//QUnit.triggerEvent(composerElement, 'paste');
109145

110-
editor.fire("custom_event");
146+
setTimeout(function() { start(); }, 100);
147+
});
148+
});
149+
150+
asyncTest("Check events drop", function() {
151+
expect(12);
152+
153+
var that = this;
154+
var editor = new wysihtml5.Editor(this.editableArea);
155+
156+
editor.on("load", function() {
157+
var composerElement = that.editableArea;
111158

159+
//if changing from drop to paste it works
160+
editor.on('drop', function(event) {
161+
ok(event, "event is defined");
162+
ok(event instanceof Event, "event is instance of 'Event'");
163+
ok(event && event.type === 'drop', "event is of type 'drop'");
164+
});
165+
166+
editor.on('paste', function(event) {
167+
ok(false, "No 'paste' event was fired.");
168+
});
169+
170+
//Assure that the event on the dom element works as expected
171+
that.editableArea.addEventListener('drop', function (event) {
172+
ok(event, "event is defined");
173+
ok(event instanceof Event, "event is instance of 'Event'");
174+
ok(event && event.type === 'drop', "event is of type 'drop'");
175+
});
176+
177+
happen.once(composerElement, {type: "drop"});
178+
//Just to show that not happen.js is the source of error
179+
var event = new Event('drop');
180+
that.editableArea.dispatchEvent(event);
181+
//QUnit.triggerEvent(composerElement, 'drop');
182+
112183
setTimeout(function() { start(); }, 100);
113184
});
114185
});
115186

187+
116188
// Placeholder tests
117189
asyncTest("Check placeholder", function() {
118190
expect(12);

0 commit comments

Comments
 (0)