@@ -24,6 +24,7 @@ import eventsEngine from 'common/core/events/core/events_engine';
2424import ariaAccessibilityTestHelper from '../../../helpers/ariaAccessibilityTestHelper.js' ;
2525
2626const LIST_ITEM_CLASS = 'dx-list-item' ;
27+ const LIST_ITEM_CONTENT_CLASS = 'dx-list-item-content' ;
2728const LIST_ITEMS_CLASS = 'dx-list-items' ;
2829const LIST_GROUP_CLASS = 'dx-list-group' ;
2930const LIST_GROUP_HEADER_CLASS = 'dx-list-group-header' ;
@@ -1187,7 +1188,7 @@ QUnit.module('options changed', moduleSetup, () => {
11871188 list . off ( 'itemSwipe' ) ;
11881189 swipeItem ( ) ;
11891190
1190- list . on ( 'itemSwipe' , swipeHandler ) ;
1191+ list . on ( { 'itemSwipe' : swipeHandler } ) ;
11911192 swipeItem ( ) ;
11921193 } ) ;
11931194
@@ -4791,67 +4792,97 @@ QUnit.module('Search', () => {
47914792 } ) ;
47924793} ) ;
47934794
4794- QUnit . module ( 'Highlighting' , ( ) => {
4795+ QUnit . module ( 'Highlighting/selecting' , moduleSetup , ( ) => {
47954796
47964797 const selectTextNodePart = ( textNode , startOffset , endOffset ) => {
47974798 const selection = window . getSelection ( ) ;
47984799 const range = document . createRange ( ) ;
4799-
48004800 selection . removeAllRanges ( ) ;
48014801 range . setStart ( textNode , startOffset ) ;
48024802 range . setEnd ( textNode , endOffset ) ;
48034803 selection . addRange ( range ) ;
4804-
48054804 return selection ;
48064805 } ;
48074806
4808- QUnit . test ( 'list item should not block native text selection events' , function ( assert ) {
4807+ const getFirstListItemAndTextNode = ( $list ) => {
4808+ const $item = $list . find ( `.${ LIST_ITEM_CLASS } ` ) . eq ( 0 ) ;
4809+ const textNode = $list . find ( `.${ LIST_ITEM_CONTENT_CLASS } ` ) . eq ( 0 ) . get ( 0 ) . firstChild ;
4810+
4811+ return { $item, textNode } ;
4812+ } ;
4813+
4814+ QUnit . test ( 'text selection should not be cleared when dragging on list item without onItemSwipe' , function ( assert ) {
48094815 const $list = $ ( '#list' ) . dxList ( {
4810- items : [ 'Item 1' , 'Item 2' ]
4816+ items : [ 'Item 1' , 'Item 2' ] ,
48114817 } ) ;
48124818
4813- const item = $list . find ( `. ${ LIST_ITEM_CLASS } ` ) . eq ( 0 ) . get ( 0 ) ;
4819+ const { $ item, textNode } = getFirstListItemAndTextNode ( $list ) ;
48144820
4815- const mouseDownEvent = new MouseEvent ( 'mousedown' , {
4816- bubbles : true ,
4817- cancelable : true ,
4818- clientX : 10 ,
4819- clientY : 10 ,
4820- } ) ;
4821+ assert . ok ( ! ! textNode , 'text node found in list item' ) ;
4822+ if ( ! textNode ) return ;
48214823
4822- const selectStartEvent = new Event ( 'selectstart' , {
4823- bubbles : true ,
4824- cancelable : true ,
4824+ selectTextNodePart ( textNode , 0 , 4 ) ;
4825+ assert . strictEqual ( window . getSelection ( ) . toString ( ) , textNode . nodeValue . slice ( 0 , 4 ) , 'text selection exists before drag' ) ;
4826+
4827+ pointerMock ( $item ) . start ( ) . down ( 0 , 0 ) . move ( 50 , 0 ) . up ( ) ;
4828+
4829+ assert . strictEqual ( window . getSelection ( ) . toString ( ) , textNode . nodeValue . slice ( 0 , 4 ) , 'text selection is preserved after horizontal drag' ) ;
4830+ window . getSelection ( ) . removeAllRanges ( ) ;
4831+ } ) ;
4832+
4833+ QUnit . test ( 'text selection should be preserved after onItemSwipe handler is removed from options' , function ( assert ) {
4834+ const $list = $ ( '#list' ) . dxList ( {
4835+ items : [ 'Item 1' , 'Item 2' ] ,
4836+ onItemSwipe : sinon . spy ( ) ,
48254837 } ) ;
4838+ const list = $list . dxList ( 'instance' ) ;
48264839
4827- const mouseDownNotCanceled = item . dispatchEvent ( mouseDownEvent ) ;
4828- const selectStartNotCanceled = item . dispatchEvent ( selectStartEvent ) ;
4840+ list . option ( 'onItemSwipe' , null ) ;
48294841
4830- assert . ok ( mouseDownNotCanceled , 'mousedown is not canceled' ) ;
4831- assert . notOk ( mouseDownEvent . defaultPrevented , 'mousedown default is not prevented' ) ;
4832- assert . ok ( selectStartNotCanceled , 'selectstart is not canceled' ) ;
4833- assert . notOk ( selectStartEvent . defaultPrevented , 'selectstart default is not prevented' ) ;
4842+ const { $item, textNode } = getFirstListItemAndTextNode ( $list ) ;
4843+ assert . ok ( ! ! textNode , 'text node found in list item' ) ;
4844+ if ( ! textNode ) return ;
4845+
4846+ selectTextNodePart ( textNode , 0 , 4 ) ;
4847+ assert . strictEqual ( window . getSelection ( ) . toString ( ) , textNode . nodeValue . slice ( 0 , 4 ) , 'text selection exists before drag' ) ;
4848+
4849+ pointerMock ( $item ) . start ( ) . down ( 0 , 0 ) . move ( 50 , 0 ) . up ( ) ;
4850+
4851+ assert . strictEqual ( window . getSelection ( ) . toString ( ) , textNode . nodeValue . slice ( 0 , 4 ) , 'text selection is preserved after drag when swipe handler is removed' ) ;
4852+ window . getSelection ( ) . removeAllRanges ( ) ;
48344853 } ) ;
48354854
4836- QUnit . test ( 'text node should be selectable via Selection API ' , function ( assert ) {
4855+ QUnit . test ( 'text selection should reflect itemSwipe on/off subscription state ' , function ( assert ) {
48374856 const $list = $ ( '#list' ) . dxList ( {
4838- items : [ 'Item 1' , 'Item 2' ]
4857+ items : [ 'Item 1' , 'Item 2' ] ,
48394858 } ) ;
48404859
4841- const $item = $list . find ( '.dx-list-item-content' ) . eq ( 0 ) ;
4842- const textNode = $item . get ( 0 ) . firstChild ;
4860+ const list = $list . dxList ( 'instance' ) ;
48434861
4844- assert . ok ( ! ! textNode , 'text node exists' ) ;
4862+ const { $item , textNode } = getFirstListItemAndTextNode ( $list ) ;
48454863
4846- if ( ! textNode ) {
4847- return ;
4848- }
4864+ assert . ok ( ! ! textNode , 'text node found in list item' ) ;
48494865
4850- const expected = textNode . textContent . slice ( 0 , 4 ) ;
4851- const selection = selectTextNodePart ( textNode , 0 , 4 ) ;
4866+ if ( ! textNode ) return ;
48524867
4853- assert . strictEqual ( selection . toString ( ) , expected , 'text is selected' ) ;
4854- selection . removeAllRanges ( ) ;
4868+ list . on ( 'itemSwipe' , sinon . spy ( ) ) ;
4869+
4870+ selectTextNodePart ( textNode , 0 , 4 ) ;
4871+ assert . strictEqual ( window . getSelection ( ) . toString ( ) , textNode . nodeValue . slice ( 0 , 4 ) , 'text selection exists before drag with subscribed swipe handler' ) ;
4872+
4873+ pointerMock ( $item ) . start ( ) . down ( 0 , 0 ) . move ( 50 , 0 ) . up ( ) ;
4874+
4875+ assert . strictEqual ( window . getSelection ( ) . toString ( ) , '' , 'text selection is cleared while swipe handler is attached' ) ;
4876+
4877+ list . off ( 'itemSwipe' ) ;
4878+
4879+ selectTextNodePart ( textNode , 0 , 4 ) ;
4880+ assert . strictEqual ( window . getSelection ( ) . toString ( ) , textNode . nodeValue . slice ( 0 , 4 ) , 'text selection exists before drag' ) ;
4881+
4882+ pointerMock ( $item ) . start ( ) . down ( 0 , 0 ) . move ( 50 , 0 ) . up ( ) ;
4883+
4884+ assert . strictEqual ( window . getSelection ( ) . toString ( ) , textNode . nodeValue . slice ( 0 , 4 ) , 'text selection is preserved after drag when swipe handler is removed' ) ;
4885+ window . getSelection ( ) . removeAllRanges ( ) ;
48554886 } ) ;
48564887} ) ;
48574888
0 commit comments