1import { A as listen_to_event_and_reset_event, D as DEV, B as bind_invalid_checkbox_value, G as current_batch, I as tick, w as untrack, x as render_effect, v as teardown, J as queue_micro_task, K as previous_batch, L as is, h as hydrating } from './index-Bq2njFOY.js'; 2 3/** @import { Batch } from '../../../reactivity/batch.js' */ 4 5/** 6 * @param {HTMLInputElement} input 7 * @param {() => unknown} get 8 * @param {(value: unknown) => void} set 9 * @returns {void} 10 */ 11function bind_value(input, get, set = get) { 12 var batches = new WeakSet(); 13 14 listen_to_event_and_reset_event(input, 'input', async (is_reset) => { 15 if (DEV && input.type === 'checkbox') { 16 // TODO should this happen in prod too? 17 bind_invalid_checkbox_value(); 18 } 19 20 /** @type {any} */ 21 var value = is_reset ? input.defaultValue : input.value; 22 value = is_numberlike_input(input) ? to_number(value) : value; 23 set(value); 24 25 if (current_batch !== null) { 26 batches.add(current_batch); 27 } 28 29 // Because `{#each ...}` blocks work by updating sources inside the flush, 30 // we need to wait a tick before checking to see if we should forcibly 31 // update the input and reset the selection state 32 await tick(); 33 34 // Respect any validation in accessors 35 if (value !== (value = get())) { 36 var start = input.selectionStart; 37 var end = input.selectionEnd; 38 var length = input.value.length; 39 40 // the value is coerced on assignment 41 input.value = value ?? ''; 42 43 // Restore selection 44 if (end !== null) { 45 var new_length = input.value.length; 46 // If cursor was at end and new input is longer, move cursor to new end 47 if (start === end && end === length && new_length > length) { 48 input.selectionStart = new_length; 49 input.selectionEnd = new_length; 50 } else { 51 input.selectionStart = start; 52 input.selectionEnd = Math.min(end, new_length); 53 } 54 } 55 } 56 }); 57 58 if ( 59 // If we are hydrating and the value has since changed, 60 // then use the updated value from the input instead. 61 (hydrating && input.defaultValue !== input.value) || 62 // If defaultValue is set, then value == defaultValue 63 // TODO Svelte 6: remove input.value check and set to empty string? 64 (untrack(get) == null && input.value) 65 ) { 66 set(is_numberlike_input(input) ? to_number(input.value) : input.value); 67 68 if (current_batch !== null) { 69 batches.add(current_batch); 70 } 71 } 72 73 render_effect(() => { 74 if (DEV && input.type === 'checkbox') { 75 // TODO should this happen in prod too? 76 bind_invalid_checkbox_value(); 77 } 78 79 var value = get(); 80 81 if (input === document.activeElement) { 82 // we need both, because in non-async mode, render effects run before previous_batch is set 83 var batch = /** @type {Batch} */ (previous_batch ?? current_batch); 84 85 // Never rewrite the contents of a focused input. We can get here if, for example, 86 // an update is deferred because of async work depending on the input: 87 // 88 // <input bind:value={query}> 89 // <p>{await find(query)}</p> 90 if (batches.has(batch)) { 91 return; 92 } 93 } 94 95 if (is_numberlike_input(input) && value === to_number(input.value)) { 96 // handles 0 vs 00 case (see https://github.com/sveltejs/svelte/issues/9959) 97 return; 98 } 99 100 if (input.type === 'date' && !value && !input.value) { 101 // Handles the case where a temporarily invalid date is set (while typing, for example with a leading 0 for the day) 102 // and prevents this state from clearing the other parts of the date input (see https://github.com/sveltejs/svelte/issues/7897) 103 return; 104 } 105 106 // don't set the value of the input if it's the same to allow 107 // minlength to work properly 108 if (value !== input.value) { 109 // @ts-expect-error the value is coerced on assignment 110 input.value = value ?? ''; 111 } 112 }); 113} 114 115/** @type {Set<HTMLInputElement[]>} */ 116const pending = new Set(); 117 118/** 119 * @param {HTMLInputElement[]} inputs 120 * @param {null | [number]} group_index 121 * @param {HTMLInputElement} input 122 * @param {() => unknown} get 123 * @param {(value: unknown) => void} set 124 * @returns {void} 125 */ 126function bind_group(inputs, group_index, input, get, set = get) { 127 var is_checkbox = input.getAttribute('type') === 'checkbox';
128 var binding_group = inputs; 129 130 // needs to be let or related code isn't treeshaken out if it's always false 131 let hydration_mismatch = false; 132 133 if (group_index !== null) { 134 for (var index of group_index) { 135 // @ts-expect-error 136 binding_group = binding_group[index] ??= []; 137 } 138 } 139 140 binding_group.push(input); 141 142 listen_to_event_and_reset_event( 143 input, 144 'change', 145 () => { 146 // @ts-ignore 147 var value = input.__value; 148 149 if (is_checkbox) { 150 value = get_binding_group_value(binding_group, value, input.checked); 151 } 152 153 set(value); 154 }, 155 // TODO better default value handling 156 () => set(is_checkbox ? [] : null) 157 ); 158 159 render_effect(() => { 160 var value = get(); 161 162 // If we are hydrating and the value has since changed, then use the update value 163 // from the input instead. 164 if (hydrating && input.defaultChecked !== input.checked) { 165 hydration_mismatch = true; 166 return; 167 } 168 169 if (is_checkbox) { 170 value = value || []; 171 // @ts-ignore 172 input.checked = value.includes(input.__value); 173 } else { 174 // @ts-ignore 175 input.checked = is(input.__value, value); 176 } 177 }); 178 179 teardown(() => { 180 var index = binding_group.indexOf(input); 181 182 if (index !== -1) { 183 binding_group.splice(index, 1); 184 } 185 }); 186 187 if (!pending.has(binding_group)) { 188 pending.add(binding_group); 189 190 queue_micro_task(() => { 191 // necessary to maintain binding group order in all insertion scenarios 192 binding_group.sort((a, b) => (a.compareDocumentPosition(b) === 4 ? -1 : 1)); 193 pending.delete(binding_group); 194 }); 195 } 196 197 queue_micro_task(() => { 198 if (hydration_mismatch) { 199 var value; 200 201 if (is_checkbox) { 202 value = get_binding_group_value(binding_group, value, input.checked); 203 } else { 204 var hydration_input = binding_group.find((input) => input.checked); 205 // @ts-ignore 206 value = hydration_input?.__value; 207 } 208 209 set(value); 210 } 211 }); 212} 213 214/** 215 * @param {HTMLInputElement} input 216 * @param {() => unknown} get 217 * @param {(value: unknown) => void} set 218 * @returns {void} 219 */ 220function bind_checked(input, get, set = get) { 221 listen_to_event_and_reset_event(input, 'change', (is_reset) => { 222 var value = is_reset ? input.defaultChecked : input.checked; 223 set(value); 224 }); 225 226 if ( 227 // If we are hydrating and the value has since changed, 228 // then use the update value from the input instead. 229 (hydrating && input.defaultChecked !== input.checked) || 230 // If defaultChecked is set, then checked == defaultChecked 231 untrack(get) == null 232 ) { 233 set(input.checked); 234 } 235 236 render_effect(() => { 237 var value = get(); 238 input.checked = Boolean(value); 239 }); 240} 241 242/** 243 * @template V 244 * @param {Array<HTMLInputElement>} group 245 * @param {V} __value 246 * @param {boolean} checked 247 * @returns {V[]} 248 */ 249function get_binding_group_value(group, __value, checked) { 250 /** @type {Set<V>} */ 251 var value = new Set(); 252 253 for (var i = 0; i < group.length; i += 1) { 254 if (group[i].checked) { 255 // @ts-ignore 256 value.add(group[i].__value); 257 } 258 } 259 260 if (!checked) { 261 value.delete(__value); 262 } 263 264 return Array.from(value); 265} 266 267/** 268 * @param {HTMLInputElement} input 269 */ 270function is_numberlike_input(input) { 271 var type = input.type; 272 return type === 'number' || type === 'range'; 273} 274 275/** 276 * @param {string} value 277 */ 278function to_number(value) { 279 return value === '' ? null : +value; 280} 281 282export { bind_checked as a, bind_value as b, bind_group as c }; 283//# sourceMappingURL=input-CXpCw23l.js.map
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.