Debian Codegraph @

tests/qunit/resources/jquery.textSelection.test.js

raw symbols SCIP
1 QUnit.module( 'jquery.textSelection', () => {
2 const sig = {
3 pre: '--~~~~'
4 };
5 const bold = {
6 pre: '\'\'\'',
7 peri: 'Bold text',
8 post: '\'\'\''
9 };
10 const h2 = {
11 pre: '== ',
12 peri: 'Heading 2',
13 post: ' ==',
14 regex: /^(\s*)(={1,6})(.*?)\2(\s*)$/,
15 regexReplace: '$1==$3==$4',
16 ownline: true
17 };
18 const ulist = {
19 pre: '* ',
20 peri: 'Bulleted list item',
21 post: '',
22 ownline: true,
23 splitlines: true
24 };
25
26 QUnit.test.each( 'encapsulateText', {
27 'Adding sig to end of text': {
28 before: {
29 text: 'Wikilove dude! ',
30 start: 15,
31 end: 15
32 },
33 after: {
34 text: 'Wikilove dude! --~~~~',
35 selected: ''
36 },
37 replace: sig
38 },
39 'Adding bold to empty': {
40 before: {
41 text: '',
42 start: 0,
43 end: 0
44 },
45 after: {
46 text: '\'\'\'Bold text\'\'\'',
47 selected: 'Bold text' // selected because it's the default
48 },
49 replace: bold
50 },
51 'Adding bold to existing text': {
52 before: {
53 text: 'Now is the time for all good men to come to the aid of their country',
54 start: 20,
55 end: 32
56 },
57 after: {
58 text: 'Now is the time for \'\'\'all good men\'\'\' to come to the aid of their country',
59 selected: '' // empty because it's not the default'
60 },
61 replace: bold
62 },
63 'ownline option: adding new h2': {
64 before: {
65 text: 'Before\nAfter',
66 start: 7,
67 end: 7
68 },
69 after: {
70 text: 'Before\n== Heading 2 ==\nAfter',
71 selected: 'Heading 2'
72 },
73 replace: h2
74 },
75 'ownline option: turn a whole line into new h2': {
76 before: {
77 text: 'Before\nMy heading\nAfter',
78 start: 7,
79 end: 17
80 },
81 after: {
82 text: 'Before\n== My heading ==\nAfter',
83 selected: ''
84 },
85 replace: h2
86 },
87 'ownline option: turn a partial line into new h2': {
88 before: {
89 text: 'BeforeMy headingAfter',
90 start: 6,
91 end: 16
92 },
93 after: {
94 text: 'Before\n== My heading ==\nAfter',
95 selected: ''
96 },
97 replace: h2
98 },
99 'splitlines option: no selection, insert new list item': {
100 before: {
101 text: 'Before\nAfter',
102 start: 7,
103 end: 7
104 },
105 after: {
106 text: 'Before\n* Bulleted list item\nAfter'
107 },
108 replace: ulist
109 },
110 'splitlines option: single partial line selection, insert new list item': {
111 before: {
112 text: 'BeforeMy List ItemAfter',
113 start: 6,
114 end: 18
115 },
116 after: {
117 text: 'Before\n* My List Item\nAfter'
118 },
119 replace: ulist
120 },
121 'splitlines option: multiple lines': {
122 before: {
123 text: 'Before\nFirst\nSecond\nThird\nAfter',
124 start: 7,
125 end: 25
126 },
127 after: {
128 text: 'Before\n* First\n* Second\n* Third\nAfter'
129 },
130 replace: ulist
131 }
132 }, ( assert, opt ) => {
133 const $textarea = $( '<textarea>' ).appendTo( '#qunit-fixture' );
134 $textarea.textSelection( 'setContents', opt.before.text );
135 const replace = Object.assign( {
136 selectionStart: opt.before.start,
137 selectionEnd: opt.before.end
138 }, opt.replace );
139 $textarea.textSelection( 'encapsulateSelection', replace );
140
141 const text = $textarea.textSelection( 'getContents' ).replace( /\r\n/g, '\n' );
142 assert.strictEqual( text, opt.after.text, 'after encapsulation' );
143
144 if ( opt.after.selected !== undefined ) {
145 const selected = $textarea.textSelection( 'getSelection' );
146 assert.strictEqual( selected, opt.after.selected, 'selected text' );
147 }
148 } );
149
150 const caretSample = 'Some big text that we like to work with. Nothing fancy... you know what I mean?';
151
152 // Default/empty selection, T36820, T33847
153 QUnit.test( 'getCaretPosition [initial]', ( assert ) => {
154 const $textarea = $( '<textarea>' ).text( caretSample ).appendTo( '#qunit-fixture' );
155 const pos = $textarea.textSelection( 'getCaretPosition', { startAndEnd: true } );
156 assert.strictEqual( pos[ 0 ], 0, 'default start' );
157 assert.strictEqual( pos[ 1 ], 0, 'default end' );
158 } );
159
160 QUnit.test.each( 'getCaretPosition', {
161 'forced empty selection': {
162 start: 7,
163 end: 7
164 },
165 'small selection': {
166 start: 6,
167 end: 11
168 }
169 }, ( assert, options ) => {
170 const $textarea = $( '<textarea>' ).text( caretSample ).appendTo( '#qunit-fixture' );
171
172 $textarea.textSelection( 'setSelection', {
173 start: options.start,
174 end: options.end
175 } );
176
177 const pos = $textarea.textSelection( 'getCaretPosition', { startAndEnd: true } );
178 assert.strictEqual( pos[ 0 ], options.start, 'Caret start should be where we set it.' );
179 assert.strictEqual( pos[ 1 ], options.end, 'Caret end should be where we set it.' );
180 } );
181 } );