forked from swiftlang/swift-docc-render
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput-helper.js
66 lines (62 loc) · 1.83 KB
/
input-helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
* This source file is part of the Swift.org open source project
*
* Copyright (c) 2022 Apple Inc. and the Swift project authors
* Licensed under Apache License v2.0 with Runtime Library Exception
*
* See https://swift.org/LICENSE.txt for license information
* See https://swift.org/CONTRIBUTORS.txt for Swift project authors
*/
/**
* A window.getSelection utility that works with all browsers.
* Firefox does not work with window.getSelection on `input` elements,
* IE does not support it at all.
* @returns {string}
*/
export function getSelectionText() {
if (window.getSelection) {
try {
const { activeElement } = document;
if (activeElement && activeElement.value) {
// firefox bug https://bugzilla.mozilla.org/show_bug.cgi?id=85686
return activeElement.value.substring(
activeElement.selectionStart,
activeElement.selectionEnd,
);
}
return window.getSelection().toString();
} catch (e) {
return '';
}
} else if (document.selection && document.selection.type !== 'Control') {
// For IE
return document.selection.createRange().text;
}
return '';
}
/**
* Moves a cursor to the end of an input
* @param {HTMLInputElement} el
*/
export function moveCursorToEnd(el) {
if (typeof el.selectionStart === 'number') {
// eslint-disable-next-line
el.selectionStart = el.selectionEnd = el.value.length;
} else if (typeof el.createTextRange !== 'undefined') {
el.focus();
const range = el.createTextRange();
range.collapse(false);
range.select();
}
}
/**
* Moves a cursor to the start of an input
* @param {HTMLInputElement} el
*/
export function moveCursorToStart(el) {
// eslint-disable-next-line
el.selectionStart = el.selectionEnd = 0;
}
export function isSingleCharacter(key) {
return /^[\w\W\s]$/.test(key);
}