How to use root method in Jasmine-core

Best JavaScript code snippet using jasmine-core

Run Jasmine-core automation tests on LambdaTest cloud grid

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

binaryTree.js

Source: binaryTree.js Github

copy
1'use strict';
2
3class Node {
4  constructor(value) {
5    this.value = value;
6    this.left = null;
7    this.right = null;
8  }
9}
10
11class BinaryTree {
12  constructor() {
13    this.root = null;
14  }
15
16  preOrder() {
17
18    let resultArr = [];
19
20    let _preOrder = (root) => {
21
22      if (root !== null) {
23        resultArr.push(root.value);
24        if (root.left !== null) {
25          _preOrder(root.left);
26        }
27        if (root.right !== null) {
28          _preOrder(root.right);
29        }
30      }
31
32    };
33    _preOrder(this.root);
34    return resultArr;
35  }
36
37  inOrder() {
38
39    let resultArr = [];
40
41    let _inOrder = (root) => {
42
43      if (root !== null) {
44
45        if (root.left !== null) {
46          _inOrder(root.left);
47        }
48
49        resultArr.push(root.value);
50
51        if (root.right !== null) {
52          _inOrder(root.right);
53        }
54      }
55    };
56    _inOrder(this.root);
57    return resultArr;
58  }
59
60  postOrder() {
61
62    let resultArr = [];
63
64    let _postOrder = (root) => {
65      if (root !== null) {
66
67        if (root.left !== null) {
68          _postOrder(root.left);
69        }
70
71        if (root.right !== null) {
72          _postOrder(root.right);
73        }
74
75        resultArr.push(root.value);
76      }
77    };
78    _postOrder(this.root);
79    return resultArr;
80  }
81
82  addBinarySearch(value) {
83
84    let _add = (inputValue, root) => {
85
86      if (root === null) {
87        root = new Node(inputValue);
88      }
89      else if (root.value === inputValue) {
90        throw `Binary Search Tree already contains ${inputValue}. Nothing was added to the tree`;
91      }
92      else if (inputValue < root.value) {
93        root.left = _add(inputValue, root.left);
94      }
95      else if (inputValue > root.value) {
96        root.right = _add(inputValue, root.right);
97      }
98
99      return root;
100    };
101
102    try {
103      this.root = _add(value, this.root);
104    } catch (error) {
105      console.log(error);
106    }
107  }
108
109  findMax() {
110    if (this.root === null) {
111      throw 'Error: Binary Tree is empty';
112    }
113    else {
114      let max = null;
115      let _preOrderMax = (root, maxValue) => {
116        if (root !== null) {
117
118          if (root.value > maxValue) {
119            maxValue = root.value;
120          }
121
122          if (root.left !== null) {
123            maxValue = _preOrderMax(root.left, maxValue);
124          }
125
126          if (root.right !== null) {
127            maxValue = _preOrderMax(root.right, maxValue);
128          }
129        }
130        return maxValue;
131      };
132
133      max = this.root.value;
134      return _preOrderMax(this.root, max);
135    }
136  }
137}
138
139module.exports = BinaryTree;
140
141
Full Screen

rootelement.js

Source: rootelement.js Github

copy
1/**
2 * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
3 * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4 */
5
6import Model from '../../src/model/model';
7import Element from '../../src/model/element';
8import RootElement from '../../src/model/rootelement';
9import count from '@ckeditor/ckeditor5-utils/src/count';
10
11describe( 'RootElement', () => {
12	describe( 'constructor()', () => {
13		it( 'should create root element without attributes', () => {
14			const model = new Model();
15			const doc = model.document;
16			const root = new RootElement( doc );
17
18			expect( root ).to.be.an.instanceof( Element );
19			expect( root ).to.have.property( 'document' ).that.equals( doc );
20			expect( count( root.getAttributes() ) ).to.equal( 0 );
21			expect( root.childCount ).to.equal( 0 );
22		} );
23	} );
24
25	describe( 'is()', () => {
26		let root;
27
28		before( () => {
29			const model = new Model();
30			const doc = model.document;
31
32			root = new RootElement( doc, '$root' );
33		} );
34
35		it( 'should return true for rootElement, element, element with same name and element name', () => {
36			expect( root.is( 'element', '$root' ) ).to.be.true;
37			expect( root.is( 'model:element', '$root' ) ).to.be.true;
38			expect( root.is( 'element' ) ).to.be.true;
39			expect( root.is( 'model:element' ) ).to.be.true;
40			expect( root.is( 'rootElement', '$root' ) ).to.be.true;
41			expect( root.is( 'model:rootElement', '$root' ) ).to.be.true;
42			expect( root.is( 'rootElement' ) ).to.be.true;
43			expect( root.is( 'model:rootElement' ) ).to.be.true;
44			expect( root.is( 'node' ) ).to.be.true;
45			expect( root.is( 'model:node' ) ).to.be.true;
46		} );
47
48		it( 'should return false for other accept values', () => {
49			expect( root.is( 'element', '$graveyard' ) ).to.be.false;
50			expect( root.is( 'model:element', '$graveyard' ) ).to.be.false;
51			expect( root.is( 'rootElement', '$graveyard' ) ).to.be.false;
52			expect( root.is( 'model:rootElement', '$graveyard' ) ).to.be.false;
53			expect( root.is( '$graveyard' ) ).to.be.false;
54			expect( root.is( '$text' ) ).to.be.false;
55			expect( root.is( '$textProxy' ) ).to.be.false;
56			expect( root.is( 'documentFragment' ) ).to.be.false;
57			expect( root.is( 'view:element' ) ).to.be.false;
58			expect( root.is( '$root' ) ).to.be.false;
59			expect( root.is( 'model:$root' ) ).to.be.false;
60			expect( root.is( 'node', '$root' ) ).to.be.false;
61			expect( root.is( 'model:node', '$root' ) ).to.be.false;
62		} );
63	} );
64} );
65
Full Screen

DependencyGraph-test.js

Source: DependencyGraph-test.js Github

