All files / src/compiler/phases/2-analyze/visitors RegularElement.js

99.42% Statements 172/173
98.03% Branches 50/51
100% Functions 1/1
99.39% Lines 163/164

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 1682x 2x 2x       2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 9479x 9479x 9479x 9479x 9479x 9479x 9479x 9479x 9479x 9479x 24x 5x 1x 1x 5x 23x 24x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 24x 9446x 9446x 9446x 9446x 9479x 9479x 9479x 44x 44x 44x 9479x 18x 18x 18x 9446x 9446x 9446x 9479x 9479x 2x 9479x 1x 1x 9446x 9446x 9446x 9446x 9446x 9446x 9446x 9446x 9479x 21x 21x 21x 9446x 9479x 1324x 1324x 1324x 1324x 1324x 5342x 5342x 5342x 5342x 5342x 5342x 4903x 5342x 441x 441x 441x 5342x 5342x 3164x 1324x 2x 1x 1x 1x 1x 1x 1x 1x 1x 2x 1323x 1323x 1323x 5342x 214x 214x 214x 4x   4x 4x 4x 4x 2178x 1964x 1964x 1964x 1964x 1925x 1964x 41x 41x 5342x 1319x 9441x 9441x 9441x 9441x 9441x 9479x 9479x 109x 9479x 61x 61x 9441x 9441x 9441x  
/** @import { AST } from '#compiler' */
/** @import { Context } from '../types' */
import { is_mathml, is_svg, is_void } from '../../../../utils.js';
import {
	is_tag_valid_with_ancestor,
	is_tag_valid_with_parent
} from '../../../../html-tree-validation.js';
import * as e from '../../../errors.js';
import * as w from '../../../warnings.js';
import { create_attribute, is_custom_element_node } from '../../nodes.js';
import { regex_starts_with_newline } from '../../patterns.js';
import { check_element } from './shared/a11y.js';
import { validate_element } from './shared/element.js';
import { mark_subtree_dynamic } from './shared/fragment.js';
 
/**
 * @param {AST.RegularElement} node
 * @param {Context} context
 */
export function RegularElement(node, context) {
	validate_element(node, context);
 
	check_element(node, context.state);
 
	node.metadata.path = [...context.path];
 
	context.state.analysis.elements.push(node);
 
	// Special case: Move the children of <textarea> into a value attribute if they are dynamic
	if (node.name === 'textarea' && node.fragment.nodes.length > 0) {
		for (const attribute of node.attributes) {
			if (attribute.type === 'Attribute' && attribute.name === 'value') {
				e.textarea_invalid_content(node);
			}
		}
 
		if (node.fragment.nodes.length > 1 || node.fragment.nodes[0].type !== 'Text') {
			const first = node.fragment.nodes[0];
			if (first.type === 'Text') {
				// The leading newline character needs to be stripped because of a qirk:
				// It is ignored by browsers if the tag and its contents are set through
				// innerHTML, but we're now setting it through the value property at which
				// point it is _not_ ignored, so we need to strip it ourselves.
				// see https://html.spec.whatwg.org/multipage/syntax.html#element-restrictions
				// see https://html.spec.whatwg.org/multipage/grouping-content.html#the-pre-element
				first.data = first.data.replace(regex_starts_with_newline, '');
				first.raw = first.raw.replace(regex_starts_with_newline, '');
			}
 
			node.attributes.push(
				create_attribute(
					'value',
					/** @type {AST.Text} */ (node.fragment.nodes.at(0)).start,
					/** @type {AST.Text} */ (node.fragment.nodes.at(-1)).end,
					// @ts-ignore
					node.fragment.nodes
				)
			);
 
			node.fragment.nodes = [];
		}
	}
 
	// Special case: single expression tag child of option element -> add "fake" attribute
	// to ensure that value types are the same (else for example numbers would be strings)
	if (
		node.name === 'option' &&
		node.fragment.nodes?.length === 1 &&
		node.fragment.nodes[0].type === 'ExpressionTag' &&
		!node.attributes.some(
			(attribute) => attribute.type === 'Attribute' && attribute.name === 'value'
		)
	) {
		const child = node.fragment.nodes[0];
		node.attributes.push(create_attribute('value', child.start, child.end, [child]));
	}
 
	const binding = context.state.scope.get(node.name);
	if (
		binding !== null &&
		binding.declaration_kind === 'import' &&
		binding.references.length === 0
	) {
		w.component_name_lowercase(node, node.name);
	}
 
	node.metadata.has_spread = node.attributes.some(
		(attribute) => attribute.type === 'SpreadAttribute'
	);
 
	node.metadata.svg = is_svg(node.name);
	node.metadata.mathml = is_mathml(node.name);
 
	if (is_custom_element_node(node) && node.attributes.length > 0) {
		// we're setting all attributes on custom elements through properties
		mark_subtree_dynamic(context.path);
	}
 
	if (context.state.parent_element) {
		let past_parent = false;
		let only_warn = false;
		const ancestors = [context.state.parent_element];
 
		for (let i = context.path.length - 1; i >= 0; i--) {
			const ancestor = context.path[i];
 
			if (
				ancestor.type === 'IfBlock' ||
				ancestor.type === 'EachBlock' ||
				ancestor.type === 'AwaitBlock' ||
				ancestor.type === 'KeyBlock'
			) {
				// We're creating a separate template string inside blocks, which means client-side this would work
				only_warn = true;
			}
 
			if (!past_parent) {
				if (ancestor.type === 'RegularElement' && ancestor.name === context.state.parent_element) {
					if (!is_tag_valid_with_parent(node.name, context.state.parent_element)) {
						if (only_warn) {
							w.node_invalid_placement_ssr(
								node,
								`\`<${node.name}>\``,
								context.state.parent_element
							);
						} else {
							e.node_invalid_placement(node, `\`<${node.name}>\``, context.state.parent_element);
						}
					}
 
					past_parent = true;
				}
			} else if (ancestor.type === 'RegularElement') {
				ancestors.push(ancestor.name);
 
				if (!is_tag_valid_with_ancestor(node.name, ancestors)) {
					if (only_warn) {
						w.node_invalid_placement_ssr(node, `\`<${node.name}>\``, ancestor.name);
					} else {
						e.node_invalid_placement(node, `\`<${node.name}>\``, ancestor.name);
					}
				}
			} else if (
				ancestor.type === 'Component' ||
				ancestor.type === 'SvelteComponent' ||
				ancestor.type === 'SvelteElement' ||
				ancestor.type === 'SvelteSelf' ||
				ancestor.type === 'SnippetBlock'
			) {
				break;
			}
		}
	}
 
	// Strip off any namespace from the beginning of the node name.
	const node_name = node.name.replace(/[a-zA-Z-]*:/g, '');
 
	if (
		context.state.analysis.source[node.end - 2] === '/' &&
		!is_void(node_name) &&
		!is_svg(node_name)
	) {
		w.element_invalid_self_closing_tag(node, node.name);
	}
 
	context.next({ ...context.state, parent_element: node.name });
}