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

98.2% Statements 164/167
95.74% Branches 45/47
100% Functions 1/1
98.11% Lines 156/159

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 1602x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 8986x 8986x 8986x 8986x 8986x 8986x 8986x 8986x 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 8953x 8953x 8953x 8953x 8986x 8986x 8986x 44x 44x 44x 8986x 18x 18x 18x 8953x 8953x 8953x 8986x 8986x 2x 8986x 1x 1x 8953x 8953x 8953x 8953x 8953x 8953x 8953x 8953x 8986x 1197x 1197x 1197x 1197x 1197x 4892x 4892x 4892x 4892x 4892x 4892x 4469x 4892x 425x 425x 425x 4892x 4892x 2882x 1197x 1x 1x 1x 1x 1x 1x 1x     1x 1197x 1197x 1197x 4892x 197x 197x 197x 4x   4x 4x 4x 4x 2010x 1813x 1813x 1813x 1813x 1778x 1813x 37x 37x 4892x 1193x 8949x 8949x 8949x 8949x 8949x 8986x 8986x 102x 8986x 54x 54x 8949x 8949x 8949x  
/** @import { RegularElement } 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 } 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';
 
/**
 * @param {RegularElement} node
 * @param {Context} context
 */
export function RegularElement(node, context) {
	validate_element(node, context);
 
	check_element(node, context.state);
 
	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 {import('#compiler').Text} */ (node.fragment.nodes.at(0)).start,
					/** @type {import('#compiler').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 (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 });
}