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 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 | 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 2x 2x 986x 6x 6x 6x 6x 986x 980x 980x 986x 2x 2x 2x 2x 7750x 34x 7750x 7716x 7716x 2x 2x 7786x 7786x 7786x 7786x 302x 302x 302x 302x 316x 316x 316x 316x 316x 316x 130x 130x 316x 316x 316x 316x 316x 302x 302x 186x 186x 186x 186x 186x 186x 186x 302x 7786x 7786x 42x 42x 42x 42x 42x 42x 42x 54x 54x 54x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 8x 8x 8x 8x 98x 46x 46x 46x 7786x 7744x 7744x 7744x 7744x 7744x 7744x 7744x 7744x 1275x 1275x 1275x 7786x 7786x 7786x 7786x 7786x 2x 2x 2x 2x 2x 2x 8232x 8232x 8298x 8298x 8298x 8298x 8298x 8246x 8246x 162x 162x 182x 182x 182x 182x 182x 182x 8246x 8298x 8232x 8232x 8232x 8232x 2x 2x 2x 2x 2x 2x 2x 2x 2x 11667x 11667x 11667x 11667x 11667x 11545x 11545x 11545x 11545x 11545x 11545x 11545x 11545x 11545x 11667x 9050x 9050x 2495x 11667x 1084x 1084x 1084x 1084x 1084x 1084x 1084x 1084x 1084x 1084x 1411x 1411x 1411x 1411x 11667x 1411x 1411x 1411x 1411x 1411x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1158x 1158x 1158x 1158x 1158x 1158x 1158x 1158x 1158x 1158x 1158x 1158x 1158x 1158x 362x 362x 362x 362x 362x 362x 664x 664x 664x 12x 12x 12x 12x 12x 652x 664x 341x 195x 195x 174x 174x 195x 195x 195x 341x 341x 341x 611x 611x 611x 309x 362x 362x 1158x 1158x 1158x 796x 796x 796x 796x 796x 3070x 38x 38x 20x 20x 20x 3070x 3032x 3032x 618x 618x 618x 3070x 796x 796x 796x 176x 176x 796x 796x 1158x 1158x 1158x 1158x 2x 2x 2x 2x 2x 2x 2x 3506x 3506x 3506x 3506x 2x 2x 2x 2x 2x 2x 2x 2x 2x 301x 301x 56x 56x 245x 245x 245x 245x 245x 245x 245x 245x 245x 14x 14x 14x 245x 245x 14x 14x 14x 245x 245x 245x 239x 239x 245x 6x 6x 6x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 11545x 11545x 11545x 11545x 11545x 11545x 11545x 11545x 11545x 11545x 11545x 11545x 11937x 260x 11937x 11677x 11677x 11937x 11545x 11545x 11545x 11545x 162x 162x 162x 162x 166x 166x 166x 166x 166x 178x 178x 178x 178x 178x 146x 146x 146x 146x 146x 178x 178x 178x 146x 178x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 56x 56x 56x 74x 178x 166x 166x 112x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 112x 112x 112x 166x 50x 50x 50x 11383x 11545x 11469x 11405x 11405x 11405x 11405x 11469x 208x 6x 6x 202x 202x 208x 208x 84x 208x 62x 62x 62x 62x 140x 140x 208x 140x 208x 104x 104x 104x 110x 110x 110x 86x 110x 46x 46x 110x 8x 8x 8x 8x 8x 8x 16x 16x 8x 110x 104x 104x 50x 50x 6x 50x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 50x 50x 50x 104x 90x 90x 90x 11469x 11469x 4x 4x 11469x 11469x 90x 90x 90x 84x 84x 84x 84x 84x 84x 84x 90x 46x 46x 44x 44x 11469x 11469x 9297x 9297x 7877x 7877x 7877x 9297x 7872x 7872x 1425x 1425x 1425x 11469x 11469x 4x 2x 2x 2x 2x 2x 11469x 11469x 1648x 1648x 1648x 852x 1648x 818x 818x 830x 830x 830x 11469x 11469x 154x 154x 154x 154x 154x 158x 70x 70x 70x 158x 154x 154x 84x 84x 70x 70x 70x 11469x 11469x 2443x 2443x 2443x 2443x 2x 2x 2x 2x 2x 2x 2x 9380x 9380x 6x 6x 6x 9380x 9380x 26x 9380x 9334x 9380x 6x 9380x 4x 9380x 4x 9380x 6x 9380x 9380x 9380x 2x 2x 2x 2x 2x 2x 2x 2x 9385x 9385x 9144x 9144x 9134x 9144x 9144x 9100x 9144x 9144x 9092x 9140x 8984x 8984x 108x 108x 108x 108x 108x 108x 9099x 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 9090x 396x 396x 28x 303x 303x 303x 2x 2x 66x 66x 66x 66x 2x 2x 2x 2x 2x 228x 228x 228x 228x 228x 228x 228x 316x 228x 228x 228x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 7607x 7607x 7607x 7607x 7607x 7607x 7607x 1429x 1429x 7607x 46x 46x 46x 7607x 7607x 7607x 7607x 7607x 56x 56x 34x 56x 22x 22x 56x 7607x 7607x 7607x 2x 2x 2x 2x 2x 2x 1742x 1742x 1742x 1742x 1742x 1742x 1742x 6224x 1060x 1060x 1060x 1060x 1060x 1048x 1048x 1060x 321x 321x 6224x 1048x 1048x 1048x 38x 38x 5164x 4116x 4116x 4078x 4116x 54x 54x 54x 54x 6224x 1704x 1742x 1383x 1383x 1383x 1383x 1383x 1383x 2007x 1383x 946x 946x 946x 946x 946x 698x 698x 698x 946x 946x 322x 322x 946x 1383x 1704x 1704x 1704x 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 { Visitors } from 'zimmerframe' */ /** @import * as Compiler from '#compiler' */ import { walk } from 'zimmerframe'; import { get_possible_values } from './utils.js'; import { regex_ends_with_whitespace, regex_starts_with_whitespace } from '../../patterns.js'; import { get_attribute_chunks, is_text_attribute } from '../../../utils/ast.js'; /** * @typedef {{ * stylesheet: Compiler.Css.StyleSheet; * element: Compiler.AST.RegularElement | Compiler.AST.SvelteElement; * from_render_tag: boolean; * }} 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 {Compiler.Css.Combinator} */ const descendant_combinator = { type: 'Combinator', name: ' ', start: -1, end: -1 }; /** @type {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_global_like: false, scoped: false } }; /** * * @param {Compiler.Css.StyleSheet} stylesheet * @param {Compiler.AST.RegularElement | Compiler.AST.SvelteElement | Compiler.AST.RenderTag} element */ export function prune(stylesheet, element) { if (element.type === 'RenderTag') { const parent = get_element_parent(element); if (!parent) return; walk(stylesheet, { stylesheet, element: parent, from_render_tag: true }, visitors); } else { walk(stylesheet, { stylesheet, element, from_render_tag: false }, visitors); } } /** @type {Visitors<Compiler.Css.Node, State>} */ const visitors = { Rule(node, context) { if (node.metadata.is_global_block) { context.visit(node.prelude); } else { context.next(); } }, ComplexSelector(node, context) { const selectors = truncate(node); const inner = selectors[selectors.length - 1]; if (node.metadata.rule?.metadata.parent_rule && selectors.length > 0) { let has_explicit_nesting_selector = false; // nesting could be inside pseudo classes like :is, :has or :where for (let selector of selectors) { walk( selector, {}, { // @ts-ignore NestingSelector() { has_explicit_nesting_selector = true; } } ); // if we found one we can break from the others if (has_explicit_nesting_selector) break; } if (!has_explicit_nesting_selector) { selectors[0] = { ...selectors[0], combinator: descendant_combinator }; selectors.unshift(nesting_selector); } } if (context.state.from_render_tag) { // We're searching for a match that crosses a render tag boundary. That means we have to both traverse up // the element tree (to see if we find an entry point) but also remove selectors from the end (assuming // they are part of the render tag we don't see). We do all possible combinations of both until we find a match. /** @type {Compiler.AST.RegularElement | Compiler.AST.SvelteElement | null} */ let element = context.state.element; while (element) { const selectors_to_check = selectors.slice(); while (selectors_to_check.length > 0) { selectors_to_check.pop(); if ( apply_selector( selectors_to_check, /** @type {Compiler.Css.Rule} */ (node.metadata.rule), element, context.state.stylesheet, true ) ) { mark(inner, element); node.metadata.used = true; return; } } element = get_element_parent(element); } } else if ( apply_selector( selectors, /** @type {Compiler.Css.Rule} */ (node.metadata.rule), context.state.element, context.state.stylesheet, true ) ) { 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 without a `:has/is/where/not(...)` modifier, these are unused for scoping purposes * @param {Compiler.Css.ComplexSelector} node */ function truncate(node) { const i = node.children.findLastIndex(({ metadata, selectors }) => { const first = selectors[0]; return ( // not after a :global selector !metadata.is_global_like && !(first.type === 'PseudoClassSelector' && first.name === 'global' && first.args === null) && // not a :global(...) without a :has/is/where/not(...) modifier (!metadata.is_global || selectors.some( (selector) => selector.type === 'PseudoClassSelector' && selector.args !== null && (selector.name === 'has' || selector.name === 'not' || selector.name === 'is' || selector.name === 'where') )) ); }); return node.children.slice(0, i + 1); } /** * @param {Compiler.Css.RelativeSelector[]} relative_selectors * @param {Compiler.Css.Rule} rule * @param {Compiler.AST.RegularElement | Compiler.AST.SvelteElement} element * @param {Compiler.Css.StyleSheet} stylesheet * @param {boolean} check_has Whether or not to check the `:has(...)` selectors * @returns {boolean} */ function apply_selector(relative_selectors, rule, element, stylesheet, check_has) { 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, check_has ); if (!possible_match) { return false; } if (relative_selector.combinator) { return apply_combinator( relative_selector.combinator, relative_selector, parent_selectors, rule, element, stylesheet, check_has ); } // 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; } /** * @param {Compiler.Css.Combinator} combinator * @param {Compiler.Css.RelativeSelector} relative_selector * @param {Compiler.Css.RelativeSelector[]} parent_selectors * @param {Compiler.Css.Rule} rule * @param {Compiler.AST.RegularElement | Compiler.AST.SvelteElement} element * @param {Compiler.Css.StyleSheet} stylesheet * @param {boolean} check_has Whether or not to check the `:has(...)` selectors * @returns {boolean} */ function apply_combinator( combinator, relative_selector, parent_selectors, rule, element, stylesheet, check_has ) { const name = combinator.name; switch (name) { case ' ': case '>': { let parent = /** @type {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 === 'SnippetBlock') { // We assume the snippet might be rendered in a place where the parent selectors match. // (We could do more static analysis and check the render tag reference to see if this snippet block continues // with elements that actually match the selector, but that would be a lot of work for little gain) return true; } if (parent.type === 'RegularElement' || parent.type === 'SvelteElement') { if (apply_selector(parent_selectors, rule, parent, stylesheet, check_has)) { // 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 {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, check_has) ) { 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; } } /** * Mark both the compound selector and the node it selects as encapsulated, * for transformation in a later step * @param {Compiler.Css.RelativeSelector} relative_selector * @param {Compiler.AST.RegularElement | Compiler.AST.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 unscopeable selector, or * is an `:is(...)` or `:where(...)` selector that contains * a global selector * @param {Compiler.Css.RelativeSelector} selector * @param {Compiler.Css.Rule} rule */ function is_global(selector, rule) { if (selector.metadata.is_global || selector.metadata.is_global_like) { return true; } for (const s of selector.selectors) { /** @type {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 {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 {Compiler.Css.RelativeSelector} relative_selector * @param {Compiler.Css.Rule} rule * @param {Compiler.AST.RegularElement | Compiler.AST.SvelteElement} element * @param {Compiler.Css.StyleSheet} stylesheet * @param {boolean} check_has Whether or not to check the `:has(...)` selectors * @returns {boolean} */ function relative_selector_might_apply_to_node( relative_selector, rule, element, stylesheet, check_has ) { // Sort :has(...) selectors in one bucket and everything else into another const has_selectors = []; const other_selectors = []; for (const selector of relative_selector.selectors) { if (selector.type === 'PseudoClassSelector' && selector.name === 'has' && selector.args) { has_selectors.push(selector); } else { other_selectors.push(selector); } } // If we're called recursively from a :has(...) selector, we're on the way of checking if the other selectors match. // In that case ignore this check (because we just came from this) to avoid an infinite loop. if (check_has && has_selectors.length > 0) { // :has(...) is special in that it means "look downwards in the CSS tree". Since our matching algorithm goes // upwards and back-to-front, we need to first check the selectors inside :has(...), then check the rest of the // selector in a way that is similar to ancestor matching. In a sense, we're treating `.x:has(.y)` as `.x .y`. for (const has_selector of has_selectors) { const complex_selectors = /** @type {Compiler.Css.SelectorList} */ (has_selector.args) .children; let matched = false; for (const complex_selector of complex_selectors) { const selectors = truncate(complex_selector); const left_most_combinator = selectors[0]?.combinator ?? descendant_combinator; // In .x:has(> y), we want to search for y, ignoring the left-most combinator // (else it would try to walk further up and fail because there are no selectors left) if (selectors.length > 0) { selectors[0] = { ...selectors[0], combinator: null }; } if ( selectors.length === 0 /* is :global(...) */ || apply_selector(selectors, rule, element, stylesheet, check_has) ) { // Treat e.g. `.x:has(.y)` as `.x .y` with the .y part already being matched, // and now looking upwards for the .x part. if ( apply_combinator( left_most_combinator, selectors[0] ?? [], [relative_selector], rule, element, stylesheet, false ) ) { complex_selector.metadata.used = true; matched = true; } } } if (!matched) { if (relative_selector.metadata.is_global && !relative_selector.metadata.is_global_like) { // Edge case: `:global(.x):has(.y)` where `.x` is global but `.y` doesn't match. // Since `used` is set to `true` for `:global(.x)` in css-analyze beforehand, and // we have no way of knowing if it's safe to set it back to `false`, we'll mark // the inner selector as used and scoped to prevent it from being pruned, which could // result in a invalid CSS output (e.g. `.x:has(/* unused .y */)`). The result // can't match a real element, so the only drawback is the missing prune. // TODO clean this up some day complex_selectors[0].metadata.used = true; complex_selectors[0].children.forEach((selector) => { selector.metadata.scoped = true; }); } return false; } } return true; } for (const selector of other_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' && selector.args !== null && relative_selector.selectors.length === 1 ) { const args = selector.args; const complex_selector = args.children[0]; return apply_selector(complex_selector.children, rule, element, stylesheet, check_has); } // We came across a :global, everything beyond it is global and therefore a potential match if (name === 'global' && selector.args === null) return true; if ((name === 'is' || name === 'where' || name === 'not') && selector.args) { let matched = false; for (const complex_selector of selector.args.children) { const relative = truncate(complex_selector); if ( relative.length === 0 /* is :global(...) */ || apply_selector(relative, rule, element, stylesheet, check_has) ) { complex_selector.metadata.used = true; matched = true; } else if (complex_selector.children.length > 1 && (name == 'is' || name == 'where')) { // foo :is(bar baz) can also mean that bar is an ancestor of foo, and baz a descendant. // We can't fully check if that actually matches with our current algorithm, so we just assume it does. // The result may not match a real element, so the only drawback is the missing prune. complex_selector.metadata.used = true; matched = true; for (const selector of relative) { selector.metadata.scoped = true; } } } if (!matched) { if ( relative_selector.metadata.is_global && !relative_selector.metadata.is_global_like ) { // Edge case: `:global(.x):is(.y)` where `.x` is global but `.y` doesn't match. // Since `used` is set to `true` for `:global(.x)` in css-analyze beforehand, and // we have no way of knowing if it's safe to set it back to `false`, we'll mark // the inner selector as used and scoped to prevent it from being pruned, which could // result in a invalid CSS output (e.g. `.x:is(/* unused .y */)`). The result // can't match a real element, so the only drawback is the missing prune. // TODO clean this up some day selector.args.children[0].metadata.used = true; selector.args.children[0].children.forEach((selector) => { selector.metadata.scoped = true; }); } 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 {Compiler.Css.Rule} */ (rule.metadata.parent_rule); for (const complex_selector of parent.prelude.children) { if (apply_selector(truncate(complex_selector), parent, element, stylesheet, check_has)) { 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 {Compiler.AST.RegularElement | Compiler.AST.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; if (is_text_attribute(attribute)) { return test_attribute(operator, expected_value, case_insensitive, attribute.value[0].data); } const chunks = get_attribute_chunks(attribute.value); 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 {Compiler.AST.RegularElement | Compiler.AST.SvelteElement | Compiler.AST.RenderTag} node * @returns {Compiler.AST.RegularElement | Compiler.AST.SvelteElement | null} */ function get_element_parent(node) { /** @type {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 {Compiler.SvelteNode} node * @returns {Compiler.SvelteNode} */ function find_previous_sibling(node) { /** @type {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 {Compiler.SvelteNode} node * @param {boolean} adjacent_only * @returns {Map<Compiler.AST.RegularElement | Compiler.AST.SvelteElement | Compiler.AST.SlotElement | Compiler.AST.RenderTag, NodeExistsValue>} */ function get_possible_element_siblings(node, adjacent_only) { /** @type {Map<Compiler.AST.RegularElement | Compiler.AST.SvelteElement | Compiler.AST.SlotElement | Compiler.AST.RenderTag, NodeExistsValue>} */ const result = new Map(); /** @type {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 {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 {Compiler.AST.EachBlock | Compiler.AST.IfBlock | Compiler.AST.AwaitBlock} relative_selector * @param {boolean} adjacent_only * @returns {Map<Compiler.AST.RegularElement, NodeExistsValue>} */ function get_possible_last_child(relative_selector, adjacent_only) { /** @typedef {Map<Compiler.AST.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<Compiler.AST.RegularElement, NodeExistsValue>} result */ function mark_as_probably(result) { for (const key of result.keys()) { result.set(key, NODE_PROBABLY_EXISTS); } } /** * @param {Compiler.SvelteNode[]} children * @param {boolean} adjacent_only */ function loop_child(children, adjacent_only) { /** @type {Map<Compiler.AST.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; } |