All files / src/compiler/phases/1-parse/read style.js

97.29% Statements 611/628
94.32% Branches 133/141
100% Functions 15/15
97.28% Lines 610/627

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 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 6282x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 396x 396x 396x 396x 396x 396x 396x 396x 396x 396x 396x 396x 396x 396x 396x 396x 396x 396x 396x 396x 2x 2x 2x 2x 2x 2x 396x 396x 396x 396x 396x 1696x 1696x 1696x 387x 387x 1309x 1696x 92x 1696x 1217x 1217x 1696x 1x 1x 1x 2x 2x 2x 2x 2x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 71x 71x 95x 25x 25x 25x 95x 95x 95x 95x 95x 95x 95x 95x 95x 95x 2x 2x 2x 2x 2x 1344x 1344x 1344x 1344x 1344x 1344x 1344x 1344x 1344x 1344x 1344x 1344x 1344x 1344x 1344x 1344x 2x 2x 2x 2x 2x 2x 1507x 1507x 1507x 1507x 1507x 1507x 1507x 1507x 1507x 1542x 1542x 1542x 1542x 1542x 1542x 1542x 1501x 1501x 1501x 1501x 1501x 1501x 1542x 35x 35x 35x 1542x       2x 2x 2x 2x 2x 2x 1542x 1542x 1542x 1542x 1542x 1542x 1542x 1542x 1542x 1542x 1542x 1542x 2469x 2469x 2469x 2469x 2469x 2469x 2469x 2469x 2469x 2469x 2469x 2469x 2469x 2469x 1542x 1542x 1542x 1542x 1542x 2569x 2569x 2569x 18x 18x 18x 18x 18x 18x 2569x 30x 30x 30x 2x 2x 2x 30x 30x 30x 30x 30x 30x 30x 2551x 4x 4x 4x 4x 4x 4x 2521x 1566x 1566x 1566x 1566x 1566x 1566x 2517x 21x 21x 21x 21x 21x 21x 21x 21x 21x 5x 5x 5x 951x 204x 204x 204x 204x 204x 204x 158x 158x 158x 204x 204x 204x 204x 204x 204x 204x 204x 930x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 34x 34x 34x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 726x 11x 11x 11x 11x 11x 11x 11x 11x 675x 40x 40x 40x 40x 40x 40x 664x 620x 620x 620x 2x 2x 2x 618x 618x 618x 618x 618x 618x 618x 618x 2567x 2567x 2567x 2567x 2569x 1536x 1536x 1536x 1536x 1536x 1536x 1536x 1536x 1536x 1536x 1536x 1536x 1536x 1536x 1536x 1536x 1536x 1031x 1031x 1031x 1031x 2449x 930x 4x 3x 3x 930x 926x 926x 926x 927x 927x 927x 927x 927x 927x 930x 1x 1x 930x 2569x       2x 2x 2x 2x 2x 1031x 1031x 1031x 1031x 1031x 1031x 1031x 1031x 817x 817x 817x 817x 817x 817x 817x 817x 817x 817x 214x 1030x 113x 113x 113x 113x 113x 113x 113x 101x 101x 101x 2x 2x 2x 2x 2x 1409x 1409x 1409x 1409x 1409x 1409x 1409x 1409x 1409x 2904x 2904x 2904x 1406x 2793x 1498x 1498x 2904x 1406x 1406x 1406x 1406x 1406x 1406x 1406x 1406x 1406x 1406x 2x 2x 2x 2x 2x 2x 2x 1498x 1498x 4x 4x 1494x 1494x 1494x 1494x 1494x 1494x 1494x 1494x 1498x 1498x 2x 2x 2x 2x 2x 1366x 1366x 1366x 1366x 1366x 1366x 1366x 1366x 1366x 1366x 1366x 1x 1x 1365x 1365x 1365x 1365x 1360x 1360x 1365x 1365x 1365x 1365x 1365x 1365x 1365x 1365x 1365x 2x 2x 2x 2x 2x 2956x 2956x 2956x 2956x 2956x 2956x 2956x 2956x 2956x 30541x 30541x 30541x     30541x   30541x 37x 30541x 180x 30504x 38x 30324x 20x 30286x 2955x 2955x 27586x 27586x 27586x 27586x 27586x 1x 1x 1x 2x 2x 2x 2x 2x 2x 34x 34x 34x 34x 34x 34x 134x 134x     134x   134x 34x 32x 32x 34x 34x 134x 100x 100x 100x 100x 100x       2x 2x 2x 2x 2x 2566x 2566x 2566x 2566x 2566x 2566x     2566x 2566x 2566x 2566x 9750x 9750x 7x 7x 9750x 7x 9743x 9736x 9736x 9736x 7170x 9736x 2566x 2566x 7184x 7184x 7184x 2566x 2566x 2x 2x 2564x 2564x 2564x 2x 2x 6142x 6142x 6142x 122x 120x 120x 120x 122x 122x 2x 2x 2x 122x 122x 122x 6142x  
import * as e from '../../../errors.js';
 
