Home / Function/ attribute_matches() — svelte Function Reference

attribute_matches() — svelte Function Reference

Architecture documentation for the attribute_matches() function in css-prune.js from the svelte codebase.

Function javascript Compiler Analyzer calls 4 called by 1

Entity Profile

Dependency Diagram

graph TD
  553f3c22_7366_321b_ab4f_fcd972b6b687["attribute_matches()"]
  cb1bf043_dade_7352_cc2b_976ffa2968d8["css-prune.js"]
  553f3c22_7366_321b_ab4f_fcd972b6b687 -->|defined in| cb1bf043_dade_7352_cc2b_976ffa2968d8
  44fb7f7e_7a78_eedb_fde3_53e386135788["relative_selector_might_apply_to_node()"]
  44fb7f7e_7a78_eedb_fde3_53e386135788 -->|calls| 553f3c22_7366_321b_ab4f_fcd972b6b687
  653284b2_68fd_eee3_0064_918a4c065d4a["is_text_attribute()"]
  553f3c22_7366_321b_ab4f_fcd972b6b687 -->|calls| 653284b2_68fd_eee3_0064_918a4c065d4a
  b88f7aca_7eab_9566_fd39_4eee932f9d42["test_attribute()"]
  553f3c22_7366_321b_ab4f_fcd972b6b687 -->|calls| b88f7aca_7eab_9566_fd39_4eee932f9d42
  f6625393_617b_8f3b_aaa5_b87527fde52f["get_attribute_chunks()"]
  553f3c22_7366_321b_ab4f_fcd972b6b687 -->|calls| f6625393_617b_8f3b_aaa5_b87527fde52f
  28a76788_b161_f034_e658_6787237c5c8a["get_possible_values()"]
  553f3c22_7366_321b_ab4f_fcd972b6b687 -->|calls| 28a76788_b161_f034_e658_6787237c5c8a
  style 553f3c22_7366_321b_ab4f_fcd972b6b687 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/svelte/src/compiler/phases/2-analyze/css/css-prune.js lines 626–735

function attribute_matches(node, name, expected_value, operator, case_insensitive) {
	for (const attribute of node.attributes) {
		if (attribute.type === 'SpreadAttribute') return true;
		if (attribute.type === 'BindDirective' && attribute.name === name) return true;

		const name_lower = name.toLowerCase();
		// match attributes against the corresponding directive but bail out on exact matching
		if (attribute.type === 'StyleDirective' && name_lower === 'style') return true;
		if (attribute.type === 'ClassDirective' && name_lower === 'class') {
			if (operator === '~=') {
				if (attribute.name === expected_value) return true;
			} else {
				return true;
			}
		}

		if (attribute.type !== 'Attribute') continue;
		if (attribute.name.toLowerCase() !== name_lower) continue;

		if (attribute.value === true) return operator === null;
		if (expected_value === null) return true;

		if (is_text_attribute(attribute)) {
			const matches = test_attribute(
				operator,
				expected_value,
				case_insensitive,
				attribute.value[0].data
			);
			// continue if we still may match against a class/style directive
			if (!matches && (name_lower === 'class' || name_lower === 'style')) continue;
			return matches;
		}

		const chunks = get_attribute_chunks(attribute.value);
		const possible_values = new Set();

		/** @type {string[]} */
		let prev_values = [];
		for (const chunk of chunks) {
			const current_possible_values = get_possible_values(chunk, name_lower === 'class');

			// impossible to find out all combinations
			if (!current_possible_values) return true;

			if (prev_values.length > 0) {
				/** @type {string[]} */
				const start_with_space = [];

				/** @type {string[]} */
				const remaining = [];

				current_possible_values.forEach((current_possible_value) => {
					if (regex_starts_with_whitespace.test(current_possible_value)) {
						start_with_space.push(current_possible_value);
					} else {
						remaining.push(current_possible_value);
					}
				});
				if (remaining.length > 0) {
					if (start_with_space.length > 0) {
						prev_values.forEach((prev_value) => possible_values.add(prev_value));
					}

					/** @type {string[]} */
					const combined = [];

					prev_values.forEach((prev_value) => {
						remaining.forEach((value) => {
							combined.push(prev_value + value);
						});
					});
					prev_values = combined;
					start_with_space.forEach((value) => {
						if (regex_ends_with_whitespace.test(value)) {
							possible_values.add(value);
						} else {
							prev_values.push(value);
						}
					});
					continue;

Domain

Subdomains

Frequently Asked Questions

What does attribute_matches() do?
attribute_matches() is a function in the svelte codebase, defined in packages/svelte/src/compiler/phases/2-analyze/css/css-prune.js.
Where is attribute_matches() defined?
attribute_matches() is defined in packages/svelte/src/compiler/phases/2-analyze/css/css-prune.js at line 626.
What does attribute_matches() call?
attribute_matches() calls 4 function(s): get_attribute_chunks, get_possible_values, is_text_attribute, test_attribute.
What calls attribute_matches()?
attribute_matches() is called by 1 function(s): relative_selector_might_apply_to_node.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free