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 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 912x 912x 2x 2x 2x 2x 7265x 7265x 7265x 7265x 224x 238x 224x 224x 224x 140x 140x 140x 140x 140x 140x 140x 224x 7265x 7265x 7265x 7265x 7265x 7265x 7265x 7265x 7265x 1110x 1110x 1110x 7265x 7265x 7265x 7265x 7265x 2x 2x 2x 2x 2x 2x 7385x 7385x 7397x 7385x 7385x 7385x 7385x 2x 2x 2x 2x 2x 2x 2x 2x 10648x 10648x 10648x 10648x 10648x 10600x 10600x 10600x 10600x 10600x 10600x 10600x 10600x 10648x 8488x 8488x 2112x 10648x 992x 992x 992x 992x 992x 198x 198x 198x 198x 198x 198x 348x 348x 348x 187x 101x 101x 82x 82x 101x 101x 101x 187x 187x 187x 311x 311x 311x 161x 198x 198x 992x 992x 992x 794x 794x 794x 794x 794x 3066x 38x 38x 20x 20x 20x 3066x 616x 616x 616x 3066x 794x 794x 794x 176x 176x 794x 794x 992x 992x 992x 992x 1120x 1120x 1120x 1120x 10648x 1120x 1120x 1120x 1120x 1120x 2x 2x 2x 2x 2x 2x 2x 2948x 2948x 2948x 2948x 2x 2x 2x 2x 2x 2x 2x 2x 2x 253x 253x 52x 52x 201x 201x 201x 201x 201x 201x 201x 201x 201x 14x 14x 14x 201x 201x 14x 14x 14x 201x 201x 201x 195x 195x 201x 6x 6x 6x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 10600x 10600x 10658x 10594x 10594x 10594x 10594x 10658x 76x 6x 6x 70x 76x 48x 48x 48x 48x 22x 76x 4x 4x 4x 8x 2x 2x 2x 8x 4x 4x 2x 2x 4x 20x 20x 20x 10658x 10658x 4x 4x 10658x 10658x 90x 90x 90x 84x 84x 84x 84x 84x 84x 84x 90x 46x 46x 44x 44x 10658x 10658x 9100x 9100x 7723x 7723x 7723x 9100x 7718x 7718x 1382x 1382x 1382x 10658x 10658x 4x 2x 2x 2x 2x 2x 10658x 10658x 1212x 1212x 1212x 640x 1212x 606x 606x 606x 606x 606x 10658x 10658x 108x 108x 108x 108x 108x 112x 46x 46x 46x 112x 108x 108x 62x 62x 46x 46x 46x 10658x 10658x 2110x 2110x 2110x 2110x 2x 2x 2x 2x 2x 2x 2x 9291x 9291x 6x 6x 6x 9291x 9291x 26x 9291x 9245x 9291x 6x 9291x 4x 9291x 4x 9291x 6x 9291x 9291x 9291x 2x 2x 2x 2x 2x 2x 2x 2x 9188x 9188x 9055x 9055x 9045x 9055x 9055x 9011x 9055x 9055x 9003x 9003x 9029x 8921x 8921x 8895x 8895x 8921x 108x 108x 108x 108x 108x 9014x 265x 265x 265x 265x 229x 263x 140x 140x 140x 140x 140x 140x 140x 410x 152x 410x 258x 258x 140x 140x 140x 76x 76x 140x 140x 140x 140x 140x 292x 486x 292x 140x 140x 140x 152x 76x 76x 76x 76x 140x 140x 140x 140x 89x 95x 43x 95x 52x 52x 89x 261x 43x 43x 263x 265x 72x 72x 9010x 396x 396x 28x 195x 195x 195x 2x 2x 66x 66x 66x 66x 2x 2x 2x 2x 2x 176x 176x 176x 176x 176x 176x 176x 282x 176x 176x 176x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 7594x 7594x 7594x 7594x 7594x 7594x 7594x 1426x 1426x 7594x 44x 44x 44x 7594x 7594x 7594x 7594x 7594x 56x 56x 34x 56x 22x 22x 56x 7594x 7594x 7594x 2x 2x 2x 2x 2x 2x 1740x 1740x 1740x 1740x 1740x 1740x 1740x 6212x 1056x 1056x 1056x 1056x 1056x 1044x 1044x 1056x 320x 320x 6212x 1048x 1048x 1048x 38x 38x 5156x 4108x 4108x 4070x 4108x 54x 54x 54x 54x 6212x 1702x 1740x 1382x 1382x 1382x 1382x 1382x 1382x 2006x 1382x 946x 946x 946x 946x 946x 698x 698x 698x 946x 946x 322x 322x 946x 1382x 1702x 1702x 1702x 2x 2x 2x 2x 2x 2x 3280x 3280x 3280x 3280x 3280x 3280x 3096x 3096x 3096x 3096x 3096x 3096x 3096x 3096x 3096x 2710x 2710x 2710x 3096x 3096x 3280x 76x 76x 76x 76x 76x 76x 76x 76x 76x 44x 44x 44x 76x 76x 184x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 92x 92x 92x 92x 108x 108x 108x 108x 3280x 3280x 2x 2x 2x 2x 2x 4268x 4268x 4268x 2188x 988x 988x 2188x 466x 466x 2x 2x 2x 2x 2x 2x 2x 10894x 10894x 16618x 10894x 10894x 2x 2x 2x 2x 2x 2x 16618x 16618x 16618x 16618x 16618x 2x 2x 5784x 5784x 5204x 5204x 5784x 2x 2x 2x 2x 2x 3854x 3854x 3854x 3854x 13770x 13770x 3866x 3866x 802x 802x 13770x 9904x 9904x 8370x 9904x 1534x 1534x 1534x 34x 34x 1534x 13770x 3854x 3854x | import { walk } from 'zimmerframe'; import { get_possible_values } from './utils.js'; import { regex_ends_with_whitespace, regex_starts_with_whitespace } from '../../patterns.js'; import { error } from '../../../errors.js'; /** * @typedef {{ * stylesheet: import('#compiler').Css.StyleSheet; * element: import('#compiler').RegularElement | import('#compiler').SvelteElement; * }} State */ /** @typedef {NODE_PROBABLY_EXISTS | NODE_DEFINITELY_EXISTS} NodeExistsValue */ const NODE_PROBABLY_EXISTS = 0; const NODE_DEFINITELY_EXISTS = 1; const whitelist_attribute_selector = new Map([ ['details', ['open']], ['dialog', ['open']] ]); /** @type {import('#compiler').Css.Combinator} */ const descendant_combinator = { type: 'Combinator', name: ' ', start: -1, end: -1 }; /** @type {import('#compiler').Css.RelativeSelector} */ const nesting_selector = { type: 'RelativeSelector', start: -1, end: -1, combinator: null, selectors: [ { type: 'NestingSelector', name: '&', start: -1, end: -1 } ], metadata: { is_global: false, is_host: false, is_root: false, scoped: false } }; /** * * @param {import('#compiler').Css.StyleSheet} stylesheet * @param {import('#compiler').RegularElement | import('#compiler').SvelteElement} element */ export function prune(stylesheet, element) { walk(stylesheet, { stylesheet, element }, visitors); } /** @type {import('zimmerframe').Visitors<import('#compiler').Css.Node, State>} */ const visitors = { ComplexSelector(node, context) { const selectors = truncate(node); const inner = selectors[selectors.length - 1]; if (node.metadata.rule?.metadata.parent_rule) { const has_explicit_nesting_selector = selectors.some((selector) => selector.selectors.some((s) => s.type === 'NestingSelector') ); if (!has_explicit_nesting_selector) { selectors[0] = { ...selectors[0], combinator: descendant_combinator }; selectors.unshift(nesting_selector); } } if ( apply_selector( selectors, /** @type {import('#compiler').Css.Rule} */ (node.metadata.rule), context.state.element, context.state.stylesheet ) ) { mark(inner, context.state.element); node.metadata.used = true; } // note: we don't call context.next() here, we only recurse into // selectors that don't belong to rules (i.e. inside `:is(...)` etc) // when we encounter them below } }; /** * Discard trailing `:global(...)` selectors, these are unused for scoping purposes * @param {import('#compiler').Css.ComplexSelector} node */ function truncate(node) { const i = node.children.findLastIndex(({ metadata }) => { return !metadata.is_global && !metadata.is_host && !metadata.is_root; }); return node.children.slice(0, i + 1); } /** * @param {import('#compiler').Css.RelativeSelector[]} relative_selectors * @param {import('#compiler').Css.Rule} rule * @param {import('#compiler').RegularElement | import('#compiler').SvelteElement} element * @param {import('#compiler').Css.StyleSheet} stylesheet * @returns {boolean} */ function apply_selector(relative_selectors, rule, element, stylesheet) { const parent_selectors = relative_selectors.slice(); const relative_selector = parent_selectors.pop(); if (!relative_selector) return false; const possible_match = relative_selector_might_apply_to_node( relative_selector, rule, element, stylesheet ); if (!possible_match) { return false; } if (relative_selector.combinator) { const name = relative_selector.combinator.name; switch (name) { case ' ': case '>': { let parent = /** @type {import('#compiler').TemplateNode | null} */ (element.parent); let parent_matched = false; let crossed_component_boundary = false; while (parent) { if (parent.type === 'Component' || parent.type === 'SvelteComponent') { crossed_component_boundary = true; } if (parent.type === 'RegularElement' || parent.type === 'SvelteElement') { if (apply_selector(parent_selectors, rule, parent, stylesheet)) { // TODO the `name === ' '` causes false positives, but removing it causes false negatives... if (name === ' ' || crossed_component_boundary) { mark(parent_selectors[parent_selectors.length - 1], parent); } parent_matched = true; } if (name === '>') return parent_matched; } parent = /** @type {import('#compiler').TemplateNode | null} */ (parent.parent); } return parent_matched || parent_selectors.every((selector) => is_global(selector, rule)); } case '+': case '~': { const siblings = get_possible_element_siblings(element, name === '+'); let sibling_matched = false; for (const possible_sibling of siblings.keys()) { if (possible_sibling.type === 'RenderTag' || possible_sibling.type === 'SlotElement') { // `{@render foo()}<p>foo</p>` with `:global(.x) + p` is a match if (parent_selectors.length === 1 && parent_selectors[0].metadata.is_global) { mark(relative_selector, element); sibling_matched = true; } } else if (apply_selector(parent_selectors, rule, possible_sibling, stylesheet)) { mark(relative_selector, element); sibling_matched = true; } } return ( sibling_matched || (get_element_parent(element) === null && parent_selectors.every((selector) => is_global(selector, rule))) ); } default: // TODO other combinators return true; } } // if this is the left-most non-global selector, mark it — we want // `x y z {...}` to become `x.blah y z.blah {...}` const parent = parent_selectors[parent_selectors.length - 1]; if (!parent || is_global(parent, rule)) { mark(relative_selector, element); } return true; } /** * Mark both the compound selector and the node it selects as encapsulated, * for transformation in a later step * @param {import('#compiler').Css.RelativeSelector} relative_selector * @param {import('#compiler').RegularElement | import('#compiler').SvelteElement} element */ function mark(relative_selector, element) { relative_selector.metadata.scoped = true; element.metadata.scoped = true; } /** * Returns `true` if the relative selector is global, meaning * it's a `:global(...)` or `:host` or `:root` selector, or * is an `:is(...)` or `:where(...)` selector that contains * a global selector * @param {import('#compiler').Css.RelativeSelector} selector * @param {import('#compiler').Css.Rule} rule */ function is_global(selector, rule) { if (selector.metadata.is_global || selector.metadata.is_host || selector.metadata.is_root) { return true; } for (const s of selector.selectors) { /** @type {import('#compiler').Css.SelectorList | null} */ let selector_list = null; let owner = rule; if (s.type === 'PseudoClassSelector') { if ((s.name === 'is' || s.name === 'where') && s.args) { selector_list = s.args; } } if (s.type === 'NestingSelector') { owner = /** @type {import('#compiler').Css.Rule} */ (rule.metadata.parent_rule); selector_list = owner.prelude; } const has_global_selectors = selector_list?.children.some((complex_selector) => { return complex_selector.children.every((relative_selector) => is_global(relative_selector, owner) ); }); if (!has_global_selectors) { return false; } } return true; } const regex_backslash_and_following_character = /\\(.)/g; /** * Ensure that `element` satisfies each simple selector in `relative_selector` * * @param {import('#compiler').Css.RelativeSelector} relative_selector * @param {import('#compiler').Css.Rule} rule * @param {import('#compiler').RegularElement | import('#compiler').SvelteElement} element * @param {import('#compiler').Css.StyleSheet} stylesheet * @returns {boolean} */ function relative_selector_might_apply_to_node(relative_selector, rule, element, stylesheet) { for (const selector of relative_selector.selectors) { if (selector.type === 'Percentage' || selector.type === 'Nth') continue; const name = selector.name.replace(regex_backslash_and_following_character, '$1'); switch (selector.type) { case 'PseudoClassSelector': { if (name === 'host' || name === 'root') { return false; } if (name === 'global' && relative_selector.selectors.length === 1) { const args = /** @type {import('#compiler').Css.SelectorList} */ (selector.args); const complex_selector = args.children[0]; return apply_selector(complex_selector.children, rule, element, stylesheet); } if ((name === 'is' || name === 'where') && selector.args) { let matched = false; for (const complex_selector of selector.args.children) { if (apply_selector(truncate(complex_selector), rule, element, stylesheet)) { complex_selector.metadata.used = true; matched = true; } } if (!matched) { return false; } } break; } case 'PseudoElementSelector': { break; } case 'AttributeSelector': { const whitelisted = whitelist_attribute_selector.get(element.name.toLowerCase()); if ( !whitelisted?.includes(selector.name.toLowerCase()) && !attribute_matches( element, selector.name, selector.value && unquote(selector.value), selector.matcher, selector.flags?.includes('i') ?? false ) ) { return false; } break; } case 'ClassSelector': { if ( !attribute_matches(element, 'class', name, '~=', false) && !element.attributes.some( (attribute) => attribute.type === 'ClassDirective' && attribute.name === name ) ) { return false; } break; } case 'IdSelector': { if (!attribute_matches(element, 'id', name, '=', false)) { return false; } break; } case 'TypeSelector': { if ( element.name.toLowerCase() !== name.toLowerCase() && name !== '*' && element.type !== 'SvelteElement' ) { return false; } break; } case 'NestingSelector': { let matched = false; const parent = /** @type {import('#compiler').Css.Rule} */ (rule.metadata.parent_rule); for (const complex_selector of parent.prelude.children) { if (apply_selector(truncate(complex_selector), parent, element, stylesheet)) { complex_selector.metadata.used = true; matched = true; } } if (!matched) { return false; } break; } } } // possible match return true; } /** * @param {any} operator * @param {any} expected_value * @param {any} case_insensitive * @param {any} value */ function test_attribute(operator, expected_value, case_insensitive, value) { if (case_insensitive) { expected_value = expected_value.toLowerCase(); value = value.toLowerCase(); } switch (operator) { case '=': return value === expected_value; case '~=': return value.split(/\s/).includes(expected_value); case '|=': return `${value}-`.startsWith(`${expected_value}-`); case '^=': return value.startsWith(expected_value); case '$=': return value.endsWith(expected_value); case '*=': return value.includes(expected_value); default: throw new Error("this shouldn't happen"); } } /** * @param {import('#compiler').RegularElement | import('#compiler').SvelteElement} node * @param {string} name * @param {string | null} expected_value * @param {string | null} operator * @param {boolean} case_insensitive */ function attribute_matches(node, name, expected_value, operator, case_insensitive) { for (const attribute of node.attributes) { if (attribute.type === 'SpreadAttribute') return true; if (attribute.type === 'BindDirective' && attribute.name === name) return true; if (attribute.type !== 'Attribute') continue; if (attribute.name.toLowerCase() !== name.toLowerCase()) continue; if (attribute.value === true) return operator === null; if (expected_value === null) return true; const chunks = attribute.value; if (chunks.length === 1) { const value = chunks[0]; if (value.type === 'Text') { return test_attribute(operator, expected_value, case_insensitive, value.data); } } const possible_values = new Set(); /** @type {string[]} */ let prev_values = []; for (const chunk of chunks) { const current_possible_values = get_possible_values(chunk); // impossible to find out all combinations if (!current_possible_values) return true; if (prev_values.length > 0) { /** @type {string[]} */ const start_with_space = []; /** @type {string[]} */ const remaining = []; current_possible_values.forEach((current_possible_value) => { if (regex_starts_with_whitespace.test(current_possible_value)) { start_with_space.push(current_possible_value); } else { remaining.push(current_possible_value); } }); if (remaining.length > 0) { if (start_with_space.length > 0) { prev_values.forEach((prev_value) => possible_values.add(prev_value)); } /** @type {string[]} */ const combined = []; prev_values.forEach((prev_value) => { remaining.forEach((value) => { combined.push(prev_value + value); }); }); prev_values = combined; start_with_space.forEach((value) => { if (regex_ends_with_whitespace.test(value)) { possible_values.add(value); } else { prev_values.push(value); } }); continue; } else { prev_values.forEach((prev_value) => possible_values.add(prev_value)); prev_values = []; } } current_possible_values.forEach((current_possible_value) => { if (regex_ends_with_whitespace.test(current_possible_value)) { possible_values.add(current_possible_value); } else { prev_values.push(current_possible_value); } }); if (prev_values.length < current_possible_values.size) { prev_values.push(' '); } if (prev_values.length > 20) { // might grow exponentially, bail out return true; } } prev_values.forEach((prev_value) => possible_values.add(prev_value)); for (const value of possible_values) { if (test_attribute(operator, expected_value, case_insensitive, value)) return true; } } return false; } /** @param {string} str */ function unquote(str) { if ((str[0] === str[str.length - 1] && str[0] === "'") || str[0] === '"') { return str.slice(1, str.length - 1); } return str; } /** * @param {import('#compiler').RegularElement | import('#compiler').SvelteElement} node * @returns {import('#compiler').RegularElement | import('#compiler').SvelteElement | null} */ function get_element_parent(node) { /** @type {import('#compiler').SvelteNode | null} */ let parent = node; while ( // @ts-expect-error TODO figure out a more elegant solution (parent = parent.parent) && parent.type !== 'RegularElement' && parent.type !== 'SvelteElement' ); return parent ?? null; } /** * Finds the given node's previous sibling in the DOM * * The Svelte `<slot>` is just a placeholder and is not actually real. Any children nodes * in `<slot>` are 'flattened' and considered as the same level as the `<slot>`'s siblings * * e.g. * ```html * <h1>Heading 1</h1> * <slot> * <h2>Heading 2</h2> * </slot> * ``` * * is considered to look like: * ```html * <h1>Heading 1</h1> * <h2>Heading 2</h2> * ``` * @param {import('#compiler').SvelteNode} node * @returns {import('#compiler').SvelteNode} */ function find_previous_sibling(node) { /** @type {import('#compiler').SvelteNode} */ let current_node = node; while ( // @ts-expect-error TODO !current_node.prev && // @ts-expect-error TODO current_node.parent?.type === 'SlotElement' ) { // @ts-expect-error TODO current_node = current_node.parent; } // @ts-expect-error current_node = current_node.prev; while (current_node?.type === 'SlotElement') { const slot_children = current_node.fragment.nodes; if (slot_children.length > 0) { current_node = slot_children.slice(-1)[0]; } else { break; } } return current_node; } /** * @param {import('#compiler').SvelteNode} node * @param {boolean} adjacent_only * @returns {Map<import('#compiler').RegularElement | import('#compiler').SvelteElement | import('#compiler').SlotElement | import('#compiler').RenderTag, NodeExistsValue>} */ function get_possible_element_siblings(node, adjacent_only) { /** @type {Map<import('#compiler').RegularElement | import('#compiler').SvelteElement | import('#compiler').SlotElement | import('#compiler').RenderTag, NodeExistsValue>} */ const result = new Map(); /** @type {import('#compiler').SvelteNode} */ let prev = node; while ((prev = find_previous_sibling(prev))) { if (prev.type === 'RegularElement') { if ( !prev.attributes.find( (attr) => attr.type === 'Attribute' && attr.name.toLowerCase() === 'slot' ) ) { result.set(prev, NODE_DEFINITELY_EXISTS); } if (adjacent_only) { break; } } else if (prev.type === 'EachBlock' || prev.type === 'IfBlock' || prev.type === 'AwaitBlock') { const possible_last_child = get_possible_last_child(prev, adjacent_only); add_to_map(possible_last_child, result); if (adjacent_only && has_definite_elements(possible_last_child)) { return result; } } else if ( prev.type === 'SlotElement' || prev.type === 'RenderTag' || prev.type === 'SvelteElement' ) { result.set(prev, NODE_PROBABLY_EXISTS); // Special case: slots, render tags and svelte:element tags could resolve to no siblings, // so we want to continue until we find a definite sibling even with the adjacent-only combinator } } if (!prev || !adjacent_only) { /** @type {import('#compiler').SvelteNode | null} */ let parent = node; while ( // @ts-expect-error TODO (parent = parent?.parent) && (parent.type === 'EachBlock' || parent.type === 'IfBlock' || parent.type === 'AwaitBlock') ) { const possible_siblings = get_possible_element_siblings(parent, adjacent_only); add_to_map(possible_siblings, result); // @ts-expect-error if (parent.type === 'EachBlock' && !parent.fallback?.nodes.includes(node)) { // `{#each ...}<a /><b />{/each}` — `<b>` can be previous sibling of `<a />` add_to_map(get_possible_last_child(parent, adjacent_only), result); } if (adjacent_only && has_definite_elements(possible_siblings)) { break; } } } return result; } /** * @param {import('#compiler').EachBlock | import('#compiler').IfBlock | import('#compiler').AwaitBlock} relative_selector * @param {boolean} adjacent_only * @returns {Map<import('#compiler').RegularElement, NodeExistsValue>} */ function get_possible_last_child(relative_selector, adjacent_only) { /** @typedef {Map<import('#compiler').RegularElement, NodeExistsValue>} NodeMap */ /** @type {NodeMap} */ const result = new Map(); if (relative_selector.type === 'EachBlock') { /** @type {NodeMap} */ const each_result = loop_child(relative_selector.body.nodes, adjacent_only); /** @type {NodeMap} */ const else_result = relative_selector.fallback ? loop_child(relative_selector.fallback.nodes, adjacent_only) : new Map(); const not_exhaustive = !has_definite_elements(else_result); if (not_exhaustive) { mark_as_probably(each_result); mark_as_probably(else_result); } add_to_map(each_result, result); add_to_map(else_result, result); } else if (relative_selector.type === 'IfBlock') { /** @type {NodeMap} */ const if_result = loop_child(relative_selector.consequent.nodes, adjacent_only); /** @type {NodeMap} */ const else_result = relative_selector.alternate ? loop_child(relative_selector.alternate.nodes, adjacent_only) : new Map(); const not_exhaustive = !has_definite_elements(if_result) || !has_definite_elements(else_result); if (not_exhaustive) { mark_as_probably(if_result); mark_as_probably(else_result); } add_to_map(if_result, result); add_to_map(else_result, result); } else if (relative_selector.type === 'AwaitBlock') { /** @type {NodeMap} */ const pending_result = relative_selector.pending ? loop_child(relative_selector.pending.nodes, adjacent_only) : new Map(); /** @type {NodeMap} */ const then_result = relative_selector.then ? loop_child(relative_selector.then.nodes, adjacent_only) : new Map(); /** @type {NodeMap} */ const catch_result = relative_selector.catch ? loop_child(relative_selector.catch.nodes, adjacent_only) : new Map(); const not_exhaustive = !has_definite_elements(pending_result) || !has_definite_elements(then_result) || !has_definite_elements(catch_result); if (not_exhaustive) { mark_as_probably(pending_result); mark_as_probably(then_result); mark_as_probably(catch_result); } add_to_map(pending_result, result); add_to_map(then_result, result); add_to_map(catch_result, result); } return result; } /** * @param {Map<unknown, NodeExistsValue>} result * @returns {boolean} */ function has_definite_elements(result) { if (result.size === 0) return false; for (const exist of result.values()) { if (exist === NODE_DEFINITELY_EXISTS) { return true; } } return false; } /** * @template T * @param {Map<T, NodeExistsValue>} from * @param {Map<T, NodeExistsValue>} to * @returns {void} */ function add_to_map(from, to) { from.forEach((exist, element) => { to.set(element, higher_existence(exist, to.get(element))); }); } /** * @param {NodeExistsValue | undefined} exist1 * @param {NodeExistsValue | undefined} exist2 * @returns {NodeExistsValue} */ function higher_existence(exist1, exist2) { // @ts-expect-error TODO figure out if this is a bug if (exist1 === undefined || exist2 === undefined) return exist1 || exist2; return exist1 > exist2 ? exist1 : exist2; } /** @param {Map<import('#compiler').RegularElement, NodeExistsValue>} result */ function mark_as_probably(result) { for (const key of result.keys()) { result.set(key, NODE_PROBABLY_EXISTS); } } /** * @param {import('#compiler').SvelteNode[]} children * @param {boolean} adjacent_only */ function loop_child(children, adjacent_only) { /** @type {Map<import('#compiler').RegularElement, NodeExistsValue>} */ const result = new Map(); for (let i = children.length - 1; i >= 0; i--) { const child = children[i]; if (child.type === 'RegularElement') { result.set(child, NODE_DEFINITELY_EXISTS); if (adjacent_only) { break; } } else if ( child.type === 'EachBlock' || child.type === 'IfBlock' || child.type === 'AwaitBlock' ) { const child_result = get_possible_last_child(child, adjacent_only); add_to_map(child_result, result); if (adjacent_only && has_definite_elements(child_result)) { break; } } } return result; } |