|
1 | | -import {getUIValue} from '.' |
2 | | -import {prepareInterceptor} from './interceptor' |
3 | | - |
| 1 | +const UIValue = Symbol('Displayed value in UI') |
4 | 2 | const UISelection = Symbol('Displayed selection in UI') |
| 3 | +const InitialValue = Symbol('Initial value to compare on blur') |
5 | 4 |
|
6 | | -interface Value extends Number { |
7 | | - [UISelection]?: typeof UISelection |
8 | | -} |
9 | | - |
10 | | -export interface UISelectionRange { |
11 | | - startOffset: number |
12 | | - endOffset: number |
| 5 | +declare global { |
| 6 | + interface Element { |
| 7 | + [UIValue]?: string |
| 8 | + [InitialValue]?: string |
| 9 | + [UISelection]?: UISelection |
| 10 | + } |
13 | 11 | } |
14 | 12 |
|
15 | | -export interface UISelection { |
| 13 | +interface UISelection { |
16 | 14 | anchorOffset: number |
17 | 15 | focusOffset: number |
18 | 16 | } |
19 | 17 |
|
20 | | -declare global { |
21 | | - interface Element { |
22 | | - [UISelection]?: UISelection |
| 18 | +export type UIValueString = String & {[UIValue]: true} |
| 19 | +export type UISelectionStart = Number & {[UISelection]: true} |
| 20 | + |
| 21 | +export function isUIValue( |
| 22 | + value: string | UIValueString, |
| 23 | +): value is UIValueString { |
| 24 | + return typeof value === 'object' && UIValue in value |
| 25 | +} |
| 26 | + |
| 27 | +export function isUISelectionStart( |
| 28 | + start: number | UISelectionStart | null, |
| 29 | +): start is UISelectionStart { |
| 30 | + return !!start && typeof start === 'object' && UISelection in start |
| 31 | +} |
| 32 | + |
| 33 | +export function setUIValue( |
| 34 | + element: HTMLInputElement | HTMLTextAreaElement, |
| 35 | + value: string, |
| 36 | +) { |
| 37 | + if (element[InitialValue] === undefined) { |
| 38 | + element[InitialValue] = element.value |
23 | 39 | } |
| 40 | + |
| 41 | + element[UIValue] = value |
| 42 | + |
| 43 | + // eslint-disable-next-line no-new-wrappers |
| 44 | + element.value = Object.assign(new String(value), { |
| 45 | + [UIValue]: true, |
| 46 | + }) as unknown as string |
| 47 | +} |
| 48 | + |
| 49 | +export function getUIValue(element: HTMLInputElement | HTMLTextAreaElement) { |
| 50 | + return element[UIValue] === undefined |
| 51 | + ? element.value |
| 52 | + : String(element[UIValue]) |
| 53 | +} |
| 54 | + |
| 55 | +/** Flag the IDL value as clean. This does not change the value.*/ |
| 56 | +export function setUIValueClean( |
| 57 | + element: HTMLInputElement | HTMLTextAreaElement, |
| 58 | +) { |
| 59 | + element[UIValue] = undefined |
| 60 | +} |
| 61 | + |
| 62 | +export function clearInitialValue( |
| 63 | + element: HTMLInputElement | HTMLTextAreaElement, |
| 64 | +) { |
| 65 | + element[InitialValue] = undefined |
| 66 | +} |
| 67 | + |
| 68 | +export function getInitialValue( |
| 69 | + element: HTMLInputElement | HTMLTextAreaElement, |
| 70 | +) { |
| 71 | + return element[InitialValue] |
24 | 72 | } |
25 | 73 |
|
26 | | -export function prepareSelectionInterceptor( |
| 74 | +export function setUISelectionRaw( |
27 | 75 | element: HTMLInputElement | HTMLTextAreaElement, |
| 76 | + selection: UISelection, |
28 | 77 | ) { |
29 | | - prepareInterceptor( |
30 | | - element, |
31 | | - 'setSelectionRange', |
32 | | - function interceptorImpl( |
33 | | - this: HTMLInputElement | HTMLTextAreaElement, |
34 | | - start: number | Value | null, |
35 | | - ...others |
36 | | - ) { |
37 | | - const isUI = start && typeof start === 'object' && start[UISelection] |
38 | | - |
39 | | - if (!isUI) { |
40 | | - this[UISelection] = undefined |
41 | | - } |
42 | | - |
43 | | - return { |
44 | | - applyNative: !!isUI, |
45 | | - realArgs: [Number(start), ...others] as [ |
46 | | - number, |
47 | | - number, |
48 | | - 'forward' | 'backward' | 'none' | undefined, |
49 | | - ], |
50 | | - } |
51 | | - }, |
52 | | - ) |
53 | | - |
54 | | - prepareInterceptor( |
55 | | - element, |
56 | | - 'selectionStart', |
57 | | - function interceptorImpl(this, v) { |
58 | | - this[UISelection] = undefined |
59 | | - |
60 | | - return {realArgs: v} |
61 | | - }, |
62 | | - ) |
63 | | - prepareInterceptor( |
64 | | - element, |
65 | | - 'selectionEnd', |
66 | | - function interceptorImpl(this, v) { |
67 | | - this[UISelection] = undefined |
68 | | - |
69 | | - return {realArgs: v} |
70 | | - }, |
71 | | - ) |
72 | | - |
73 | | - prepareInterceptor( |
74 | | - element, |
75 | | - 'select', |
76 | | - function interceptorImpl(this: HTMLInputElement | HTMLTextAreaElement) { |
77 | | - this[UISelection] = { |
78 | | - anchorOffset: 0, |
79 | | - focusOffset: getUIValue(element).length, |
80 | | - } |
81 | | - |
82 | | - return {realArgs: [] as []} |
83 | | - }, |
84 | | - ) |
| 78 | + element[UISelection] = selection |
85 | 79 | } |
86 | 80 |
|
87 | 81 | export function setUISelection( |
@@ -120,20 +114,26 @@ export function setUISelection( |
120 | 114 | } |
121 | 115 |
|
122 | 116 | // eslint-disable-next-line no-new-wrappers |
123 | | - const startObj = new Number(startOffset) |
124 | | - ;(startObj as Value)[UISelection] = UISelection |
| 117 | + const startObj = Object.assign(new Number(startOffset), { |
| 118 | + [UISelection]: true, |
| 119 | + }) as unknown as number |
125 | 120 |
|
126 | 121 | try { |
127 | | - element.setSelectionRange(startObj as number, endOffset) |
| 122 | + element.setSelectionRange(startObj, endOffset) |
128 | 123 | } catch { |
129 | 124 | // DOMException for invalid state is expected when calling this |
130 | 125 | // on an element without support for setSelectionRange |
131 | 126 | } |
132 | 127 | } |
133 | 128 |
|
| 129 | +export type UISelectionRange = UISelection & { |
| 130 | + startOffset: number |
| 131 | + endOffset: number |
| 132 | +} |
| 133 | + |
134 | 134 | export function getUISelection( |
135 | 135 | element: HTMLInputElement | HTMLTextAreaElement, |
136 | | -) { |
| 136 | +): UISelectionRange { |
137 | 137 | const sel = element[UISelection] ?? { |
138 | 138 | anchorOffset: element.selectionStart ?? 0, |
139 | 139 | focusOffset: element.selectionEnd ?? 0, |
|
0 commit comments