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

81.19% Statements 95/117
96.55% Branches 28/29
80% Functions 4/5
81.03% Lines 94/116

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 1172x 2x 2x 2x 2x 2x 2x 2x 6x 6x 6x 6x 6x 1x 1x 1x 6x 5x 2x 5x 4x 1x 3x 3x 3x 4x 4x 4x 4x 6x 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 382x 382x 382x 2x 2x 2x 2x 2x 2x 2x 2x 131x 79x 58x 131x 52x 16x 16x 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) {
		if (dom.classList.contains(class_name)) return;
		dom.classList.add(class_name);
	} else {
		if (!dom.classList.contains(class_name)) return;
		dom.classList.remove(class_name);
	}
}