const REGEX_MATCHER = /^[~^$*|]?=/;
const REGEX_CLOSING_BRACKET = /[\s\]]/;
const REGEX_ATTRIBUTE_FLAGS = /^[a-zA-Z]+/; // only `i` and `s` are valid today, but make it future-proof
const REGEX_COMBINATOR = /^(\+|~|>|\|\|)/;
const REGEX_PERCENTAGE = /^\d+(\.\d+)?%/;
const REGEX_NTH_OF =
	/^(even|odd|\+?(\d+|\d*n(\s*[+-]\s*\d+)?)|-\d*n(\s*\+\s*\d+))((?=\s*[,)])|\s+of\s+)/;
const REGEX_WHITESPACE_OR_COLON = /[\s:]/;
const REGEX_LEADING_HYPHEN_OR_DIGIT = /-?\d/;
const REGEX_VALID_IDENTIFIER_CHAR = /[a-zA-Z0-9_-]/;
const REGEX_COMMENT_CLOSE = /\*\//;
const REGEX_HTML_COMMENT_CLOSE = /-->/;
 
/**
 * @param {import('../index.js').Parser} parser
 * @param {number} start
 * @param {Array<import('#compiler').Attribute | import('#compiler').SpreadAttribute | import('#compiler').Directive>} attributes
 * @returns {import('#compiler').Css.StyleSheet}
 */
export default function read_style(parser, start, attributes) {
	const content_start = parser.index;
	const children = read_body(parser, '</style');
	const content_end = parser.index;
 
	parser.read(/^<\/style\s*>/);
 
	return {
		type: 'StyleSheet',
		start,
		end: parser.index,
		attributes,
		children,
		content: {
			start: content_start,
			end: content_end,
			styles: parser.template.slice(content_start, content_end),
			comment: null
		}
	};
}
 
/**
 * @param {import('../index.js').Parser} parser
 * @param {string} close
 * @returns {any[]}
 */
function read_body(parser, close) {
	/** @type {Array<import('#compiler').Css.Rule | import('#compiler').Css.Atrule>} */
	const children = [];
 
	while (parser.index < parser.template.length) {
		allow_comment_or_whitespace(parser);
 
		if (parser.match(close)) {
			return children;
		}
 
		if (parser.match('@')) {
			children.push(read_at_rule(parser));
		} else {
			children.push(read_rule(parser));
		}
	}
 
	e.expected_token(parser.template.length, close);
}
 
/**
 * @param {import('../index.js').Parser} parser
 * @returns {import('#compiler').Css.Atrule}
 */
function read_at_rule(parser) {
	const start = parser.index;
	parser.eat('@', true);
 
	const name = read_identifier(parser);
 
	const prelude = read_value(parser);
 
	/** @type {import('#compiler').Css.Block | null} */
	let block = null;
 
	if (parser.match('{')) {
		// e.g. `@media (...) {...}`
		block = read_block(parser);
	} else {
		// e.g. `@import '...'`
		parser.eat(';', true);
	}
 
	return {
		type: 'Atrule',
		start,
		end: parser.index,
		name,
		prelude,
		block
	};
}
 
/**
 * @param {import('../index.js').Parser} parser
 * @returns {import('#compiler').Css.Rule}
 */
function read_rule(parser) {
	const start = parser.index;
 
	return {
		type: 'Rule',
		prelude: read_selector_list(parser),
		block: read_block(parser),
		start,
		end: parser.index,
		metadata: {
			parent_rule: null,
			has_local_selectors: false,
			is_global_block: false
		}
	};
}
 