copy
1'use strict';
2
3jest
4  .dontMock('../index')
5  .dontMock('q')
6  .dontMock('path')
7  .dontMock('absolute-path')
8  .dontMock('../docblock')
9  .setMock('../../../ModuleDescriptor', function(data) {return data;});
10
11describe('DependencyGraph', function() {
12  var DependencyGraph;
13  var fileWatcher;
14  var fs;
15
16  beforeEach(function() {
17    fs = require('fs');
18    DependencyGraph = require('../index');
19
20    fileWatcher = {
21      on: function() {
22        return this;
23      }
24    };
25  });
26
27  describe('getOrderedDependencies', function() {
28    pit('should get dependencies', function() {
29      var root = '/root';
30      fs.__setMockFilesystem({
31        'root': {
32          'index.js': [
33            '/**',
34            ' * @providesModule index',
35            ' */',
36            'require("a")'
37          ].join('\n'),
38          'a.js': [
39            '/**',
40            ' * @providesModule a',
41            ' */',
42          ].join('\n'),
43        }
44      });
45
46      var dgraph = new DependencyGraph({
47        roots: [root],
48        fileWatcher: fileWatcher
49      });
50      return dgraph.load().then(function() {
51        expect(dgraph.getOrderedDependencies('/root/index.js'))
52          .toEqual([
53            {id: 'index', path: '/root/index.js', dependencies: ['a']},
54            {id: 'a', path: '/root/a.js', dependencies: []},
55          ]);
56      });
57    });
58
59    pit('should get dependencies', function() {
60      var root = '/root';
61      fs.__setMockFilesystem({
62        'root': {
63          'index.js': [
64            '/**',
65            ' * @providesModule index',
66            ' */',
67            'require("image!a")'
68          ].join('\n'),
69          'imgs': {
70            'a.png': ''
71          },
72        }
73      });
74
75      var dgraph = new DependencyGraph({
76        roots: [root],
77        fileWatcher: fileWatcher,
78        assetRoots: ['/root/imgs']
79      });
80      return dgraph.load().then(function() {
81        expect(dgraph.getOrderedDependencies('/root/index.js'))
82          .toEqual([
83            {id: 'index', path: '/root/index.js', dependencies: ['image!a']},
84            {  id: 'image!a',
85               path: '/root/imgs/a.png',
86               dependencies: [],
87               isAsset: true
88            },
89          ]);
90      });
91    });
92
93    pit('should get recursive dependencies', function() {
94      var root = '/root';
95      fs.__setMockFilesystem({
96        'root': {
97          'index.js': [
98            '/**',
99            ' * @providesModule index',
100            ' */',
101            'require("a")',
102          ].join('\n'),
103          'a.js': [
104            '/**',
105            ' * @providesModule a',
106            ' */',
107            'require("index")',
108          ].join('\n'),
109        }
110      });
111
112      var dgraph = new DependencyGraph({
113        roots: [root],
114        fileWatcher: fileWatcher
115      });
116      return dgraph.load().then(function() {
117        expect(dgraph.getOrderedDependencies('/root/index.js'))
118          .toEqual([
119            {id: 'index', path: '/root/index.js', dependencies: ['a']},
120            {id: 'a', path: '/root/a.js', dependencies: ['index']},
121          ]);
122      });
123    });
124
125    pit('should work with packages', function() {
126      var root = '/root';
127      fs.__setMockFilesystem({
128        'root': {
129          'index.js': [
130            '/**',
131            ' * @providesModule index',
132            ' */',
133            'require("aPackage")',
134          ].join('\n'),
135          'aPackage': {
136            'package.json': JSON.stringify({
137              name: 'aPackage',
138              main: 'main.js'
139            }),
140            'main.js': 'lol'
141          }
142        }
143      });
144
145      var dgraph = new DependencyGraph({
146        roots: [root],
147        fileWatcher: fileWatcher
148      });
149      return dgraph.load().then(function() {
150        expect(dgraph.getOrderedDependencies('/root/index.js'))
151          .toEqual([
152            {id: 'index', path: '/root/index.js', dependencies: ['aPackage']},
153            { id: 'aPackage/main',
154              path: '/root/aPackage/main.js',
155              dependencies: []
156            },
157          ]);
158      });
159    });
160
161    pit('should ignore malformed packages', function() {
162      var root = '/root';
163      fs.__setMockFilesystem({
164        'root': {
165          'index.js': [
166            '/**',
167            ' * @providesModule index',
168            ' */',
169            'require("aPackage")',
170          ].join('\n'),
171          'aPackage': {
172            'package.json': 'lol',
173            'main.js': 'lol'
174          }
175        }
176      });
177
178      var dgraph = new DependencyGraph({
179        roots: [root],
180        fileWatcher: fileWatcher
181      });
182      return dgraph.load().then(function() {
183        expect(dgraph.getOrderedDependencies('/root/index.js'))
184          .toEqual([
185            {id: 'index', path: '/root/index.js', dependencies: ['aPackage']},
186          ]);
187      });
188    });
189
190    pit('can have multiple modules with the same name', function() {
191      var root = '/root';
192      fs.__setMockFilesystem({
193        'root': {
194          'index.js': [
195            '/**',
196            ' * @providesModule index',
197            ' */',
198            'require("b")',
199          ].join('\n'),
200          'b.js': [
201            '/**',
202            ' * @providesModule b',
203            ' */',
204          ].join('\n'),
205          'c.js': [
206            '/**',
207            ' * @providesModule c',
208            ' */',
209          ].join('\n'),
210          'somedir': {
211            'somefile.js': [
212              '/**',
213              ' * @providesModule index',
214              ' */',
215              'require("c")',
216            ].join('\n')
217          }
218        }
219      });
220
221      var dgraph = new DependencyGraph({
222        roots: [root],
223        fileWatcher: fileWatcher
224      });
225      return dgraph.load().then(function() {
226        expect(dgraph.getOrderedDependencies('/root/somedir/somefile.js'))
227          .toEqual([
228            { id: 'index',
229              path: '/root/somedir/somefile.js',
230              dependencies: ['c']
231            },
232            { id: 'c',
233              path: '/root/c.js',
234              dependencies: []
235            },
236          ]);
237      });
238    });
239
240    pit('providesModule wins when conflict with package', function() {
241      var root = '/root';
242      fs.__setMockFilesystem({
243        'root': {
244          'index.js': [
245            '/**',
246            ' * @providesModule index',
247            ' */',
248            'require("aPackage")',
249          ].join('\n'),
250          'b.js': [
251            '/**',
252            ' * @providesModule aPackage',
253            ' */',
254          ].join('\n'),
255          'aPackage': {
256            'package.json': JSON.stringify({
257              name: 'aPackage',
258              main: 'main.js'
259            }),
260            'main.js': 'lol'
261          }
262        }
263      });
264
265      var dgraph = new DependencyGraph({
266        roots: [root],
267        fileWatcher: fileWatcher
268      });
269      return dgraph.load().then(function() {
270        expect(dgraph.getOrderedDependencies('/root/index.js'))
271          .toEqual([
272            { id: 'index',
273              path: '/root/index.js',
274              dependencies: ['aPackage']
275            },
276            { id: 'aPackage',
277              path: '/root/b.js',
278              dependencies: []
279            },
280          ]);
281      });
282    });
283
284    pit('should be forgiving with missing requires', function() {
285      var root = '/root';
286      fs.__setMockFilesystem({
287        'root': {
288          'index.js': [
289            '/**',
290            ' * @providesModule index',
291            ' */',
292            'require("lolomg")',
293          ].join('\n')
294        }
295      });
296
297      var dgraph = new DependencyGraph({
298        roots: [root],
299        fileWatcher: fileWatcher
300      });
301      return dgraph.load().then(function() {
302        expect(dgraph.getOrderedDependencies('/root/index.js'))
303          .toEqual([
304            { id: 'index',
305              path: '/root/index.js',
306              dependencies: ['lolomg']
307            }
308          ]);
309      });
310    });
311
312    pit('should work with packages with subdirs', function() {
313      var root = '/root';
314      fs.__setMockFilesystem({
315        'root': {
316          'index.js': [
317            '/**',
318            ' * @providesModule index',
319            ' */',
320            'require("aPackage/subdir/lolynot")',
321          ].join('\n'),
322          'aPackage': {
323            'package.json': JSON.stringify({
324              name: 'aPackage',
325              main: 'main.js'
326            }),
327            'main.js': 'lol',
328            'subdir': {
329              'lolynot.js': 'lolynot'
330            }
331          }
332        }
333      });
334
335      var dgraph = new DependencyGraph({
336        roots: [root],
337        fileWatcher: fileWatcher
338      });
339      return dgraph.load().then(function() {
340        expect(dgraph.getOrderedDependencies('/root/index.js'))
341          .toEqual([
342            { id: 'index',
343              path: '/root/index.js',
344              dependencies: ['aPackage/subdir/lolynot']
345            },
346            { id: 'aPackage/subdir/lolynot',
347              path: '/root/aPackage/subdir/lolynot.js',
348              dependencies: []
349            },
350          ]);
351      });
352    });
353
354    pit('should work with packages with symlinked subdirs', function() {
355      var root = '/root';
356      fs.__setMockFilesystem({
357        'symlinkedPackage': {
358          'package.json': JSON.stringify({
359            name: 'aPackage',
360            main: 'main.js'
361          }),
362          'main.js': 'lol',
363          'subdir': {
364            'lolynot.js': 'lolynot'
365          }
366        },
367        'root': {
368          'index.js': [
369            '/**',
370            ' * @providesModule index',
371            ' */',
372            'require("aPackage/subdir/lolynot")',
373          ].join('\n'),
374          'aPackage': { SYMLINK: '/symlinkedPackage' },
375        }
376      });
377
378      var dgraph = new DependencyGraph({
379        roots: [root],
380        fileWatcher: fileWatcher
381      });
382      return dgraph.load().then(function() {
383        expect(dgraph.getOrderedDependencies('/root/index.js'))
384          .toEqual([
385            { id: 'index',
386              path: '/root/index.js',
387              dependencies: ['aPackage/subdir/lolynot']
388            },
389            { id: 'aPackage/subdir/lolynot',
390              path: '/symlinkedPackage/subdir/lolynot.js',
391              dependencies: []
392            },
393          ]);
394      });
395    });
396
397    pit('should work with relative modules in packages', function() {
398      var root = '/root';
399      fs.__setMockFilesystem({
400        'root': {
401          'index.js': [
402            '/**',
403            ' * @providesModule index',
404            ' */',
405            'require("aPackage")',
406          ].join('\n'),
407          'aPackage': {
408            'package.json': JSON.stringify({
409              name: 'aPackage',
410              main: 'main.js'
411            }),
412            'main.js': 'require("./subdir/lolynot")',
413            'subdir': {
414              'lolynot.js': 'require("../other")'
415            },
416            'other.js': 'some code'
417          }
418        }
419      });
420
421      var dgraph = new DependencyGraph({
422        roots: [root],
423        fileWatcher: fileWatcher
424      });
425      return dgraph.load().then(function() {
426        expect(dgraph.getOrderedDependencies('/root/index.js'))
427          .toEqual([
428            { id: 'index',
429              path: '/root/index.js',
430              dependencies: ['aPackage']
431            },
432            { id: 'aPackage/main',
433              path: '/root/aPackage/main.js',
434              dependencies: ['./subdir/lolynot']
435            },
436            { id: 'aPackage/subdir/lolynot',
437              path: '/root/aPackage/subdir/lolynot.js',
438              dependencies: ['../other']
439            },
440            { id: 'aPackage/other',
441              path: '/root/aPackage/other.js',
442              dependencies: []
443            },
444          ]);
445      });
446    });
447  });
448
449  describe('file watch updating', function() {
450    var triggerFileChange;
451
452    beforeEach(function() {
453      fileWatcher = {
454        on: function(eventType, callback) {
455          if (eventType !== 'all') {
456            throw new Error('Can only handle "all" event in watcher.');
457          }
458          triggerFileChange = callback;
459          return this;
460        }
461      };
462    });
463
464    pit('updates module dependencies', function() {
465      var root = '/root';
466      var filesystem = fs.__setMockFilesystem({
467        'root': {
468          'index.js': [
469            '/**',
470            ' * @providesModule index',
471            ' */',
472            'require("aPackage")',
473            'require("foo")'
474          ].join('\n'),
475          'foo': [
476            '/**',
477            ' * @providesModule foo',
478            ' */',
479            'require("aPackage")'
480          ].join('\n'),
481          'aPackage': {
482            'package.json': JSON.stringify({
483              name: 'aPackage',
484              main: 'main.js'
485            }),
486            'main.js': 'main',
487          }
488        }
489      });
490
491      var dgraph = new DependencyGraph({
492        roots: [root],
493        fileWatcher: fileWatcher
494      });
495      return dgraph.load().then(function() {
496        filesystem.root['index.js'] =
497          filesystem.root['index.js'].replace('require("foo")', '');
498        triggerFileChange('change', 'index.js', root);
499        return dgraph.load().then(function() {
500          expect(dgraph.getOrderedDependencies('/root/index.js'))
501            .toEqual([
502            { id: 'index',
503              path: '/root/index.js',
504              dependencies: ['aPackage']
505            },
506            { id: 'aPackage/main',
507              path: '/root/aPackage/main.js',
508              dependencies: []
509            },
510          ]);
511        });
512      });
513    });
514
515    pit('updates module dependencies on file change', function() {
516      var root = '/root';
517      var filesystem = fs.__setMockFilesystem({
518        'root': {
519          'index.js': [
520            '/**',
521            ' * @providesModule index',
522            ' */',
523            'require("aPackage")',
524            'require("foo")'
525          ].join('\n'),
526          'foo.js': [
527            '/**',
528            ' * @providesModule foo',
529            ' */',
530            'require("aPackage")'
531          ].join('\n'),
532          'aPackage': {
533            'package.json': JSON.stringify({
534              name: 'aPackage',
535              main: 'main.js'
536            }),
537            'main.js': 'main',
538          }
539        }
540      });
541
542      var dgraph = new DependencyGraph({
543        roots: [root],
544        fileWatcher: fileWatcher
545      });
546      return dgraph.load().then(function() {
547        filesystem.root['index.js'] =
548          filesystem.root['index.js'].replace('require("foo")', '');
549        triggerFileChange('change', 'index.js', root);
550        return dgraph.load().then(function() {
551          expect(dgraph.getOrderedDependencies('/root/index.js'))
552            .toEqual([
553            { id: 'index',
554              path: '/root/index.js',
555              dependencies: ['aPackage']
556            },
557            { id: 'aPackage/main',
558              path: '/root/aPackage/main.js',
559              dependencies: []
560            },
561          ]);
562        });
563      });
564    });
565
566    pit('updates module dependencies on file delete', function() {
567      var root = '/root';
568      var filesystem = fs.__setMockFilesystem({
569        'root': {
570          'index.js': [
571            '/**',
572            ' * @providesModule index',
573            ' */',
574            'require("aPackage")',
575            'require("foo")'
576          ].join('\n'),
577          'foo.js': [
578            '/**',
579            ' * @providesModule foo',
580            ' */',
581            'require("aPackage")'
582          ].join('\n'),
583          'aPackage': {
584            'package.json': JSON.stringify({
585              name: 'aPackage',
586              main: 'main.js'
587            }),
588            'main.js': 'main',
589          }
590        }
591      });
592
593      var dgraph = new DependencyGraph({
594        roots: [root],
595        fileWatcher: fileWatcher
596      });
597      return dgraph.load().then(function() {
598        delete filesystem.root.foo;
599        triggerFileChange('delete', 'foo.js', root);
600        return dgraph.load().then(function() {
601          expect(dgraph.getOrderedDependencies('/root/index.js'))
602            .toEqual([
603            { id: 'index',
604              path: '/root/index.js',
605              dependencies: ['aPackage', 'foo']
606            },
607            { id: 'aPackage/main',
608              path: '/root/aPackage/main.js',
609              dependencies: []
610            },
611          ]);
612        });
613      });
614    });
615
616    pit('updates module dependencies on file add', function() {
617      var root = '/root';
618      var filesystem = fs.__setMockFilesystem({
619        'root': {
620          'index.js': [
621            '/**',
622            ' * @providesModule index',
623            ' */',
624            'require("aPackage")',
625            'require("foo")'
626          ].join('\n'),
627          'foo.js': [
628            '/**',
629            ' * @providesModule foo',
630            ' */',
631            'require("aPackage")'
632          ].join('\n'),
633          'aPackage': {
634            'package.json': JSON.stringify({
635              name: 'aPackage',
636              main: 'main.js'
637            }),
638            'main.js': 'main',
639          }
640        }
641      });
642
643      var dgraph = new DependencyGraph({
644        roots: [root],
645        fileWatcher: fileWatcher
646      });
647      return dgraph.load().then(function() {
648        filesystem.root['bar.js'] = [
649          '/**',
650          ' * @providesModule bar',
651          ' */',
652          'require("foo")'
653        ].join('\n');
654        triggerFileChange('add', 'bar.js', root);
655
656        filesystem.root.aPackage['main.js'] = 'require("bar")';
657        triggerFileChange('change', 'aPackage/main.js', root);
658
659        return dgraph.load().then(function() {
660          expect(dgraph.getOrderedDependencies('/root/index.js'))
661            .toEqual([
662            { id: 'index',
663              path: '/root/index.js',
664              dependencies: ['aPackage', 'foo']
665            },
666            { id: 'aPackage/main',
667              path: '/root/aPackage/main.js',
668              dependencies: ['bar']
669            },
670            { id: 'bar',
671              path: '/root/bar.js',
672              dependencies: ['foo']
673            },
674            { id: 'foo',
675              path: '/root/foo.js',
676              dependencies: ['aPackage']
677            },
678          ]);
679        });
680      });
681    });
682
683    pit('runs changes through ignore filter', function() {
684      var root = '/root';
685      var filesystem = fs.__setMockFilesystem({
686        'root': {
687          'index.js': [
688            '/**',
689            ' * @providesModule index',
690            ' */',
691            'require("aPackage")',
692            'require("foo")'
693          ].join('\n'),
694          'foo.js': [
695            '/**',
696            ' * @providesModule foo',
697            ' */',
698            'require("aPackage")'
699          ].join('\n'),
700          'aPackage': {
701            'package.json': JSON.stringify({
702              name: 'aPackage',
703              main: 'main.js'
704            }),
705            'main.js': 'main',
706          }
707        }
708      });
709
710      var dgraph = new DependencyGraph({
711        roots: [root],
712        fileWatcher: fileWatcher,
713        ignoreFilePath: function(filePath) {
714          if (filePath === '/root/bar.js') {
715            return true;
716          }
717          return false;
718        }
719      });
720      return dgraph.load().then(function() {
721        filesystem.root['bar.js'] = [
722          '/**',
723          ' * @providesModule bar',
724          ' */',
725          'require("foo")'
726        ].join('\n');
727        triggerFileChange('add', 'bar.js', root);
728
729        filesystem.root.aPackage['main.js'] = 'require("bar")';
730        triggerFileChange('change', 'aPackage/main.js', root);
731
732        return dgraph.load().then(function() {
733          expect(dgraph.getOrderedDependencies('/root/index.js'))
734            .toEqual([
735              { id: 'index',
736                path: '/root/index.js',
737                dependencies: ['aPackage', 'foo']
738              },
739              { id: 'aPackage/main',
740                path: '/root/aPackage/main.js',
741                dependencies: ['bar']
742              },
743              { id: 'foo',
744                path: '/root/foo.js',
745                dependencies: ['aPackage']
746              },
747            ]);
748        });
749      });
750    });
751
752    pit('should ignore directory updates', function() {
753      var root = '/root';
754      fs.__setMockFilesystem({
755        'root': {
756          'index.js': [
757            '/**',
758            ' * @providesModule index',
759            ' */',
760            'require("aPackage")',
761            'require("foo")'
762          ].join('\n'),
763          'foo.js': [
764            '/**',
765            ' * @providesModule foo',
766            ' */',
767            'require("aPackage")'
768          ].join('\n'),
769          'aPackage': {
770            'package.json': JSON.stringify({
771              name: 'aPackage',
772              main: 'main.js'
773            }),
774            'main.js': 'main',
775          }
776        }
777      });
778      var dgraph = new DependencyGraph({
779        roots: [root],
780        fileWatcher: fileWatcher
781      });
782      return dgraph.load().then(function() {
783        triggerFileChange('change', 'aPackage', '/root', {
784          isDirectory: function(){ return true; }
785        });
786        return dgraph.load().then(function() {
787          expect(dgraph.getOrderedDependencies('/root/index.js'))
788            .toEqual([
789              { id: 'index',
790                path: '/root/index.js',
791                dependencies: ['aPackage', 'foo']
792              },
793              { id: 'aPackage/main',
794                path: '/root/aPackage/main.js',
795                dependencies: []
796              },
797              { id: 'foo',
798                path: '/root/foo.js',
799                dependencies: ['aPackage']
800              },
801            ]);
802        });
803      });
804    });
805  });
806});
807
Full Screen

