<commit_before>import { IconifyIconName } from '@iconify/core/lib/icon/name';
import { getStorage, getIcon } from '@iconify/core/lib/storage';
import { coreModules } from '@iconify/core/lib/modules';
import { FullIconifyIcon } from '@iconify/core/lib/icon';
import { findPlaceholders } from './finder';
import { IconifyElementData, elementDataProperty } from './element';
import { renderIcon } from './render';
import { pauseObserver, resumeObserver } from './observer';
import { getRoot } from './root';

/**
 * Flag to avoid scanning DOM too often
 */
let scanQueued = false;

/**
 * Icons have been loaded
 */
function checkPendingIcons(): void {
	if (!scanQueued) {
		scanQueued = true;
		setTimeout(() => {
			if (scanQueued) {
				scanQueued = false;
				scanDOM();
			}
		});
	}
}

/**
 * Compare Icon objects. Returns true if icons are identical.
 *
 * Note: null means icon is invalid, so null to null comparison = false.
 */
const compareIcons = (
	icon1: IconifyIconName | null,
	icon2: IconifyIconName | null
): boolean => {
	return (
		icon1 !== null &&
		icon2 !== null &&
		icon1.name === icon2.name &&
		icon1.prefix === icon2.prefix
	);
};

/**
 * Scan DOM for placeholders
 */
export function scanDOM(root?: HTMLElement): void {
	scanQueued = false;

	// Observer
	let paused = false;

	// List of icons to load: [provider][prefix][name] = boolean
	const loadIcons: Record<
		string,
		Record<string, Record<string, boolean>>
	> = Object.create(null);

	// Get root node and placeholders
	if (!root) {
		root = getRoot();
	}
	findPlaceholders(root).forEach((item) => {
		const element = item.element;
		const iconName = item.name;
		const provider = iconName.provider;
		const prefix = iconName.prefix;
		const name = iconName.name;
		let data: IconifyElementData = element[elementDataProperty];

		// Icon has not been updated since last scan
		if (data !== void 0 && compareIcons(data.name, iconName)) {
			// Icon name was not changed and data is set - quickly return if icon is missing or still loading
			switch (data.status) {
				case 'missing':
					return;

				case 'loading':
					if (
						coreModules.api &&
						coreModules.api.isPending({ provider, prefix, name })
					) {
						// Pending
						return;
					}
			}
		}<commit_msg>Do not scan DOM in SVG framework if DOM is not ready yet<commit_after>import { IconifyIconName } from '@iconify/core/lib/icon/name';
import { getStorage, getIcon } from '@iconify/core/lib/storage';
import { coreModules } from '@iconify/core/lib/modules';
import { FullIconifyIcon } from '@iconify/core/lib/icon';
import { findPlaceholders } from './finder';
import { IconifyElementData, elementDataProperty } from './element';
import { renderIcon } from './render';
import { pauseObserver, resumeObserver } from './observer';
import { getRoot } from './root';

/**
 * Flag to avoid scanning DOM too often
 */
let scanQueued = false;

/**
 * Icons have been loaded
 */
function checkPendingIcons(): void {
	if (!scanQueued) {
		scanQueued = true;
		setTimeout(() => {
			if (scanQueued) {
				scanQueued = false;
				scanDOM();
			}
		});
	}
}

/**
 * Compare Icon objects. Returns true if icons are identical.
 *
 * Note: null means icon is invalid, so null to null comparison = false.
 */
const compareIcons = (
	icon1: IconifyIconName | null,
	icon2: IconifyIconName | null
): boolean => {
	return (
		icon1 !== null &&
		icon2 !== null &&
		icon1.name === icon2.name &&
		icon1.prefix === icon2.prefix
	);
};

/**
 * Scan DOM for placeholders
 */
export function scanDOM(root?: HTMLElement): void {
	scanQueued = false;

	// Observer
	let paused = false;

	// List of icons to load: [provider][prefix][name] = boolean
	const loadIcons: Record<
		string,
		Record<string, Record<string, boolean>>
	> = Object.create(null);

	// Get root node and placeholders
	if (!root) {
		root = getRoot();
	}
	if (!root || !root.querySelectorAll) {
		return;
	}
	findPlaceholders(root).forEach((item) => {
		const element = item.element;
		const iconName = item.name;
		const provider = iconName.provider;
		const prefix = iconName.prefix;
		const name = iconName.name;
		let data: IconifyElementData = element[elementDataProperty];

		// Icon has not been updated since last scan
		if (data !== void 0 && compareIcons(data.name, iconName)) {
			// Icon name was not changed and data is set - quickly return if icon is missing or still loading
			switch (data.status) {
				case 'missing':
					return;

				case 'loading':
					if (
						coreModules.api &&
						coreModules.api.isPending({ provider, prefix, name })
					) {
						// Pending
						return;
					}
			}
		}
{'fraction_non_alphanumeric': 0.13434125269978403, 'fraction_numerical': 0.0038876889848812094, 'mean_word_length': 3.35653809971778, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '917959', 'n_tokens_mistral': 1603, 'n_tokens_neox': 1433, 'n_words': 438}