All files / src/compiler/phases/2-analyze/css css-analyze.js

99.29% Statements 282/284
99% Branches 99/100
100% Functions 9/9
99.28% Lines 279/281

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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 2822x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 5661x 5661x 5661x 5661x 5661x 5661x 411x 335x 335x 335x 335x 379x 411x 5661x 5661x 2x 2x 2x 2x 2x 2790x 2790x 2790x 2790x 255x 2790x 2790x 2x 2x 2x 2x 115x 20x 16x 16x 20x 115x 115x 2x 2x 1872x 1872x 1872x 1872x 1872x 1872x 196x 196x 196x 5x 5x 5x 3x 3x 5x 2x 196x 1872x 1863x 1863x 1872x 2863x 3035x 3035x 3035x 224x 224x 224x 4x 4x 220x 220x 224x 1x 1x 219x 219x 219x 219x 219x 224x 9x 224x 6x 6x 224x 3035x 2852x 1852x 1852x 1852x 1852x 1852x 1852x 1852x 1852x 1872x 107x 1872x 28x 28x 28x 28x 28x 7x 7x 28x 2x 2x 2880x 2880x 2880x 2880x 2880x 2880x 8x 2880x 3x 3x 2877x 2880x 2880x 2880x 2710x 2710x 2710x 2691x 11x 11x 11x 11x 11x 11x 2710x 2710x 2877x 2877x 2877x 2877x 2877x 2877x 2x 2x 1544x 1544x 1544x 1558x 1558x 1558x 2559x 2559x 2559x 8x 8x 8x 2559x 2559x 44x 44x 12x 12x 2x 2x 12x 12x 12x 2559x 1558x 1558x 1544x 1544x 1544x 44x 1x 1x 43x 43x 43x 70x 70x 42x 1x 1x 41x 70x 1x 1x 43x 43x 44x     41x 44x 2x 2x 39x 39x 39x 39x 44x 13x 44x 44x 3x 44x 1x 1x 44x 1538x 1538x 1538x 1538x 1538x 1538x 1538x 1519x 1519x 1519x 1538x 2x 2x 41x 41x 41x 41x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 41x 37x 37x 37x 37x 1x 37x 1x 1x 39x 39x 39x 2x 2x 2x 2x 2x 2x 2x 425x 425x  
/** @import { ComponentAnalysis } from '../../types.js' */
/** @import { Css } from '#compiler' */
/** @import { Visitors } from 'zimmerframe' */
import { walk } from 'zimmerframe';
import * as e from '../../../errors.js';
import { is_keyframes_node } from '../../css.js';
 
/**
 * @typedef {Visitors<
 *   Css.Node,
 *   {
 *     keyframes: string[];
 *     rule: Css.Rule | null;
 *   }
 * >} CssVisitors
 */
 
/**
 * True if is `:global(...)` or `:global`
 * @param {Css.RelativeSelector} relative_selector
 * @returns {relative_selector is Css.RelativeSelector & { selectors: [Css.PseudoClassSelector, ...Array<Css.PseudoClassSelector | Css.PseudoElementSelector>] }}
 */
function is_global(relative_selector) {
	const first = relative_selector.selectors[0];
 
	return (
		first.type === 'PseudoClassSelector' &&
		first.name === 'global' &&
		(first.args === null ||
			// Only these two selector types keep the whole selector global, because e.g.
			// :global(button).x means that the selector is still scoped because of the .x
			relative_selector.selectors.every(
				(selector) =>
					selector.type === 'PseudoClassSelector' || selector.type === 'PseudoElementSelector'
			))
	);
}
 
/**
 * True if is `:global`
 * @param {Css.SimpleSelector} simple_selector
 */
function is_global_block_selector(simple_selector) {
	return (
		simple_selector.type === 'PseudoClassSelector' &&
		simple_selector.name === 'global' &&
		simple_selector.args === null
	);
}
 
