All files / src/internal/client/dom/elements/bindings input.js

92.49% Statements 234/253
88.13% Branches 52/59
85.71% Functions 6/7
92.24% Lines 226/245

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 2462x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 259x 259x 259x 207x       207x 207x 207x 207x 207x 207x 207x 207x 2x 2x 2x 259x 259x 259x 688x       688x 688x 688x 688x 688x 688x 1x 1x 1x 687x 688x 43x 43x 43x 644x 688x         644x 644x 644x 688x 305x 305x 305x 259x 259x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 331x 331x 331x 331x 331x 331x 331x 331x 280x 280x 280x 331x 331x 331x 331x 331x 331x 331x 331x 128x 128x 128x 128x 114x 114x 128x 128x 331x 331x 331x 331x 331x 331x 1262x 1262x 1262x 1262x 1262x 2x 2x 2x 1260x 1262x 1008x 1008x 1008x 1262x 252x 252x 252x 331x 331x 331x 331x 331x 331x 331x 331x 331x 331x 331x 117x 117x 117x 117x 117x 117x 117x 117x 331x 331x 331x 2x 2x 2x   2x 2x 2x 2x 2x 2x 2x 2x 331x 331x 2x 2x 2x 2x 2x 2x 2x 2x 87x 36x 36x 87x 87x 87x 6x 6x 87x 87x 199x 199x 87x 87x 2x 2x 2x 2x 2x 2x 2x 2x 114x 114x 114x 114x 374x 162x 162x 162x 374x 114x 114x 34x 34x 114x 114x 114x 2x 2x 2x 2x 894x 894x 894x 894x 2x 2x 2x 2x 100x 100x 100x 2x 2x 2x 2x 2x 2x 2x                  
import { DEV } from 'esm-env';
import { render_effect, teardown } from '../../../reactivity/effects.js';
import { listen_to_event_and_reset_event } from './shared.js';
import * as e from '../../../errors.js';
import { get_proxied_value, is } from '../../../proxy.js';
import { queue_micro_task } from '../../task.js';
import { hydrating } from '../../hydration.js';
import { is_runes } from '../../../runtime.js';
 
/**
 * @param {HTMLInputElement} input
 * @param {() => unknown} get
 * @param {(value: unknown) => void} set
 * @returns {void}
 */
export function bind_value(input, get, set = get) {
	var runes = is_runes();
 
	listen_to_event_and_reset_event(input, 'input', () => {
		if (DEV && input.type === 'checkbox') {
			// TODO should this happen in prod too?
			e.bind_invalid_checkbox_value();
		}
 
		/** @type {unknown} */
		var value = is_numberlike_input(input) ? to_number(input.value) : input.value;
		set(value);
 
		// In runes mode, respect any validation in accessors (doesn't apply in legacy mode,
		// because we use mutable state which ensures the render effect always runs)
		if (runes && value !== (value = get())) {
			// @ts-expect-error the value is coerced on assignment
			input.value = value ?? '';
		}
	});
 
	render_effect(() => {
		if (DEV && input.type === 'checkbox') {
			// TODO should this happen in prod too?
			e.bind_invalid_checkbox_value();
		}
 
		var value = get();
 
		// If we are hydrating and the value has since changed, then use the update value
		// from the input instead.
		if (hydrating && input.defaultValue !== input.value) {
			set(input.value);
			return;
		}
 
		if (is_numberlike_input(input) && value === to_number(input.value)) {
			// handles 0 vs 00 case (see https://github.com/sveltejs/svelte/issues/9959)
			return;
		}
 
		if (input.type === 'date' && !value && !input.value) {
			// Handles the case where a temporarily invalid date is set (while typing, for example with a leading 0 for the day)
			// and prevents this state from clearing the other parts of the date input (see https://github.com/sveltejs/svelte/issues/7897)
			return;
		}
 
		// don't set the value of the input if it's the same to allow
		// minlength to work properly
		if (value !== input.value) {
			// @ts-expect-error the value is coerced on assignment
			input.value = value ?? '';
		}
	});
}
 
