source: node_modules/toggle-selection/index.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 780 bytes
RevLine 
[d24f17c]1
2module.exports = function () {
3 var selection = document.getSelection();
4 if (!selection.rangeCount) {
5 return function () {};
6 }
7 var active = document.activeElement;
8
9 var ranges = [];
10 for (var i = 0; i < selection.rangeCount; i++) {
11 ranges.push(selection.getRangeAt(i));
12 }
13
14 switch (active.tagName.toUpperCase()) { // .toUpperCase handles XHTML
15 case 'INPUT':
16 case 'TEXTAREA':
17 active.blur();
18 break;
19
20 default:
21 active = null;
22 break;
23 }
24
25 selection.removeAllRanges();
26 return function () {
27 selection.type === 'Caret' &&
28 selection.removeAllRanges();
29
30 if (!selection.rangeCount) {
31 ranges.forEach(function(range) {
32 selection.addRange(range);
33 });
34 }
35
36 active &&
37 active.focus();
38 };
39};
Note: See TracBrowser for help on using the repository browser.