How to use rng method in fast-check-monorepo

Best JavaScript code snippet using fast-check-monorepo

Selection.js

Source:Selection.js Github

copy

Full Screen

1ModuleLoader.require([2 "tinymce/caret/CaretContainer",3 "tinymce/text/Zwsp",4 "tinymce/Env"5], function(CaretContainer, Zwsp, Env) {6 module("tinymce.dom.Selection", {7 setupModule: function() {8 QUnit.stop();9 tinymce.init({10 selector: "textarea",11 add_unload_trigger: false,12 skin: false,13 forced_root_block: '',14 entities: 'raw',15 valid_styles: {16 '*': 'color,font-size,font-family,background-color,font-weight,font-style,text-decoration,float,margin,margin-top,margin-right,margin-bottom,margin-left,display'17 },18 custom_elements: 'custom1,~custom2',19 extended_valid_elements: 'custom1,custom2',20 init_instance_callback: function(ed) {21 window.editor = ed;22 QUnit.start();23 }24 });25 }26 });27 test('getContent', function() {28 var rng, eventObj;29 // Get selected contents30 editor.setContent('<p>text</p>');31 rng = editor.dom.createRng();32 rng.setStart(editor.getBody(), 0);33 rng.setEnd(editor.getBody(), 1);34 editor.selection.setRng(rng);35 equal(editor.selection.getContent(), '<p>text</p>', 'Get selected contents');36 // Get selected contents (collapsed)37 editor.setContent('<p>text</p>');38 rng = editor.dom.createRng();39 rng.setStart(editor.getBody(), 0);40 rng.setEnd(editor.getBody(), 0);41 editor.selection.setRng(rng);42 equal(editor.selection.getContent(), '', 'Get selected contents (collapsed)');43 // Get selected contents, onGetContent event44 eventObj = {};45 function handler(event) {46 eventObj = event;47 }48 editor.on('GetContent', handler);49 editor.setContent('<p>text</p>');50 rng = editor.dom.createRng();51 rng.setStart(editor.getBody(), 0);52 rng.setEnd(editor.getBody(), 1);53 editor.selection.setRng(rng);54 editor.selection.getContent();55 equal(eventObj.content, '<p>text</p>', 'Get selected contents, onGetContent event');56 editor.off('GetContent', handler);57 });58 test('setContent', function() {59 var rng, eventObj;60 // Set contents at selection61 editor.setContent('<p>text</p>');62 rng = editor.dom.createRng();63 rng.setStart(editor.getBody(), 0);64 rng.setEnd(editor.getBody(), 1);65 editor.selection.setRng(rng);66 editor.selection.setContent('<div>test</div>');67 equal(editor.getContent(), '<div>test</div>', 'Set contents at selection');68 // Set contents at selection (collapsed)69 editor.setContent('<p>text</p>');70 rng = editor.dom.createRng();71 rng.setStart(editor.getBody(), 0);72 rng.setEnd(editor.getBody(), 0);73 editor.selection.setRng(rng);74 editor.selection.setContent('<div>test</div>');75 equal(editor.getContent(), '<div>test</div>\n<p>text</p>', 'Set contents at selection (collapsed)');76 // Insert in middle of paragraph77 editor.setContent('<p>beforeafter</p>');78 rng = editor.dom.createRng();79 rng.setStart(editor.getBody().firstChild.firstChild, 'before'.length);80 rng.setEnd(editor.getBody().firstChild.firstChild, 'before'.length);81 editor.selection.setRng(rng);82 editor.selection.setContent('<br />');83 equal(editor.getContent(), '<p>before<br />after</p>', 'Set contents at selection (inside paragraph)');84 // Check the caret is left in the correct position.85 rng = editor.selection.getRng(true);86 if (document.createRange) {87 equal(rng.startContainer, editor.getBody().firstChild, 'Selection start container');88 equal(rng.startOffset, 2, 'Selection start offset');89 equal(rng.endContainer, editor.getBody().firstChild, 'Selection end container');90 equal(rng.endOffset, 2, 'Selection end offset');91 } else {92 // TridentSelection resolves indexed text nodes93 equal(rng.startContainer, editor.getBody().firstChild.lastChild, 'Selection start container');94 equal(rng.startOffset, 0, 'Selection start offset');95 equal(rng.endContainer, editor.getBody().firstChild.lastChild, 'Selection end container');96 equal(rng.endOffset, 0, 'Selection end offset');97 }98 editor.setContent('<p>text</p>');99 rng = editor.dom.createRng();100 rng.setStart(editor.getBody(), 0);101 rng.setEnd(editor.getBody(), 0);102 editor.selection.setRng(rng);103 editor.selection.setContent('');104 equal(editor.getContent(), '<p>text</p>', 'Set contents to empty at selection (collapsed)');105 rng = editor.selection.getRng(true);106 if (!document.createRange) {107 // The old IE selection can only be positioned in text nodes108 equal(rng.startContainer, editor.getBody().firstChild.firstChild, 'Selection start container');109 equal(rng.startOffset, 0, 'Selection start offset');110 equal(rng.endContainer, editor.getBody().firstChild.firstChild, 'Selection end container');111 equal(rng.endOffset, 0, 'Selection end offset');112 } else {113 equal(rng.startContainer, editor.getBody(), 'Selection start container');114 equal(rng.startOffset, 0, 'Selection start offset');115 equal(rng.endContainer, editor.getBody(), 'Selection end container');116 equal(rng.endOffset, 0, 'Selection end offset');117 }118 // Set selected contents, onSetContent event119 eventObj = {};120 function handler(event) {121 eventObj = event;122 }123 editor.on('SetContent', handler);124 editor.setContent('<p>text</p>');125 rng = editor.dom.createRng();126 rng.setStart(editor.getBody(), 0);127 rng.setEnd(editor.getBody(), 1);128 editor.selection.setRng(rng);129 editor.selection.setContent('<div>text</div>');130 equal(eventObj.content, '<div>text</div>', 'Set selected contents, onSetContent event');131 editor.off('SetContent', handler);132 });133 test('getStart/getEnd', function() {134 var rng;135 // Selected contents136 editor.setContent('<p id="a">text</p><p id="b">text</p>');137 rng = editor.dom.createRng();138 rng.setStart(editor.getBody().firstChild.firstChild, 0);139 rng.setEnd(editor.getBody().lastChild.firstChild, 0);140 editor.selection.setRng(rng);141 equal(editor.selection.getStart().id, 'a', 'Selected contents (getStart)');142 equal(editor.selection.getEnd().id, 'b', 'Selected contents (getEnd)');143 // Selected contents (collapsed)144 editor.setContent('<p id="a">text</p>\n<p id="b">text</p>');145 rng = editor.dom.createRng();146 rng.setStart(editor.getBody().firstChild.firstChild, 0);147 rng.setEnd(editor.getBody().firstChild.firstChild, 0);148 editor.selection.setRng(rng);149 equal(editor.selection.getStart().id, 'a', 'Selected contents (getStart, collapsed)');150 equal(editor.selection.getEnd().id, 'a', 'Selected contents (getEnd, collapsed)');151 });152 test('getBookmark/setBookmark (persistent)', function() {153 var rng, bookmark;154 // Get persistent bookmark simple text selection155 editor.setContent('text');156 rng = editor.dom.createRng();157 rng.setStart(editor.getBody().firstChild, 1);158 rng.setEnd(editor.getBody().firstChild, 3);159 editor.selection.setRng(rng);160 bookmark = editor.selection.getBookmark();161 equal(editor.getContent(), 'text', 'Editor contents (text)');162 editor.selection.moveToBookmark(bookmark);163 equal(editor.selection.getContent(), 'ex', 'Selected contents (text)');164 // Get persistent bookmark multiple elements text selection165 editor.setContent('<p>text</p>\n<p>text</p>');166 rng = editor.dom.createRng();167 rng.setStart(editor.getBody().firstChild.firstChild, 1);168 rng.setEnd(editor.getBody().lastChild.firstChild, 3);169 editor.selection.setRng(rng);170 bookmark = editor.selection.getBookmark();171 equal(editor.getContent(), '<p>text</p>\n<p>text</p>', 'Editor contents (elements)');172 editor.selection.moveToBookmark(bookmark);173 equal(editor.selection.getContent(), '<p>ext</p>\n<p>tex</p>', 'Selected contents (elements)');174 });175 test('getBookmark/setBookmark (simple)', function() {176 var rng, bookmark;177 // Get persistent bookmark simple text selection178 editor.setContent('text');179 rng = editor.dom.createRng();180 rng.setStart(editor.getBody().firstChild, 1);181 rng.setEnd(editor.getBody().firstChild, 3);182 editor.selection.setRng(rng);183 bookmark = editor.selection.getBookmark(1);184 equal(editor.getContent(), 'text', 'Editor contents (text)');185 editor.selection.moveToBookmark(bookmark);186 equal(editor.selection.getContent(), 'ex', 'Selected contents (text)');187 // Get persistent bookmark multiple elements text selection188 editor.setContent('<p>text</p>\n<p>text</p>');189 rng = editor.dom.createRng();190 rng.setStart(editor.getBody().firstChild.firstChild, 1);191 rng.setEnd(editor.getBody().lastChild.firstChild, 3);192 editor.selection.setRng(rng);193 bookmark = editor.selection.getBookmark(1);194 equal(editor.getContent(), '<p>text</p>\n<p>text</p>', 'Editor contents (elements)');195 editor.selection.moveToBookmark(bookmark);196 equal(editor.selection.getContent(), '<p>ext</p>\n<p>tex</p>', 'Selected contents (elements)');197 });198 test('getBookmark/setBookmark (nonintrusive) - simple text selection', function() {199 var rng, bookmark;200 expect(2);201 editor.setContent('text');202 rng = editor.dom.createRng();203 rng.setStart(editor.getBody().firstChild, 1);204 rng.setEnd(editor.getBody().firstChild, 3);205 editor.selection.setRng(rng);206 bookmark = editor.selection.getBookmark(2);207 equal(editor.getContent(), 'text', 'Editor contents (text)');208 editor.selection.moveToBookmark(bookmark);209 equal(editor.selection.getContent(), 'ex', 'Selected contents (text)');210 });211 test('getBookmark/setBookmark (nonintrusive) - Get non intrusive bookmark simple element selection', function() {212 var rng, bookmark;213 expect(1);214 // Get non intrusive bookmark simple element selection215 editor.setContent('<p>text<em>a<strong>b</strong>c</em></p>');216 rng = editor.dom.createRng();217 rng.setStart(editor.dom.select('em')[0], 1);218 rng.setEnd(editor.dom.select('em')[0], 2);219 editor.selection.setRng(rng);220 bookmark = editor.selection.getBookmark(2);221 editor.selection.moveToBookmark(bookmark);222 equal(editor.selection.getContent(), '<strong>b</strong>', 'Selected contents (element)');223 });224 test('getBookmark/setBookmark (nonintrusive) - Get non intrusive bookmark multiple elements text selection', function() {225 var rng, bookmark;226 expect(2);227 // Get non intrusive bookmark multiple elements text selection228 editor.setContent('<p>text</p>\n<p>text</p>');229 rng = editor.dom.createRng();230 rng.setStart(editor.getBody().firstChild.firstChild, 1);231 rng.setEnd(editor.getBody().lastChild.firstChild, 3);232 editor.selection.setRng(rng);233 bookmark = editor.selection.getBookmark(2);234 equal(editor.getContent(), '<p>text</p>\n<p>text</p>', 'Editor contents (elements)');235 editor.selection.moveToBookmark(bookmark);236 equal(editor.selection.getContent(), '<p>ext</p>\n<p>tex</p>', 'Selected contents (elements)');237 });238 test('getBookmark/setBookmark (nonintrusive)', function() {239 var rng, bookmark;240 expect(2);241 // Get non intrusive bookmark multiple elements text selection fragmented242 editor.setContent('<p>text</p><p>text</p>');243 editor.dom.select('p')[0].appendChild(editor.dom.doc.createTextNode('a'));244 editor.dom.select('p')[0].appendChild(editor.dom.doc.createTextNode('a'));245 editor.dom.select('p')[0].appendChild(editor.dom.doc.createTextNode('a'));246 editor.dom.select('p')[0].appendChild(editor.dom.doc.createTextNode('text'));247 rng = editor.dom.createRng();248 rng.setStart(editor.getBody().firstChild.lastChild, 1);249 rng.setEnd(editor.getBody().lastChild.firstChild, 3);250 editor.selection.setRng(rng);251 bookmark = editor.selection.getBookmark(2);252 equal(editor.getContent(), '<p>textaaatext</p>\n<p>text</p>', 'Editor contents (fragmented, elements)');253 editor.selection.moveToBookmark(bookmark);254 equal(editor.selection.getContent(), '<p>ext</p>\n<p>tex</p>', 'Selected contents (fragmented, elements)');255 });256 test('getBookmark/setBookmark (nonintrusive) - fragmentext text (normalized)', function() {257 var rng, bookmark;258 expect(2);259 // Get non intrusive bookmark multiple elements text selection fragmented260 editor.setContent('<p>text</p><p>text</p>');261 editor.dom.select('p')[0].appendChild(editor.dom.doc.createTextNode('a'));262 editor.dom.select('p')[0].appendChild(editor.dom.doc.createTextNode('a'));263 editor.dom.select('p')[0].appendChild(editor.dom.doc.createTextNode('a'));264 editor.dom.select('p')[0].appendChild(editor.dom.doc.createTextNode('text'));265 rng = editor.dom.createRng();266 rng.setStart(editor.getBody().firstChild.lastChild, 1);267 rng.setEnd(editor.getBody().lastChild.firstChild, 3);268 editor.selection.setRng(rng);269 bookmark = editor.selection.getBookmark(2, true);270 editor.setContent(editor.getContent());271 equal(editor.getContent(), '<p>textaaatext</p>\n<p>text</p>', 'Editor contents (fragmented, elements)');272 editor.selection.moveToBookmark(bookmark);273 equal(editor.selection.getContent(), '<p>ext</p>\n<p>tex</p>', 'Selected contents (fragmented, elements)');274 });275 test('getBookmark/setBookmark (nonintrusive) - Get bookmark before image', function() {276 var rng, bookmark;277 expect(4);278 editor.setContent('<p><img src="about:blank" /></p>');279 rng = editor.dom.createRng();280 rng.setStart(editor.getBody().firstChild, 0);281 rng.setEnd(editor.getBody().firstChild, 0);282 editor.selection.setRng(rng);283 bookmark = editor.selection.getBookmark(2, true);284 editor.getBody().innerHTML = editor.getBody().innerHTML;285 editor.selection.moveToBookmark(bookmark);286 rng = editor.selection.getRng(true);287 equal(rng.startContainer, editor.getBody().firstChild);288 equal(rng.startOffset, 0);289 equal(rng.endContainer, editor.getBody().firstChild);290 equal(rng.endOffset, 0);291 });292 test('getBookmark/setBookmark (nonintrusive) - Get bookmark before/after image', function() {293 var rng, bookmark;294 expect(4);295 editor.setContent('<p><img src="about:blank" /></p>');296 rng = editor.dom.createRng();297 rng.setStart(editor.getBody().firstChild, 0);298 rng.setEnd(editor.getBody().firstChild, 1);299 editor.selection.setRng(rng);300 bookmark = editor.selection.getBookmark(2, true);301 editor.getBody().innerHTML = editor.getBody().innerHTML;302 editor.selection.moveToBookmark(bookmark);303 rng = editor.selection.getRng(true);304 equal(rng.startContainer, editor.getBody().firstChild);305 equal(rng.startOffset, 0);306 equal(rng.endContainer, editor.getBody().firstChild);307 equal(rng.endOffset, 1);308 });309 test('getBookmark/setBookmark (nonintrusive) - Get bookmark after image', function() {310 var rng, bookmark;311 expect(4);312 editor.setContent('<p><img src="about:blank" /></p>');313 rng = editor.dom.createRng();314 rng.setStart(editor.getBody().firstChild, 1);315 rng.setEnd(editor.getBody().firstChild, 1);316 editor.selection.setRng(rng);317 bookmark = editor.selection.getBookmark(2, true);318 editor.getBody().innerHTML = editor.getBody().innerHTML;319 editor.selection.moveToBookmark(bookmark);320 rng = editor.selection.getRng(true);321 equal(rng.startContainer, editor.getBody().firstChild);322 equal(rng.startOffset, 1);323 equal(rng.endContainer, editor.getBody().firstChild);324 equal(rng.endOffset, 1);325 });326 test('getBookmark/setBookmark (nonintrusive) - Get bookmark before element', function() {327 var rng, bookmark;328 expect(4);329 editor.setContent('abc<b>123</b>');330 rng = editor.dom.createRng();331 rng.setStart(editor.getBody().firstChild, 0);332 rng.setEnd(editor.getBody().firstChild, 2);333 editor.selection.setRng(rng);334 bookmark = editor.selection.getBookmark(2, true);335 editor.getBody().innerHTML = editor.getBody().innerHTML;336 editor.selection.moveToBookmark(bookmark);337 rng = editor.selection.getRng(true);338 equal(rng.startContainer, editor.getBody().firstChild);339 equal(rng.startOffset, 0);340 equal(rng.endContainer, editor.getBody().firstChild);341 equal(rng.endOffset, 2);342 });343 test('getBookmark/setBookmark (nonintrusive) - Get bookmark after element', function() {344 var rng, bookmark;345 expect(4);346 // Get bookmark after element347 editor.setContent('<b>123</b>abc');348 rng = editor.dom.createRng();349 rng.setStart(editor.getBody().lastChild, 1);350 rng.setEnd(editor.getBody().lastChild, 2);351 editor.selection.setRng(rng);352 bookmark = editor.selection.getBookmark(2, true);353 editor.getBody().innerHTML = editor.getBody().innerHTML;354 editor.selection.moveToBookmark(bookmark);355 rng = editor.selection.getRng(true);356 equal(rng.startContainer, editor.getBody().lastChild);357 equal(rng.startOffset, 1);358 equal(rng.endContainer, editor.getBody().lastChild);359 equal(rng.endOffset, 2);360 });361 test('getBookmark/setBookmark (nonintrusive) - Get bookmark inside element', function() {362 var rng, bookmark;363 expect(4);364 editor.setContent('abc<b>123</b>abc');365 rng = editor.dom.createRng();366 rng.setStart(editor.getBody().childNodes[1].firstChild, 1);367 rng.setEnd(editor.getBody().childNodes[1].firstChild, 2);368 editor.selection.setRng(rng);369 bookmark = editor.selection.getBookmark(2, true);370 editor.getBody().innerHTML = editor.getBody().innerHTML;371 editor.selection.moveToBookmark(bookmark);372 rng = editor.selection.getRng(true);373 equal(rng.startContainer, editor.getBody().childNodes[1].firstChild);374 equal(rng.startOffset, 1);375 equal(rng.endContainer, editor.getBody().childNodes[1].firstChild);376 equal(rng.endOffset, 2);377 });378 test('getBookmark/setBookmark (nonintrusive) - Get bookmark inside root text', function() {379 var rng, bookmark;380 expect(4);381 editor.setContent('abc');382 rng = editor.dom.createRng();383 rng.setStart(editor.getBody().firstChild, 1);384 rng.setEnd(editor.getBody().firstChild, 2);385 editor.selection.setRng(rng);386 bookmark = editor.selection.getBookmark(2, true);387 editor.getBody().innerHTML = editor.getBody().innerHTML;388 editor.selection.moveToBookmark(bookmark);389 rng = editor.selection.getRng(true);390 equal(rng.startContainer, editor.getBody().firstChild);391 equal(rng.startOffset, 1);392 equal(rng.endContainer, editor.getBody().firstChild);393 equal(rng.endOffset, 2);394 });395 test('getBookmark/setBookmark (nonintrusive) - Get bookmark inside complex html', function() {396 var rng, bookmark;397 expect(4);398 editor.setContent('<p>abc</p>123<p>123</p><p>123<b>123</b><table><tr><td>abc</td></tr></table></p>');399 editor.execCommand('SelectAll');400 Utils.setSelection('td', 1, 'td', 2);401 bookmark = editor.selection.getBookmark(2, true);402 editor.getBody().innerHTML = editor.getBody().innerHTML;403 editor.selection.moveToBookmark(bookmark);404 rng = editor.selection.getRng(true);405 equal(rng.startContainer, editor.dom.select('td')[0].firstChild);406 equal(rng.startOffset, 1);407 equal(rng.endContainer, editor.dom.select('td')[0].firstChild);408 equal(rng.endOffset, 2);409 });410 test('getBookmark/setBookmark on cE=false', function() {411 var bookmark;412 editor.setContent('text<span contentEditable="false">1</span>');413 editor.selection.select(editor.$('span')[0]);414 bookmark = editor.selection.getBookmark(2);415 editor.setContent('text<span contentEditable="false">1</span>');416 editor.selection.moveToBookmark(bookmark);417 equal(editor.selection.getNode(), editor.$('span')[0]);418 });419 test('getBookmark/setBookmark before cE=false', function() {420 var rng, bookmark;421 editor.setContent('<p><input><span contentEditable="false">1</span></p>');422 CaretContainer.insertInline(editor.$('span')[0], true);423 rng = editor.dom.createRng();424 rng.setStart(editor.$('span')[0].previousSibling, 0);425 rng.setEnd(editor.$('span')[0].previousSibling, 0);426 editor.selection.setRng(rng);427 bookmark = editor.selection.getBookmark(2);428 editor.setContent('<p><input><span contentEditable="false">1</span></p>');429 editor.selection.moveToBookmark(bookmark);430 equal(editor.selection.getNode(), editor.$('span')[0]);431 });432 test('getBookmark/setBookmark before cE=false block', function() {433 var rng, bookmark;434 editor.setContent('<p contentEditable="false">1</p>');435 CaretContainer.insertBlock('p', editor.$('p')[0], true);436 rng = editor.dom.createRng();437 rng.setStart(editor.$('p')[0], 0);438 rng.setEnd(editor.$('p')[0], 0);439 editor.selection.setRng(rng);440 bookmark = editor.selection.getBookmark(2);441 editor.setContent('<p contentEditable="false">1</p>');442 editor.selection.moveToBookmark(bookmark);443 equal(editor.selection.getNode(), editor.$('p')[0]);444 });445 test('select empty TD', function() {446 editor.getBody().innerHTML = '<table><tr><td><br></td></tr></table>';447 editor.selection.select(editor.dom.select('td')[0], true);448 equal(editor.selection.getRng(true).startContainer.nodeName, 'TD');449 });450 test('select first p', 2, function() {451 editor.setContent('<p>text1</p><p>text2</p>');452 editor.selection.select(editor.dom.select('p')[0]);453 equal(editor.selection.getContent(), '<p>text1</p>', 'Select simple element, content');454 equal(editor.selection.getStart().nodeName, 'P', 'Select simple element, nodeName');455 });456 test('select table', 2, function() {457 editor.setContent('<table><tbody><tr><td>text1</td></tr></tbody></table>');458 editor.selection.select(editor.dom.select('table')[0]);459 equal(editor.selection.getContent(), '<table>\n<tbody>\n<tr>\n<td>text1</td>\n</tr>\n</tbody>\n</table>', 'Select complex element, content');460 equal(editor.selection.getNode().nodeName, 'TABLE', 'Select complex element, nodeName');461 });462 test('select table text 1', 2, function() {463 editor.setContent('<table><tbody><tr><td id="a">text1</td><td id="b">text2</td></tr></tbody></table>');464 editor.selection.select(editor.dom.select('table')[0], true);465 equal(editor.selection.getStart().id, 'a', 'Expand to text content 1 (start)');466 equal(editor.selection.getEnd().id, 'b', 'Expand to text content 1 (end)');467 });468 test('select table text 2', 2, function() {469 editor.setContent('<table><tbody><tr><td id="a"><br /></td><td id="b"><br /></td></tr></tbody></table>');470 editor.selection.select(editor.dom.select('table')[0], true);471 equal(editor.dom.getParent(editor.selection.getStart(), 'td').id, 'a', 'Expand to text content 2 (start)');472 equal(editor.dom.getParent(editor.selection.getEnd(), 'td').id, 'b', 'Expand to text content 2 (end)');473 });474 test('getNode', function() {475 var rng;476 editor.setContent('<p id="p1"><span id="s1">span1</span> word <span id="s2">span2</span> word <span id="s3">span3</span></p>');477 rng = editor.dom.createRng();478 rng.setStart(editor.dom.get('s1').firstChild, 0);479 rng.setEnd(editor.dom.get('s1').nextSibling, 0);480 editor.selection.setRng(rng);481 deepEqual(editor.selection.getNode(), editor.dom.get('s1'), 'Detect selection ends immediately after node at start of paragraph.');482 rng = editor.dom.createRng();483 rng.setStart(editor.dom.get('s2').previousSibling, editor.dom.get('s2').previousSibling.length);484 rng.setEnd(editor.dom.get('s2').nextSibling, 0);485 editor.selection.setRng(rng);486 deepEqual(editor.selection.getNode(), editor.dom.get('s2'), 'Detect selection immediately surrounds node in middle of paragraph.');487 rng = editor.dom.createRng();488 rng.setStart(editor.dom.get('s3').previousSibling, editor.dom.get('s3').previousSibling.length);489 rng.setEnd(editor.dom.get('s3').lastChild, editor.dom.get('s3').lastChild.length);490 editor.selection.setRng(rng);491 deepEqual(editor.selection.getNode(), editor.dom.get('s3'), 'Detect selection starts immediately before node at end of paragraph.');492 rng = editor.dom.createRng();493 rng.setStart(editor.dom.get('s2').previousSibling, editor.dom.get('s2').previousSibling.length);494 rng.setEnd(editor.dom.get('s3').lastChild, editor.dom.get('s3').lastChild.length);495 editor.selection.setRng(rng);496 deepEqual(editor.selection.getNode(), editor.dom.get('p1'), 'Detect selection wrapping multiple nodes does not collapse.');497 });498 test('normalize to text node from document', function() {499 var rng;500 if (tinymce.isOpera || tinymce.isIE) {501 ok(true, "Skipped on Opera/IE since Opera doesn't let you to set the range to document and IE will steal focus.");502 return;503 }504 editor.setContent('<p>text</p>');505 rng = editor.dom.createRng();506 rng.setStart(editor.getDoc(), 0);507 rng.setEnd(editor.getDoc(), 0);508 editor.selection.setRng(rng);509 editor.selection.normalize();510 rng = editor.selection.getRng(true);511 equal(rng.startContainer.nodeType, 3, 'startContainer node type');512 equal(rng.startOffset, 0, 'startContainer offset');513 equal(rng.endContainer.nodeType, 3, 'endContainer node type');514 equal(rng.endOffset, 0, 'endOffset offset');515 });516 test('normalize to br from document', function() {517 var rng;518 if (tinymce.isOpera || tinymce.isIE) {519 ok(true, "Skipped on Opera/IE since Opera doesn't let you to set the range to document and IE will steal focus.");520 return;521 }522 editor.setContent('<p><br /></p>');523 rng = editor.dom.createRng();524 rng.setStart(editor.getDoc(), 0);525 rng.setEnd(editor.getDoc(), 0);526 editor.selection.setRng(rng);527 editor.selection.normalize();528 rng = editor.selection.getRng(true);529 equal(rng.startContainer.nodeName, 'P', 'startContainer node name');530 equal(rng.startContainer.nodeType, 1, 'startContainer node type');531 equal(rng.startOffset, 0, 'startContainer offset');532 equal(rng.endContainer.nodeType, 1, 'endContainer node type');533 equal(rng.endOffset, 0, 'endOffset offset');534 });535 // Only run on browser with W3C DOM Range support536 if (tinymce.Env.range) {537 test('normalize with contentEditable:false element', function() {538 var rng;539 editor.setContent('<p>a<b contentEditable="false">b</b>c</p>');540 rng = editor.dom.createRng();541 rng.setStart(editor.getBody().firstChild.lastChild, 0);542 rng.setEnd(editor.getBody().firstChild.lastChild, 0);543 editor.selection.setRng(rng);544 editor.selection.normalize();545 rng = editor.selection.getRng(true);546 equal(rng.collapsed, true);547 equal(CaretContainer.isCaretContainer(rng.startContainer), true);548 });549 test('normalize with contentEditable:false parent and contentEditable:true child element', function() {550 editor.setContent('<p contentEditable="false">a<em contentEditable="true">b</em></p>');551 Utils.setSelection('em', 0);552 editor.selection.normalize();553 var rng = editor.selection.getRng(true);554 equal(rng.collapsed, true);555 equal(rng.startContainer.nodeType, 3);556 equal(rng.startContainer.data, 'b');557 // WebKit is in some state state here, so lets restore it558 rng.setStart(editor.getBody(), 0);559 rng.setEnd(editor.getBody(), 0);560 editor.selection.setRng(rng);561 });562 test('normalize with contentEditable:true parent and contentEditable:false child element', function() {563 editor.setContent('<p contentEditable="true">a<em contentEditable="false">b</em></p>');564 Utils.setSelection('em', 0);565 editor.selection.normalize();566 var rng = editor.selection.getRng(true);567 if (Env.ie && Env.ie < 12) {568 // IE automatically normalizes569 ok(rng.startContainer.parentNode.contentEditable != 'false');570 } else {571 equal(CaretContainer.isCaretContainer(rng.startContainer), true);572 }573 // Excluding assert on IE since it's a minor issue574 if (tinymce.ie) {575 equal(rng.startOffset, 1);576 }577 });578 test('normalize to text node from body', function() {579 var rng;580 editor.setContent('<p>text</p>');581 rng = editor.dom.createRng();582 rng.setStart(editor.getBody(), 0);583 rng.setEnd(editor.getBody(), 0);584 editor.selection.setRng(rng);585 editor.selection.normalize();586 rng = editor.selection.getRng(true);587 equal(rng.startContainer.nodeType, 3, 'startContainer node type');588 equal(rng.startOffset, 0, 'startContainer offset');589 equal(rng.endContainer.nodeType, 3, 'endContainer node type');590 equal(rng.endOffset, 0, 'endOffset offset');591 });592 test('normalize to br from body', function() {593 var rng;594 editor.setContent('<p><br /></p>');595 rng = editor.dom.createRng();596 rng.setStart(editor.getBody(), 0);597 rng.setEnd(editor.getBody(), 0);598 editor.selection.setRng(rng);599 editor.selection.normalize();600 rng = editor.selection.getRng(true);601 equal(rng.startContainer.nodeName, 'P', 'startContainer node name');602 equal(rng.startContainer.nodeType, 1, 'startContainer node type');603 equal(rng.startOffset, 0, 'startContainer offset');604 equal(rng.endContainer.nodeType, 1, 'endContainer node type');605 equal(rng.endOffset, 0, 'endOffset offset');606 });607 test('normalize ignore img', function() {608 var rng;609 editor.getBody().innerHTML = '<img src="about:blank " />';610 rng = editor.dom.createRng();611 rng.setStart(editor.getBody(), 0);612 rng.setEnd(editor.getBody(), 1);613 editor.selection.setRng(rng);614 editor.selection.normalize();615 rng = editor.selection.getRng(true);616 equal(rng.startContainer.nodeName, 'BODY', 'startContainer node name');617 equal(rng.startContainer.nodeType, 1, 'startContainer node type');618 equal(rng.startOffset, 0, 'startContainer offset');619 equal(rng.endContainer.nodeName, 'BODY', 'endContainer node name');620 equal(rng.endContainer.nodeType, 1, 'endContainer node type');621 equal(rng.endOffset, 1, 'endOffset offset');622 });623 test('normalize to before/after img', function() {624 var rng;625 editor.getBody().innerHTML = '<p><img src="about:blank " /></p>';626 rng = editor.dom.createRng();627 rng.setStart(editor.getBody(), 0);628 rng.setEnd(editor.getBody(), 1);629 editor.selection.setRng(rng);630 editor.selection.normalize();631 rng = editor.selection.getRng(true);632 equal(rng.startContainer.nodeName, 'P', 'startContainer node name');633 equal(rng.startContainer.nodeType, 1, 'startContainer node type');634 equal(rng.startOffset, 0, 'startContainer offset');635 equal(rng.endContainer.nodeName, 'P', 'endContainer node name');636 equal(rng.endContainer.nodeType, 1, 'endContainer node type');637 equal(rng.endOffset, 1, 'endOffset offset');638 });639 test('normalize to text node inside P', function() {640 var rng;641 editor.getBody().innerHTML = '<p>abc</p>';642 rng = editor.dom.createRng();643 rng.setStart(editor.getBody(), 0);644 rng.setEnd(editor.getBody(), 1);645 editor.selection.setRng(rng);646 editor.selection.normalize();647 rng = editor.selection.getRng(true);648 equal(rng.startContainer.nodeName, '#text', 'startContainer node name');649 equal(rng.startOffset, 0, 'startContainer offset');650 equal(rng.endContainer.nodeName, '#text', 'endContainer node name');651 equal(rng.endOffset, 3, 'endOffset offset');652 });653 test('normalize lean left if at the start of text node', function() {654 var rng;655 editor.getBody().innerHTML = '<p><b>a</b><i>b</i></p>';656 Utils.setSelection('i', 0);657 editor.selection.normalize();658 rng = editor.selection.getRng(true);659 equal(rng.startContainer.nodeName, '#text', 'startContainer node name');660 equal(rng.startContainer.parentNode.nodeName, 'B');661 equal(rng.startOffset, 1, 'startContainer offset');662 equal(rng.endContainer.nodeName, '#text');663 equal(rng.endContainer.parentNode.nodeName, 'B');664 equal(rng.endOffset, 1, 'endOffset offset');665 });666 test('normalize lean start to the right if at end of text node', function() {667 var rng;668 editor.getBody().innerHTML = '<p><b>a</b><i>b</i></p>';669 Utils.setSelection('b', 1, 'i', 1);670 editor.selection.normalize();671 rng = editor.selection.getRng(true);672 equal(rng.startContainer.nodeName, '#text', 'startContainer node name');673 equal(rng.startContainer.parentNode.nodeName, 'I');674 equal(rng.startOffset, 0, 'startContainer offset');675 equal(rng.endContainer.nodeName, '#text');676 equal(rng.endContainer.parentNode.nodeName, 'I');677 equal(rng.endOffset, 1, 'endOffset offset');678 });679 test('normalize lean left but break before br', function() {680 var rng;681 editor.getBody().innerHTML = '<p>a<br><b>b</b></p>';682 Utils.setSelection('b', 0);683 editor.selection.normalize();684 rng = editor.selection.getRng(true);685 equal(rng.startContainer.nodeValue, 'b');686 equal(rng.startOffset, 0);687 });688 test('normalize lean left but break before img', function() {689 var rng;690 editor.getBody().innerHTML = '<p>a<img><b>b</b></p>';691 Utils.setSelection('b', 0);692 editor.selection.normalize();693 rng = editor.selection.getRng(true);694 equal(rng.startContainer.nodeValue, 'b');695 equal(rng.startOffset, 0);696 });697 test('normalize lean left but don\'t walk out the parent block', function() {698 var rng;699 editor.getBody().innerHTML = '<p>a</p><p><b>b</b></p>';700 Utils.setSelection('b', 0);701 editor.selection.normalize();702 rng = editor.selection.getRng(true);703 equal(rng.startContainer.nodeValue, 'b');704 equal(rng.startOffset, 0);705 });706 test('normalize lean left into empty inline elements when caret is before br', function() {707 var rng;708 editor.getBody().innerHTML = '<p><i><b></b></i><br /></p>';709 rng = editor.dom.createRng();710 rng.setStartBefore(editor.getBody().firstChild.lastChild);711 rng.setEndBefore(editor.getBody().firstChild.lastChild);712 editor.selection.setRng(rng);713 editor.selection.normalize();714 rng = editor.selection.getRng(true);715 equal(rng.startContainer.nodeName, 'B');716 equal(rng.startOffset, 0);717 });718 test('normalize lean left from br into formatter caret container', function() {719 var rng;720 editor.getBody().innerHTML = '<p><span id="_mce_caret">' + Zwsp.ZWSP + '</span><br /></p>';721 rng = editor.dom.createRng();722 rng.setStartBefore(editor.getBody().firstChild.lastChild);723 rng.setEndBefore(editor.getBody().firstChild.lastChild);724 editor.selection.setRng(rng);725 editor.selection.normalize();726 rng = editor.selection.getRng(true);727 equal(rng.startContainer.nodeType, 3);728 equal(rng.startOffset, 1);729 });730 test('normalize don\'t lean left into empty inline elements if there is a br element after caret', function() {731 var rng;732 editor.getBody().innerHTML = '<p><i><b></b></i><br /><br /></p>';733 rng = editor.dom.createRng();734 rng.setStartBefore(editor.getBody().firstChild.lastChild);735 rng.setEndBefore(editor.getBody().firstChild.lastChild);736 editor.selection.setRng(rng);737 editor.selection.normalize();738 rng = editor.selection.getRng(true);739 equal(rng.startContainer.nodeName, 'P');740 equal(rng.startOffset, 2);741 });742 test('normalize don\'t lean left into empty inline elements if there is a br element before caret', function() {743 var rng;744 editor.getBody().innerHTML = '<p><i><b><br /></b></i><br /></p>';745 rng = editor.dom.createRng();746 rng.setStartBefore(editor.getBody().firstChild.lastChild);747 rng.setEndBefore(editor.getBody().firstChild.lastChild);748 editor.selection.setRng(rng);749 editor.selection.normalize();750 rng = editor.selection.getRng(true);751 equal(rng.startContainer.nodeName, 'P');752 equal(rng.startOffset, 1);753 });754 test('normalize don\'t move start/end if it\'s before/after table', function() {755 var rng;756 editor.getBody().innerHTML = '<table><tr><td>X</td></tr></table>';757 rng = editor.dom.createRng();758 rng.setStartBefore(editor.getBody().firstChild);759 rng.setEndAfter(editor.getBody().lastChild);760 editor.selection.setRng(rng);761 editor.selection.normalize();762 rng = editor.selection.getRng(true);763 equal(rng.startContainer.nodeName, 'BODY');764 equal(rng.startOffset, 0);765 equal(rng.endContainer.nodeName, 'BODY');766 equal(rng.endOffset, 1);767 });768 test('normalize after paragraph', function() {769 var rng;770 editor.getBody().innerHTML = '<p>a</p>';771 rng = editor.dom.createRng();772 rng.setStartAfter(editor.getBody().firstChild);773 rng.setEndAfter(editor.getBody().lastChild);774 editor.selection.setRng(rng);775 editor.selection.normalize();776 rng = editor.selection.getRng(true);777 equal(rng.startContainer.nodeName, '#text');778 equal(rng.startOffset, 1);779 equal(rng.endContainer.nodeName, '#text');780 equal(rng.endOffset, 1);781 });782 test('normalize caret after trailing BR', function() {783 var rng;784 editor.setContent('<p>a<br /></p>');785 rng = editor.dom.createRng();786 rng.setStart(editor.getBody().firstChild, 2);787 rng.setEnd(editor.getBody().firstChild, 2);788 editor.selection.setRng(rng);789 editor.selection.normalize();790 rng = editor.selection.getRng(true);791 equal(rng.startContainer.nodeName, '#text', 'startContainer node name');792 equal(rng.startOffset, 1, 'startContainer offset');793 equal(rng.endContainer.nodeName, '#text', 'endContainer node name');794 equal(rng.endOffset, 1, 'endOffset offset');795 });796 test('normalize caret after bogus block BR', function() {797 var rng;798 editor.setContent('<p><br /></p>');799 rng = editor.dom.createRng();800 rng.setStart(editor.getBody().firstChild, 1);801 rng.setEnd(editor.getBody().firstChild, 1);802 editor.selection.setRng(rng);803 editor.selection.normalize();804 rng = editor.selection.getRng(true);805 equal(rng.startContainer.nodeName, 'P', 'startContainer node name');806 equal(rng.startOffset, 0, 'startContainer offset');807 equal(rng.endContainer.nodeName, 'P', 'endContainer node name');808 equal(rng.endOffset, 0, 'endOffset offset');809 });810 test('normalize after table should not move', function() {811 var rng;812 if (tinymce.isOpera || tinymce.isIE) {813 ok(true, "Skipped on Opera/IE since Opera doesn't let you to set the range to document and IE will steal focus.");814 return;815 }816 editor.setContent('a<table><tr><td>b</td></tr></table>');817 rng = editor.dom.createRng();818 rng.setStart(editor.getBody(), 0);819 rng.setEnd(editor.getBody(), 1);820 editor.selection.setRng(rng);821 editor.selection.normalize();822 rng = editor.selection.getRng(true);823 equal(rng.endContainer, editor.getBody());824 equal(rng.endOffset, 1);825 });826 /*827 test('normalize caret after last BR in block', function() {828 var rng;829 editor.setContent('<p><br /><br /></p>');830 rng = editor.dom.createRng();831 rng.setStart(editor.getBody().firstChild, 2);832 rng.setEnd(editor.getBody().firstChild, 2);833 editor.selection.setRng(rng);834 editor.selection.normalize();835 rng = editor.selection.getRng(true);836 equal(rng.startContainer.nodeName, 'P', 'startContainer node name');837 equal(rng.startOffset, 1, 'startContainer offset');838 equal(rng.endContainer.nodeName, 'P', 'endContainer node name');839 equal(rng.endOffset, 1, 'endOffset offset');840 });841 */842 test('normalize caret after double BR', function() {843 var rng;844 editor.setContent('<p>a<br /><br /></p>');845 rng = editor.dom.createRng();846 rng.setStart(editor.getBody().firstChild, 3);847 rng.setEnd(editor.getBody().firstChild, 3);848 editor.selection.setRng(rng);849 editor.selection.normalize();850 rng = editor.selection.getRng(true);851 equal(rng.startContainer.nodeName, 'P', 'startContainer node name');852 equal(rng.startOffset, 3, 'startContainer offset');853 equal(rng.endContainer.nodeName, 'P', 'endContainer node name');854 equal(rng.endOffset, 3, 'endOffset offset');855 });856 }857 test('custom elements', function() {858 var rng;859 editor.setContent('<custom1>test</custom1><custom2>test</custom2>');860 rng = editor.dom.createRng();861 rng.setStart(editor.getBody(), 0);862 rng.setEnd(editor.getBody(), 2);863 editor.selection.setRng(rng);864 equal(editor.selection.getContent(), '<custom1>test</custom1><custom2>test</custom2>');865 });866 test('selectorChanged', function() {867 var newState, newArgs;868 editor.selection.selectorChanged('a[href]', function(state, args) {869 newState = state;870 newArgs = args;871 });872 editor.getBody().innerHTML = '<p><a href="#">text</a></p>';873 Utils.setSelection('a', 0, 'a', 4);874 editor.nodeChanged();875 ok(newState);876 equal(newArgs.selector, 'a[href]');877 equal(newArgs.node, editor.getBody().firstChild.firstChild);878 equal(newArgs.parents.length, 2);879 editor.getBody().innerHTML = '<p>text</p>';880 Utils.setSelection('p', 0, 'p', 4);881 editor.nodeChanged();882 equal(newArgs.selector, 'a[href]');883 equal(newArgs.node, editor.getBody().firstChild);884 equal(newArgs.parents.length, 1);885 });886 test('setRng', function() {887 var rng = editor.dom.createRng();888 editor.setContent('<p>x</p>');889 rng.setStart(editor.$('p')[0].firstChild, 0);890 rng.setEnd(editor.$('p')[0].firstChild, 1);891 editor.selection.setRng(rng);892 editor.selection.setRng(null);893 rng = editor.selection.getRng(true);894 equal(rng.startContainer.nodeName, '#text');895 equal(rng.startOffset, 0);896 equal(rng.endContainer.nodeName, '#text');897 equal(rng.endOffset, 1);898 });899 test('getRng should return null if win.document is not defined or null', function() {900 var win = editor.selection.win,901 rng = editor.dom.createRng();902 editor.setContent('<p>x</p>');903 rng.setStart(editor.$('p')[0].firstChild, 0);904 rng.setEnd(editor.$('p')[0].firstChild, 1);905 editor.selection.setRng(rng);906 editor.selection.setRng(null);907 editor.selection.win = {};908 rng = editor.selection.getRng(true);909 equal(rng, null);910 editor.selection.win = {document:null};911 rng = editor.selection.getRng(true);912 equal(rng, null);913 editor.selection.win = win;914 });...

Full Screen

Full Screen

TridentSelection.js

Source:TridentSelection.js Github

copy

Full Screen

1module("tinymce.dom.TridentSelection", {2 setupModule: function() {3 if (window.getSelection) {4 return;5 }6 QUnit.stop();7 document.getElementById('view').innerHTML = '<textarea></textarea>';8 tinymce.init({9 selector: "textarea",10 elements: "elm1",11 add_unload_trigger: false,12 disable_nodechange: true,13 skin: false,14 init_instance_callback: function(ed) {15 window.editor = ed;16 QUnit.start();17 }18 });19 }20});21if (!window.getSelection) {22 test("Selection of element containing text", function(){23 expect(5);24 25 editor.setContent('<p>123</p>', {26 format: 'raw'27 });28 29 var rng = editor.dom.createRng();30 rng.setStart(editor.getBody(), 0);31 rng.setEnd(editor.getBody(), 1);32 editor.selection.setRng(rng);33 34 rng = editor.selection.getRng(1);35 equal(rng.startContainer.nodeName, '#text', 'Start container is text node');36 equal(rng.endContainer.nodeName, '#text', 'End container is text node');37 equal(rng.startOffset, 0, 'Start of text node');38 equal(rng.endOffset, 3, 'End of text node');39 equal(editor.dom.getOuterHTML(rng.cloneContents()), '123', 'Contents matches');40 });41 42 test("Single image selection", function(){43 expect(6);44 45 editor.setContent('<p><img src="about:blank" /></p>', {46 format: 'raw'47 });48 49 var rng = editor.dom.createRng();50 rng.setStartBefore(editor.dom.select('img')[0]);51 rng.setEndAfter(editor.dom.select('img')[0]);52 editor.selection.setRng(rng);53 54 rng = editor.selection.getRng(1);55 equal(rng.startContainer.nodeName, 'P', 'Check startContainer');56 equal(rng.endContainer.nodeName, 'P', 'Check endContainer');57 equal(rng.startOffset, 0, 'Check startOffset');58 equal(rng.endOffset, 1, 'Check endOffset');59 equal(Utils.cleanHtml(editor.dom.getOuterHTML(rng.cloneContents()).toLowerCase()), '<img src="about:blank">', 'Check contents');60 ok(editor.selection.getRng().item, 'Check if it\'s a control selection');61 });62 63 test("Multiple image selection", function(){64 expect(6);65 66 editor.setContent('<p><img src="about:blank" /><img src="about:blank" /></p>', {67 format: 'raw'68 });69 70 var rng = editor.dom.createRng();71 rng.setStartBefore(editor.dom.select('img')[0]);72 rng.setEndAfter(editor.dom.select('img')[1]);73 editor.selection.setRng(rng);74 75 rng = editor.selection.getRng(1);76 equal(rng.startContainer.nodeName, 'P');77 equal(rng.endContainer.nodeName, 'P');78 equal(rng.startOffset, 0);79 equal(rng.endOffset, 2);80 equal(editor.dom.getOuterHTML(rng.cloneContents()).toLowerCase(), '<img src="about:blank"><img src="about:blank">');81 ok(editor.selection.getRng().parentElement, 'Is text selection');82 });83 84 test("Text node selection", function(){85 expect(5);86 87 editor.setContent('<p>1234</p>', {88 format: 'raw'89 });90 91 var rng = editor.dom.createRng();92 rng.setStart(editor.getBody().firstChild.firstChild, 1);93 rng.setEnd(editor.getBody().firstChild.firstChild, 3);94 editor.selection.setRng(rng);95 96 rng = editor.selection.getRng(1);97 equal(rng.startContainer.nodeName, '#text');98 equal(rng.endContainer.nodeName, '#text');99 equal(rng.startOffset, 1);100 equal(rng.endOffset, 3);101 equal(editor.dom.getOuterHTML(rng.cloneContents()), '23');102 });103 104 test("Text node selection between two elements", function(){105 expect(5);106 107 editor.setContent('<p>1234</p><p>abcd</p>', {108 format: 'raw'109 });110 111 var rng = editor.dom.createRng();112 rng.setStart(editor.getBody().firstChild.firstChild, 1);113 rng.setEnd(editor.getBody().childNodes[1].firstChild, 3);114 editor.selection.setRng(rng);115 116 rng = editor.selection.getRng(1);117 equal(rng.startContainer.nodeName, '#text');118 equal(rng.endContainer.nodeName, '#text');119 equal(rng.startOffset, 1);120 equal(rng.endOffset, 3);121 equal(editor.dom.getOuterHTML(rng.cloneContents()).replace(/[\r\n]/g, '').toLowerCase(), '<p>234</p><p>abc</p>');122 123 editor.setContent('<p>1</p><p>1234</p><p>abcd</p><p>2</p>', {124 format: 'raw'125 });126 });127 128 test("Mixed selection start at element end at text", function(){129 expect(5);130 131 editor.setContent('<p><img src="about:blank" />text</p>', {132 format: 'raw'133 });134 135 var rng = editor.dom.createRng();136 rng.setStartBefore(editor.dom.select('img')[0]);137 rng.setEnd(editor.getBody().firstChild.lastChild, 3);138 editor.selection.setRng(rng);139 140 rng = editor.selection.getRng(1);141 equal(rng.startContainer.nodeName, 'P');142 equal(rng.endContainer.nodeName, '#text');143 equal(rng.startOffset, 0);144 equal(rng.endOffset, 3);145 equal(editor.dom.getOuterHTML(rng.cloneContents()).toLowerCase(), '<img src="about:blank">tex');146 });147 148 test("Mixed selection start at text end at element", function(){149 expect(5);150 151 editor.setContent('<p>text<img src="about:blank" /></p>', {152 format: 'raw'153 });154 155 var rng = editor.dom.createRng();156 rng.setStart(editor.getBody().firstChild.firstChild, 1);157 rng.setEndAfter(editor.getBody().firstChild.lastChild);158 editor.selection.setRng(rng);159 160 rng = editor.selection.getRng(1);161 162 equal(rng.startContainer.nodeName, '#text');163 equal(rng.startOffset, 1);164 165 equal(rng.endContainer.nodeName, 'P');166 equal(rng.endOffset, 2);167 168 equal(editor.dom.getOuterHTML(rng.cloneContents()).toLowerCase(), 'ext<img src="about:blank">');169 });170 171 test("Caret position before image", function(){172 expect(4);173 174 editor.setContent('<p><img src="about:blank" /><img src="about:blank" /></p>', {175 format: 'raw'176 });177 178 var rng = editor.dom.createRng();179 rng.setStartBefore(editor.dom.select('img')[0]);180 rng.setEndBefore(editor.dom.select('img')[0]);181 editor.selection.setRng(rng);182 183 rng = editor.selection.getRng(1);184 equal(rng.startContainer.nodeName, 'P');185 equal(rng.endContainer.nodeName, 'P');186 equal(rng.startOffset, 0);187 equal(rng.endOffset, 0);188 });189 190 test("Caret position between images", function(){191 expect(4);192 193 editor.setContent('<p><img src="about:blank" /><img src="about:blank" /></p>', {194 format: 'raw'195 });196 197 var rng = editor.dom.createRng();198 rng.setStartAfter(editor.dom.select('img')[0]);199 rng.setEndAfter(editor.dom.select('img')[0]);200 editor.selection.setRng(rng);201 202 rng = editor.selection.getRng(1);203 equal(rng.startContainer.nodeName, 'P');204 equal(rng.endContainer.nodeName, 'P');205 equal(rng.startOffset, 1);206 equal(rng.endOffset, 1);207 });208 209 test("Caret position after image", function(){210 expect(4);211 212 editor.setContent('<p><img src="about:blank" /><img src="about:blank" /></p>', {213 format: 'raw'214 });215 216 var rng = editor.dom.createRng();217 rng.setStartAfter(editor.dom.select('img')[1]);218 rng.setEndAfter(editor.dom.select('img')[1]);219 editor.selection.setRng(rng);220 221 rng = editor.selection.getRng(1);222 equal(rng.startContainer.nodeName, 'P');223 equal(rng.endContainer.nodeName, 'P');224 equal(rng.startOffset, 2);225 equal(rng.endOffset, 2);226 });227 228 test("Selection of empty text element", function(){229 expect(6);230 231 editor.setContent('<div></div>', {232 format: 'raw'233 });234 235 var rng = editor.dom.createRng();236 rng.setStart(editor.getBody(), 0);237 rng.setEnd(editor.getBody(), 1);238 editor.selection.setRng(rng);239 240 rng = editor.selection.getRng(true);241 equal(rng.startContainer.nodeName, 'BODY');242 equal(rng.endContainer.nodeName, 'BODY');243 equal(rng.startOffset, 0);244 equal(rng.endOffset, 1);245 equal(rng.startContainer.childNodes[0].innerHTML, '');246 equal(editor.dom.getOuterHTML(rng.cloneContents()).toLowerCase(), '<div></div>');247 });248 test("Selection of empty text element with caret inside", function(){249 expect(6);250 251 editor.setContent('<div></div>', {252 format: 'raw'253 });254 255 var rng = editor.dom.createRng();256 rng.setStart(editor.getBody().firstChild, 0);257 rng.setEnd(editor.getBody().firstChild, 0);258 editor.selection.setRng(rng);259 260 rng = editor.selection.getRng(true);261 equal(rng.startContainer.nodeName, 'DIV');262 equal(rng.endContainer.nodeName, 'DIV');263 equal(rng.startOffset, 0);264 equal(rng.endOffset, 0);265 equal(rng.startContainer.innerHTML, '');266 equal(editor.dom.getOuterHTML(rng.cloneContents()).toLowerCase(), '');267 });268 /*test("Caret position before table", function() {269 var table, rng;270 editor.focus();271 editor.setContent('<p>Before</p><table id="table" border="1"><tr><td>Cell 1</td><td>Cell 2</td></tr><tr><td>Cell 3</td><td>Cell 4</td></tr></table><p>After</p>');272 table = editor.dom.get('table');273 rng = editor.selection.getRng();274 rng.moveToElementText(table);275 rng.move('character', -1);276 rng.select();277 rng = editor.selection.getRng(1);278 equal(rng.startContainer.nodeName, 'BODY');279 equal(rng.startOffset, 1);280 equal(rng.endContainer.nodeName, 'BODY');281 equal(rng.endOffset, 1);282 });*/283 test("Selection end within empty element", function() {284 var rng;285 editor.focus();286 editor.getBody().innerHTML = '<p>123</p><p></p>';287 rng = editor.execCommand('SelectAll');288 rng = editor.selection.getRng(true);289 equal(rng.startContainer.nodeName, '#text');290 equal(rng.startOffset, 0);291 equal(rng.endContainer.nodeName, 'BODY');292 equal(rng.endOffset, 2);293 });294 test("Selection after paragraph", function() {295 var rng;296 editor.focus();297 editor.getBody().innerHTML = '<p>123</p><p>abcd</p>';298 rng = editor.dom.createRng();299 rng.setStart(editor.getBody().firstChild, 1);300 rng.setEnd(editor.getBody().firstChild, 1);301 editor.selection.setRng(rng);302 rng = editor.selection.getRng(true);303 ok(rng.startContainer == rng.endContainer);304 equal(rng.startContainer.nodeName, '#text');305 equal(rng.startOffset, 3);306 equal(rng.endContainer.nodeName, '#text');307 equal(rng.endOffset, 3);308 });309 test("Selection of text outside of a block element", function() {310 var r;311 312 editor.settings.forced_root_block = '';313 editor.focus();314 editor.getBody().innerHTML = '<ul><li>Item</li></ul>Text';315 r = editor.dom.createRng();316 r.setStart(editor.getBody().lastChild, 2);317 r.setEnd(editor.getBody().lastChild, 2);318 editor.selection.setRng(r);319 r = editor.selection.getRng(true);320 equal(r.startContainer, editor.getBody().lastChild, "Start container is text node.");321 equal(r.endContainer, editor.getBody().lastChild, "End container is text node.");322 equal(r.startOffset, 2);323 equal(r.endOffset, 2);324 equal(editor.selection.getStart(), editor.getBody(), "Selection start is body.");325 deepEqual(editor.selection.getSelectedBlocks(), [], "No blocks selected.");326 });327 test("Resizable element text selection", function() {328 var r;329 editor.getBody().innerHTML = '<div style="width: 100px; height:100px;"><table><tr><td>.</td></tr></table>abc</div>';330 editor.focus();331 r = editor.dom.createRng();332 r.setStart(editor.getBody().firstChild.lastChild, 1);333 r.setEnd(editor.getBody().firstChild.lastChild, 2);334 editor.selection.setRng(r);335 r = editor.selection.getRng(true);336 equal(r.startContainer, editor.getBody().firstChild.lastChild, "Start container is text node.");337 equal(r.endContainer, editor.getBody().firstChild.lastChild, "End container is text node.");338 equal(r.startOffset, 1);339 equal(r.endOffset, 2);340 });341 test("Resizable element before table selection", function() {342 var r;343 editor.getBody().innerHTML = '<div style="width: 100px; height:100px;"><table><tr><td>.</td></tr></table></div>';344 editor.focus();345 r = editor.dom.createRng();346 r.setStart(editor.getBody().firstChild, 0);347 r.setEnd(editor.getBody().firstChild, 0);348 editor.selection.setRng(r);349 r = editor.selection.getRng(true);350 equal(r.startContainer, editor.getBody().firstChild, "Start container is div node.");351 equal(r.endContainer, editor.getBody().firstChild, "End container is div node.");352 equal(r.startOffset, 0);353 equal(r.endOffset, 0);354 });355 test("Fragmented text nodes after element", function() {356 var r;357 editor.getBody().innerHTML = '<b>x</b>';358 editor.getBody().appendChild(editor.getDoc().createTextNode('1'));359 editor.getBody().appendChild(editor.getDoc().createTextNode('23'));360 editor.getBody().appendChild(editor.getDoc().createTextNode('456'));361 editor.getBody().appendChild(editor.getDoc().createTextNode('7890'));362 editor.focus();363 r = editor.dom.createRng();364 r.setStart(editor.getBody().lastChild, 1);365 r.setEnd(editor.getBody().lastChild, 1);366 editor.selection.setRng(r);367 r = editor.selection.getRng(true);368 equal(r.startContainer, editor.getBody().lastChild, "Start container is last text node.");369 equal(r.endContainer, editor.getBody().lastChild, "End container is last text node.");370 equal(r.startOffset, 1);371 equal(r.endOffset, 1);372 r = editor.dom.createRng();373 r.setStart(editor.getBody().childNodes[2], 2);374 r.setEnd(editor.getBody().childNodes[2], 2);375 editor.selection.setRng(r);376 r = editor.selection.getRng(true);377 equal(r.startContainer, editor.getBody().childNodes[2], "Start container is second text node.");378 equal(r.endContainer, editor.getBody().childNodes[2], "End container is second text node.");379 equal(r.startOffset, 2);380 equal(r.endOffset, 2);381 r = editor.dom.createRng();382 r.setStart(editor.getBody().childNodes[3], 0);383 r.setEnd(editor.getBody().childNodes[3], 1);384 editor.selection.setRng(r);385 r = editor.selection.getRng(true);386 equal(r.startContainer, editor.getBody().childNodes[2], "Start container is second text node (lean left).");387 equal(r.endContainer, editor.getBody().childNodes[3], "End container is third text node.");388 equal(r.startOffset, 2);389 equal(r.endOffset, 1);390 });391 test("Fragmented text nodes before element", function() {392 var r;393 editor.getBody().innerHTML = '';394 editor.getBody().appendChild(editor.getDoc().createTextNode('1'));395 editor.getBody().appendChild(editor.getDoc().createTextNode('23'));396 editor.getBody().appendChild(editor.getDoc().createTextNode('456'));397 editor.getBody().appendChild(editor.getDoc().createTextNode('7890'));398 editor.getBody().appendChild(editor.dom.create('b', null, 'x'));399 editor.focus();400 r = editor.dom.createRng();401 r.setStart(editor.getBody().childNodes[3], 1);402 r.setEnd(editor.getBody().childNodes[3], 1);403 editor.selection.setRng(r);404 r = editor.selection.getRng(true);405 equal(r.startContainer, editor.getBody().childNodes[3], "Start container is last text node.");406 equal(r.endContainer, editor.getBody().childNodes[3], "End container is last text node.");407 equal(r.startContainer.nodeValue, '7890');408 equal(r.startOffset, 1);409 equal(r.endOffset, 1);410 r = editor.dom.createRng();411 r.setStart(editor.getBody().childNodes[1], 2);412 r.setEnd(editor.getBody().childNodes[1], 2);413 editor.selection.setRng(r);414 r = editor.selection.getRng(true);415 equal(r.startContainer, editor.getBody().childNodes[2], "Start container is second text node. (lean right)");416 equal(r.endContainer, editor.getBody().childNodes[2], "End container is second text node.");417 equal(r.startContainer.nodeValue, '456');418 equal(r.startOffset, 0);419 equal(r.endOffset, 0);420 r = editor.dom.createRng();421 r.setStart(editor.getBody().childNodes[1], 0);422 r.setEnd(editor.getBody().childNodes[1], 1);423 editor.selection.setRng(r);424 r = editor.selection.getRng(true);425 equal(r.startContainer, editor.getBody().childNodes[1], "Start container is second text node.");426 equal(r.endContainer, editor.getBody().childNodes[1], "End container is third text node.");427 equal(r.startContainer.nodeValue, '23');428 equal(r.endContainer.nodeValue, '23');429 equal(r.startOffset, 0);430 equal(r.endOffset, 1);431 });432 test("Non contentEditable elements", function() {433 var r;434 editor.getBody().innerHTML = '<span contentEditable="false">a</span><span contentEditable="false">a</span><span contentEditable="false">a</span>';435 editor.focus();436 r = editor.dom.createRng();437 r.setStart(editor.getBody(), 0);438 r.setEnd(editor.getBody(), 0);439 editor.selection.setRng(r);440 r = editor.selection.getRng(true);441 equal(r.startContainer, editor.getBody(), "Start container is before first nonEditable.");442 equal(r.endContainer, editor.getBody(), "End container is before first nonEditable.");443 equal(r.startOffset, 0);444 equal(r.endOffset, 0);445 r = editor.dom.createRng();446 r.setStart(editor.getBody(), 0);447 r.setEnd(editor.getBody(), 1);448 editor.selection.setRng(r);449 r = editor.selection.getRng(true);450 equal(r.startContainer, editor.getBody(), "Start container before first nonEditable.");451 equal(r.endContainer, editor.getBody(), "End container is after first nonEditable.");452 equal(r.startOffset, 0);453 equal(r.endOffset, 1);454 r = editor.dom.createRng();455 r.setStart(editor.getBody(), 0);456 r.setEnd(editor.getBody(), 2);457 editor.selection.setRng(r);458 r = editor.selection.getRng(true);459 equal(r.startContainer, editor.getBody(), "Start container before first nonEditable.");460 equal(r.endContainer, editor.getBody(), "End container is after second nonEditable.");461 equal(r.startOffset, 0);462 equal(r.endOffset, 2);463 r = editor.dom.createRng();464 r.setStart(editor.getBody(), 1);465 r.setEnd(editor.getBody(), 1);466 editor.selection.setRng(r);467 r = editor.selection.getRng(true);468 equal(r.startContainer, editor.getBody(), "Start container is before second nonEditable.");469 equal(r.endContainer, editor.getBody(), "End container is div before second nonEditable.");470 equal(r.startOffset, 1);471 equal(r.endOffset, 1);472 r = editor.dom.createRng();473 r.setStart(editor.getBody(), 2);474 r.setEnd(editor.getBody(), 2);475 editor.selection.setRng(r);476 r = editor.selection.getRng(true);477 equal(r.startContainer, editor.getBody(), "Start container is after last nonEditable.");478 equal(r.endContainer, editor.getBody(), "End container is after last nonEditable.");479 equal(r.startOffset, 2);480 equal(r.endOffset, 2);481 });482} else {483 test("Skipped ie_selection tests as not running in IE.", function() {484 ok(true, "Dummy assert");485 });...

Full Screen

Full Screen

rng.js

Source:rng.js Github

copy

Full Screen

1// Random number generator - requires a PRNG backend, e.g. prng4.js2// For best results, put code like3// <body onClick='rng_seed_time();' onKeyPress='rng_seed_time();'>4// in your main HTML document.5var rng_state;6var rng_pool;7var rng_pptr;8// Mix in a 32-bit integer into the pool9function rng_seed_int(x) {10 rng_pool[rng_pptr++] ^= x & 255;11 rng_pool[rng_pptr++] ^= (x >> 8) & 255;12 rng_pool[rng_pptr++] ^= (x >> 16) & 255;13 rng_pool[rng_pptr++] ^= (x >> 24) & 255;14 if(rng_pptr >= rng_psize) rng_pptr -= rng_psize;15}16// Mix in the current time (w/milliseconds) into the pool17function rng_seed_time() {18 rng_seed_int(new Date().getTime());19}20// Initialize the pool with junk if needed.21if(rng_pool == null) {22 rng_pool = new Array();23 rng_pptr = 0;24 var t;25 if(window.crypto && window.crypto.getRandomValues) {26 // Use webcrypto if available27 var ua = new Uint8Array(32);28 window.crypto.getRandomValues(ua);29 for(t = 0; t < 32; ++t)30 rng_pool[rng_pptr++] = ua[t];31 }32 if(navigator.appName == "Netscape" && navigator.appVersion < "5" && window.crypto) {33 // Extract entropy (256 bits) from NS4 RNG if available34 var z = window.crypto.random(32);35 for(t = 0; t < z.length; ++t)36 rng_pool[rng_pptr++] = z.charCodeAt(t) & 255;37 } 38 while(rng_pptr < rng_psize) { // extract some randomness from Math.random()39 t = Math.floor(65536 * Math.random());40 rng_pool[rng_pptr++] = t >>> 8;41 rng_pool[rng_pptr++] = t & 255;42 }43 rng_pptr = 0;44 rng_seed_time();45 //rng_seed_int(window.screenX);46 //rng_seed_int(window.screenY);47}48function rng_get_byte() {49 if(rng_state == null) {50 rng_seed_time();51 rng_state = prng_newstate();52 rng_state.init(rng_pool);53 for(rng_pptr = 0; rng_pptr < rng_pool.length; ++rng_pptr)54 rng_pool[rng_pptr] = 0;55 rng_pptr = 0;56 //rng_pool = null;57 }58 // TODO: allow reseeding after first request59 return rng_state.next();60}61function rng_get_bytes(ba) {62 var i;63 for(i = 0; i < ba.length; ++i) ba[i] = rng_get_byte();64}65function SecureRandom() {}...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1import {rng} from 'fast-check';2import {rng} from 'fast-check';3import {rng} from 'fast-check';4import {rng} from 'fast-check';5import {rng} from 'fast-check';6import {rng} from 'fast-check';7import {rng} from 'fast-check';8import {rng} from 'fast-check';9import {rng} from 'fast-check';10import {rng} from 'fast-check';11import {rng} from 'fast-check';12import {rng} from 'fast-check';13import {rng} from 'fast-check';14import {rng} from 'fast-check';15import {rng} from 'fast-check';16import {rng} from 'fast-check';17import {rng} from 'fast-check';18import {rng} from 'fast-check';19import {rng} from 'fast-check';20import {rng} from 'fast-check';

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require('fast-check');2const { rng } = require('fast-check/lib/check/arbitrary/definition/RngArbitraryWrapper.js');3const { Random } = require('fast-check/lib/random/generator/Random.js');4const { RandomNumberGenerator } = require('fast-check/lib/random/RandomNumberGenerator.js');5const { RandomArray } = require('fast-check/lib/random/array/RandomArray.js');6const rngArbitrary = rng();7const random = new Random(new RandomNumberGenerator(new RandomArray(0)));8const randomValue = rngArbitrary.generate(random);9const fc = require('fast-check');10const { rng } = require('fast-check/lib/check/arbitrary/definition/RngArbitraryWrapper.js');11const rngArbitrary = rng();12const randomValue = rngArbitrary.generate(fc.random());

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require('fast-check');2const { rng } = fc;3const myRng = rng();4console.log(myRng.nextInt());5const fastify = require('fastify')({ logger: true })6const mongoose = require('mongoose')7const User = require('./models/user')8const start = async () => {9 try {10 })11 console.log('Connected to DB')12 } catch (err) {13 console.log(err)14 }15 fastify.get('/users', async (request, reply) => {16 const users = await User.find({})17 })18 fastify.post('/users', async (request, reply) => {19 const user = new User(request.body)20 await user.save()21 })22 fastify.get('/users/:id', async (request, reply) => {23 const user = await User.findById(request.params.id)24 })25 fastify.delete('/users/:

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run fast-check-monorepo automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful