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 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 | 26x 26x 26x 26x 26x 26x 26x 26x 26x 3203x 3203x 3203x 13x 3203x 3203x 3203x 548x 74x 568x 82x 78x 5x 26x 1706x 1005x 5971x 50x 1126x 10x 171x 9x 138x 18x 40x 1566x 23x 68x 9x 179x 76x 103x 3924x 26x 1464x 1464x 1464x 1464x 2752x 3118x 3118x 3118x 72x 453x 1464x 1464x 1464x 39x 26x 1221x 1221x 1221x 643x 13x 421x 421x 418x 413x 355x 2x 353x 351x 350x 2x 348x 78x 34x 44x 532x 435x 79x 77x 2x 1033x 175x 79x 96x 47x 187x 1379x 225x 35060x 26x 1441x 697x 3814x 26x 23x 3x 136x 26x 12x 47x 26x 33x 33x 33x 10x 43x 26x 473x 104x 133x 7734x 26x 92x 92x 148x 128x 92x 39x 3089x 26x 69x 69x 83x 26x 26x 26x 10x 202x 26x 43x 29x 1678x 26x 35x 15x 1617x 26x 48x 16x 3155x 26x 12x 12x 12x 7x 12x 12x 12x 3x 1626x 26x 67x 67x 60x 29x 26x 49x 10x 10x 1x 9x 25x 1649x 26x 10x 3x 3x 3x 5x 1613x 26x 8x 4x 4x 1608x 26x 22x 22x 22x 22x 22x 10x 12x 20x 22x 30x 1681x 26x 12x 5x 33x 26x 10x 4x 28x 26x 12x 12x 13x 12x 13x 33x 20x 1x 1569x 26x 79x 79x 79x 101x 35x 4289x 26x 56x 25x 24x 26x 26x 26x 26x 23x 23x 23x 23x 16x 25x 10x 23x 3222x 26x 12x 4x 4x 3x 1x 2x 1600x 26x 11x 11x 11x 12x 3x 1598x 26x 40x 40x 40x 3451x 26x 5x 1x 24x 26x 35x 35x 3403x 26x 16x 9x 9x 8x 1x 7x 1693x 26x 19x 19x 18x 20x 8x 1686x 26x 26x 26x 13x 1633x 26x 11x 29x 26x 26x 26x 26x 26x 26x 10x 5x 1x 1x 3x 26x 15x 15x 15x 8x 6x 9x 9x 9x 29x 29x 9x 9x 9x 1x 1580x 26x 26x 26x 26x 10x 10x 9x 9x 3x 6x 10x 1555x | import * as NodeLogic from './nodeLogic'; import {MimeType} from '../api/pipeline'; /** * Recognized node types */ export enum NodeType { Tag = 'tag', Text = 'text', Comment = 'comment', CDATA = 'cdata', ProcessingInstruction = 'processingInstruction', Document = 'document' } /** * A DOM node */ export abstract class Node { /** * Type of this node */ readonly nodeType: NodeType; /** * Parent of this node, or null if there is none. * Do not modify this directly, use NodeLogic or instance methods. */ parentNode: NodeWithChildren | null = null; /** * Previous sibling node, or null if none exists. * Do not modify this directly, use NodeLogic or instance methods. */ prevSibling: Node | null = null; /** * Next sibling node, or null if none exists. * Do not modify this directly, use NodeLogic or instance methods. */ nextSibling: Node | null = null; /** * The closest previous sibling that is a TagNode, or null if none exists. */ get prevSiblingTag(): TagNode | null { return NodeLogic.getPreviousTag(this); } /** * The closest following sibling that is a TagNode, or null if none exists. */ get nextSiblingTag(): TagNode | null { return NodeLogic.getNextTag(this); } /** * The closest previous sibling that is a TextNode, or null if none exists. */ get prevSiblingText(): TextNode | null { return NodeLogic.getPreviousText(this); } /** * The closest following sibling that is a TextNode, or null if none exists. */ get nextSiblingText(): TextNode | null { return NodeLogic.getNextText(this); } /** * Extra DOM data associated with this node. * This object prototypically inherits from the parent node's nodeData. */ readonly nodeData: Record<PropertyKey, unknown> = {}; /** * Set of unique string tags applied to this node. * These tags do not inherit and are unique to each instance. * Meaning is tag-specific. */ readonly nodeTags = new Set<string>(); /** * Creates a new Node * @param nodeType Type of this node */ constructor(nodeType: NodeType) { this.nodeType = nodeType; } /** * Inserts a sibling node after this one * @param node Node to insert */ appendSibling(node: Node): void { NodeLogic.appendSibling(node, this); } /** * Inserts a sibling node before this one * @param node Node to insert */ prependSibling(node: Node): void { NodeLogic.prependSibling(node, this); } /** * Remove this node and all children from the DOM */ removeSelf(): void { NodeLogic.detachNode(this); } /** * Remove this node and all children from the DOM, and insert one or more nodes in its place * @param nodes Nodes to insert in replacement. Can be empty. */ replaceSelf(nodes: Node[]): void { NodeLogic.replaceNode(this, nodes); } /** * Clone this node * @param deep If true, child nodes will be cloned * @param callback Optional callback for each node after cloning * @returns The generated clone */ abstract clone(deep?: boolean, callback?: (oldNode: Node, newNode: Node) => void): Node; /** * Serializes this node into HTML. * Child nodes will be automatically serialize. * * @returns HTML text representation of this node */ toHtml(): string { return NodeLogic.serializeNode(this); } /** * Finds the next node that matches a callback. * The next node is defined as the first matching child, sibling, or sibling's child node starting from this node. * This only checks child and sibling nodes, it will not move further up the DOM tree. * To check only sibling nodes, set node to false. * @param matcher Callback to test nodes * @param deep If true or not specified, then child nodes will be checked * @return The first matching node, or null if none found. */ findNext(matcher: (node: Node) => boolean, deep = true): Node | null { return NodeLogic.findNextNode(this, matcher, deep); } } /** * Base type for any type of node that can have children */ export abstract class NodeWithChildren extends Node { /** * Children of this node. * Do not modify this directly, use NodeLogic or instance methods. */ childNodes: Node[] = []; /** * The first child node of this parent, or null if there are no children. */ get firstChild(): Node | null { return NodeLogic.getFirstNode(this.childNodes); } /** * The last child node of this parent, or null if there are no children. */ get lastChild(): Node | null { return NodeLogic.getLastNode(this.childNodes); } /** * The first child node that is a TagNode, or null if there is none. */ get firstChildTag(): TagNode | null { return NodeLogic.getFirstTag(this.childNodes); } /** * The last child node that is a TagNode, or null if there is none. */ get lastChildTag(): TagNode | null { return NodeLogic.getLastTag(this.childNodes); } /** * The first child node that is a TextNode, or null if there is none. */ get firstChildText(): TextNode | null { return NodeLogic.getFirstText(this.childNodes); } /** * The last child node that is a TextNode, or null if there is none. */ get lastChildText(): TextNode | null { return NodeLogic.getLastText(this.childNodes); } /** * Gets all child TagNodes from this parent * @returns array of TagNodes containing all child tags */ getChildTags(): TagNode[] { return NodeLogic.getChildTags(this); } /** * Inserts a child at the end of this parent's children * @param child Node to insert */ appendChild(child: Node): void { NodeLogic.appendChild(this, child); } /** * Inserts a child at the start of this parent's children * @param child Node to insert */ prependChild(child: Node): void { NodeLogic.prependChild(this, child); } /** * Removes all children from this node */ clear(): void { NodeLogic.clear(this); } /** * Append multiple children to the end of this parent's child list * @param children Child nodes to append. Can be empty. */ appendChildren(children: Node[]): void { NodeLogic.appendChildNodes(this, children); } /** * Prepend multiple children to the start of this parent's child list * @param children Child nodes to prepend. Can be empty. */ prependChildren(children: Node[]): void { NodeLogic.prependChildNodes(this, children); } /** * Finds the first child node that matches a condition. * Returns null if no match found. * @param matcher Callback to test condition * @param deep If true, children of child nodes will also be searched. * @returns First matching child, or null */ findChildNode(matcher: (node: Node) => boolean, deep = true): Node | null { return NodeLogic.findChildNode(this, matcher, deep); } /** * Finds all child nodes that match a condition. * Returns an empty array if no matches found. * @param matcher Callback to test condition * @param deep If true, children of child nodes will also be searched. * @returns Array of all Nodes that match condition */ findChildNodes(matcher: (node: Node) => boolean, deep = true): Node[] { return NodeLogic.findChildNodes(this, matcher, deep); } /** * Finds the first child TagNode that matches a condition. * Returns null if no match found. * @param matcher Callback to test condition * @param deep If true, children of child nodes will also be searched. * @returns First TagNode that matches the condition, or null. */ findChildTag(matcher: (tag: TagNode) => boolean, deep = true): TagNode | null { return NodeLogic.findChildTag(this, matcher, deep); } /** * Finds all child TagNodes that match a condition. * Returns an empty array if no matches found. * @param matcher Callback to test condition * @param deep If true, children of child nodes will also be searched. * @returns Array of all TagNodes that match condition */ findChildTags(matcher: (tag: TagNode) => boolean, deep = true): TagNode[] { return NodeLogic.findChildTags(this, matcher, deep); } /** * Finds the first child node of a specified type. * Returns null if none found. * @param nodeType Type of node to match * @param deep If true, children of child nodes will also be searched. * @returns The first node that matches, or null */ findChildNodeByNodeType(nodeType: NodeType.Document, deep?: boolean): DocumentNode | null; findChildNodeByNodeType(nodeType: NodeType.Tag, deep?: boolean): TagNode | null; findChildNodeByNodeType(nodeType: NodeType.Text, deep?: boolean): TextNode | null; findChildNodeByNodeType(nodeType: NodeType.Comment, deep?: boolean): CommentNode | null; findChildNodeByNodeType(nodeType: NodeType.CDATA, deep?: boolean): CDATANode | null; findChildNodeByNodeType(nodeType: NodeType.ProcessingInstruction, deep?: boolean): ProcessingInstructionNode | null; findChildNodeByNodeType(nodeType: NodeType, deep?: boolean): Node | null findChildNodeByNodeType(nodeType: NodeType, deep = true): Node | null { return this.findChildNode(node => node.nodeType === nodeType, deep); } /** * Finds all child nodes of a specified type. * @param nodeType Type of node to match * @param deep If true, children of child nodes will also be searched. * @returns Array of all matching child nodes */ findChildNodesByNodeType(nodeType: NodeType.Document, deep?: boolean): DocumentNode[]; findChildNodesByNodeType(nodeType: NodeType.Tag, deep?: boolean): TagNode[]; findChildNodesByNodeType(nodeType: NodeType.Text, deep?: boolean): TextNode[]; findChildNodesByNodeType(nodeType: NodeType.Comment, deep?: boolean): CommentNode[]; findChildNodesByNodeType(nodeType: NodeType.CDATA, deep?: boolean): CDATANode[]; findChildNodesByNodeType(nodeType: NodeType.ProcessingInstruction, deep?: boolean): ProcessingInstructionNode[]; findChildNodesByNodeType(nodeType: NodeType, deep?: boolean): Node[]; findChildNodesByNodeType(nodeType: NodeType, deep = true): Node[] { return this.findChildNodes(node => node.nodeType === nodeType, deep); } /** * Finds the first child TagNode with the specified tag name. * Result may be a subclass of TagNode if a custom tag type is specified. * Returns null if none found. * @param tagName Tag name to match * @param deep If true, children of child nodes will also be searched. * @returns First matching TagNode, or null. */ findChildTagByTagName(tagName: 'm-fragment', deep?: boolean): MFragmentNode | null; findChildTagByTagName(tagName: 'm-content', deep?: boolean): MContentNode | null; findChildTagByTagName(tagName: 'm-slot', deep?: boolean): MSlotNode | null; findChildTagByTagName(tagName: 'm-var', deep?: boolean): MVarNode | null; findChildTagByTagName(tagName: 'm-scope', deep?: boolean): MScopeNode | null; findChildTagByTagName(tagName: 'm-import', deep?: boolean): MImportNode | null; findChildTagByTagName(tagName: 'm-if', deep?: boolean): MIfNode | null; findChildTagByTagName(tagName: 'm-for', deep?: boolean): MForNode | null; findChildTagByTagName(tagName: 'm-data', deep?: boolean): MDataNode | null; findChildTagByTagName(tagName: 'style', deep?: boolean): StyleNode | null; findChildTagByTagName(tagName: 'script', deep?: boolean): ScriptNode | null; findChildTagByTagName(tagName: 'a', deep?: boolean): AnchorNode | null; findChildTagByTagName(tagName: 'm-whitespace', deep?: boolean): MWhitespaceNode | null; findChildTagByTagName(tagName: string, deep?: boolean): TagNode | null; findChildTagByTagName(tagName: string, deep = true): TagNode | null { return this.findChildTag(tag => tag.tagName === tagName, deep); } /** * Finds all child TagNodes with the specified tag name. * Results may be a subclass of TagNode if a custom tag type is specified. * Returns null if none found. * @param tagName Tag name to match * @param deep If true, children of child nodes will also be searched. * @returns Array of all matching TagNodes */ findChildTagsByTagName(tagName: 'm-fragment', deep?: boolean): MFragmentNode[]; findChildTagsByTagName(tagName: 'm-content', deep?: boolean): MContentNode[]; findChildTagsByTagName(tagName: 'm-slot', deep?: boolean): MSlotNode[]; findChildTagsByTagName(tagName: 'm-var', deep?: boolean): MVarNode[]; findChildTagsByTagName(tagName: 'm-scope', deep?: boolean): MScopeNode[]; findChildTagsByTagName(tagName: 'm-import', deep?: boolean): MImportNode[]; findChildTagsByTagName(tagName: 'm-if', deep?: boolean): MIfNode[]; findChildTagsByTagName(tagName: 'm-for', deep?: boolean): MForNode[]; findChildTagsByTagName(tagName: 'm-data', deep?: boolean): MDataNode[]; findChildTagsByTagName(tagName: 'style', deep?: boolean): StyleNode[]; findChildTagsByTagName(tagName: 'script', deep?: boolean): ScriptNode[]; findChildTagsByTagName(tagName: 'a', deep?: boolean): AnchorNode[]; findChildTagsByTagName(tagName: 'm-whitespace', deep?: boolean): MWhitespaceNode[]; findChildTagsByTagName(tagName: string, deep?: boolean): TagNode[]; findChildTagsByTagName(tagName: string, deep = true): TagNode[] { return this.findChildTags(tag => tag.tagName === tagName, deep); } /** * Removes all children from this node and creates a new DocumentNode containing them. * @returns the created DocumentNode */ createDomFromChildren(): DocumentNode { return NodeLogic.createDomFromChildren(this); } /** * Removes this node from the DOM, but optionally preserves children. * If keepChildren is true, then child nodes will be reattached to the DOM in place of this node. * Effectively "promotes" child nodes. * @param keepChildren If true, child nodes will be kept. */ removeSelf(keepChildren = false): void { if (keepChildren) { NodeLogic.replaceNode(this, this.childNodes); } else { super.removeSelf(); } } /** * Returns true if a node is an instance of NodeWithChildren * @param node Node to check */ static isNodeWithChildren(node: Node): node is NodeWithChildren { return node.nodeType === NodeType.CDATA || node.nodeType === NodeType.Tag || node.nodeType === NodeType.Document; } } /** * Parent type for any node that includes textual data */ export abstract class NodeWithText extends Node { /** * Indicates if the text contained by this node is whitespace-sensitive. * If true, then the exact whitespace should be preserved. * This includes leading and trailing newlines */ isWhitespaceSensitive = false; private _text = ''; private _textContent = ''; private _hasContent = false; /** * Text contained by this node */ get text(): string { return this._text; } set text(newText: string) { this._text = newText; this._textContent = newText.trim(); this._hasContent = /\S/.test(newText); } /** * Text contained by this node, with leading and trailing whitespace trimmed. */ get textContent(): string { return this._textContent; } /** * If this node contains any non-whitespace text */ get hasContent(): boolean { return this._hasContent; } /** * Creates a new NodeWithText * @param nodeType Type of node, for Node() constructor * @param text Content of this NodeWithText * @param isWhitespaceSensitive If true, whitespace in this node is considered significant. See {@link NodeWithText.isWhitespaceSensitive}. */ protected constructor(nodeType: NodeType, text: string, isWhitespaceSensitive = false) { super(nodeType); this.text = text; this.isWhitespaceSensitive = isWhitespaceSensitive; } /** * Returns true if a node is an instance of NodeWithText * @param node Node to check */ static isNodeWithText(node: Node): node is NodeWithText { return node.nodeType === NodeType.Text || node.nodeType === NodeType.Comment; } } /** * A tag node */ export class TagNode extends NodeWithChildren { /** * Name of the tag */ readonly tagName: string; /** * Attributes on this tag node. * Do not modify directly, use attribute API instance methods. */ protected attributes: Map<string, unknown>; /** * Creates a new TagNode * @param tagName Type of tag * @param attributes Optional Map of attributes to initialize this tag with */ constructor(tagName: string, attributes?: Map<string, unknown>) { super(NodeType.Tag); this.tagName = tagName; this.attributes = attributes ?? new Map<string, unknown>(); } /** * Checks if this tag has a specified attribute * @param name Name of the attribute * @returns True if the attribute exists, false otherwise */ hasAttribute(name: string): boolean { return this.attributes.has(name); } /** * Checks if this tag has a specified attribute with a non-null and non-undefined value * @param name Name of the attribute * @returns True if the attribute exists and is non-nullish, false otherwise */ hasValueAttribute(name: string): boolean { return this.hasAttribute(name) && this.getAttribute(name) != null; } /** * Gets the value of an attribute, or undefined the attribute does not exist * @param name Name of the attribute * @returns Value of the attribute, converted to a string if not null or undefined */ getAttribute(name: string): string | null | undefined { const value = this.getRawAttribute(name); if (value === undefined) return undefined; if (value === null) return null; return String(value); } /** * Gets the value of an attribute, or throws an exception if the attribute does not exist * @param name Name of the attribute * @returns Value of the attribute, converted to a string if not null * @throws If the attribute does not exist */ getRequiredAttribute(name: string): string | null { if (!this.hasAttribute(name)) { throw new Error(`Missing required attribute '${ name }'`); } // cannot be undefined, because we check above return this.getAttribute(name) as string | null; } /** * Gets the value of an attribute, or throws an exception if the attribute does not exist or the value is null * @param name Name of the attribute * @returns Value of the attribute, converted to a string * @throws If the attribute does not exist * @throws If the attribute is null */ getRequiredValueAttribute(name: string): string { const attr: string | null = this.getRequiredAttribute(name); if (attr == null) { throw new Error(`Missing value for required value attribute '${ name }'`); } return attr; } /** * Gets the value of an attribute, or returns undefined if the attribute does not exist. * Throws an exception if attribute value is null. * * @param name Name of the attribute * @returns Value of the attribute (converted to a string) or undefined if it does not exist * @throws If the attribute exists but is null */ getOptionalValueAttribute(name: string): string | undefined { if (this.hasAttribute(name)) { return this.getRequiredValueAttribute(name); } else { return undefined; } } /** * Gets the raw (non-string) value of an attribute. May be any value. * @param name Name of the attribute * @returns Raw value of the attribute */ getRawAttribute(name: string): unknown { return this.attributes.get(name); } /** * Sets the value of an attribute * @param name Name of the attribute * @param value Value of the attribute */ setAttribute(name: string, value: string | null): void { this.setRawAttribute(name, value); } /** * Sets the value of an attribute, or deletes it if undefined. * @param name Name of the attribute. * @param value Value to set, or undefined to delete */ setOptionalValueAttribute(name: string, value: string | undefined): void { if (value === undefined) { this.deleteAttribute(name); } else { this.setRawAttribute(name, value); } } /** * Sets an attribute to any value, even a non-string * @param name Name of the attribute * @param value Value of the attribute */ setRawAttribute(name: string, value: unknown): void { this.attributes.set(name, value); } /** * Sets the value of a boolean attribute. * A boolean attribute has no value, it only exists or does not exist. * * @param name Name of the attribute * @param value Value of the attribute */ setBooleanAttribute(name: string, value: boolean): void { if (value) { this.setAttribute(name, null); } else { this.deleteAttribute(name); } } /** * Sets the value of an attribute that cannot be null * @param name Name of the attribute * @param value Value of the attribute */ setRequiredValueAttribute(name: string, value: string): void { this.setAttribute(name, value); } /** * Removes an attribute from this tag * @param name Name of the attribute */ deleteAttribute(name: string): void { this.attributes.delete(name); } /** * Gets a ReadonlyMap containing all attributes on this tag. * Attributes are exposed raw - not converted to strings. * @returns a read-only Map containing all attributes */ getAttributes(): ReadonlyMap<string, unknown> { return this.attributes; } clone(deep = true, callback?: (oldNode: Node, newNode: Node) => void): TagNode { return NodeLogic.cloneTagNode(this, deep, callback); } /** * Returns true if a node is an instance of TagNode * @param node Node to check */ static isTagNode(node: Node): node is TagNode { return node.nodeType === NodeType.Tag; } } /** * A text node */ export class TextNode extends NodeWithText { /** * Create a new TextNode * @param text Optional text to initialize with. Defaults to an empty string * @param isWhitespaceSensitive {@link See NodeWithText.isWhitespaceSensitive} */ constructor(text = '', isWhitespaceSensitive?: boolean) { super(NodeType.Text, text, isWhitespaceSensitive); } clone(deep?: boolean, callback?: (oldNode: Node, newNode: Node) => void): TextNode { return NodeLogic.cloneTextNode(this, callback); } /** * Returns true if a node is an instance of TextNode * @param node Node to check */ static isTextNode(node: Node): node is TextNode { return node.nodeType === NodeType.Text; } } /** * An HTML comment. */ export class CommentNode extends NodeWithText { /** * Create a new comment node * @param text Optional comment text to initialize with * @param isWhitespaceSensitive {@link See NodeWithText.isWhitespaceSensitive} */ constructor(text = '', isWhitespaceSensitive?: boolean) { super(NodeType.Comment, text, isWhitespaceSensitive); } clone(deep?: boolean, callback?: (oldNode: Node, newNode: Node) => void): CommentNode { return NodeLogic.cloneCommentNode(this, callback); } /** * Returns true if a node is an instance of CommentNode * @param node Node to check */ static isCommentNode(node: Node): node is CommentNode { return node.nodeType === NodeType.Comment; } } /** * A CDATA node */ export class CDATANode extends NodeWithChildren { constructor() { super(NodeType.CDATA); } clone(deep = true, callback?: (oldNode: Node, newNode: Node) => void): CDATANode { return NodeLogic.cloneCDATANode(this, deep, callback); } /** * Returns true if a node is an instance of CDATANode * @param node Node to check */ static isCDATANode(node: Node): node is CDATANode { return node.nodeType === NodeType.CDATA; } } /** * A Processing instruction node */ export class ProcessingInstructionNode extends Node { /** * Name of this processing instruction */ name: string; /** * Data contained by this node */ data: string; /** * Creates a new Processing Instruction * @param name Name value * @param data Data value */ constructor(name = '', data = '') { super(NodeType.ProcessingInstruction); this.name = name; this.data = data; } clone(deep?: boolean, callback?: (oldNode: Node, newNode: Node) => void): ProcessingInstructionNode { return NodeLogic.cloneProcessingInstructionNode(this, callback); } /** * Returns true if a node is an instance of ProcessingInstructionNode * @param node Node to check */ static isProcessingInstructionNode(node: Node): node is ProcessingInstructionNode { return node.nodeType === NodeType.ProcessingInstruction; } } /** * A document node. * Is a "root" node, so it cannot have a parent. * DOM trees are not required to have a DocumentNode root, but it exists as a utility to make DOM processing easier. */ export class DocumentNode extends NodeWithChildren { constructor() { super(NodeType.Document); } /** * Set the root scope of this DOM. * The root scope is inherited by all nodes, but is read-only. * @param rootScope Scope object to inherit from */ setRootScope(rootScope: Record<PropertyKey, unknown> | null): void { Object.setPrototypeOf(this.nodeData, rootScope); } clone(deep = true, callback?: (oldNode: Node, newNode: Node) => void): DocumentNode { return NodeLogic.cloneDocumentNode(this, deep, callback); } /** * Returns true if a node is an instance of DocumentNode * @param node Node to check */ static isDocumentNode(node: Node): node is DocumentNode { return node.nodeType === NodeType.Document; } } /** * Implements <m-fragment> tag. * MFragmentNode defines a reference to an external HTML fragment. * This node will be replaced with the compiled, plain HTML form of the external fragment. */ export class MFragmentNode extends TagNode { /** * Create a new MFragmentNode * @param src Path to fragment * @param attributes Optional attributes */ constructor(src: string, attributes?: Map<string, unknown>) { super('m-fragment', attributes); this.src = src; } /** * Parameters to the external reference. */ get parameters(): Array<[string, unknown]> { return Array.from(this.attributes.entries()).filter(attr => attr[0] !== 'src'); } /** * Path to the source of the external reference */ get src(): string { return this.getRequiredValueAttribute('src'); } set src(newSrc: string) { this.setAttribute('src', newSrc); } clone(deep = true, callback?: (oldNode: Node, newNode: Node) => void): MFragmentNode { return NodeLogic.cloneMFragmentNode(this, deep, callback); } /** * Returns true if a node is an instance of MFragmentNode * @param node Node to check */ static isMFragmentNode(node: Node): node is MFragmentNode { return TagNode.isTagNode(node) && node.tagName === 'm-fragment'; } } /** * Parent type for any custom tag that refers to a slot. * Slot name is stored in the "slot" attribute and defaults to "[default]". */ export abstract class SlotReferenceNode extends TagNode { /** * Creates a new SlotReferenceNode * @param tagName Tag name of the implementing node * @param slot Name of the slot * @param attributes Optional attributes */ protected constructor(tagName: string, slot?: string, attributes?: Map<string, unknown>) { super(tagName, attributes); // populate slot name if missing this.setAttribute('slot', slot ?? SlotReferenceNode.DefaultSlotName); } /** * Name of the slot */ get slot(): string { return this.getRequiredValueAttribute('slot'); } set slot(newSlotName: string) { this.setAttribute('slot', newSlotName); } abstract clone(deep: boolean, callback?: (oldNode: Node, newNode: Node) => void): SlotReferenceNode; static readonly DefaultSlotName = '[default]'; } /** * <m-content> tag. * MContentNode defines content that should be placed into a slot when compiling an external reference. */ export class MContentNode extends SlotReferenceNode { /** * Creates a new MSlotNode * @param slot Optional name of target slot * @param attributes Optional attributes */ constructor(slot?: string, attributes?: Map<string, unknown>) { super('m-content', slot, attributes); } clone(deep = true, callback?: (oldNode: Node, newNode: Node) => void): MContentNode { return NodeLogic.cloneMContentNode(this, deep, callback); } /** * Returns true if a node is an instance of MContentNode * @param node Node to check */ static isMContentNode(node: Node): node is MContentNode { return TagNode.isTagNode(node) && node.tagName === 'm-content'; } } /** * <m-slot> tag. * Defines a slot that can receive a DOM subtree from <m-content> */ export class MSlotNode extends SlotReferenceNode { /** * Creates a new MSlotNode * @param slot Optional slot name * @param attributes Optional attributes */ constructor(slot?: string, attributes?: Map<string, unknown>) { super('m-slot', slot, attributes); } clone(deep = true, callback?: (oldNode: Node, newNode: Node) => void): MSlotNode { return NodeLogic.cloneMSlotNode(this, deep, callback); } /** * Returns true if a node is an instance of MSlotNode * @param node Node to check */ static isMSlotNode(node: Node): node is MSlotNode { return TagNode.isTagNode(node) && node.tagName === 'm-slot'; } } /** * <m-var> tag. * Defines local variables that are inserted into the parent node's scope instead of this node's scope. * Effectively defines variables for its following siblings rather than its children. */ export class MVarNode extends TagNode { /** * Create a new MVarNode * @param attributes Optional attributes to convert to variables */ constructor(attributes?: Map<string, unknown>) { super('m-var', attributes); } clone(deep = true, callback?: (oldNode: Node, newNode: Node) => void): MVarNode { return NodeLogic.cloneMVarNode(this, deep, callback); } /** * Returns true if a node is an instance of MVarNode * @param node Node to check */ static isMVarNode(node: Node): node is MVarNode { return TagNode.isTagNode(node) && node.tagName === 'm-var'; } } /** * <m-scope> tag. * Defines variables that are added to this node's scope, and therefore available only to child nodes. * Enables the concept of a "local scope". */ export class MScopeNode extends TagNode { /** * Create a new MScopeNode * @param attributes Optional attributes */ constructor(attributes?: Map<string, unknown>) { super('m-scope', attributes); } clone(deep = true, callback?: (oldNode: Node, newNode: Node) => void): MScopeNode { return NodeLogic.cloneMScopeNode(this, deep, callback); } /** * Returns true if a node is an instance of MScopeNode * @param node Node to check */ static isMScopeNode(node: Node): node is MScopeNode { return TagNode.isTagNode(node) && node.tagName === 'm-scope'; } } /** * <m-import> tag. * Defines a shorthand "alias" to an external reference. * The "as" attribute specifies the tag name that will become an alias for this import. * The "src" attribute specifies the path to the external reference. * MImportNode registers imports into the parent scope, like MVarNode. */ export class MImportNode extends TagNode { /** * Creates a new MImportNode. * @param src Path to reference * @param as Tag name to use as alias * @param attributes Optional attributes. */ constructor(src: string, as: string, attributes?: Map<string, unknown>) { super('m-import', attributes); this.src = src; this.as = as; } /** * Path to the import source */ get src(): string { return this.getRequiredValueAttribute('src'); } set src(newSrc: string) { this.setAttribute('src', newSrc); } /** * Alias for the import */ get as(): string { return this.getRequiredValueAttribute('as'); } set as(newAs: string) { this.setAttribute('as', newAs); } clone(deep = true, callback?: (oldNode: Node, newNode: Node) => void): MImportNode { return NodeLogic.cloneMImportNode(this, deep, callback); } /** * Returns true if a node is an instance of MImportNode * @param node Node to check */ static isMImportNode(node: Node): node is MImportNode { return TagNode.isTagNode(node) && node.tagName === 'm-import'; } } /** * Parent class for any Tag that contains a conditional <m-if>, <m-else-if>, etc. * The conditional value is stored in the ? attribute. */ export abstract class ConditionalNode extends TagNode { /** * Creates a new ConditionalNode * @param tagName Tag name of implementing node * @param condition Conditional value, or a string to be compiled into a conditional value * @param attributes Optional attributes */ protected constructor(tagName: string, condition: unknown, attributes?: Map<string, unknown>) { super(tagName, attributes); this.setRawAttribute('?', condition); } /** * The raw value of the conditional in this conditional. * If this node has been processed by TemplateTextModule, then this can be any value. * Otherwise, it will be the uncompiled string expression. */ get condition(): unknown { return this.getRawAttribute('?'); } set condition(newCondition: unknown) { this.setRawAttribute('?', newCondition); } /** * If the conditional has been compiled, then this is the conditional value coerced into a boolean. * If not compiled, then this will true if a conditional expression exists and false if not. */ get isTruthy(): boolean { return !!this.condition; } /** * Gets the preceding conditional node, if it exists. * This respects the conditional's ordering rules. */ abstract get prevConditional(): ConditionalNode | null; /** * Gets the following conditional node, if it exists. * This respects the conditional's ordering rules. */ abstract get nextConditional(): ConditionalNode | null; } /** * <m-if> node. This is a conditional that selectively compiles or deletes it's contents based on a conditional expression. * An <m-if> node can have no preceding conditionals, but may have an <m-else-if> or an <m-else> as a following node. */ export class MIfNode extends ConditionalNode { /** * Creates a new MIfNode * @param condition Conditional value * @param attributes Optional attributes */ constructor(condition: unknown, attributes?: Map<string, unknown>) { super('m-if', condition, attributes); } get prevConditional(): null { return null; } get nextConditional(): ConditionalNode | null { const nextTag = this.nextSiblingTag; if (nextTag != null && (MElseIfNode.isMElseIfNode(nextTag) || MElseNode.isMElseNode(nextTag))) { return nextTag; } else { return null; } } clone(deep = true, callback?: (oldNode: Node, newNode: Node) => void): MIfNode { return NodeLogic.cloneMIfNode(this, deep, callback); } /** * Returns true if a node is an instance of MIfNode * @param node Node to check */ static isMIfNode(node: Node): node is MIfNode { return TagNode.isTagNode(node) && node.tagName === 'm-if'; } } /** * <m-else-if> node. Like <m-if>, this conditional will only render its content if its condition is true. * However, there is an additional constraint that an <m-else-if> node will only render if all preceding <m-if> and <m-else-if> nodes evaluate to false. * An <m-else-if> node can be preceded by <m-if> or <m-else-if>, and followed by <m-else> and <m-else-if>. */ export class MElseIfNode extends ConditionalNode { /** * Creates a new MElseIfNode * @param condition Conditional value * @param attributes Optional attributes */ constructor(condition: unknown, attributes?: Map<string, unknown>) { super('m-else-if', condition, attributes); } get prevConditional(): ConditionalNode | null { const prevTag = this.prevSiblingTag; if (prevTag != null && (MIfNode.isMIfNode(prevTag) || MElseIfNode.isMElseIfNode(prevTag))) { return prevTag; } else { return null; } } get nextConditional(): ConditionalNode | null { const nextTag = this.nextSiblingTag; Eif (nextTag != null && (MElseIfNode.isMElseIfNode(nextTag) || MElseNode.isMElseNode(nextTag))) { return nextTag; } else { return null; } } clone(deep = true, callback?: (oldNode: Node, newNode: Node) => void): MElseIfNode { return NodeLogic.cloneMElseIfNode(this, deep, callback); } /** * Returns true if a node is an instance of MElseIfNode * @param node Node to check */ static isMElseIfNode(node: Node): node is MElseIfNode { return TagNode.isTagNode(node) && node.tagName === 'm-else-if'; } } /** * <m-else> node. This is a conditional that always evaluates to true. * However, like <m-else-if> there is an additional constraint that an <m-else> node will only render if all preceding <m-if> and <m-else-if> nodes evaluate to false. * An <m-else> node can have no following conditionals, but may have an <m-else-if> or an <m-if> as a preceding node. */ export class MElseNode extends ConditionalNode { /** * Create a new MElseNode * @param attributes Optional attributes */ constructor(attributes?: Map<string, unknown>) { super('m-else', true, attributes); } get prevConditional(): ConditionalNode | null { const prevTag = this.prevSiblingTag; if (prevTag != null && (MIfNode.isMIfNode(prevTag) || MElseIfNode.isMElseIfNode(prevTag))) { return prevTag; } else { return null; } } get nextConditional(): null { return null; } clone(deep = true, callback?: (oldNode: Node, newNode: Node) => void): MElseNode { return NodeLogic.cloneMElseNode(this, deep, callback); } /** * Returns true if a node is an instance of MElseNode * @param node Node to check */ static isMElseNode(node: Node): node is MElseNode { return TagNode.isTagNode(node) && node.tagName === 'm-else'; } } /** * Base class for <m-for> tags. * MForNode implements a for loop. * An expression is evaluated, and for each resulting value the child nodes are repeated. * Each copy is compiled with the current iteration value and index exposed through specified variables. * The "var" attribute contains the name of the variable containing the iteration value. * The "index" attribute optionally contains the name of the variable to hold the iteration index. */ export abstract class MForNode extends TagNode { /** * Name of the attribute that stores the expression for this <m-for> */ readonly expressionAttrName: string; /** * Create a new MForNode * @param expressionAttrName Name of the attribute that contains the expression * @param expression Value of the expression * @param varName Name of the variable to store the current iteration value * @param indexName Optional name of the variable to store the current iteration index. * @param attributes Optional attributes */ protected constructor(expressionAttrName: string, expression: unknown, varName: string, indexName: string | undefined, attributes?: Map<string, unknown>) { super('m-for', attributes); this.expressionAttrName = expressionAttrName; this.setRawAttribute(expressionAttrName, expression); this.setAttribute('var', varName); if (indexName !== undefined) { this.setAttribute('index', indexName); } else { this.deleteAttribute('index'); } } /** * The raw value of the expression in this <m-for>. * If this node has been processed by TemplateTextModule, then this will be the array or object to iterate. * Otherwise, it will be the uncompiled string expression. */ get expression(): unknown { return this.getRawAttribute(this.expressionAttrName); } set expression(newOf: unknown) { this.setRawAttribute(this.expressionAttrName, newOf); } /** * Name of the variable binding */ get varName(): string { return this.getRequiredValueAttribute('var'); } set varName(newVar: string) { this.setAttribute('var', newVar); } /** * Name of the index binding */ get indexName(): string | undefined { return this.getOptionalValueAttribute('index'); } set indexName(newIndexName: string | undefined) { if (newIndexName !== undefined) { this.setAttribute('index', newIndexName); } else { this.deleteAttribute('index'); } } /** * Returns true if a node is an instance of MForNode * @param node Node to check */ static isMForNode(node: Node): node is MForNode { return TagNode.isTagNode(node) && node.tagName === 'm-for'; } } /** * Variant of <m-for> that implements a for...of loop * Expression is stored in the "of" attribute. */ export class MForOfNode extends MForNode { /** * Create a new MForOfLoop * @param expression Expression that is or evaluates to an array-like object * @param varName Name of variable to store the current value * @param indexName Name of variable to store the current index * @param attributes Optional attributes */ constructor(expression: unknown, varName: string, indexName: string | undefined, attributes?: Map<string, unknown>) { super('of', expression, varName, indexName, attributes); } clone(deep = true, callback?: (oldNode: Node, newNode: Node) => void): MForOfNode { return NodeLogic.cloneMForOfNode(this, deep, callback); } /** * Returns true if a node is an MForNode using a for...of expression * @param node Node to check */ static isMForOfNode(node: Node): node is MForOfNode { return MForNode.isMForNode(node) && node.expressionAttrName === 'of'; } } /** * Variant of <m-for> that implements a for...in loop. * Expression is stored in the "in" attribute. */ export class MForInNode extends MForNode { /** * Create a new MForInLoop * @param expression Expression that is or evaluates to an object * @param varName Name of variable to store the current value * @param indexName Name of variable to store the current index * @param attributes Optional attributes */ constructor(expression: unknown, varName: string, indexName: string | undefined, attributes?: Map<string, unknown>) { super('in', expression, varName, indexName, attributes); } clone(deep = true, callback?: (oldNode: Node, newNode: Node) => void): MForInNode { return NodeLogic.cloneMForInNode(this, deep, callback); } /** * Returns true if a node is an MForNode using a for...in expression * @param node Node to check */ static isMForInNode(node: Node): node is MForInNode { return MForNode.isMForNode(node) && node.expressionAttrName === 'in'; } } /** * Supported MIME types of data contents */ export type MDataNodeType = MimeType.JSON | MimeType.TEXT; /** * A data reference defined in an <m-data> node */ export interface MDataNodeRef { /** * Path to the data file */ resPath: string; /** * Name of the scope variable to bind to */ varName: string; } /** * <m-data> node. * Loads data files into the containing scope. * Once loaded, content is standard mutable scope data. * Multiple data files can be defined on the same tag. */ export class MDataNode extends TagNode { constructor(type: MDataNodeType, attributes?: Map<string, unknown>) { super('m-data', attributes); this.type = type; } /** * MIME type of data loaded by this MDataNode */ get type(): MDataNodeType { return this.getRequiredValueAttribute('type') as MDataNodeType; } set type(newType: MDataNodeType) { this.setRequiredValueAttribute('type', newType); } /** * Data references defined by this MDataNode. * This is recomputed at each access, so cache into a local var if repeated access is needed. */ get references(): MDataNodeRef[] { return Array.from(this.attributes.entries()) // skip the "type" attribute as it is reserved .filter(entry => entry[0] !== 'type') // convert remaining attributes into MDataNodeRef objects .map((entry): MDataNodeRef => { return { resPath: String(entry[1]), varName: entry[0] }; }); } clone(deep = true, callback?: (oldNode: Node, newNode: Node) => void): MDataNode { return NodeLogic.cloneMDataNode(this, deep, callback); } /** * Returns true if a node is an MDataNode * @param node Node to check */ static isMDataNode(node: Node): node is MDataNode { return TagNode.isTagNode(node) && node.tagName === 'm-data'; } } /** * <style> node. * May be compiled (processed by mooltipage) or not (ignored by mooltipage) */ export abstract class StyleNode extends TagNode { protected constructor(compiled: boolean, lang?: string, attributes?: Map<string, unknown>) { super('style', attributes); this.setBooleanAttribute('compiled', compiled); this.setOptionalValueAttribute('lang', lang); } /** * If this StyleNode is being processed by Mooltipage */ get compiled(): boolean { return this.hasAttribute('compiled'); } /** * Language of this style tag. */ get lang(): string | undefined { return this.getOptionalValueAttribute('lang'); } set lang(newLang: string | undefined) { this.setOptionalValueAttribute('lang', newLang); } /** * Returns true if a node is a StyleNode * @param node Node to check */ static isStyleNode(node: Node): node is StyleNode { return TagNode.isTagNode(node) && node.tagName === 'style'; } } /** * Style node that will be ignored by Mooltipage */ export class UncompiledStyleNode extends StyleNode { constructor(lang?: string, attributes?: Map<string, unknown>) { super(false, lang, attributes); } clone(deep = true, callback?: (oldNode: Node, newNode: Node) => void): UncompiledStyleNode { return NodeLogic.cloneUncompiledStyleNode(this, deep, callback); } /** * Returns true if a node is a UncompiledStyleNode * @param node Node to check */ static isUncompiledStyleNode(node: Node): node is UncompiledStyleNode { return StyleNode.isStyleNode(node) && !node.compiled; } } /** * Recognized binding mode for compiled <style> nodes. * Controls how the stylesheet will be linked to the page */ export enum StyleNodeBind { /** * Stylesheet will be placed in an inline <style> block in the page <head> section. */ HEAD = 'head', /** * Stylesheet will be placed in an external CSS file and referenced via a <link> tag. */ LINK = 'link' } /** * Parent class for specialized types of compiled <style> nodes */ export abstract class CompiledStyleNode extends StyleNode { /** * If this style node points to an external stylesheet. */ readonly isExternal: boolean; protected constructor(isExternal: boolean, bindType = StyleNodeBind.HEAD, skipFormat = false, lang?: string, attributes?: Map<string, unknown>) { super(true, lang, attributes); this.isExternal = isExternal; this.bind = bindType; this.skipFormat = skipFormat; } /** * The bind type of this <style> node */ get bind(): StyleNodeBind { return this.getAttribute('bind') as StyleNodeBind; } set bind(newBindType: StyleNodeBind) { this.setAttribute('bind', newBindType); } get skipFormat(): boolean { return this.hasAttribute('skip-format'); } set skipFormat(newSkipFormat: boolean) { this.setBooleanAttribute('skip-format', newSkipFormat); } /** * Returns true if a node is a CompiledStyleNode * @param node Node to check */ static isCompiledStyleNode(node: Node): node is CompiledStyleNode { return StyleNode.isStyleNode(node) && node.compiled; } } /** * <style> node that is compiled and contains an inline stylesheet */ export class InternalStyleNode extends CompiledStyleNode { constructor(bindType?: StyleNodeBind, skipFormat?: boolean, lang?: string, attributes?: Map<string, unknown>) { super(false, bindType, skipFormat, lang, attributes); } /** * Text content of the stylesheet. * Will never be null or undefined. * If no stylesheet is present, then an empty string is returned. */ get styleContent(): string { const styleTextNode = this.firstChildText; if (styleTextNode != null) { return styleTextNode.text; } else { return ''; } } clone(deep = true, callback?: (oldNode: Node, newNode: Node) => void): InternalStyleNode { return NodeLogic.cloneInternalStyleNode(this, deep, callback); } /** * Returns true if a node is an InternalStyleNode * @param node Node to check */ static isInternalStyleNode(node: Node): node is InternalStyleNode { return CompiledStyleNode.isCompiledStyleNode(node) && !node.isExternal; } } /** * <style> node that is compiled and points to an external stylesheet */ export class ExternalStyleNode extends CompiledStyleNode { constructor(src: string, bindType?: StyleNodeBind, skipFormat?: boolean, lang?: string, attributes?: Map<string, unknown>) { super(true, bindType, skipFormat, lang, attributes); this.src = src; } /** * Path to the stylesheet */ get src(): string { return this.getRequiredValueAttribute('src'); } set src(newSrc: string) { this.setRequiredValueAttribute('src', newSrc); } clone(deep = true, callback?: (oldNode: Node, newNode: Node) => void): ExternalStyleNode { return NodeLogic.cloneExternalStyleNode(this, deep, callback); } /** * Returns true if a node is an ExternalStyleNode * @param node Node to check */ static isExternalStyleNode(node: Node): node is ExternalStyleNode { return CompiledStyleNode.isCompiledStyleNode(node) && node.isExternal; } } /** * <script> tag. * Can be compiled (processed by Mooltipage) or not (ignored by mooltipage) */ export abstract class ScriptNode extends TagNode { protected constructor(compiled: boolean, attributes?: Map<string, unknown>) { super('script', attributes); this.setBooleanAttribute('compiled', compiled); } /** * If this <script> is compiled by mooltipage or ignored */ get compiled(): boolean { return this.hasAttribute('compiled'); } /** * Returns true if a node is a ScriptNode * @param node Node to check */ static isScriptNode(node: Node): node is ScriptNode { return TagNode.isTagNode(node) && node.tagName === 'script'; } } /** * Script node that will be ignored by Mooltipage */ export class UncompiledScriptNode extends ScriptNode { constructor(attributes?: Map<string, unknown>) { super(false, attributes); } clone(deep = true, callback?: (oldNode: Node, newNode: Node) => void): UncompiledScriptNode { return NodeLogic.cloneUncompiledScriptNode(this, deep, callback); } /** * Returns true if a node is an UncompiledScriptNode * @param node Node to check */ static isUncompiledScriptNode(node: Node): node is UncompiledScriptNode { return ScriptNode.isScriptNode(node) && !node.compiled; } } /** * Parent class for any specialize <script> nodes */ export abstract class CompiledScriptNode extends ScriptNode { /** * If this script is loaded from an external file or inline code */ readonly isExternal: boolean; protected constructor(isExternal: boolean, attributes?: Map<string, unknown>) { super(true, attributes); this.isExternal = isExternal; } /** * Returns true if a node is a CompiledScriptNode * @param node Node to check */ static isCompiledScriptNode(node: Node): node is CompiledScriptNode { return ScriptNode.isScriptNode(node) && node.compiled; } } /** * <script> node that is compiled from an inline script */ export class InternalScriptNode extends CompiledScriptNode { constructor(attributes?: Map<string, unknown>) { super(false, attributes); } /** * Contents of the script. * Will never be null or undefined. * If no script is included, then an empty string is returned */ get scriptContent(): string { const scriptText = this.firstChildText; if (scriptText != null) { return scriptText.text; } else { return ''; } } clone(deep = true, callback?: (oldNode: Node, newNode: Node) => void): InternalScriptNode { return NodeLogic.cloneInternalScriptNode(this, deep, callback); } /** * Returns true if a node is a InternalScriptNode * @param node Node to check */ static isInternalScriptNode(node: Node): node is InternalScriptNode { return CompiledScriptNode.isCompiledScriptNode(node) && !node.isExternal; } } /** * <script> node that is compiled from an external file */ export class ExternalScriptNode extends CompiledScriptNode { constructor(src: string, attributes?: Map<string, unknown>) { super(true, attributes); this.src = src; } /** * Path to source file */ get src(): string { return this.getRequiredValueAttribute('src'); } set src(newSrc: string) { this.setRequiredValueAttribute('src', newSrc); } clone(deep = true, callback?: (oldNode: Node, newNode: Node) => void): ExternalScriptNode { return NodeLogic.cloneExternalScriptNode(this, deep, callback); } /** * Returns true if a node is an ExternalScriptNode * @param node Node to check */ static isExternalScriptNode(node: Node): node is ExternalScriptNode { return CompiledScriptNode.isCompiledScriptNode(node) && node.isExternal; } } /** * Abstract base for the anchor tag (<a>) */ export abstract class AnchorNode extends TagNode { protected constructor(compiled: boolean, attributes?: Map<string, unknown>) { super('a', attributes); this.setBooleanAttribute('compiled', compiled); } /** * If true, then this anchor tag will be processed by Mooltipage. * If false, then it will be passed on unchanged. */ get compiled(): boolean { return this.hasAttribute('compiled'); } /** * Returns true if a node is an AnchorNode * @param node Node to check */ static isAnchorNode(node: Node): node is AnchorNode { return TagNode.isTagNode(node) && node.tagName === 'a'; } } /** * AnchorTag that will be ignored by Mooltipage */ export class UncompiledAnchorNode extends AnchorNode { constructor(attributes?: Map<string, unknown>) { super(false, attributes); } clone(deep = true, callback?: (oldNode: Node, newNode: Node) => void): UncompiledAnchorNode { return NodeLogic.cloneUncompiledAnchorNode(this, deep, callback); } /** * Returns true if a node is an UncompiledAnchorNode * @param node Node to check */ static isUncompiledAnchorNode(node: Node): node is UncompiledAnchorNode { return AnchorNode.isAnchorNode(node) && !node.compiled; } } /** * Resolution modes for the anchor tag. * This controls how the "href" attribute is handled. */ export enum AnchorNodeResolve { /** * Value will be resolved relative to the root fragment or page */ ROOT = 'root', /** * Value will be resolved based on the relative location of the current fragment */ LOCAL = 'local', /** * Value will be resolved relative to the base path (inPath) of the project. */ BASE = 'base', /** * No compile-time resolution will be performed. * Href will be left unchanged for the browser to handle. */ NONE = 'none' } /** * Parses a string value as an {@link AnchorNodeResolve}. * An undefined or null value will be default to {@link AnchorNodeResolve.NONE}. * * @param value String value to pass * @returns Parsed value of {@link value} or default. * @throws If {@link value} is not a valid value for {@link AnchorNodeResolve}. */ export function parseAnchorNodeResolve(value: string | undefined): AnchorNodeResolve { // this is AWFUL but I don't know a better way to do it with how TS implements enums switch (value) { case AnchorNodeResolve.ROOT: return AnchorNodeResolve.ROOT; case AnchorNodeResolve.LOCAL: return AnchorNodeResolve.LOCAL; case AnchorNodeResolve.BASE: return AnchorNodeResolve.BASE; case AnchorNodeResolve.NONE: case undefined: case null: return AnchorNodeResolve.NONE; default: throw new Error(`Invalid value of AnchorNodeResolve: "${ value }"`); } } /** * Anchor node that will be processed by Mooltipage */ export class CompiledAnchorNode extends AnchorNode { /** * Creates a new CompiledAnchorNode * @param href Value to link to * @param resolve Optional resolution style for {@link href} * @param attributes Optional extra attributes */ constructor(href: string, resolve?: AnchorNodeResolve, attributes?: Map<string, unknown>) { super(true, attributes); this.setAttribute('href', href); this.setAttribute('resolve', resolve ?? AnchorNodeResolve.NONE); } /** * Href value of this anchor node. */ get href(): string { return this.getRequiredValueAttribute('href'); } set href(newHref: string) { this.setAttribute('href', newHref); } /** * Resolution mode to use compiling {@link href}. * Defaults to {@link AnchorNodeResolve.NONE} */ get resolve(): AnchorNodeResolve { const resolveValue = this.getOptionalValueAttribute('resolve'); return parseAnchorNodeResolve(resolveValue); } /** * Converts this {@link CompiledAnchorNode} into an {@link UncompiledAnchorNode}. * The converted node will have the same attributes, excluding mooltipage directives. * @return an {@link UncompiledAnchorNode} matching this node. */ toUncompiled(): UncompiledAnchorNode { // clone attributes, but skip special attributes const newAttrs = new Map(Array.from(this.getAttributes().entries()).filter(entry => { const attrName = entry[0]; return attrName !== 'resolve' && attrName !== 'compiled'; })); // create uncompiled instance const uncompiled = new UncompiledAnchorNode(newAttrs); // copy child nodes uncompiled.appendChildren(this.childNodes); return uncompiled; } clone(deep = true, callback?: (oldNode: Node, newNode: Node) => void): CompiledAnchorNode { return NodeLogic.cloneCompiledAnchorNode(this, deep, callback); } /** * Returns true if a node is an CompiledAnchorNode * @param node Node to check */ static isCompiledAnchorNode(node: Node): node is CompiledAnchorNode { return AnchorNode.isAnchorNode(node) && node.compiled; } } /** * Supported whitespace-handling modes for <m-whitespace> */ export enum MWhitespaceNodeMode { /** * Markup is whitespace-sensitive */ SENSITIVE = 'sensitive', /** * Markup is not whitespace-sensitive */ INSENSITIVE = 'insensitive' } /** * Sets the whitespace handling for all descendent nodes of this node. * See {@link MWhitespaceNodeMode} for available whitespace modes */ export class MWhitespaceNode extends TagNode { /** * Create a new MWhitespaceNode * @param mode Whitespace handling mode to use * @param attributes Optional attributes */ constructor(mode = MWhitespaceNodeMode.SENSITIVE, attributes?: Map<string, unknown>) { super('m-whitespace', attributes); this.mode = mode; } /** * Whitespace handling mode to apply to descendants of this node */ get mode(): MWhitespaceNodeMode { const modeAttr = this.getAttribute('mode')?.toLowerCase(); switch (modeAttr) { case MWhitespaceNodeMode.INSENSITIVE: return MWhitespaceNodeMode.INSENSITIVE; case MWhitespaceNodeMode.SENSITIVE: case null: case undefined: return MWhitespaceNodeMode.SENSITIVE; default: throw new Error(`MWhitespaceNode: Invalid value of 'mode' attribute: ${ modeAttr }`); } } set mode(newMode: MWhitespaceNodeMode) { this.setAttribute('mode', newMode); } clone(deep = true, callback?: (oldNode: Node, newNode: Node) => void): MWhitespaceNode { return NodeLogic.cloneMWhitespaceNode(this, deep, callback); } /** * Returns true if a node is an MWhitespaceNode * @param node Node to check */ static isMWhitespaceNode(node: Node): node is MWhitespaceNode { return TagNode.isTagNode(node) && node.tagName === 'm-whitespace'; } } |