Node.js

Source: Node.js Github

copy
1module("tinymce.html.Node");
2
3test('construction', function() {
4	var node;
5
6	expect(15);
7
8	node = new tinymce.html.Node('#text', 3);
9	equal(node.name, '#text');
10	equal(node.type, 3);
11
12	node = new tinymce.html.Node('#comment', 8);
13	equal(node.name, '#comment');
14	equal(node.type, 8);
15
16	node = new tinymce.html.Node('b', 1);
17	equal(node.name, 'b');
18	equal(node.type, 1);
19	deepEqual(node.attributes, []);
20
21	node = new tinymce.html.Node('#pi', 7);
22	equal(node.name, '#pi');
23	equal(node.type, 7);
24
25	node = new tinymce.html.Node('#doctype', 10);
26	equal(node.name, '#doctype');
27	equal(node.type, 10);
28
29	node = new tinymce.html.Node('#cdata', 4);
30	equal(node.name, '#cdata');
31	equal(node.type, 4);
32
33	node = new tinymce.html.Node('#frag', 11);
34	equal(node.name, '#frag');
35	equal(node.type, 11);
36});
37
38test('append inside empty node', function() {
39	var root, node;
40
41	expect(10);
42
43	root = new tinymce.html.Node('#frag', 11);
44	node = root.append(new tinymce.html.Node('b', 1));
45	ok(root.firstChild.parent === root);
46	equal(root.firstChild.next, undefined);
47	equal(root.firstChild.prev, undefined);
48	equal(root.firstChild.firstChild, undefined);
49	equal(root.firstChild.lastChild, undefined);
50	ok(node.parent === root);
51	equal(node.next, undefined);
52	equal(node.prev, undefined);
53	equal(node.firstChild, undefined);
54	equal(node.lastChild, undefined);
55});
56
57test('append node after node', function() {
58	var root, node, node2;
59
60	expect(17);
61
62	root = new tinymce.html.Node('#frag', 11);
63	node2 = root.append(new tinymce.html.Node('a', 1));
64	node = root.append(new tinymce.html.Node('b', 1));
65	ok(root.firstChild.parent === root, 'root.firstChild.parent === root');
66	ok(root.firstChild === node2, 'root.firstChild');
67	ok(root.lastChild === node, 'root.firstChild');
68	ok(root.firstChild.next === node, 'root.firstChild.next');
69	equal(root.firstChild.prev, undefined, 'root.firstChild.prev');
70	equal(root.firstChild.firstChild, undefined, 'root.firstChild.firstChild');
71	equal(root.firstChild.lastChild, undefined, 'root.firstChild.lastChild');
72	ok(node2.parent === root, 'node2.parent === root');
73	ok(node2.next === node, 'node2.next');
74	equal(node2.prev, undefined, 'node2.prev');
75	equal(node2.firstChild, undefined, 'node2.firstChild');
76	equal(node2.lastChild, undefined, 'node2.lastChild');
77	ok(node.parent === root, 'node.parent === root');
78	equal(node.next, undefined, 'node.next');
79	ok(node.prev === node2, 'node.prev');
80	equal(node.firstChild, undefined, 'node.firstChild');
81	equal(node.lastChild, undefined, 'node.lastChild');
82});
83
84test('append existing node before other existing node', function() {
85	var root, node, node2;
86
87	expect(8);
88
89	root = new tinymce.html.Node('#frag', 11);
90	node = root.append(new tinymce.html.Node('a', 1));
91	node2 = root.append(new tinymce.html.Node('b', 1));
92	root.append(node);
93	ok(root.firstChild === node2, 'root.firstChild');
94	ok(root.lastChild === node, 'root.lastChild');
95	equal(node.next, undefined, 'node.next');
96	ok(node.prev === node2, 'node.prev');
97	ok(node.parent === root, 'node.parent');
98	ok(node2.parent === root, 'node2.parent');
99	equal(node2.prev, undefined, 'node2.prev');
100	ok(node2.next === node, 'node2.next');
101});
102
103test('remove unattached node', function() {
104	expect(1);
105
106	ok(!new tinymce.html.Node('#text', 3).remove().parent);
107});
108
109test('remove single child', function() {
110	var root, node;
111
112	expect(6);
113
114	root = new tinymce.html.Node('#frag', 11);
115	node = root.append(new tinymce.html.Node('p', 1));
116	node = root.firstChild.remove();
117	equal(root.firstChild, undefined);
118	equal(root.lastChild, undefined);
119	equal(node.parent, undefined);
120	equal(node.next, undefined);
121	equal(node.prev, undefined);
122	equal(node.name, 'p');
123});
124
125test('remove middle node', function() {
126	var root, node, node2, node3;
127
128	expect(9);
129
130	root = new tinymce.html.Node('#frag', 11);
131	node = root.append(new tinymce.html.Node('a', 1));
132	node2 = root.append(new tinymce.html.Node('b', 1));
133	node3 = root.append(new tinymce.html.Node('c', 1));
134	node2.remove();
135	equal(node2.parent, undefined);
136	equal(node2.next, undefined);
137	equal(node2.prev, undefined);
138	ok(root.firstChild === node, 'root.firstChild');
139	ok(root.lastChild === node3, 'root.lastChild');
140	ok(node.next === node3, 'node.next');
141	equal(node.prev, undefined, 'node.prev');
142	ok(node3.prev, node, 'node3.prev');
143	equal(node3.next, undefined, 'node3.next');
144});
145
146test('insert after last', function() {
147	var fragment, root, node, node2;
148
149	expect(5);
150
151	fragment = new tinymce.html.Node('#frag', 11);
152	root = fragment.append(new tinymce.html.Node('body', 1));
153	node = root.append(new tinymce.html.Node('a', 1));
154	node2 = root.insert(new tinymce.html.Node('x', 1), node);
155	ok(root.firstChild === node, 'root.firstChild');
156	ok(root.lastChild === node2, 'root.lastChild');
157	ok(node.next === node2, 'node.next');
158	ok(node2.prev === node, 'node2.prev');
159	ok(node2.parent === root, 'node3.next');
160});
161
162test('insert before first', function() {
163	var fragment, root, node, node2;
164
165	expect(8);
166
167	fragment = new tinymce.html.Node('#frag', 11);
168	root = fragment.append(new tinymce.html.Node('body', 1));
169	node = root.append(new tinymce.html.Node('a', 1));
170	node2 = root.insert(new tinymce.html.Node('x', 1), node, true);
171	ok(root.firstChild === node2, 'root.firstChild');
172	ok(root.lastChild === node, 'root.lastChild');
173	ok(node2.parent === root, 'node2.lastChild');
174	ok(node2.next === node, 'node2.next');
175	ok(node2.prev === undefined, 'node2.prev');
176	ok(node.parent === root, 'node.lastChild');
177	ok(node.next === undefined, 'node.next');
178	ok(node.prev === node2, 'node.prev');
179});
180
181test('insert before second', function() {
182	var fragment, root, node, node2, node3;
183
184	expect(5);
185
186	fragment = new tinymce.html.Node('#frag', 11);
187	root = fragment.append(new tinymce.html.Node('body', 1));
188	node = root.append(new tinymce.html.Node('a', 1));
189	node2 = root.append(new tinymce.html.Node('b', 1));
190	node3 = root.insert(new tinymce.html.Node('x', 1), node2, true);
191	ok(root.firstChild === node, 'root.firstChild');
192	ok(root.lastChild === node2, 'root.lastChild');
193	ok(node3.parent === root, 'node3.parent');
194	ok(node3.next === node2, 'node3.next');
195	ok(node3.prev === node, 'node3.prev');
196});
197
198test('insert after and between two nodes', function() {
199	var root, node, node2, node3, fragment;
200
201	expect(7);
202
203	fragment = new tinymce.html.Node('#frag', 11);
204	root = fragment.append(new tinymce.html.Node('body', 1));
205	node = root.append(new tinymce.html.Node('a', 1));
206	node2 = root.append(new tinymce.html.Node('b', 1));
207	node3 = root.insert(new tinymce.html.Node('x', 1), node);
208	ok(root.firstChild === node, 'root.firstChild');
209	ok(root.lastChild === node2, 'root.lastChild');
210	ok(node.next === node3, 'node.next');
211	ok(node2.prev === node3, 'node2.prev');
212	ok(node3.parent === root, 'node3.next');
213	ok(node3.next === node2, 'node3.next');
214	ok(node3.prev === node, 'node3.prev');
215});
216
217test('replace single child', function() {
218	var root, node1, node2;
219
220	expect(5);
221
222	root = new tinymce.html.Node('#frag', 11);
223	node1 = root.append(new tinymce.html.Node('b', 1));
224	node2 = root.append(new tinymce.html.Node('em', 1));
225	node1.replace(node2);
226	ok(root.firstChild === node2, 'root.firstChild');
227	ok(root.lastChild === node2, 'root.lastChild');
228	ok(node2.parent === root, 'node2.parent');
229	ok(!node2.next, 'node2.next');
230	ok(!node2.prev, 'node2.prev');
231});
232
233test('replace first child', function() {
234	var root, node1, node2, node3;
235
236	expect(5);
237
238	root = new tinymce.html.Node('#frag', 11);
239	node1 = root.append(new tinymce.html.Node('b', 1));
240	node2 = root.append(new tinymce.html.Node('em', 1));
241	node3 = root.append(new tinymce.html.Node('b', 1));
242	node1.replace(node2);
243	ok(root.firstChild === node2, 'root.firstChild');
244	ok(root.lastChild === node3, 'root.lastChild');
245	ok(node2.parent === root, 'node2.parent');
246	ok(node2.next === node3, 'node2.next');
247	ok(!node2.prev, 'node2.prev');
248});
249
250test('replace last child', function() {
251	var root, node1, node2, node3;
252
253	expect(5);
254
255	root = new tinymce.html.Node('#frag', 11);
256	node1 = root.append(new tinymce.html.Node('b', 1));
257	node3 = root.append(new tinymce.html.Node('b', 1));
258	node2 = root.append(new tinymce.html.Node('em', 1));
259	node3.replace(node2);
260	ok(root.firstChild === node1, 'root.firstChild');
261	ok(root.lastChild === node2, 'root.lastChild');
262	ok(node2.parent === root, 'node2.parent');
263	ok(!node2.next, 'node2.next');
264	ok(node2.prev === node1, 'node2.prev');
265});
266
267test('replace middle child', function() {
268	var root, node1, node2, node3, node4;
269
270	expect(5);
271
272	root = new tinymce.html.Node('#frag', 11);
273	node1 = root.append(new tinymce.html.Node('b', 1));
274	node2 = root.append(new tinymce.html.Node('b', 1));
275	node3 = root.append(new tinymce.html.Node('b', 1));
276	node4 = root.append(new tinymce.html.Node('em', 1));
277	node2.replace(node4);
278	ok(root.firstChild === node1, 'root.firstChild');
279	ok(root.lastChild === node3, 'root.lastChild');
280	ok(node4.parent === root, 'node4.parent');
281	ok(node4.next === node3, 'node4.next');
282	ok(node4.prev === node1, 'node4.prev');
283});
284
285test('attr', 22, function() {
286	var node;
287
288	node = new tinymce.html.Node('b', 1);
289	deepEqual(node.attributes, []);
290	node.attr('attr1', 'value1');
291	equal(node.attr('attr1'), 'value1');
292	equal(node.attr('attr2'), undefined);
293	deepEqual(node.attributes, [{name: 'attr1', value: 'value1'}]);
294	deepEqual(node.attributes.map, {'attr1': 'value1'});
295
296	node = new tinymce.html.Node('b', 1);
297	deepEqual(node.attributes, []);
298	node.attr('attr1', 'value1');
299	node.attr('attr1', 'valueX');
300	equal(node.attr('attr1'), 'valueX');
301	deepEqual(node.attributes, [{name: 'attr1', value: 'valueX'}]);
302	deepEqual(node.attributes.map, {'attr1': 'valueX'});
303
304	node = new tinymce.html.Node('b', 1);
305	deepEqual(node.attributes, []);
306	node.attr('attr1', 'value1');
307	node.attr('attr2', 'value2');
308	equal(node.attr('attr1'), 'value1');
309	equal(node.attr('attr2'), 'value2');
310	deepEqual(node.attributes, [{name: 'attr1', value: 'value1'}, {name: 'attr2', value: 'value2'}]);
311	deepEqual(node.attributes.map, {'attr1': 'value1', 'attr2': 'value2'});
312
313	node = new tinymce.html.Node('b', 1);
314	deepEqual(node.attributes, []);
315	node.attr('attr1', 'value1');
316	node.attr('attr1', null);
317	equal(node.attr('attr1'), undefined);
318	deepEqual(node.attributes, []);
319	deepEqual(node.attributes.map, {});
320
321	node = new tinymce.html.Node('b', 1);
322	node.attr({a:'1', b:'2'});
323	deepEqual(node.attributes, [{name: 'a', value: '1'}, {name: 'b', value: '2'}]);
324	deepEqual(node.attributes.map, {a:'1', b:'2'});
325
326	node = new tinymce.html.Node('b', 1);
327	node.attr(null);
328	deepEqual(node.attributes, []);
329	deepEqual(node.attributes.map, {});
330});
331
332test('clone', function() {
333	var root, node, clone;
334
335	expect(16);
336
337	node = new tinymce.html.Node('#text', 3);
338	node.value = 'value';
339	clone = node.clone();
340	equal(clone.name, '#text');
341	equal(clone.type, 3);
342	equal(clone.value, 'value');
343	equal(clone.parent, undefined);
344	equal(clone.next, undefined);
345	equal(clone.prev, undefined);
346
347	root = new tinymce.html.Node('#frag', 11);
348	node = new tinymce.html.Node('#text', 3);
349	node.value = 'value';
350	root.append(node);
351	equal(clone.name, '#text');
352	equal(clone.type, 3);
353	equal(clone.value, 'value');
354	equal(clone.parent, undefined);
355	equal(clone.next, undefined);
356	equal(clone.prev, undefined);
357
358	node = new tinymce.html.Node('b', 1);
359	node.attr('id', 'id');
360	node.attr('class', 'class');
361	node.attr('title', 'title');
362	clone = node.clone();
363	equal(clone.name, 'b');
364	equal(clone.type, 1);
365	deepEqual(clone.attributes, [{name: 'class', value: 'class'}, {name: 'title', value: 'title'}]);
366	deepEqual(clone.attributes.map, {'class': 'class', 'title': 'title'});
367});
368
369test('unwrap', function() {
370	var root, node1, node2, node3;
371
372	expect(7);
373
374	root = new tinymce.html.Node('#frag', 11);
375	node1 = root.append(new tinymce.html.Node('b', 1));
376	node2 = node1.append(new tinymce.html.Node('em', 1));
377	node1.unwrap();
378	ok(root.firstChild === node2, 'root.firstChild');
379	ok(root.lastChild === node2, 'root.lastChild');
380	ok(node2.parent === root, 'node2.parent');
381
382	root = new tinymce.html.Node('#frag', 11);
383	node1 = root.append(new tinymce.html.Node('b', 1));
384	node2 = node1.append(new tinymce.html.Node('em', 1));
385	node3 = node1.append(new tinymce.html.Node('span', 1));
386	node1.unwrap();
387	ok(root.firstChild === node2, 'root.firstChild');
388	ok(root.lastChild === node3, 'root.lastChild');
389	ok(node2.parent === root, 'node2.parent');
390	ok(node3.parent === root, 'node3.parent');
391});
392
393test('empty', function() {
394	var root, node1, node2;
395
396	expect(4);
397
398	root = new tinymce.html.Node('#frag', 11);
399	node1 = root.append(new tinymce.html.Node('b', 1));
400	node2 = node1.append(new tinymce.html.Node('em', 1));
401	node1.empty();
402	ok(root.firstChild === node1, 'root.firstChild');
403	ok(root.lastChild === node1, 'root.firstChild');
404	ok(!node1.firstChild, 'node1.firstChild');
405	ok(!node1.lastChild, 'node1.firstChild');
406});
407
408test('isEmpty', function() {
409	var root, node1, node2;
410
411	expect(9);
412
413	root = new tinymce.html.Node('#frag', 11);
414	node1 = root.append(new tinymce.html.Node('p', 1));
415	node2 = node1.append(new tinymce.html.Node('b', 1));
416	ok(root.isEmpty({img: 1}), 'Is empty 1');
417	ok(node1.isEmpty({img: 1}), 'Is empty 2');
418
419	root = new tinymce.html.Node('#frag', 11);
420	node1 = root.append(new tinymce.html.Node('p', 1));
421	node2 = node1.append(new tinymce.html.Node('img', 1));
422	ok(!root.isEmpty({img: 1}), 'Is not empty 1');
423	ok(!node1.isEmpty({img: 1}), 'Is not empty 2');
424
425	root = new tinymce.html.Node('#frag', 11);
426	node1 = root.append(new tinymce.html.Node('p', 1));
427	node2 = node1.append(new tinymce.html.Node('#text', 3));
428	node2.value = 'X';
429	ok(!root.isEmpty({img: 1}), 'Is not empty 3');
430	ok(!node1.isEmpty({img: 1}), 'Is not empty 4');
431
432	root = new tinymce.html.Node('#frag', 11);
433	node1 = root.append(new tinymce.html.Node('p', 1));
434	node2 = node1.append(new tinymce.html.Node('#text', 3));
435	node2.value = '';
436	ok(root.isEmpty({img: 1}), 'Is empty 4');
437	ok(node1.isEmpty({img: 1}), 'Is empty 5');
438
439	root = new tinymce.html.Node('#frag', 11);
440	node1 = root.append(new tinymce.html.Node('a', 1)).attr('name', 'x');
441	ok(!root.isEmpty({img: 1}), 'Contains anchor with name attribute.');
442});
443
Full Screen