/**
 * @param {import('../index.js').Parser} parser
 * @param {boolean} [inside_pseudo_class]
 * @returns {import('#compiler').Css.SelectorList}
 */
function read_selector_list(parser, inside_pseudo_class = false) {
	/** @type {import('#compiler').Css.ComplexSelector[]} */
	const children = [];
 
	allow_comment_or_whitespace(parser);
 
	const start = parser.index;
 
	while (parser.index < parser.template.length) {
		children.push(read_selector(parser, inside_pseudo_class));
 
		const end = parser.index;
 
		parser.allow_whitespace();
 
		if (inside_pseudo_class ? parser.match(')') : parser.match('{')) {
			return {
				type: 'SelectorList',
				start,
				end,
				children
			};
		} else {
			parser.eat(',', true);
			allow_comment_or_whitespace(parser);
		}
	}

	e.unexpected_eof(parser.template.length);
}
 
/**
 * @param {import('../index.js').Parser} parser
 * @param {boolean} [inside_pseudo_class]
 * @returns {import('#compiler').Css.ComplexSelector}
 */
function read_selector(parser, inside_pseudo_class = false) {
	const list_start = parser.index;
 
	/** @type {import('#compiler').Css.RelativeSelector[]} */
	const children = [];
 
	/**
	 * @param {import('#compiler').Css.Combinator | null} combinator
	 * @param {number} start
	 * @returns {import('#compiler').Css.RelativeSelector}
	 */
	function create_selector(combinator, start) {
		return {
			type: 'RelativeSelector',
			combinator,
			selectors: [],
			start,
			end: -1,
			metadata: {
				is_global: false,
				is_host: false,
				is_root: false,
				scoped: false
			}
		};
	}
 
	/** @type {import('#compiler').Css.RelativeSelector} */
	let relative_selector = create_selector(null, parser.index);
 
	while (parser.index < parser.template.length) {
		let start = parser.index;
 
		if (parser.eat('&')) {
			relative_selector.selectors.push({
				type: 'NestingSelector',
				name: '&',
				start,
				end: parser.index
			});
		} else if (parser.eat('*')) {
			let name = '*';
 
			if (parser.eat('|')) {
				// * is the namespace (which we ignore)
				name = read_identifier(parser);
			}
 
			relative_selector.selectors.push({
				type: 'TypeSelector',
				name,
				start,
				end: parser.index
			});
		} else if (parser.eat('#')) {
			relative_selector.selectors.push({
				type: 'IdSelector',
				name: read_identifier(parser),
				start,
				end: parser.index
			});
		} else if (parser.eat('.')) {
			relative_selector.selectors.push({
				type: 'ClassSelector',
				name: read_identifier(parser),
				start,
				end: parser.index
			});
		} else if (parser.eat('::')) {
			relative_selector.selectors.push({
				type: 'PseudoElementSelector',
				name: read_identifier(parser),
				start,
				end: parser.index
			});
			// We read the inner selectors of a pseudo element to ensure it parses correctly,
			// but we don't do anything with the result.
			if (parser.eat('(')) {
				read_selector_list(parser, true);
				parser.eat(')', true);
			}
		} else if (parser.eat(':')) {
			const name = read_identifier(parser);
 
			/** @type {null | import('#compiler').Css.SelectorList} */
			let args = null;
 
			if (parser.eat('(')) {
				args = read_selector_list(parser, true);
				parser.eat(')', true);
			}
 
			relative_selector.selectors.push({
				type: 'PseudoClassSelector',
				name,
				args,
				start,
				end: parser.index
			});
		} else if (parser.eat('[')) {
			parser.allow_whitespace();
			const name = read_identifier(parser);
			parser.allow_whitespace();
 
			/** @type {string | null} */
			let value = null;
 
			const matcher = parser.read(REGEX_MATCHER);
 
			if (matcher) {
				parser.allow_whitespace();
				value = read_attribute_value(parser);
			}
 
			parser.allow_whitespace();
 
			const flags = parser.read(REGEX_ATTRIBUTE_FLAGS);
 
			parser.allow_whitespace();
			parser.eat(']', true);
 
			relative_selector.selectors.push({
				type: 'AttributeSelector',
				start,
				end: parser.index,
				name,
				matcher,
				value,
				flags
			});
		} else if (inside_pseudo_class && parser.match_regex(REGEX_NTH_OF)) {
			// nth of matcher must come before combinator matcher to prevent collision else the '+' in '+2n-1' would be parsed as a combinator
 
			relative_selector.selectors.push({
				type: 'Nth',
				value: /**@type {string} */ (parser.read(REGEX_NTH_OF)),
				start,
				end: parser.index
			});
		} else if (parser.match_regex(REGEX_PERCENTAGE)) {
			relative_selector.selectors.push({
				type: 'Percentage',
				value: /** @type {string} */ (parser.read(REGEX_PERCENTAGE)),
				start,
				end: parser.index
			});
		} else if (!parser.match_regex(REGEX_COMBINATOR)) {
			let name = read_identifier(parser);
 
			if (parser.eat('|')) {
				// we ignore the namespace when trying to find matching element classes
				name = read_identifier(parser);
			}
 
			relative_selector.selectors.push({
				type: 'TypeSelector',
				name,
				start,
				end: parser.index
			});
		}
 
		const index = parser.index;
		parser.allow_whitespace();
 
		if (parser.match(',') || (inside_pseudo_class ? parser.match(')') : parser.match('{'))) {
			// rewind, so we know whether to continue building the selector list
			parser.index = index;
 
			relative_selector.end = index;
			children.push(relative_selector);
 
			return {
				type: 'ComplexSelector',
				start: list_start,
				end: index,
				children,
				metadata: {
					rule: null,
					used: false
				}
			};
		}
 
		parser.index = index;
		const combinator = read_combinator(parser);
 
		if (combinator) {
			if (relative_selector.selectors.length === 0) {
				if (!inside_pseudo_class) {
					e.invalid_css_selector(start);
				}
			} else {
				relative_selector.end = index;
				children.push(relative_selector);
			}
 
			// ...and start a new one
			relative_selector = create_selector(combinator, combinator.start);
 
			parser.allow_whitespace();
 
			if (parser.match(',') || (inside_pseudo_class ? parser.match(')') : parser.match('{'))) {
				e.invalid_css_selector(parser.index);
			}
		}
	}

	e.unexpected_eof(parser.template.length);
}
 
