domemittermixin.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global document, Event, MouseEvent */
  6. /* bender-tags: ui */
  7. 'use strict';
  8. import testUtils from '/tests/ckeditor5/_utils/utils.js';
  9. import extend from '/ckeditor5/utils/lib/lodash/extend.js';
  10. import DOMEmitterMixin from '/ckeditor5/ui/domemittermixin.js';
  11. import EmitterMixin from '/ckeditor5/utils/emittermixin.js';
  12. let emitter, domEmitter, node;
  13. testUtils.createSinonSandbox();
  14. const getEmitterInstance = () => extend( {}, EmitterMixin );
  15. const getDOMEmitterInstance = () => extend( {}, DOMEmitterMixin );
  16. const getDOMNodeInstance = () => document.createElement( 'div' );
  17. function updateEmitterInstance() {
  18. emitter = getEmitterInstance();
  19. }
  20. function updateDOMEmitterInstance() {
  21. domEmitter = getDOMEmitterInstance();
  22. }
  23. function updateDOMNodeInstance() {
  24. node = getDOMNodeInstance();
  25. }
  26. describe( 'DOMEmitterMixin', () => {
  27. beforeEach( () => {
  28. updateEmitterInstance();
  29. updateDOMEmitterInstance();
  30. updateDOMNodeInstance();
  31. } );
  32. describe( 'listenTo', () => {
  33. it( 'should listen to EmitterMixin events', () => {
  34. const spy = testUtils.sinon.spy();
  35. domEmitter.listenTo( emitter, 'test', spy );
  36. emitter.fire( 'test' );
  37. sinon.assert.calledOnce( spy );
  38. } );
  39. it( 'should listen to native DOM events', () => {
  40. const spy = testUtils.sinon.spy();
  41. domEmitter.listenTo( node, 'test', spy );
  42. node.dispatchEvent( new Event( 'test' ) );
  43. sinon.assert.calledOnce( spy );
  44. } );
  45. // #187
  46. it( 'should work for DOM Nodes belonging to another window', () => {
  47. const spy = testUtils.sinon.spy();
  48. const iframe = document.createElement( 'iframe' );
  49. document.body.appendChild( iframe );
  50. const iframeNode = iframe.contentWindow.document.createElement( 'div' );
  51. domEmitter.listenTo( iframeNode, 'test', spy );
  52. iframeNode.dispatchEvent( new Event( 'test' ) );
  53. sinon.assert.calledOnce( spy );
  54. } );
  55. } );
  56. describe( 'stopListening', () => {
  57. it( 'should stop listening to a specific event callback', () => {
  58. const spy1 = testUtils.sinon.spy();
  59. const spy2 = testUtils.sinon.spy();
  60. domEmitter.listenTo( node, 'event1', spy1 );
  61. domEmitter.listenTo( node, 'event2', spy2 );
  62. node.dispatchEvent( new Event( 'event1' ) );
  63. node.dispatchEvent( new Event( 'event2' ) );
  64. domEmitter.stopListening( node, 'event1', spy1 );
  65. node.dispatchEvent( new Event( 'event1' ) );
  66. node.dispatchEvent( new Event( 'event2' ) );
  67. sinon.assert.calledOnce( spy1 );
  68. sinon.assert.calledTwice( spy2 );
  69. } );
  70. it( 'should stop listening to an specific event', () => {
  71. const spy1a = testUtils.sinon.spy();
  72. const spy1b = testUtils.sinon.spy();
  73. const spy2 = testUtils.sinon.spy();
  74. domEmitter.listenTo( node, 'event1', spy1a );
  75. domEmitter.listenTo( node, 'event1', spy1b );
  76. domEmitter.listenTo( node, 'event2', spy2 );
  77. node.dispatchEvent( new Event( 'event1' ) );
  78. node.dispatchEvent( new Event( 'event2' ) );
  79. sinon.assert.calledOnce( spy1a );
  80. sinon.assert.calledOnce( spy1b );
  81. sinon.assert.calledOnce( spy2 );
  82. domEmitter.stopListening( node, 'event1' );
  83. node.dispatchEvent( new Event( 'event1' ) );
  84. node.dispatchEvent( new Event( 'event2' ) );
  85. sinon.assert.calledOnce( spy1a );
  86. sinon.assert.calledOnce( spy1b );
  87. sinon.assert.calledTwice( spy2 );
  88. } );
  89. it( 'should stop listening to all events from a specific node', () => {
  90. const spy1 = testUtils.sinon.spy();
  91. const spy2 = testUtils.sinon.spy();
  92. domEmitter.listenTo( node, 'event1', spy1 );
  93. domEmitter.listenTo( node, 'event2', spy2 );
  94. node.dispatchEvent( new Event( 'event1' ) );
  95. node.dispatchEvent( new Event( 'event2' ) );
  96. domEmitter.stopListening( node );
  97. node.dispatchEvent( new Event( 'event1' ) );
  98. node.dispatchEvent( new Event( 'event2' ) );
  99. sinon.assert.calledOnce( spy1 );
  100. sinon.assert.calledOnce( spy2 );
  101. } );
  102. it( 'should stop listening to everything', () => {
  103. const spy1 = testUtils.sinon.spy();
  104. const spy2 = testUtils.sinon.spy();
  105. const node1 = getDOMNodeInstance();
  106. const node2 = getDOMNodeInstance();
  107. domEmitter.listenTo( node1, 'event1', spy1 );
  108. domEmitter.listenTo( node2, 'event2', spy2 );
  109. expect( domEmitter ).to.have.property( '_listeningTo' );
  110. node1.dispatchEvent( new Event( 'event1' ) );
  111. node2.dispatchEvent( new Event( 'event2' ) );
  112. domEmitter.stopListening();
  113. node1.dispatchEvent( new Event( 'event1' ) );
  114. node2.dispatchEvent( new Event( 'event2' ) );
  115. sinon.assert.calledOnce( spy1 );
  116. sinon.assert.calledOnce( spy2 );
  117. expect( domEmitter ).to.not.have.property( '_listeningTo' );
  118. } );
  119. it( 'should not stop other nodes when a non-listened node is provided', () => {
  120. const spy = testUtils.sinon.spy();
  121. const node1 = getDOMNodeInstance();
  122. const node2 = getDOMNodeInstance();
  123. domEmitter.listenTo( node1, 'test', spy );
  124. domEmitter.stopListening( node2 );
  125. node1.dispatchEvent( new Event( 'test' ) );
  126. sinon.assert.called( spy );
  127. } );
  128. it( 'should pass DOM Event data to the listener', () => {
  129. const spy = testUtils.sinon.spy();
  130. const node = getDOMNodeInstance();
  131. domEmitter.listenTo( node, 'click', spy );
  132. const mouseEvent = new MouseEvent( 'click', {
  133. screenX: 10,
  134. screenY: 20
  135. } );
  136. node.dispatchEvent( mouseEvent );
  137. sinon.assert.calledOnce( spy );
  138. expect( spy.args[ 0 ][ 1 ] ).to.be.equal( mouseEvent );
  139. } );
  140. it( 'should detach native DOM event listener proxy, specific event', () => {
  141. const spy1a = testUtils.sinon.spy();
  142. const spy1b = testUtils.sinon.spy();
  143. domEmitter.listenTo( node, 'test', spy1a );
  144. const proxyEmitter = domEmitter._getProxyEmitter( node );
  145. const spy2 = testUtils.sinon.spy( proxyEmitter, 'fire' );
  146. node.dispatchEvent( new Event( 'test' ) );
  147. sinon.assert.calledOnce( spy1a );
  148. sinon.assert.calledOnce( spy2 );
  149. domEmitter.stopListening( node, 'test' );
  150. node.dispatchEvent( new Event( 'test' ) );
  151. sinon.assert.calledOnce( spy1a );
  152. sinon.assert.calledOnce( spy2 );
  153. // Attach same event again.
  154. domEmitter.listenTo( node, 'test', spy1b );
  155. node.dispatchEvent( new Event( 'test' ) );
  156. expect( proxyEmitter ).to.be.equal( domEmitter._getProxyEmitter( node ) );
  157. sinon.assert.calledOnce( spy1a );
  158. sinon.assert.calledOnce( spy1b );
  159. sinon.assert.calledTwice( spy2 );
  160. } );
  161. it( 'should detach native DOM event listener proxy, specific callback', () => {
  162. const spy1a = testUtils.sinon.spy();
  163. const spy1b = testUtils.sinon.spy();
  164. const spy1c = testUtils.sinon.spy();
  165. domEmitter.listenTo( node, 'test', spy1a );
  166. domEmitter.listenTo( node, 'test', spy1b );
  167. const proxyEmitter = domEmitter._getProxyEmitter( node );
  168. const spy2 = testUtils.sinon.spy( proxyEmitter, 'fire' );
  169. node.dispatchEvent( new Event( 'test' ) );
  170. sinon.assert.calledOnce( spy1a );
  171. sinon.assert.calledOnce( spy1b );
  172. sinon.assert.calledOnce( spy2 );
  173. domEmitter.stopListening( node, 'test', spy1a );
  174. node.dispatchEvent( new Event( 'test' ) );
  175. sinon.assert.calledOnce( spy1a );
  176. sinon.assert.calledTwice( spy1b );
  177. sinon.assert.calledTwice( spy2 );
  178. domEmitter.stopListening( node, 'test', spy1b );
  179. node.dispatchEvent( new Event( 'test' ) );
  180. sinon.assert.calledOnce( spy1a );
  181. sinon.assert.calledTwice( spy1b );
  182. sinon.assert.calledTwice( spy2 );
  183. // Attach same event again.
  184. domEmitter.listenTo( node, 'test', spy1c );
  185. node.dispatchEvent( new Event( 'test' ) );
  186. expect( proxyEmitter ).to.be.equal( domEmitter._getProxyEmitter( node ) );
  187. sinon.assert.calledOnce( spy1a );
  188. sinon.assert.calledTwice( spy1b );
  189. sinon.assert.calledOnce( spy1c );
  190. sinon.assert.calledThrice( spy2 );
  191. } );
  192. it( 'should detach native DOM event listener proxy, specific emitter', () => {
  193. const spy1a = testUtils.sinon.spy();
  194. const spy1b = testUtils.sinon.spy();
  195. const spy1c = testUtils.sinon.spy();
  196. const spy1d = testUtils.sinon.spy();
  197. domEmitter.listenTo( node, 'test1', spy1a );
  198. domEmitter.listenTo( node, 'test2', spy1b );
  199. const proxyEmitter = domEmitter._getProxyEmitter( node );
  200. const spy2 = testUtils.sinon.spy( proxyEmitter, 'fire' );
  201. node.dispatchEvent( new Event( 'test1' ) );
  202. node.dispatchEvent( new Event( 'test2' ) );
  203. sinon.assert.calledOnce( spy1a );
  204. sinon.assert.calledOnce( spy1b );
  205. sinon.assert.calledTwice( spy2 );
  206. domEmitter.stopListening( node );
  207. node.dispatchEvent( new Event( 'test1' ) );
  208. node.dispatchEvent( new Event( 'test2' ) );
  209. sinon.assert.calledOnce( spy1a );
  210. sinon.assert.calledOnce( spy1b );
  211. sinon.assert.calledTwice( spy2 );
  212. // Attach same event again.
  213. domEmitter.listenTo( node, 'test1', spy1c );
  214. domEmitter.listenTo( node, 'test2', spy1d );
  215. // Old proxy emitter died when stopped listening to the node.
  216. const proxyEmitter2 = domEmitter._getProxyEmitter( node );
  217. const spy3 = testUtils.sinon.spy( proxyEmitter2, 'fire' );
  218. node.dispatchEvent( new Event( 'test1' ) );
  219. node.dispatchEvent( new Event( 'test2' ) );
  220. expect( proxyEmitter ).to.not.be.equal( proxyEmitter2 );
  221. sinon.assert.calledOnce( spy1a );
  222. sinon.assert.calledOnce( spy1b );
  223. sinon.assert.calledOnce( spy1c );
  224. sinon.assert.calledOnce( spy1d );
  225. sinon.assert.calledTwice( spy2 );
  226. sinon.assert.calledTwice( spy3 );
  227. } );
  228. it( 'should detach native DOM event listener proxy, everything', () => {
  229. const spy1a = testUtils.sinon.spy();
  230. const spy1b = testUtils.sinon.spy();
  231. const spy1c = testUtils.sinon.spy();
  232. const spy1d = testUtils.sinon.spy();
  233. domEmitter.listenTo( node, 'test1', spy1a );
  234. domEmitter.listenTo( node, 'test2', spy1b );
  235. const proxyEmitter = domEmitter._getProxyEmitter( node );
  236. const spy2 = testUtils.sinon.spy( proxyEmitter, 'fire' );
  237. node.dispatchEvent( new Event( 'test1' ) );
  238. node.dispatchEvent( new Event( 'test2' ) );
  239. sinon.assert.calledOnce( spy1a );
  240. sinon.assert.calledOnce( spy1b );
  241. sinon.assert.calledTwice( spy2 );
  242. domEmitter.stopListening();
  243. node.dispatchEvent( new Event( 'test1' ) );
  244. node.dispatchEvent( new Event( 'test2' ) );
  245. sinon.assert.calledOnce( spy1a );
  246. sinon.assert.calledOnce( spy1b );
  247. sinon.assert.calledTwice( spy2 );
  248. // Attach same event again.
  249. domEmitter.listenTo( node, 'test1', spy1c );
  250. domEmitter.listenTo( node, 'test2', spy1d );
  251. // Old proxy emitter died when stopped listening to the node.
  252. const proxyEmitter2 = domEmitter._getProxyEmitter( node );
  253. const spy3 = testUtils.sinon.spy( proxyEmitter2, 'fire' );
  254. node.dispatchEvent( new Event( 'test1' ) );
  255. node.dispatchEvent( new Event( 'test2' ) );
  256. expect( proxyEmitter ).to.not.be.equal( proxyEmitter2 );
  257. sinon.assert.calledOnce( spy1a );
  258. sinon.assert.calledOnce( spy1b );
  259. sinon.assert.calledOnce( spy1c );
  260. sinon.assert.calledOnce( spy1d );
  261. sinon.assert.calledTwice( spy2 );
  262. sinon.assert.calledTwice( spy3 );
  263. } );
  264. // #187
  265. it( 'should work for DOM Nodes belonging to another window', () => {
  266. const spy = testUtils.sinon.spy();
  267. const iframe = document.createElement( 'iframe' );
  268. document.body.appendChild( iframe );
  269. const iframeNode = iframe.contentWindow.document.createElement( 'div' );
  270. domEmitter.listenTo( iframeNode, 'test', spy );
  271. iframeNode.dispatchEvent( new Event( 'test' ) );
  272. domEmitter.stopListening( iframeNode );
  273. iframeNode.dispatchEvent( new Event( 'test' ) );
  274. sinon.assert.calledOnce( spy );
  275. } );
  276. } );
  277. } );