All files / src/internal/client/dom/elements class.js

80.86% Statements 93/115
96% Branches 24/25
80% Functions 4/5
80.7% Lines 92/114

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 1152x 2x 2x 2x 2x 2x 2x 2x 4x 4x 4x 4x 4x 1x 1x 1x 4x 3x 2x 3x 2x 1x 1x 1x 1x 2x 2x 2x 2x 4x 2x 2x 2x 2x 2x 2x 2x                                             2x 2x 2x 2x 2x 2x 2x 376x 376x 376x 376x 376x 49x 49x 49x 375x 327x 38x 327x 289x 289x 289x 289x 36x 289x 253x 253x 289x 289x 289x 289x 376x 2x 2x 2x 2x 2x 2x 380x 380x 380x 2x 2x 2x 2x 2x 2x 2x 2x 131x 79x 127x 52x 52x 131x  
import { hydrating } from '../hydration.js';
 
/**
 * @param {SVGElement} dom
 * @param {string} value
 * @returns {void}
 */
export function set_svg_class(dom, value) {
	// @ts-expect-error need to add __className to patched prototype
	var prev_class_name = dom.__className;
	var next_class_name = to_class(value);
 
	if (hydrating && dom.getAttribute('class') === next_class_name) {
		// In case of hydration don't reset the class as it's already correct.
		// @ts-expect-error need to add __className to patched prototype
		dom.__className = next_class_name;
	} else if (
		prev_class_name !== next_class_name ||
		(hydrating && dom.getAttribute('class') !== next_class_name)
	) {
		if (next_class_name === '') {
			dom.removeAttribute('class');
		} else {
			dom.setAttribute('class', next_class_name);
		}
 
		// @ts-expect-error need to add __className to patched prototype
		dom.__className = next_class_name;
	}
}
 
/**
 * @param {MathMLElement} dom
 * @param {string} value
 * @returns {void}
 */
export function set_mathml_class(dom, value) {
	// @ts-expect-error need to add __className to patched prototype
	var prev_class_name = dom.__className;
	var next_class_name = to_class(value);

	if (hydrating && dom.getAttribute('class') === next_class_name) {
		// In case of hydration don't reset the class as it's already correct.
		// @ts-expect-error need to add __className to patched prototype
		dom.__className = next_class_name;
	} else if (
		prev_class_name !== next_class_name ||
		(hydrating && dom.getAttribute('class') !== next_class_name)
	) {
		if (next_class_name === '') {
			dom.removeAttribute('class');
		} else {
			dom.setAttribute('class', next_class_name);
		}

		// @ts-expect-error need to add __className to patched prototype
		dom.__className = next_class_name;
	}
}
 
/**
 * @param {HTMLElement} dom
 * @param {string} value
 * @returns {void}
 */
export function set_class(dom, value) {
	// @ts-expect-error need to add __className to patched prototype
	var prev_class_name = dom.__className;
	var next_class_name = to_class(value);
 
	if (hydrating && dom.className === next_class_name) {
		// In case of hydration don't reset the class as it's already correct.
		// @ts-expect-error need to add __className to patched prototype
		dom.__className = next_class_name;
	} else if (
		prev_class_name !== next_class_name ||
		(hydrating && dom.className !== next_class_name)
	) {
		// Removing the attribute when the value is only an empty string causes
		// peformance issues vs simply making the className an empty string. So
		// we should only remove the class if the the value is nullish.
		if (value == null) {
			dom.removeAttribute('class');
		} else {
			dom.className = next_class_name;
		}
 
		// @ts-expect-error need to add __className to patched prototype
		dom.__className = next_class_name;
	}
}
 
/**
 * @template V
 * @param {V} value
 * @returns {string | V}
 */
function to_class(value) {
	return value == null ? '' : value;
}
 
/**
 * @param {Element} dom
 * @param {string} class_name
 * @param {boolean} value
 * @returns {void}
 */
export function toggle_class(dom, class_name, value) {
	if (value) {
		dom.classList.add(class_name);
	} else {
		dom.classList.remove(class_name);
	}
}