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

97.3% Statements 613/630
94.36% Branches 134/142
100% Functions 15/15
97.29% Lines 612/629

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 628 629 6302x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 428x 428x 428x 428x 428x 428x 428x 428x 428x 428x 428x 428x 428x 428x 428x 428x 428x 428x 428x 428x 2x 2x 2x 2x 2x 2x 428x 428x 428x 428x 428x 1803x 1803x 1803x 419x 419x 1384x 1803x 100x 1802x 1284x 1284x 1803x 1x 1x 1x 2x 2x 2x 2x 2x 114x 114x 114x 114x 114x 114x 114x 114x 114x 114x 114x 114x 89x 89x 113x 25x 25x 25x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 2x 2x 2x 2x 2x 1455x 1455x 1455x 1455x 1455x 1455x 1455x 1455x 1455x 1455x 1455x 1455x 1455x 1455x 1455x 1455x 2x 2x 2x 2x 2x 2x 1638x 1638x 1638x 1638x 1638x 1638x 1638x 1638x 1638x 1675x 1675x 1675x 1675x 1675x 1675x 1675x 1632x 1632x 1632x 1632x 1632x 1632x 1675x 37x 37x 37x 1675x       2x 2x 2x 2x 2x 2x 1675x 1675x 1675x 1675x 1675x 1675x 1675x 1675x 1675x 1675x 1675x 1675x 2645x 2645x 2645x 2645x 2645x 2645x 2645x 2645x 2645x 2645x 2645x 2645x 2645x 1675x 1675x 1675x 1675x 1675x 2774x 2774x 2774x 37x 37x 37x 37x 37x 37x 2774x 32x 32x 32x 2x 2x 2x 32x 32x 32x 32x 32x 32x 32x 2737x 4x 4x 4x 4x 4x 4x 2705x 1628x 1628x 1628x 1628x 1628x 1628x 2701x 31x 31x 31x 31x 31x 31x 31x 31x 31x 7x 7x 7x 1073x 258x 258x 258x 258x 258x 258x 176x 176x 176x 258x 258x 258x 258x 258x 258x 258x 258x 1042x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 36x 36x 36x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 784x 11x 11x 11x 11x 11x 11x 11x 11x 731x 40x 40x 40x 40x 40x 40x 720x 676x 676x 676x 2x 2x 2x 674x 674x 674x 674x 674x 674x 674x 674x 2772x 2772x 2772x 2772x 2774x 1669x 1669x 1669x 1669x 1669x 1669x 1669x 1669x 1669x 1669x 1669x 1669x 1669x 1669x 1669x 1669x 1669x 1103x 1103x 1103x 1103x 2645x 973x 4x 3x 3x 973x 969x 969x 969x 970x 970x 970x 970x 970x 970x 973x 1x 1x 973x 2774x       2x 2x 2x 2x 2x 1103x 1103x 1103x 1103x 1103x 1103x 1103x 1103x 825x 825x 825x 825x 825x 825x 825x 825x 825x 825x 278x 1102x 148x 148x 148x 148x 148x 148x 148x 130x 130x 130x 2x 2x 2x 2x 2x 1538x 1538x 1538x 1538x 1538x 1538x 1538x 1538x 1538x 3183x 3183x 3183x 1535x 3133x 1648x 1648x 3183x 1535x 1535x 1535x 1535x 1535x 1535x 1535x 1535x 1535x 1535x 2x 2x 2x 2x 2x 2x 2x 1648x 1648x 14x 14x 1634x 1634x 1634x 1634x 1634x 1634x 1634x 1634x 1648x 1648x 2x 2x 2x 2x 2x 1462x 1462x 1462x 1462x 1462x 1462x 1462x 1462x 1462x 1462x 1462x 1462x 1x 1x 1461x 1461x 1461x 1461x 1456x 1456x 1461x 1461x 1461x 1461x 1461x 1461x 1461x 1461x 1461x 2x 2x 2x 2x 2x 3210x 3210x 3210x 3210x 3210x 3210x 3210x 3210x 3210x 32987x 32987x 32987x     32987x   32987x 53x 32987x 194x 32934x 54x 32740x 20x 32686x 3209x 3209x 29778x 29778x 29778x 29778x 29778x 1x 1x 1x 2x 2x 2x 2x 2x 2x 36x 36x 36x 36x 36x 36x 150x 150x     150x   150x 36x 34x 34x 36x 36x 150x 114x 114x 114x 114x 114x       2x 2x 2x 2x 2x 2768x 2768x 2768x 2768x 2768x 2768x     2768x 2768x 2768x 2768x 10928x 10928x 7x 7x 10928x 7x 10921x 10914x 10914x 10914x 8146x 10914x 2768x 2768x 8160x 8160x 8160x 2768x 2768x 2x 2x 2766x 2766x 2766x 2x 2x 11102x 11102x 11102x 157x 155x 155x 155x 157x 157x 2x 2x 2x 157x 157x 157x 11102x  
/** @import { AST, Css, Directive } from '#compiler' */
/** @import { Parser } from '../index.js' */
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 {Parser} parser
 * @param {number} start
 * @param {Array<AST.Attribute | AST.SpreadAttribute | Directive>} attributes
 * @returns {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 {Parser} parser
 * @param {string} close
 * @returns {any[]}
 */
function read_body(parser, close) {
	/** @type {Array<Css.Rule | 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 {Parser} parser
 * @returns {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 {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 {Parser} parser
 * @returns {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 {Parser} parser
 * @param {boolean} [inside_pseudo_class]
 * @returns {Css.SelectorList}
 */
function read_selector_list(parser, inside_pseudo_class = false) {
	/** @type {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;
 
		allow_comment_or_whitespace(parser);
 
		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 {Parser} parser
 * @param {boolean} [inside_pseudo_class]
 * @returns {Css.ComplexSelector}
 */
function read_selector(parser, inside_pseudo_class = false) {
	const list_start = parser.index;
 
	/** @type {Css.RelativeSelector[]} */
	const children = [];
 
	/**
	 * @param {Css.Combinator | null} combinator
	 * @param {number} start
	 * @returns {Css.RelativeSelector}
	 */
	function create_selector(combinator, start) {
		return {
			type: 'RelativeSelector',
			combinator,
			selectors: [],
			start,
			end: -1,
			metadata: {
				is_global: false,
				is_global_like: false,
				scoped: false
			}
		};
	}
 
	/** @type {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 | 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;
		allow_comment_or_whitespace(parser);
 
		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.css_selector_invalid(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.css_selector_invalid(parser.index);
			}
		}
	}

	e.unexpected_eof(parser.template.length);
}
 
/**
 * @param {Parser} parser
 * @returns {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 {Parser} parser
 * @returns {Css.Block}
 */
function read_block(parser) {
	const start = parser.index;
 
	parser.eat('{', true);
 
	/** @type {Array<Css.Declaration | Css.Rule | 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 {Parser} parser
 * @returns {Css.Declaration | Css.Rule | 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 {Parser} parser
 * @returns {Css.Declaration}
 */
function read_declaration(parser) {
	const start = parser.index;
 
	const property = parser.read_until(REGEX_WHITESPACE_OR_COLON);
	parser.allow_whitespace();
	parser.eat(':');
	let index = parser.index;
	parser.allow_whitespace();
 
	const value = read_value(parser);
 
	if (!value && !property.startsWith('--')) {
		e.css_empty_declaration({ start, end: index });
	}
 
	const end = parser.index;
 
	if (!parser.match('}')) {
		parser.eat(';', true);
	}
 
	return {
		type: 'Declaration',
		start,
		end,
		property,
		value
	};
}
 
/**
 * @param {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 {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 {Parser} parser
 */
function read_identifier(parser) {
	const start = parser.index;
 
	let identifier = '';
 
	if (parser.match('--') || parser.match_regex(REGEX_LEADING_HYPHEN_OR_DIGIT)) {
		e.css_expected_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.css_expected_identifier(start);
	}
 
	return identifier;
}
 
/** @param {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();
	}
}