/**
 * @param {import('../index.js').Parser} parser
 * @returns {import('#compiler').Css.Combinator | null}
 */
function read_combinator(parser) {
	const start = parser.index;
	parser.allow_whitespace();
 
	const index = parser.index;
	const name = parser.read(REGEX_COMBINATOR);
 
	if (name) {
		const end = parser.index;
		parser.allow_whitespace();
 
		return {
			type: 'Combinator',
			name,
			start: index,
			end
		};
	}
 
	if (parser.index !== start) {
		return {
			type: 'Combinator',
			name: ' ',
			start,
			end: parser.index
		};
	}
 
	return null;
}
 
/**
 * @param {import('../index.js').Parser} parser
 * @returns {import('#compiler').Css.Block}
 */
function read_block(parser) {
	const start = parser.index;
 
	parser.eat('{', true);
 
	/** @type {Array<import('#compiler').Css.Declaration | import('#compiler').Css.Rule | import('#compiler').Css.Atrule>} */
	const children = [];
 
	while (parser.index < parser.template.length) {
		allow_comment_or_whitespace(parser);
 
		if (parser.match('}')) {
			break;
		} else {
			children.push(read_block_item(parser));
		}
	}
 
	parser.eat('}', true);
 
	return {
		type: 'Block',
		start,
		end: parser.index,
		children
	};
}
 
/**
 * Reads a declaration, rule or at-rule
 *
 * @param {import('../index.js').Parser} parser
 * @returns {import('#compiler').Css.Declaration | import('#compiler').Css.Rule | import('#compiler').Css.Atrule}
 */
function read_block_item(parser) {
	if (parser.match('@')) {
		return read_at_rule(parser);
	}
 
	// read ahead to understand whether we're dealing with a declaration or a nested rule.
	// this involves some duplicated work, but avoids a try-catch that would disguise errors
	const start = parser.index;
	read_value(parser);
	const char = parser.template[parser.index];
	parser.index = start;
 
	return char === '{' ? read_rule(parser) : read_declaration(parser);
}
 