/** @type {CssVisitors} */
const css_visitors = {
	Atrule(node, context) {
		if (is_keyframes_node(node)) {
			if (!node.prelude.startsWith('-global-')) {
				context.state.keyframes.push(node.prelude);
			}
		}
 
		context.next();
	},
	ComplexSelector(node, context) {
		context.next(); // analyse relevant selectors first
 
		{
			const global = node.children.find(is_global);
 
			if (global) {
				const idx = node.children.indexOf(global);
 
				if (global.selectors[0].args !== null && idx !== 0 && idx !== node.children.length - 1) {
					// ensure `:global(...)` is not used in the middle of a selector (but multiple `global(...)` in sequence are ok)
					for (let i = idx + 1; i < node.children.length; i++) {
						if (!is_global(node.children[i])) {
							e.css_global_invalid_placement(global.selectors[0]);
						}
					}
				}
			}
		}
 
		// ensure `:global(...)` do not lead to invalid css after `:global()` is removed
		for (const relative_selector of node.children) {
			for (let i = 0; i < relative_selector.selectors.length; i++) {
				const selector = relative_selector.selectors[i];
 
				if (selector.type === 'PseudoClassSelector' && selector.name === 'global') {
					const child = selector.args?.children[0].children[0];
					// ensure `:global(element)` to be at the first position in a compound selector
					if (child?.selectors[0].type === 'TypeSelector' && i !== 0) {
						e.css_global_invalid_selector_list(selector);
					}
 
					// ensure `:global(.class)` is not followed by a type selector, eg: `:global(.class)element`
					if (relative_selector.selectors[i + 1]?.type === 'TypeSelector') {
						e.css_type_selector_invalid_placement(relative_selector.selectors[i + 1]);
					}
 
					// ensure `:global(...)`contains a single selector
					// (standalone :global() with multiple selectors is OK)
					if (
						selector.args !== null &&
						selector.args.children.length > 1 &&
						(node.children.length > 1 || relative_selector.selectors.length > 1)
					) {
						e.css_global_invalid_selector(selector);
					}
				}
			}
		}
 
		node.metadata.rule = context.state.rule;
 
		node.metadata.used ||= node.children.every(
			({ metadata }) => metadata.is_global || metadata.is_global_like
		);
 
		if (
			node.metadata.rule?.metadata.parent_rule &&
			node.children[0]?.selectors[0]?.type === 'NestingSelector'
		) {
			const parent_is_global = node.metadata.rule.metadata.parent_rule.prelude.children.some(
				(child) => child.children.length === 1 && child.children[0].metadata.is_global
			);
			// mark `&:hover` in `:global(.foo) { &:hover { color: green }}` as used
			if (parent_is_global) {
				node.metadata.used = true;
			}
		}
	},
	RelativeSelector(node, context) {
		const parent = /** @type {Css.ComplexSelector} */ (context.path.at(-1));
 
		if (
			node.combinator != null &&
			!context.state.rule?.metadata.parent_rule &&
			parent.children[0] === node &&
			context.path.at(-3)?.type !== 'PseudoClassSelector'
		) {
			e.css_selector_invalid(node.combinator);
		}
 
		node.metadata.is_global = node.selectors.length >= 1 && is_global(node);
 
		if (node.selectors.length === 1) {
			const first = node.selectors[0];
			node.metadata.is_global_like ||=
				(first.type === 'PseudoClassSelector' && first.name === 'host') ||
				(first.type === 'PseudoElementSelector' &&
					[
						'view-transition',
						'view-transition-group',
						'view-transition-old',
						'view-transition-new',
						'view-transition-image-pair'
					].includes(first.name));
		}
 
		node.metadata.is_global_like ||= !!node.selectors.find(
			(child) => child.type === 'PseudoClassSelector' && child.name === 'root'
		);
 
		context.next();
	},
	Rule(node, context) {
		node.metadata.parent_rule = context.state.rule;
 
		node.metadata.is_global_block = node.prelude.children.some((selector) => {
			let is_global_block = false;
 
			for (const child of selector.children) {
				const idx = child.selectors.findIndex(is_global_block_selector);
 
				if (is_global_block) {
					// All selectors after :global are unscoped
					child.metadata.is_global_like = true;
				}
 
				if (idx !== -1) {
					is_global_block = true;
					for (let i = idx + 1; i < child.selectors.length; i++) {
						walk(/** @type {Css.Node} */ (child.selectors[i]), null, {
							ComplexSelector(node) {
								node.metadata.used = true;
							}
						});
					}
				}
			}
 
			return is_global_block;
		});
 
		if (node.metadata.is_global_block) {
			if (node.prelude.children.length > 1) {
				e.css_global_block_invalid_list(node.prelude);
			}
 
			const complex_selector = node.prelude.children[0];
			const global_selector = complex_selector.children.find((r, selector_idx) => {
				const idx = r.selectors.findIndex(is_global_block_selector);
				if (idx === 0) {
					if (r.selectors.length > 1 && selector_idx === 0 && node.metadata.parent_rule === null) {
						e.css_global_block_invalid_modifier_start(r.selectors[1]);
					}
					return true;
				} else if (idx !== -1) {
					e.css_global_block_invalid_modifier(r.selectors[idx]);
				}
			});
 
			if (!global_selector) {
				throw new Error('Internal error: global block without :global selector');
			}
 
			if (global_selector.combinator && global_selector.combinator.name !== ' ') {
				e.css_global_block_invalid_combinator(global_selector, global_selector.combinator.name);
			}
 
			const declaration = node.block.children.find((child) => child.type === 'Declaration');
 
			if (
				declaration &&
				// :global { color: red; } is invalid, but foo :global { color: red; } is valid
				node.prelude.children.length === 1 &&
				node.prelude.children[0].children.length === 1 &&
				node.prelude.children[0].children[0].selectors.length === 1
			) {
				e.css_global_block_invalid_declaration(declaration);
			}
		}
 
		context.next({
			...context.state,
			rule: node
		});
 
		node.metadata.has_local_selectors = node.prelude.children.some((selector) => {
			return selector.children.some(
				({ metadata }) => !metadata.is_global && !metadata.is_global_like
			);
		});
	},
	NestingSelector(node, context) {
		const rule = /** @type {Css.Rule} */ (context.state.rule);
		const parent_rule = rule.metadata.parent_rule;
 
		if (!parent_rule) {
			// https://developer.mozilla.org/en-US/docs/Web/CSS/Nesting_selector#using_outside_nested_rule
			const children = rule.prelude.children;
			const selectors = children[0].children[0].selectors;
			if (
				children.length > 1 ||
				selectors.length > 1 ||
				selectors[0].type !== 'PseudoClassSelector' ||
				selectors[0].name !== 'global' ||
				selectors[0].args?.children[0]?.children[0].selectors[0] !== node
			) {
				e.css_nesting_selector_invalid_placement(node);
			}
		} else if (
			// :global { &.foo { ... } } is invalid
			parent_rule.metadata.is_global_block &&
			!parent_rule.metadata.parent_rule &&
			parent_rule.prelude.children[0].children.length === 1 &&
			parent_rule.prelude.children[0].children[0].selectors.length === 1
		) {
			e.css_global_block_invalid_modifier_start(node);
		}
 
		context.next();
	}
};
 
/**
 * @param {Css.StyleSheet} stylesheet
 * @param {ComponentAnalysis} analysis
 */
export function analyze_css(stylesheet, analysis) {
	walk(stylesheet, { keyframes: analysis.css.keyframes, rule: null }, css_visitors);
}