@ckeditor/ckeditor5-utils 35.0.0 → 35.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +5 -5
- package/src/areconnectedthroughproperties.js +75 -0
- package/src/ckeditorerror.js +195 -0
- package/src/collection.js +619 -0
- package/src/comparearrays.js +45 -0
- package/src/config.js +216 -0
- package/src/count.js +22 -0
- package/src/diff.js +113 -0
- package/src/difftochanges.js +76 -0
- package/src/dom/createelement.js +41 -0
- package/src/dom/emittermixin.js +301 -0
- package/src/dom/getancestors.js +27 -0
- package/src/dom/getborderwidths.js +24 -0
- package/src/dom/getcommonancestor.js +25 -0
- package/src/dom/getdatafromelement.js +20 -0
- package/src/dom/getpositionedancestor.js +23 -0
- package/src/dom/global.js +23 -0
- package/src/dom/indexof.js +21 -0
- package/src/dom/insertat.js +17 -0
- package/src/dom/iscomment.js +17 -0
- package/src/dom/isnode.js +24 -0
- package/src/dom/isrange.js +16 -0
- package/src/dom/istext.js +16 -0
- package/src/dom/isvisible.js +23 -0
- package/src/dom/iswindow.js +25 -0
- package/src/dom/position.js +328 -0
- package/src/dom/rect.js +364 -0
- package/src/dom/remove.js +18 -0
- package/src/dom/resizeobserver.js +145 -0
- package/src/dom/scroll.js +270 -0
- package/src/dom/setdatainelement.js +20 -0
- package/src/dom/tounit.js +25 -0
- package/src/elementreplacer.js +43 -0
- package/src/emittermixin.js +471 -0
- package/src/env.js +168 -0
- package/src/eventinfo.js +26 -0
- package/src/fastdiff.js +229 -0
- package/src/first.js +20 -0
- package/src/focustracker.js +103 -0
- package/src/index.js +36 -0
- package/src/inserttopriorityarray.js +21 -0
- package/src/isiterable.js +16 -0
- package/src/keyboard.js +222 -0
- package/src/keystrokehandler.js +114 -0
- package/src/language.js +20 -0
- package/src/locale.js +79 -0
- package/src/mapsequal.js +27 -0
- package/src/mix.js +44 -0
- package/src/nth.js +28 -0
- package/src/objecttomap.js +25 -0
- package/src/observablemixin.js +605 -0
- package/src/priorities.js +32 -0
- package/src/spy.js +22 -0
- package/src/toarray.js +7 -0
- package/src/tomap.js +27 -0
- package/src/translation-service.js +180 -0
- package/src/uid.js +55 -0
- package/src/unicode.js +91 -0
- package/src/version.js +148 -0
|
@@ -0,0 +1,471 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* @module utils/emittermixin
|
|
7
|
+
*/
|
|
8
|
+
import EventInfo from './eventinfo';
|
|
9
|
+
import uid from './uid';
|
|
10
|
+
import priorities from './priorities';
|
|
11
|
+
import insertToPriorityArray from './inserttopriorityarray';
|
|
12
|
+
// To check if component is loaded more than once.
|
|
13
|
+
import './version';
|
|
14
|
+
import CKEditorError from './ckeditorerror';
|
|
15
|
+
const _listeningTo = Symbol('listeningTo');
|
|
16
|
+
const _emitterId = Symbol('emitterId');
|
|
17
|
+
const _delegations = Symbol('delegations');
|
|
18
|
+
/**
|
|
19
|
+
* Mixin that injects the {@link ~Emitter events API} into its host.
|
|
20
|
+
*
|
|
21
|
+
* Read more about the concept of emitters in the:
|
|
22
|
+
* * {@glink framework/guides/architecture/core-editor-architecture#event-system-and-observables Event system and observables}
|
|
23
|
+
* section of the {@glink framework/guides/architecture/core-editor-architecture Core editor architecture} guide.
|
|
24
|
+
* * {@glink framework/guides/deep-dive/event-system Event system} deep dive guide.
|
|
25
|
+
*
|
|
26
|
+
* @mixin EmitterMixin
|
|
27
|
+
* @implements module:utils/emittermixin~Emitter
|
|
28
|
+
*/
|
|
29
|
+
const EmitterMixin = {
|
|
30
|
+
/**
|
|
31
|
+
* @inheritDoc
|
|
32
|
+
*/
|
|
33
|
+
on(event, callback, options = {}) {
|
|
34
|
+
this.listenTo(this, event, callback, options);
|
|
35
|
+
},
|
|
36
|
+
/**
|
|
37
|
+
* @inheritDoc
|
|
38
|
+
*/
|
|
39
|
+
once(event, callback, options) {
|
|
40
|
+
let wasFired = false;
|
|
41
|
+
const onceCallback = (event, ...args) => {
|
|
42
|
+
// Ensure the callback is called only once even if the callback itself leads to re-firing the event
|
|
43
|
+
// (which would call the callback again).
|
|
44
|
+
if (!wasFired) {
|
|
45
|
+
wasFired = true;
|
|
46
|
+
// Go off() at the first call.
|
|
47
|
+
event.off();
|
|
48
|
+
// Go with the original callback.
|
|
49
|
+
callback.call(this, event, ...args);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
// Make a similar on() call, simply replacing the callback.
|
|
53
|
+
this.listenTo(this, event, onceCallback, options);
|
|
54
|
+
},
|
|
55
|
+
/**
|
|
56
|
+
* @inheritDoc
|
|
57
|
+
*/
|
|
58
|
+
off(event, callback) {
|
|
59
|
+
this.stopListening(this, event, callback);
|
|
60
|
+
},
|
|
61
|
+
/**
|
|
62
|
+
* @inheritDoc
|
|
63
|
+
*/
|
|
64
|
+
listenTo(emitter, event, callback, options = {}) {
|
|
65
|
+
let emitterInfo, eventCallbacks;
|
|
66
|
+
// _listeningTo contains a list of emitters that this object is listening to.
|
|
67
|
+
// This list has the following format:
|
|
68
|
+
//
|
|
69
|
+
// _listeningTo: {
|
|
70
|
+
// emitterId: {
|
|
71
|
+
// emitter: emitter,
|
|
72
|
+
// callbacks: {
|
|
73
|
+
// event1: [ callback1, callback2, ... ]
|
|
74
|
+
// ....
|
|
75
|
+
// }
|
|
76
|
+
// },
|
|
77
|
+
// ...
|
|
78
|
+
// }
|
|
79
|
+
if (!this[_listeningTo]) {
|
|
80
|
+
this[_listeningTo] = {};
|
|
81
|
+
}
|
|
82
|
+
const emitters = this[_listeningTo];
|
|
83
|
+
if (!_getEmitterId(emitter)) {
|
|
84
|
+
_setEmitterId(emitter);
|
|
85
|
+
}
|
|
86
|
+
const emitterId = _getEmitterId(emitter);
|
|
87
|
+
if (!(emitterInfo = emitters[emitterId])) {
|
|
88
|
+
emitterInfo = emitters[emitterId] = {
|
|
89
|
+
emitter,
|
|
90
|
+
callbacks: {}
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
if (!(eventCallbacks = emitterInfo.callbacks[event])) {
|
|
94
|
+
eventCallbacks = emitterInfo.callbacks[event] = [];
|
|
95
|
+
}
|
|
96
|
+
eventCallbacks.push(callback);
|
|
97
|
+
// Finally register the callback to the event.
|
|
98
|
+
addEventListener(this, emitter, event, callback, options);
|
|
99
|
+
},
|
|
100
|
+
/**
|
|
101
|
+
* @inheritDoc
|
|
102
|
+
*/
|
|
103
|
+
stopListening(emitter, event, callback) {
|
|
104
|
+
const emitters = this[_listeningTo];
|
|
105
|
+
let emitterId = emitter && _getEmitterId(emitter);
|
|
106
|
+
const emitterInfo = (emitters && emitterId) ? emitters[emitterId] : undefined;
|
|
107
|
+
const eventCallbacks = (emitterInfo && event) ? emitterInfo.callbacks[event] : undefined;
|
|
108
|
+
// Stop if nothing has been listened.
|
|
109
|
+
if (!emitters || (emitter && !emitterInfo) || (event && !eventCallbacks)) {
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
// All params provided. off() that single callback.
|
|
113
|
+
if (callback) {
|
|
114
|
+
removeEventListener(this, emitter, event, callback);
|
|
115
|
+
// We must remove callbacks as well in order to prevent memory leaks.
|
|
116
|
+
// See https://github.com/ckeditor/ckeditor5/pull/8480
|
|
117
|
+
const index = eventCallbacks.indexOf(callback);
|
|
118
|
+
if (index !== -1) {
|
|
119
|
+
if (eventCallbacks.length === 1) {
|
|
120
|
+
delete emitterInfo.callbacks[event];
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
removeEventListener(this, emitter, event, callback);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
// Only `emitter` and `event` provided. off() all callbacks for that event.
|
|
128
|
+
else if (eventCallbacks) {
|
|
129
|
+
while ((callback = eventCallbacks.pop())) {
|
|
130
|
+
removeEventListener(this, emitter, event, callback);
|
|
131
|
+
}
|
|
132
|
+
delete emitterInfo.callbacks[event];
|
|
133
|
+
}
|
|
134
|
+
// Only `emitter` provided. off() all events for that emitter.
|
|
135
|
+
else if (emitterInfo) {
|
|
136
|
+
for (event in emitterInfo.callbacks) {
|
|
137
|
+
this.stopListening(emitter, event);
|
|
138
|
+
}
|
|
139
|
+
delete emitters[emitterId];
|
|
140
|
+
}
|
|
141
|
+
// No params provided. off() all emitters.
|
|
142
|
+
else {
|
|
143
|
+
for (emitterId in emitters) {
|
|
144
|
+
this.stopListening(emitters[emitterId].emitter);
|
|
145
|
+
}
|
|
146
|
+
delete this[_listeningTo];
|
|
147
|
+
}
|
|
148
|
+
},
|
|
149
|
+
/**
|
|
150
|
+
* @inheritDoc
|
|
151
|
+
*/
|
|
152
|
+
fire(eventOrInfo, ...args) {
|
|
153
|
+
try {
|
|
154
|
+
const eventInfo = eventOrInfo instanceof EventInfo ? eventOrInfo : new EventInfo(this, eventOrInfo);
|
|
155
|
+
const event = eventInfo.name;
|
|
156
|
+
let callbacks = getCallbacksForEvent(this, event);
|
|
157
|
+
// Record that the event passed this emitter on its path.
|
|
158
|
+
eventInfo.path.push(this);
|
|
159
|
+
// Handle event listener callbacks first.
|
|
160
|
+
if (callbacks) {
|
|
161
|
+
// Arguments passed to each callback.
|
|
162
|
+
const callbackArgs = [eventInfo, ...args];
|
|
163
|
+
// Copying callbacks array is the easiest and most secure way of preventing infinite loops, when event callbacks
|
|
164
|
+
// are added while processing other callbacks. Previous solution involved adding counters (unique ids) but
|
|
165
|
+
// failed if callbacks were added to the queue before currently processed callback.
|
|
166
|
+
// If this proves to be too inefficient, another method is to change `.on()` so callbacks are stored if same
|
|
167
|
+
// event is currently processed. Then, `.fire()` at the end, would have to add all stored events.
|
|
168
|
+
callbacks = Array.from(callbacks);
|
|
169
|
+
for (let i = 0; i < callbacks.length; i++) {
|
|
170
|
+
callbacks[i].callback.apply(this, callbackArgs);
|
|
171
|
+
// Remove the callback from future requests if off() has been called.
|
|
172
|
+
if (eventInfo.off.called) {
|
|
173
|
+
// Remove the called mark for the next calls.
|
|
174
|
+
delete eventInfo.off.called;
|
|
175
|
+
this._removeEventListener(event, callbacks[i].callback);
|
|
176
|
+
}
|
|
177
|
+
// Do not execute next callbacks if stop() was called.
|
|
178
|
+
if (eventInfo.stop.called) {
|
|
179
|
+
break;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
// Delegate event to other emitters if needed.
|
|
184
|
+
const delegations = this[_delegations];
|
|
185
|
+
if (delegations) {
|
|
186
|
+
const destinations = delegations.get(event);
|
|
187
|
+
const passAllDestinations = delegations.get('*');
|
|
188
|
+
if (destinations) {
|
|
189
|
+
fireDelegatedEvents(destinations, eventInfo, args);
|
|
190
|
+
}
|
|
191
|
+
if (passAllDestinations) {
|
|
192
|
+
fireDelegatedEvents(passAllDestinations, eventInfo, args);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return eventInfo.return;
|
|
196
|
+
}
|
|
197
|
+
catch (err) {
|
|
198
|
+
// @if CK_DEBUG // throw err;
|
|
199
|
+
/* istanbul ignore next */
|
|
200
|
+
CKEditorError.rethrowUnexpectedError(err, this);
|
|
201
|
+
}
|
|
202
|
+
},
|
|
203
|
+
/**
|
|
204
|
+
* @inheritDoc
|
|
205
|
+
*/
|
|
206
|
+
delegate(...events) {
|
|
207
|
+
return {
|
|
208
|
+
to: (emitter, nameOrFunction) => {
|
|
209
|
+
if (!this[_delegations]) {
|
|
210
|
+
this[_delegations] = new Map();
|
|
211
|
+
}
|
|
212
|
+
// Originally there was a for..of loop which unfortunately caused an error in Babel that didn't allow
|
|
213
|
+
// build an application. See: https://github.com/ckeditor/ckeditor5-react/issues/40.
|
|
214
|
+
events.forEach(eventName => {
|
|
215
|
+
const destinations = this[_delegations].get(eventName);
|
|
216
|
+
if (!destinations) {
|
|
217
|
+
this[_delegations].set(eventName, new Map([[emitter, nameOrFunction]]));
|
|
218
|
+
}
|
|
219
|
+
else {
|
|
220
|
+
destinations.set(emitter, nameOrFunction);
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
},
|
|
226
|
+
/**
|
|
227
|
+
* @inheritDoc
|
|
228
|
+
*/
|
|
229
|
+
stopDelegating(event, emitter) {
|
|
230
|
+
if (!this[_delegations]) {
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
if (!event) {
|
|
234
|
+
this[_delegations].clear();
|
|
235
|
+
}
|
|
236
|
+
else if (!emitter) {
|
|
237
|
+
this[_delegations].delete(event);
|
|
238
|
+
}
|
|
239
|
+
else {
|
|
240
|
+
const destinations = this[_delegations].get(event);
|
|
241
|
+
if (destinations) {
|
|
242
|
+
destinations.delete(emitter);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
},
|
|
246
|
+
/**
|
|
247
|
+
* @inheritDoc
|
|
248
|
+
*/
|
|
249
|
+
_addEventListener(event, callback, options) {
|
|
250
|
+
createEventNamespace(this, event);
|
|
251
|
+
const lists = getCallbacksListsForNamespace(this, event);
|
|
252
|
+
const priority = priorities.get(options.priority);
|
|
253
|
+
const callbackDefinition = {
|
|
254
|
+
callback,
|
|
255
|
+
priority
|
|
256
|
+
};
|
|
257
|
+
// Add the callback to all callbacks list.
|
|
258
|
+
for (const callbacks of lists) {
|
|
259
|
+
// Add the callback to the list in the right priority position.
|
|
260
|
+
insertToPriorityArray(callbacks, callbackDefinition);
|
|
261
|
+
}
|
|
262
|
+
},
|
|
263
|
+
/**
|
|
264
|
+
* @inheritDoc
|
|
265
|
+
*/
|
|
266
|
+
_removeEventListener(event, callback) {
|
|
267
|
+
const lists = getCallbacksListsForNamespace(this, event);
|
|
268
|
+
for (const callbacks of lists) {
|
|
269
|
+
for (let i = 0; i < callbacks.length; i++) {
|
|
270
|
+
if (callbacks[i].callback == callback) {
|
|
271
|
+
// Remove the callback from the list (fixing the next index).
|
|
272
|
+
callbacks.splice(i, 1);
|
|
273
|
+
i--;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
};
|
|
279
|
+
export default EmitterMixin;
|
|
280
|
+
/**
|
|
281
|
+
* Checks if `listeningEmitter` listens to an emitter with given `listenedToEmitterId` and if so, returns that emitter.
|
|
282
|
+
* If not, returns `null`.
|
|
283
|
+
*
|
|
284
|
+
* @internal
|
|
285
|
+
* @protected
|
|
286
|
+
* @param {module:utils/emittermixin~Emitter} listeningEmitter An emitter that listens.
|
|
287
|
+
* @param {String} listenedToEmitterId Unique emitter id of emitter listened to.
|
|
288
|
+
* @returns {module:utils/emittermixin~Emitter|null}
|
|
289
|
+
*/
|
|
290
|
+
export function _getEmitterListenedTo(listeningEmitter, listenedToEmitterId) {
|
|
291
|
+
const listeningTo = listeningEmitter[_listeningTo];
|
|
292
|
+
if (listeningTo && listeningTo[listenedToEmitterId]) {
|
|
293
|
+
return listeningTo[listenedToEmitterId].emitter;
|
|
294
|
+
}
|
|
295
|
+
return null;
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Sets emitter's unique id.
|
|
299
|
+
*
|
|
300
|
+
* **Note:** `_emitterId` can be set only once.
|
|
301
|
+
*
|
|
302
|
+
* @internal
|
|
303
|
+
* @protected
|
|
304
|
+
* @param {module:utils/emittermixin~Emitter} emitter An emitter for which id will be set.
|
|
305
|
+
* @param {String} [id] Unique id to set. If not passed, random unique id will be set.
|
|
306
|
+
*/
|
|
307
|
+
export function _setEmitterId(emitter, id) {
|
|
308
|
+
if (!emitter[_emitterId]) {
|
|
309
|
+
emitter[_emitterId] = id || uid();
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* Returns emitter's unique id.
|
|
314
|
+
*
|
|
315
|
+
* @internal
|
|
316
|
+
* @protected
|
|
317
|
+
* @param {module:utils/emittermixin~Emitter} emitter An emitter which id will be returned.
|
|
318
|
+
* @returns {String|undefined}
|
|
319
|
+
*/
|
|
320
|
+
export function _getEmitterId(emitter) {
|
|
321
|
+
return emitter[_emitterId];
|
|
322
|
+
}
|
|
323
|
+
// Gets the internal `_events` property of the given object.
|
|
324
|
+
// `_events` property store all lists with callbacks for registered event names.
|
|
325
|
+
// If there were no events registered on the object, empty `_events` object is created.
|
|
326
|
+
function getEvents(source) {
|
|
327
|
+
if (!source._events) {
|
|
328
|
+
Object.defineProperty(source, '_events', {
|
|
329
|
+
value: {}
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
return source._events;
|
|
333
|
+
}
|
|
334
|
+
// Creates event node for generic-specific events relation architecture.
|
|
335
|
+
function makeEventNode() {
|
|
336
|
+
return {
|
|
337
|
+
callbacks: [],
|
|
338
|
+
childEvents: []
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
// Creates an architecture for generic-specific events relation.
|
|
342
|
+
// If needed, creates all events for given eventName, i.e. if the first registered event
|
|
343
|
+
// is foo:bar:abc, it will create foo:bar:abc, foo:bar and foo event and tie them together.
|
|
344
|
+
// It also copies callbacks from more generic events to more specific events when
|
|
345
|
+
// specific events are created.
|
|
346
|
+
function createEventNamespace(source, eventName) {
|
|
347
|
+
const events = getEvents(source);
|
|
348
|
+
// First, check if the event we want to add to the structure already exists.
|
|
349
|
+
if (events[eventName]) {
|
|
350
|
+
// If it exists, we don't have to do anything.
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
// In other case, we have to create the structure for the event.
|
|
354
|
+
// Note, that we might need to create intermediate events too.
|
|
355
|
+
// I.e. if foo:bar:abc is being registered and we only have foo in the structure,
|
|
356
|
+
// we need to also register foo:bar.
|
|
357
|
+
// Currently processed event name.
|
|
358
|
+
let name = eventName;
|
|
359
|
+
// Name of the event that is a child event for currently processed event.
|
|
360
|
+
let childEventName = null;
|
|
361
|
+
// Array containing all newly created specific events.
|
|
362
|
+
const newEventNodes = [];
|
|
363
|
+
// While loop can't check for ':' index because we have to handle generic events too.
|
|
364
|
+
// In each loop, we truncate event name, going from the most specific name to the generic one.
|
|
365
|
+
// I.e. foo:bar:abc -> foo:bar -> foo.
|
|
366
|
+
while (name !== '') {
|
|
367
|
+
if (events[name]) {
|
|
368
|
+
// If the currently processed event name is already registered, we can be sure
|
|
369
|
+
// that it already has all the structure created, so we can break the loop here
|
|
370
|
+
// as no more events need to be registered.
|
|
371
|
+
break;
|
|
372
|
+
}
|
|
373
|
+
// If this event is not yet registered, create a new object for it.
|
|
374
|
+
events[name] = makeEventNode();
|
|
375
|
+
// Add it to the array with newly created events.
|
|
376
|
+
newEventNodes.push(events[name]);
|
|
377
|
+
// Add previously processed event name as a child of this event.
|
|
378
|
+
if (childEventName) {
|
|
379
|
+
events[name].childEvents.push(childEventName);
|
|
380
|
+
}
|
|
381
|
+
childEventName = name;
|
|
382
|
+
// If `.lastIndexOf()` returns -1, `.substr()` will return '' which will break the loop.
|
|
383
|
+
name = name.substr(0, name.lastIndexOf(':'));
|
|
384
|
+
}
|
|
385
|
+
if (name !== '') {
|
|
386
|
+
// If name is not empty, we found an already registered event that was a parent of the
|
|
387
|
+
// event we wanted to register.
|
|
388
|
+
// Copy that event's callbacks to newly registered events.
|
|
389
|
+
for (const node of newEventNodes) {
|
|
390
|
+
node.callbacks = events[name].callbacks.slice();
|
|
391
|
+
}
|
|
392
|
+
// Add last newly created event to the already registered event.
|
|
393
|
+
events[name].childEvents.push(childEventName);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
// Gets an array containing callbacks list for a given event and it's more specific events.
|
|
397
|
+
// I.e. if given event is foo:bar and there is also foo:bar:abc event registered, this will
|
|
398
|
+
// return callback list of foo:bar and foo:bar:abc (but not foo).
|
|
399
|
+
function getCallbacksListsForNamespace(source, eventName) {
|
|
400
|
+
const eventNode = getEvents(source)[eventName];
|
|
401
|
+
if (!eventNode) {
|
|
402
|
+
return [];
|
|
403
|
+
}
|
|
404
|
+
let callbacksLists = [eventNode.callbacks];
|
|
405
|
+
for (let i = 0; i < eventNode.childEvents.length; i++) {
|
|
406
|
+
const childCallbacksLists = getCallbacksListsForNamespace(source, eventNode.childEvents[i]);
|
|
407
|
+
callbacksLists = callbacksLists.concat(childCallbacksLists);
|
|
408
|
+
}
|
|
409
|
+
return callbacksLists;
|
|
410
|
+
}
|
|
411
|
+
// Get the list of callbacks for a given event, but only if there any callbacks have been registered.
|
|
412
|
+
// If there are no callbacks registered for given event, it checks if this is a specific event and looks
|
|
413
|
+
// for callbacks for it's more generic version.
|
|
414
|
+
function getCallbacksForEvent(source, eventName) {
|
|
415
|
+
let event;
|
|
416
|
+
if (!source._events || !(event = source._events[eventName]) || !event.callbacks.length) {
|
|
417
|
+
// There are no callbacks registered for specified eventName.
|
|
418
|
+
// But this could be a specific-type event that is in a namespace.
|
|
419
|
+
if (eventName.indexOf(':') > -1) {
|
|
420
|
+
// If the eventName is specific, try to find callback lists for more generic event.
|
|
421
|
+
return getCallbacksForEvent(source, eventName.substr(0, eventName.lastIndexOf(':')));
|
|
422
|
+
}
|
|
423
|
+
else {
|
|
424
|
+
// If this is a top-level generic event, return null;
|
|
425
|
+
return null;
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
return event.callbacks;
|
|
429
|
+
}
|
|
430
|
+
// Fires delegated events for given map of destinations.
|
|
431
|
+
//
|
|
432
|
+
// @private
|
|
433
|
+
// * @param {Map.<utils.Emitter>} destinations A map containing
|
|
434
|
+
// `[ {@link module:utils/emittermixin~Emitter}, "event name" ]` pair destinations.
|
|
435
|
+
// * @param {utils.EventInfo} eventInfo The original event info object.
|
|
436
|
+
// * @param {Array.<*>} fireArgs Arguments the original event was fired with.
|
|
437
|
+
function fireDelegatedEvents(destinations, eventInfo, fireArgs) {
|
|
438
|
+
for (let [emitter, name] of destinations) {
|
|
439
|
+
if (!name) {
|
|
440
|
+
name = eventInfo.name;
|
|
441
|
+
}
|
|
442
|
+
else if (typeof name == 'function') {
|
|
443
|
+
name = name(eventInfo.name);
|
|
444
|
+
}
|
|
445
|
+
const delegatedInfo = new EventInfo(eventInfo.source, name);
|
|
446
|
+
delegatedInfo.path = [...eventInfo.path];
|
|
447
|
+
emitter.fire(delegatedInfo, ...fireArgs);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
// Helper for registering event callback on the emitter.
|
|
451
|
+
function addEventListener(listener, emitter, event, callback, options) {
|
|
452
|
+
if (emitter._addEventListener) {
|
|
453
|
+
emitter._addEventListener(event, callback, options);
|
|
454
|
+
}
|
|
455
|
+
else {
|
|
456
|
+
// Allow listening on objects that do not implement Emitter interface.
|
|
457
|
+
// This is needed in some tests that are using mocks instead of the real objects with EmitterMixin mixed.
|
|
458
|
+
listener._addEventListener.call(emitter, event, callback, options);
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
// Helper for removing event callback from the emitter.
|
|
462
|
+
function removeEventListener(listener, emitter, event, callback) {
|
|
463
|
+
if (emitter._removeEventListener) {
|
|
464
|
+
emitter._removeEventListener(event, callback);
|
|
465
|
+
}
|
|
466
|
+
else {
|
|
467
|
+
// Allow listening on objects that do not implement Emitter interface.
|
|
468
|
+
// This is needed in some tests that are using mocks instead of the real objects with EmitterMixin mixed.
|
|
469
|
+
listener._removeEventListener.call(emitter, event, callback);
|
|
470
|
+
}
|
|
471
|
+
}
|
package/src/env.js
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
+
*/
|
|
5
|
+
/* globals navigator:false */
|
|
6
|
+
/**
|
|
7
|
+
* @module utils/env
|
|
8
|
+
*/
|
|
9
|
+
const userAgent = navigator.userAgent.toLowerCase();
|
|
10
|
+
/**
|
|
11
|
+
* A namespace containing environment and browser information.
|
|
12
|
+
*
|
|
13
|
+
* @namespace
|
|
14
|
+
*/
|
|
15
|
+
const env = {
|
|
16
|
+
/**
|
|
17
|
+
* Indicates that the application is running on Macintosh.
|
|
18
|
+
*
|
|
19
|
+
* @static
|
|
20
|
+
* @type {Boolean}
|
|
21
|
+
*/
|
|
22
|
+
isMac: isMac(userAgent),
|
|
23
|
+
/**
|
|
24
|
+
* Indicates that the application is running on Windows.
|
|
25
|
+
*
|
|
26
|
+
* @static
|
|
27
|
+
* @type {Boolean}
|
|
28
|
+
*/
|
|
29
|
+
isWindows: isWindows(userAgent),
|
|
30
|
+
/**
|
|
31
|
+
* Indicates that the application is running in Firefox (Gecko).
|
|
32
|
+
*
|
|
33
|
+
* @static
|
|
34
|
+
* @type {Boolean}
|
|
35
|
+
*/
|
|
36
|
+
isGecko: isGecko(userAgent),
|
|
37
|
+
/**
|
|
38
|
+
* Indicates that the application is running in Safari.
|
|
39
|
+
*
|
|
40
|
+
* @static
|
|
41
|
+
* @type {Boolean}
|
|
42
|
+
*/
|
|
43
|
+
isSafari: isSafari(userAgent),
|
|
44
|
+
/**
|
|
45
|
+
* Indicates the the application is running in iOS.
|
|
46
|
+
*
|
|
47
|
+
* @static
|
|
48
|
+
* @type {Boolean}
|
|
49
|
+
*/
|
|
50
|
+
isiOS: isiOS(userAgent),
|
|
51
|
+
/**
|
|
52
|
+
* Indicates that the application is running on Android mobile device.
|
|
53
|
+
*
|
|
54
|
+
* @static
|
|
55
|
+
* @type {Boolean}
|
|
56
|
+
*/
|
|
57
|
+
isAndroid: isAndroid(userAgent),
|
|
58
|
+
/**
|
|
59
|
+
* Indicates that the application is running in a browser using the Blink engine.
|
|
60
|
+
*
|
|
61
|
+
* @static
|
|
62
|
+
* @type {Boolean}
|
|
63
|
+
*/
|
|
64
|
+
isBlink: isBlink(userAgent),
|
|
65
|
+
/**
|
|
66
|
+
* Environment features information.
|
|
67
|
+
*
|
|
68
|
+
* @memberOf module:utils/env~env
|
|
69
|
+
* @namespace
|
|
70
|
+
*/
|
|
71
|
+
features: {
|
|
72
|
+
/**
|
|
73
|
+
* Indicates that the environment supports ES2018 Unicode property escapes — like `\p{P}` or `\p{L}`.
|
|
74
|
+
* More information about unicode properties might be found
|
|
75
|
+
* [in Unicode Standard Annex #44](https://www.unicode.org/reports/tr44/#GC_Values_Table).
|
|
76
|
+
*
|
|
77
|
+
* @type {Boolean}
|
|
78
|
+
*/
|
|
79
|
+
isRegExpUnicodePropertySupported: isRegExpUnicodePropertySupported()
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
export default env;
|
|
83
|
+
/**
|
|
84
|
+
* Checks if User Agent represented by the string is running on Macintosh.
|
|
85
|
+
*
|
|
86
|
+
* @param {String} userAgent **Lowercase** `navigator.userAgent` string.
|
|
87
|
+
* @returns {Boolean} Whether User Agent is running on Macintosh or not.
|
|
88
|
+
*/
|
|
89
|
+
export function isMac(userAgent) {
|
|
90
|
+
return userAgent.indexOf('macintosh') > -1;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Checks if User Agent represented by the string is running on Windows.
|
|
94
|
+
*
|
|
95
|
+
* @param {String} userAgent **Lowercase** `navigator.userAgent` string.
|
|
96
|
+
* @returns {Boolean} Whether User Agent is running on Windows or not.
|
|
97
|
+
*/
|
|
98
|
+
export function isWindows(userAgent) {
|
|
99
|
+
return userAgent.indexOf('windows') > -1;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Checks if User Agent represented by the string is Firefox (Gecko).
|
|
103
|
+
*
|
|
104
|
+
* @param {String} userAgent **Lowercase** `navigator.userAgent` string.
|
|
105
|
+
* @returns {Boolean} Whether User Agent is Firefox or not.
|
|
106
|
+
*/
|
|
107
|
+
export function isGecko(userAgent) {
|
|
108
|
+
return !!userAgent.match(/gecko\/\d+/);
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Checks if User Agent represented by the string is Safari.
|
|
112
|
+
*
|
|
113
|
+
* @param {String} userAgent **Lowercase** `navigator.userAgent` string.
|
|
114
|
+
* @returns {Boolean} Whether User Agent is Safari or not.
|
|
115
|
+
*/
|
|
116
|
+
export function isSafari(userAgent) {
|
|
117
|
+
return userAgent.indexOf(' applewebkit/') > -1 && userAgent.indexOf('chrome') === -1;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Checks if User Agent represented by the string is running in iOS.
|
|
121
|
+
*
|
|
122
|
+
* @param {String} userAgent **Lowercase** `navigator.userAgent` string.
|
|
123
|
+
* @returns {Boolean} Whether User Agent is running in iOS or not.
|
|
124
|
+
*/
|
|
125
|
+
export function isiOS(userAgent) {
|
|
126
|
+
// "Request mobile site" || "Request desktop site".
|
|
127
|
+
return !!userAgent.match(/iphone|ipad/i) || (isMac(userAgent) && navigator.maxTouchPoints > 0);
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Checks if User Agent represented by the string is Android mobile device.
|
|
131
|
+
*
|
|
132
|
+
* @param {String} userAgent **Lowercase** `navigator.userAgent` string.
|
|
133
|
+
* @returns {Boolean} Whether User Agent is Safari or not.
|
|
134
|
+
*/
|
|
135
|
+
export function isAndroid(userAgent) {
|
|
136
|
+
return userAgent.indexOf('android') > -1;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Checks if User Agent represented by the string is Blink engine.
|
|
140
|
+
*
|
|
141
|
+
* @param {String} userAgent **Lowercase** `navigator.userAgent` string.
|
|
142
|
+
* @returns {Boolean} Whether User Agent is Blink engine or not.
|
|
143
|
+
*/
|
|
144
|
+
export function isBlink(userAgent) {
|
|
145
|
+
// The Edge browser before switching to the Blink engine used to report itself as Chrome (and "Edge/")
|
|
146
|
+
// but after switching to the Blink it replaced "Edge/" with "Edg/".
|
|
147
|
+
return userAgent.indexOf('chrome/') > -1 && userAgent.indexOf('edge/') < 0;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Checks if the current environment supports ES2018 Unicode properties like `\p{P}` or `\p{L}`.
|
|
151
|
+
* More information about unicode properties might be found
|
|
152
|
+
* [in Unicode Standard Annex #44](https://www.unicode.org/reports/tr44/#GC_Values_Table).
|
|
153
|
+
*
|
|
154
|
+
* @returns {Boolean}
|
|
155
|
+
*/
|
|
156
|
+
export function isRegExpUnicodePropertySupported() {
|
|
157
|
+
let isSupported = false;
|
|
158
|
+
// Feature detection for Unicode properties. Added in ES2018. Currently Firefox does not support it.
|
|
159
|
+
// See https://github.com/ckeditor/ckeditor5-mention/issues/44#issuecomment-487002174.
|
|
160
|
+
try {
|
|
161
|
+
// Usage of regular expression literal cause error during build (ckeditor/ckeditor5-dev#534).
|
|
162
|
+
isSupported = 'ć'.search(new RegExp('[\\p{L}]', 'u')) === 0;
|
|
163
|
+
}
|
|
164
|
+
catch (error) {
|
|
165
|
+
// Firefox throws a SyntaxError when the group is unsupported.
|
|
166
|
+
}
|
|
167
|
+
return isSupported;
|
|
168
|
+
}
|
package/src/eventinfo.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* @module utils/eventinfo
|
|
7
|
+
*/
|
|
8
|
+
import spy from './spy';
|
|
9
|
+
/**
|
|
10
|
+
* The event object passed to event callbacks. It is used to provide information about the event as well as a tool to
|
|
11
|
+
* manipulate it.
|
|
12
|
+
*/
|
|
13
|
+
export default class EventInfo {
|
|
14
|
+
/**
|
|
15
|
+
* @param {Object} source The emitter.
|
|
16
|
+
* @param {String} name The event name.
|
|
17
|
+
*/
|
|
18
|
+
constructor(source, name) {
|
|
19
|
+
this.source = source;
|
|
20
|
+
this.name = name;
|
|
21
|
+
this.path = [];
|
|
22
|
+
// The following methods are defined in the constructor because they must be re-created per instance.
|
|
23
|
+
this.stop = spy();
|
|
24
|
+
this.off = spy();
|
|
25
|
+
}
|
|
26
|
+
}
|