/**
 * @param {import('../index.js').Parser} parser
 * @returns {import('#compiler').Css.Declaration}
 */
function read_declaration(parser) {
	const start = parser.index;
 
	const property = parser.read_until(REGEX_WHITESPACE_OR_COLON);
	parser.allow_whitespace();
	parser.eat(':');
	parser.allow_whitespace();
 
	const value = read_value(parser);
 
	if (!value && !property.startsWith('--')) {
		e.invalid_css_declaration(parser.index);
	}
 
	const end = parser.index;
 
	if (!parser.match('}')) {
		parser.eat(';', true);
	}
 
	return {
		type: 'Declaration',
		start,
		end,
		property,
		value
	};
}
 
/**
 * @param {import('../index.js').Parser} parser
 * @returns {string}
 */
function read_value(parser) {
	let value = '';
	let escaped = false;
	let in_url = false;
 
	/** @type {null | '"' | "'"} */
	let quote_mark = null;
 
	while (parser.index < parser.template.length) {
		const char = parser.template[parser.index];
 
		if (escaped) {
			value += '\\' + char;
			escaped = false;
		} else if (char === '\\') {
			escaped = true;
		} else if (char === quote_mark) {
			quote_mark = null;
		} else if (char === ')') {
			in_url = false;
		} else if (quote_mark === null && (char === '"' || char === "'")) {
			quote_mark = char;
		} else if (char === '(' && value.slice(-3) === 'url') {
			in_url = true;
		} else if ((char === ';' || char === '{' || char === '}') && !in_url && !quote_mark) {
			return value.trim();
		}
 
		value += char;
 
		parser.index++;
	}
 
	e.unexpected_eof(parser.template.length);
}
 
/**
 * Read a property that may or may not be quoted, e.g.
 * `foo` or `'foo bar'` or `"foo bar"`
 * @param {import('../index.js').Parser} parser
 */
function read_attribute_value(parser) {
	let value = '';
	let escaped = false;
	const quote_mark = parser.eat('"') ? '"' : parser.eat("'") ? "'" : null;
 
	while (parser.index < parser.template.length) {
		const char = parser.template[parser.index];
		if (escaped) {
			value += '\\' + char;
			escaped = false;
		} else if (char === '\\') {
			escaped = true;
		} else if (quote_mark ? char === quote_mark : REGEX_CLOSING_BRACKET.test(char)) {
			if (quote_mark) {
				parser.eat(quote_mark, true);
			}
 
			return value.trim();
		} else {
			value += char;
		}
 
		parser.index++;
	}

	e.unexpected_eof(parser.template.length);
}
 
/**
 * https://www.w3.org/TR/CSS21/syndata.html#value-def-identifier
 * @param {import('../index.js').Parser} parser
 */
function read_identifier(parser) {
	const start = parser.index;
 
	let identifier = '';
 
	if (parser.match('--') || parser.match_regex(REGEX_LEADING_HYPHEN_OR_DIGIT)) {
		e.invalid_css_identifier(start);
	}
 
	let escaped = false;
 
	while (parser.index < parser.template.length) {
		const char = parser.template[parser.index];
		if (escaped) {
			identifier += '\\' + char;
			escaped = false;
		} else if (char === '\\') {
			escaped = true;
		} else if (
			/** @type {number} */ (char.codePointAt(0)) >= 160 ||
			REGEX_VALID_IDENTIFIER_CHAR.test(char)
		) {
			identifier += char;
		} else {
			break;
		}
 
		parser.index++;
	}
 
	if (identifier === '') {
		e.invalid_css_identifier(start);
	}
 
	return identifier;
}
 
/** @param {import('../index.js').Parser} parser */
function allow_comment_or_whitespace(parser) {
	parser.allow_whitespace();
	while (parser.match('/*') || parser.match('<!--')) {
		if (parser.eat('/*')) {
			parser.read_until(REGEX_COMMENT_CLOSE);
			parser.eat('*/', true);
		}
 
		if (parser.eat('<!--')) {
			parser.read_until(REGEX_HTML_COMMENT_CLOSE);
			parser.eat('-->', true);
		}
 
		parser.allow_whitespace();
	}
}