10 examples of 'document selection createrange' in JavaScript

Every line of 'document selection createrange' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
24function createRangeFromNode(node) {
25 var range = node.ownerDocument.createRange()
26 try {
27 range.selectNode(node)
28 } catch (e) {
29 range.selectNodeContents(node)
30 }
31 return range
32}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

72function createRange(startContainer, startOffset, endContainer, endOffset) {
73 var rng = tinymce.DOM.createRng();
74
75 rng.setStart(startContainer, startOffset);
76
77 if (endContainer) {
78 rng.setEnd(endContainer, endOffset);
79 }
80
81 return rng;
82}
64getRangeAt(index: number): Range {
65 return new Range();
66}
72function createRange(node, chars, range) {
73 if (!range) {
74 range = document.createRange()
75 range.selectNode(node);
76 range.setStart(node, 0);
77 }
78
79 if (chars.count === 0) {
80 range.setEnd(node, chars.count);
81 } else if (node && chars.count >0) {
82 if (node.nodeType === Node.TEXT_NODE) {
83 if (node.textContent.length < chars.count) {
84 chars.count -= node.textContent.length;
85 } else {
86 range.setEnd(node, chars.count);
87 chars.count = 0;
88 }
89 } else {
90 for (var lp = 0; lp < node.childNodes.length; lp++) {
91 range = createRange(node.childNodes[lp], chars, range);
92
93 if (chars.count === 0) {
94 break;
95 }
96 }
97 }
98 }
99
100 return range;
101};
13public static toRanges(selection: Selection): Range[] {
14
15 const result = [];
16
17 for (let idx = 0; idx < selection.rangeCount; idx++) {
18
19 // note that we almost always have 1 selection
20 const range = selection.getRangeAt(idx);
21 result.push(range);
22
23 }
24
25 return result;
26}
137function activeRange(doc) {
138 // "Let selection be the result of calling getSelection() on the Document."
139 //
140 // We call getSelection() on defaultView instead, because Firefox and Opera
141 // don't follow the DOM Range spec here:
142 // https://bugzilla.mozilla.org/show_bug.cgi?id=636512
143 var selection = doc.defaultView.getSelection();
144
145 // "If there are no Ranges associated with selection, return null."
146 if (selection.rangeCount == 0) {
147 return null;
148 }
149
150 // "Let start be the boundary point with the earliest position among all of
151 // selection's Ranges' starts."
152 var startNode = null;
153 var startOffset = null;
154 for (var i = 0; i < selection.rangeCount; i++) {
155 if (startNode === null) {
156 startNode = selection.getRangeAt(i).startContainer;
157 startOffset = selection.getRangeAt(i).startOffset;
158 continue;
159 }
160 var testRange = doc.createRange();
161 testRange.setStart(startNode, startOffset);
162 if (testRange.compareBoundaryPoints(Range.START_TO_START, selection.getRangeAt(i)) < 0) {
163 startNode = selection.getRangeAt(i).startContainer;
164 startOffset = selection.getRangeAt(i).startOffset;
165 }
166 }
167
168 // "Return the last Range in selection whose start is start."
169 for (var i = selection.rangeCount - 1; i >= 0; i--) {
170 if (selection.getRangeAt(i).startContainer == startNode
171 && selection.getRangeAt(i).startOffset == startOffset) {
172 return selection.getRangeAt(i);
173 }
174 }
175}
511function getRangeDocument(range) {
512 return dom.getDocument(range.startContainer);
513}
74function convertRange( range ) {
75 var rng;
76
77 if ( range instanceof CKEDITOR.dom.range ) {
78 rng = rangy.createRange( range.document.$ );
79 rng.setStart( range.startContainer.$, range.startOffset );
80 rng.setEnd( range.endContainer.$, range.endOffset );
81 }
82 else
83 {
84 rng = new CKEDITOR.dom.range( new CKEDITOR.dom.document( range.getDocument() ) );
85 rng.setStart( new CKEDITOR.dom.node( range.startContainer ), range.startOffset );
86 rng.setEnd( new CKEDITOR.dom.node( range.endContainer ), range.endOffset );
87 }
88
89 return rng;
90}
16function createRange(newStart, newEnd) {
17 return {
18 start: newStart,
19 count: newEnd - newStart
20 };
21}
339getSelectionRange() {
340 if (window.getSelection) {
341 const sel = window.getSelection();
342 if (sel.getRangeAt && sel.rangeCount) {
343 return sel.getRangeAt(0);
344 }
345 } else if (document.selection && document.selection.createRange) {
346 return document.selection.createRange();
347 }
348 return null;
349}

Related snippets