CaretBookmark.js

Source: CaretBookmark.js Github

copy
1ModuleLoader.require([
2	'tinymce/caret/CaretBookmark',
3	'tinymce/caret/CaretPosition'
4], function(CaretBookmark, CaretPosition) {
5	var assertCaretPosition = Utils.assertCaretPosition;
6
7	module('tinymce.caret.CaretBookmark');
8
9	function getRoot() {
10		return document.getElementById('view');
11	}
12
13	function setupHtml(html) {
14		getRoot().innerHTML = html;
15	}
16
17	function createTextPos(textNode, offset) {
18		return new CaretPosition(textNode, offset);
19	}
20
21	test('create element index', function() {
22		setupHtml('<b></b><i></i><b></b>');
23		equal(CaretBookmark.create(getRoot(), CaretPosition.before(getRoot().childNodes[0])), 'b[0],before');
24		equal(CaretBookmark.create(getRoot(), CaretPosition.before(getRoot().childNodes[1])), 'i[0],before');
25		equal(CaretBookmark.create(getRoot(), CaretPosition.before(getRoot().childNodes[2])), 'b[1],before');
26		equal(CaretBookmark.create(getRoot(), CaretPosition.after(getRoot().childNodes[2])), 'b[1],after');
27	});
28
29	test('create text index', function() {
30		setupHtml('a<b></b>b<b></b>ccc');
31		equal(CaretBookmark.create(getRoot(), createTextPos(getRoot().childNodes[0], 0)), 'text()[0],0');
32		equal(CaretBookmark.create(getRoot(), createTextPos(getRoot().childNodes[2], 1)), 'text()[1],1');
33		equal(CaretBookmark.create(getRoot(), createTextPos(getRoot().childNodes[4], 3)), 'text()[2],3');
34	});
35
36	test('create text index on fragmented text nodes', function() {
37		setupHtml('a');
38		getRoot().appendChild(document.createTextNode('b'));
39		getRoot().appendChild(document.createTextNode('c'));
40		getRoot().appendChild(document.createElement('b'));
41		getRoot().appendChild(document.createTextNode('d'));
42		getRoot().appendChild(document.createTextNode('e'));
43
44		equal(getRoot().childNodes.length, 6);
45		equal(CaretBookmark.create(getRoot(), createTextPos(getRoot().childNodes[0], 0)), 'text()[0],0');
46		equal(CaretBookmark.create(getRoot(), createTextPos(getRoot().childNodes[1], 0)), 'text()[0],1');
47		equal(CaretBookmark.create(getRoot(), createTextPos(getRoot().childNodes[2], 0)), 'text()[0],2');
48		equal(CaretBookmark.create(getRoot(), createTextPos(getRoot().childNodes[4], 0)), 'text()[1],0');
49		equal(CaretBookmark.create(getRoot(), createTextPos(getRoot().childNodes[5], 0)), 'text()[1],1');
50	});
51
52	test('create br element index', function() {
53		setupHtml('<p><br data-mce-bogus="1"></p><p><br></p>');
54		equal(CaretBookmark.create(getRoot(), CaretPosition.before(getRoot().firstChild.firstChild)), 'p[0]/br[0],before');
55		equal(CaretBookmark.create(getRoot(), CaretPosition.before(getRoot().lastChild.firstChild)), 'p[1]/br[0],before');
56	});
57
58	test('create deep element index', function() {
59		setupHtml('<p><span>a</span><span><b id="a"></b><b id="b"></b><b id="c"></b></span></p>');
60		equal(CaretBookmark.create(getRoot(), CaretPosition.before(document.getElementById('a'))), 'p[0]/span[1]/b[0],before');
61		equal(CaretBookmark.create(getRoot(), CaretPosition.before(document.getElementById('b'))), 'p[0]/span[1]/b[1],before');
62		equal(CaretBookmark.create(getRoot(), CaretPosition.before(document.getElementById('c'))), 'p[0]/span[1]/b[2],before');
63		equal(CaretBookmark.create(getRoot(), CaretPosition.after(document.getElementById('c'))), 'p[0]/span[1]/b[2],after');
64	});
65
66	test('create deep text index', function() {
67		setupHtml('<p><span>a</span><span id="x">a<b></b>b<b></b>ccc</span></p>');
68		equal(CaretBookmark.create(getRoot(), createTextPos(document.getElementById('x').childNodes[0], 0)), 'p[0]/span[1]/text()[0],0');
69		equal(CaretBookmark.create(getRoot(), createTextPos(document.getElementById('x').childNodes[2], 1)), 'p[0]/span[1]/text()[1],1');
70		equal(CaretBookmark.create(getRoot(), createTextPos(document.getElementById('x').childNodes[4], 3)), 'p[0]/span[1]/text()[2],3');
71	});
72
73	test('create element index from bogus', function() {
74		setupHtml('<b></b><span data-mce-bogus="1"><b></b><span data-mce-bogus="1"><b></b><b></b></span></span>');
75		equal(CaretBookmark.create(getRoot(), CaretPosition.before(getRoot().lastChild.lastChild.childNodes[1])), 'b[3],before');
76	});
77
78	test('resolve element index', function() {
79		setupHtml('<b></b><i></i><b></b>');
80		assertCaretPosition(CaretBookmark.resolve(getRoot(), 'b[0],before'), CaretPosition.before(getRoot().childNodes[0]));
81		assertCaretPosition(CaretBookmark.resolve(getRoot(), 'b[1],before'), CaretPosition.before(getRoot().childNodes[2]));
82		assertCaretPosition(CaretBookmark.resolve(getRoot(), 'b[1],after'), CaretPosition.after(getRoot().childNodes[2]));
83		assertCaretPosition(CaretBookmark.resolve(getRoot(), 'i[0],before'), CaretPosition.before(getRoot().childNodes[1]));
84	});
85
86	test('resolve odd element names', function() {
87		setupHtml('<h-2X>abc</h-2X>');
88		assertCaretPosition(CaretBookmark.resolve(getRoot(), 'h-2X[0]/text()[0],2'), createTextPos(getRoot().childNodes[0].firstChild, 2));
89	});
90
91	test('resolve deep element index', function() {
92		setupHtml('<p><span>a</span><span><b id="a"></b><b id="b"></b><b id="c"></b></span></p>');
93		assertCaretPosition(CaretBookmark.resolve(getRoot(), 'p[0]/span[1]/b[0],before'), CaretPosition.before(document.getElementById('a')));
94		assertCaretPosition(CaretBookmark.resolve(getRoot(), 'p[0]/span[1]/b[1],before'), CaretPosition.before(document.getElementById('b')));
95		assertCaretPosition(CaretBookmark.resolve(getRoot(), 'p[0]/span[1]/b[2],before'), CaretPosition.before(document.getElementById('c')));
96		assertCaretPosition(CaretBookmark.resolve(getRoot(), 'p[0]/span[1]/b[2],after'), CaretPosition.after(document.getElementById('c')));
97	});
98
99	test('resolve text index', function() {
100		setupHtml('a<b></b>b<b></b>ccc');
101		assertCaretPosition(CaretBookmark.resolve(getRoot(), 'text()[0],0'), createTextPos(getRoot().childNodes[0], 0));
102		assertCaretPosition(CaretBookmark.resolve(getRoot(), 'text()[1],1'), createTextPos(getRoot().childNodes[2], 1));
103		assertCaretPosition(CaretBookmark.resolve(getRoot(), 'text()[2],3'), createTextPos(getRoot().childNodes[4], 3));
104	});
105
106	test('resolve text index on fragmented text nodes', function() {
107		setupHtml('a');
108		getRoot().appendChild(document.createTextNode('b'));
109		getRoot().appendChild(document.createTextNode('c'));
110		getRoot().appendChild(document.createElement('b'));
111		getRoot().appendChild(document.createTextNode('d'));
112		getRoot().appendChild(document.createTextNode('e'));
113
114		equal(getRoot().childNodes.length, 6);
115		assertCaretPosition(CaretBookmark.resolve(getRoot(), 'text()[0],0'), createTextPos(getRoot().childNodes[0], 0));
116		assertCaretPosition(CaretBookmark.resolve(getRoot(), 'text()[0],1'), createTextPos(getRoot().childNodes[0], 1));
117		assertCaretPosition(CaretBookmark.resolve(getRoot(), 'text()[0],2'), createTextPos(getRoot().childNodes[1], 1));
118		assertCaretPosition(CaretBookmark.resolve(getRoot(), 'text()[0],3'), createTextPos(getRoot().childNodes[2], 1));
119		assertCaretPosition(CaretBookmark.resolve(getRoot(), 'text()[0],4'), createTextPos(getRoot().childNodes[2], 1));
120		assertCaretPosition(CaretBookmark.resolve(getRoot(), 'text()[1],0'), createTextPos(getRoot().childNodes[4], 0));
121		assertCaretPosition(CaretBookmark.resolve(getRoot(), 'text()[1],1'), createTextPos(getRoot().childNodes[4], 1));
122		assertCaretPosition(CaretBookmark.resolve(getRoot(), 'text()[1],2'), createTextPos(getRoot().childNodes[5], 1));
123	});
124
125	test('resolve text index with to high offset', function() {
126		setupHtml('abc');
127		assertCaretPosition(CaretBookmark.resolve(getRoot(), 'text()[0],10'), createTextPos(getRoot().childNodes[0], 3));
128	});
129
130	test('resolve invalid paths', function() {
131		setupHtml('<b><i></i></b>');
132		equal(CaretBookmark.resolve(getRoot(), 'x[0]/y[1]/z[2]'), null);
133		equal(CaretBookmark.resolve(getRoot(), 'b[0]/i[2]'), null);
134		equal(CaretBookmark.resolve(getRoot(), 'x'), null);
135		equal(CaretBookmark.resolve(getRoot(), null), null);
136	});
137});
138
Full Screen