/** @type {Set<HTMLInputElement[]>} */
const pending = new Set();
 
/**
 * @param {HTMLInputElement[]} inputs
 * @param {null | [number]} group_index
 * @param {HTMLInputElement} input
 * @param {() => unknown} get
 * @param {(value: unknown) => void} set
 * @returns {void}
 */
export function bind_group(inputs, group_index, input, get, set = get) {
	var is_checkbox = input.getAttribute('type') === 'checkbox';
	var binding_group = inputs;
 
	// needs to be let or related code isn't treeshaken out if it's always false
	let hydration_mismatch = false;
 
	if (group_index !== null) {
		for (var index of group_index) {
			// @ts-expect-error
			binding_group = binding_group[index] ??= [];
		}
	}
 
	binding_group.push(input);
 
	listen_to_event_and_reset_event(
		input,
		'change',
		() => {
			// @ts-ignore
			var value = input.__value;
 
			if (is_checkbox) {
				value = get_binding_group_value(binding_group, value, input.checked);
			}
 
			set(value);
		},
		// TODO better default value handling
		() => set(is_checkbox ? [] : null)
	);
 
	render_effect(() => {
		var value = get();
 
		// If we are hydrating and the value has since changed, then use the update value
		// from the input instead.
		if (hydrating && input.defaultChecked !== input.checked) {
			hydration_mismatch = true;
			return;
		}
 
		if (is_checkbox) {
			value = value || [];
			// @ts-ignore
			input.checked = get_proxied_value(value).includes(get_proxied_value(input.__value));
		} else {
			// @ts-ignore
			input.checked = is(input.__value, value);
		}
	});
 
	teardown(() => {
		var index = binding_group.indexOf(input);
 
		if (index !== -1) {
			binding_group.splice(index, 1);
		}
	});
 
	if (!pending.has(binding_group)) {
		pending.add(binding_group);
 
		queue_micro_task(() => {
			// necessary to maintain binding group order in all insertion scenarios
			binding_group.sort((a, b) => (a.compareDocumentPosition(b) === 4 ? -1 : 1));
			pending.delete(binding_group);
		});
	}
 
	queue_micro_task(() => {
		if (hydration_mismatch) {
			var value;
 
			if (is_checkbox) {
				value = get_binding_group_value(binding_group, value, input.checked);
			} else {
				var hydration_input = binding_group.find((input) => input.checked);
				// @ts-ignore
				value = hydration_input?.__value;
			}
 
			set(value);
		}
	});
}
 
/**
 * @param {HTMLInputElement} input
 * @param {() => unknown} get
 * @param {(value: unknown) => void} set
 * @returns {void}
 */
export function bind_checked(input, get, set = get) {
	listen_to_event_and_reset_event(input, 'change', () => {
		var value = input.checked;
		set(value);
	});
 
	if (get() == undefined) {
		set(false);
	}
 
	render_effect(() => {
		var value = get();
		input.checked = Boolean(value);
	});
}
 
/**
 * @template V
 * @param {Array<HTMLInputElement>} group
 * @param {V} __value
 * @param {boolean} checked
 * @returns {V[]}
 */
function get_binding_group_value(group, __value, checked) {
	var value = new Set();
 
	for (var i = 0; i < group.length; i += 1) {
		if (group[i].checked) {
			// @ts-ignore
			value.add(group[i].__value);
		}
	}
 
	if (!checked) {
		value.delete(__value);
	}
 
	return Array.from(value);
}
 
/**
 * @param {HTMLInputElement} input
 */
function is_numberlike_input(input) {
	var type = input.type;
	return type === 'number' || type === 'range';
}
 
/**
 * @param {string} value
 */
function to_number(value) {
	return value === '' ? null : +value;
}
 
/**
 * @param {HTMLInputElement} input
 * @param {() => FileList | null} get
 * @param {(value: FileList | null) => void} set
 */
export function bind_files(input, get, set = get) {
	listen_to_event_and_reset_event(input, 'change', () => {
		set(input.files);
	});

	render_effect(() => {
		input.files = get();
	});
}