CaretPosition.js

Source: CaretPosition.js Github

copy
1ModuleLoader.require([
2	"tinymce/Env",
3	"tinymce/caret/CaretPosition"
4], function(Env, CaretPosition) {
5	module("tinymce.caret.CaretPosition");
6
7	if (!Env.ceFalse) {
8		return;
9	}
10
11	var createRange = Utils.createRange,
12		assertCaretPosition = Utils.assertCaretPosition,
13		assertRange = Utils.assertRange;
14
15	function getRoot() {
16		return document.getElementById('view');
17	}
18
19	function setupHtml(html) {
20		tinymce.$(getRoot()).empty();
21		getRoot().innerHTML = html;
22	}
23
24	test('Constructor', function() {
25		setupHtml('abc');
26		strictEqual(new CaretPosition(getRoot(), 0).container(), getRoot());
27		strictEqual(new CaretPosition(getRoot(), 1).offset(), 1);
28		strictEqual(new CaretPosition(getRoot().firstChild, 0).container(), getRoot().firstChild);
29		strictEqual(new CaretPosition(getRoot().firstChild, 1).offset(), 1);
30	});
31
32	test('fromRangeStart', function() {
33		setupHtml('abc');
34		assertCaretPosition(CaretPosition.fromRangeStart(createRange(getRoot(), 0)), new CaretPosition(getRoot(), 0));
35		assertCaretPosition(CaretPosition.fromRangeStart(createRange(getRoot(), 1)), new CaretPosition(getRoot(), 1));
36		assertCaretPosition(CaretPosition.fromRangeStart(createRange(getRoot().firstChild, 1)), new CaretPosition(getRoot().firstChild, 1));
37	});
38
39	test('fromRangeEnd', function() {
40		setupHtml('abc');
41		assertCaretPosition(CaretPosition.fromRangeEnd(createRange(getRoot(), 0, getRoot(), 1)), new CaretPosition(getRoot(), 1));
42		assertCaretPosition(CaretPosition.fromRangeEnd(createRange(getRoot().firstChild, 0, getRoot().firstChild, 1)), new CaretPosition(getRoot().firstChild, 1));
43	});
44
45	test('after', function() {
46		setupHtml('abc<b>123</b>');
47		assertCaretPosition(CaretPosition.after(getRoot().firstChild), new CaretPosition(getRoot(), 1));
48		assertCaretPosition(CaretPosition.after(getRoot().lastChild), new CaretPosition(getRoot(), 2));
49	});
50
51	test('before', function() {
52		setupHtml('abc<b>123</b>');
53		assertCaretPosition(CaretPosition.before(getRoot().firstChild), new CaretPosition(getRoot(), 0));
54		assertCaretPosition(CaretPosition.before(getRoot().lastChild), new CaretPosition(getRoot(), 1));
55	});
56
57	test('isAtStart', function() {
58		setupHtml('abc<b>123</b>123');
59		ok(new CaretPosition(getRoot(), 0).isAtStart());
60		ok(!new CaretPosition(getRoot(), 1).isAtStart());
61		ok(!new CaretPosition(getRoot(), 3).isAtStart());
62		ok(new CaretPosition(getRoot().firstChild, 0).isAtStart());
63		ok(!new CaretPosition(getRoot().firstChild, 1).isAtStart());
64		ok(!new CaretPosition(getRoot().firstChild, 3).isAtStart());
65	});
66
67	test('isAtEnd', function() {
68		setupHtml('abc<b>123</b>123');
69		ok(new CaretPosition(getRoot(), 3).isAtEnd());
70		ok(!new CaretPosition(getRoot(), 2).isAtEnd());
71		ok(!new CaretPosition(getRoot(), 0).isAtEnd());
72		ok(new CaretPosition(getRoot().firstChild, 3).isAtEnd());
73		ok(!new CaretPosition(getRoot().firstChild, 0).isAtEnd());
74		ok(!new CaretPosition(getRoot().firstChild, 1).isAtEnd());
75	});
76
77	test('toRange', function() {
78		setupHtml('abc');
79		assertRange(new CaretPosition(getRoot(), 0).toRange(), createRange(getRoot(), 0));
80		assertRange(new CaretPosition(getRoot(), 1).toRange(), createRange(getRoot(), 1));
81		assertRange(new CaretPosition(getRoot().firstChild, 1).toRange(), createRange(getRoot().firstChild, 1));
82	});
83
84	test('isEqual', function() {
85		setupHtml('abc');
86		equal(new CaretPosition(getRoot(), 0).isEqual(new CaretPosition(getRoot(), 0)), true);
87		equal(new CaretPosition(getRoot(), 1).isEqual(new CaretPosition(getRoot(), 0)), false);
88		equal(new CaretPosition(getRoot(), 0).isEqual(new CaretPosition(getRoot().firstChild, 0)), false);
89	});
90
91	test('isVisible', function() {
92		setupHtml('<b>  abc</b>');
93		equal(new CaretPosition(getRoot().firstChild.firstChild, 0).isVisible(), false);
94		equal(new CaretPosition(getRoot().firstChild.firstChild, 3).isVisible(), true);
95	});
96
97	test('getClientRects', function() {
98		setupHtml(
99			'<b>abc</b>' +
100			'<div contentEditable="false">1</div>' +
101			'<div contentEditable="false">2</div>' +
102			'<div contentEditable="false">2</div>' +
103			'<input style="margin: 10px">' +
104			'<input style="margin: 10px">' +
105			'<input style="margin: 10px">' +
106			'<p>123</p>' +
107			'<br>'
108		);
109
110		equal(new CaretPosition(getRoot().firstChild.firstChild, 0).getClientRects().length, 1);
111		equal(new CaretPosition(getRoot(), 1).getClientRects().length, 1);
112		equal(new CaretPosition(getRoot(), 2).getClientRects().length, 2);
113		equal(new CaretPosition(getRoot(), 3).getClientRects().length, 2);
114		equal(new CaretPosition(getRoot(), 4).getClientRects().length, 2);
115		equal(new CaretPosition(getRoot(), 5).getClientRects().length, 1);
116		equal(new CaretPosition(getRoot(), 6).getClientRects().length, 1);
117		equal(new CaretPosition(getRoot(), 7).getClientRects().length, 1);
118		equal(new CaretPosition(getRoot(), 8).getClientRects().length, 1);
119		equal(new CaretPosition(getRoot(), 9).getClientRects().length, 0);
120	});
121
122	test('getClientRects between inline node and cE=false', function() {
123		setupHtml(
124			'<span contentEditable="false">def</span>' +
125			'<b>ghi</b>'
126		);
127
128		equal(new CaretPosition(getRoot(), 1).getClientRects().length, 1);
129	});
130
131	test('getClientRects at last visible character', function() {
132		setupHtml('<b>a  </b>');
133
134		equal(new CaretPosition(getRoot().firstChild.firstChild, 1).getClientRects().length, 1);
135	});
136
137	test('getClientRects at extending character', function() {
138		setupHtml('a\u0301b');
139
140		equal(new CaretPosition(getRoot().firstChild, 0).getClientRects().length, 1);
141		equal(new CaretPosition(getRoot().firstChild, 1).getClientRects().length, 0);
142		equal(new CaretPosition(getRoot().firstChild, 2).getClientRects().length, 1);
143	});
144
145	test('getClientRects at whitespace character', function() {
146		setupHtml('  a  ');
147
148		equal(new CaretPosition(getRoot().firstChild, 0).getClientRects().length, 0);
149		equal(new CaretPosition(getRoot().firstChild, 1).getClientRects().length, 0);
150		equal(new CaretPosition(getRoot().firstChild, 2).getClientRects().length, 1);
151		equal(new CaretPosition(getRoot().firstChild, 3).getClientRects().length, 1);
152		equal(new CaretPosition(getRoot().firstChild, 4).getClientRects().length, 0);
153		equal(new CaretPosition(getRoot().firstChild, 5).getClientRects().length, 0);
154	});
155
156	test('getNode', function() {
157		setupHtml('<b>abc</b><input><input>');
158
159		equal(new CaretPosition(getRoot().firstChild.firstChild, 0).getNode(), getRoot().firstChild.firstChild);
160		equal(new CaretPosition(getRoot(), 1).getNode(), getRoot().childNodes[1]);
161		equal(new CaretPosition(getRoot(), 2).getNode(), getRoot().childNodes[2]);
162		equal(new CaretPosition(getRoot(), 3).getNode(), getRoot().childNodes[2]);
163	});
164
165	test('getNode (before)', function() {
166		setupHtml('<b>abc</b><input><input>');
167
168		equal(new CaretPosition(getRoot().firstChild.firstChild, 0).getNode(true), getRoot().firstChild.firstChild);
169		equal(new CaretPosition(getRoot(), 1).getNode(true), getRoot().childNodes[0]);
170		equal(new CaretPosition(getRoot(), 2).getNode(true), getRoot().childNodes[1]);
171		equal(new CaretPosition(getRoot(), 3).getNode(true), getRoot().childNodes[2]);
172	});
173});
174
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Jasmine-core on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)