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 | 1x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 1x 1x 112x 1x 1x 65x 6x 59x 63x 63x 65x 40x 63x 78x 19x 63x 1x 446x 446x 1x 1x 1x 1x 1x 145x 145x 145x 145x 145x 145x 520x 19x 19x 225x 225x 272x 145x 105x 145x 253x 218x 218x 9x 7x 5x 7x 2x 1x 9x 218x 9x 9x 218x 218x 4x 35x 11x 11x 11x 24x 145x 5x 145x 1x 121x 204x 188x 188x 192x 192x 16x 1x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 5x 5x 5x 4x 4x 8x 1x 5x 5x 5x 5x 5x 112x 186x 186x 112x 22x 22x 19x 19x 57x 145x 88x 112x 5x 5x 5x 112x 112x 112x 112x 112x 112x 1x 1x 1x 1x 1x 112x 1x 112x 4x 4x 4x 4x 1x 1x 2x 4x 1x 92x 92x 1x 182x 182x 182x 182x 1x 174x 174x 174x 174x 1x 90x 90x 1x 156x 74x 82x 29x 53x 29x 24x 22x 2x 154x 154x 1x 74x 74x 74x 74x 53x 53x 74x 22x 22x 22x 4x 74x 1x 82x 33x 33x 49x 41x 41x 41x 41x 4x 4x 4x 4x 41x 41x 4x 4x 41x 41x 41x 41x 8x 8x 8x 8x 82x 1x 30x 30x 30x 30x 30x 12x 12x 11x 13x 30x 30x 30x 6x 6x 5x 5x 30x 24x 30x 47x 44x 3x 30x 30x 30x 1x 180x 2x 4x 2x 2x 2x 2x 2x 2x 2x 178x 178x 346x 346x 39x 33x 33x 307x 4x 4x 4x 303x 39x 39x 264x 11x 11x 253x 253x 124x 129x 129x 129x 129x 129x 1x 29x 29x 29x 29x 29x 29x 7x 9x 48x 25x 29x 29x 29x 29x 29x 165x 89x 13x 8x 8x 76x 2x 2x 1x 76x 76x 76x 89x 29x 85x 85x 60x 9x 9x 9x 51x 25x 85x 85x 85x 85x 47x 47x 38x 85x 85x 85x 9x 76x 29x 29x 76x 33x 85x 1x 84x 85x 20x 20x 60x 28x 4x 4x 4x 4x 24x 19x 19x 20x 20x 14x 85x 3x 3x 82x 85x 29x 1x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 12x 14x 11x 3x 12x 11x 11x 11x 3x 3x 3x 3x 3x 11x 12x 12x 12x 1x 11x 11x 11x 2x 2x 2x 9x 9x 9x 16x 9x 7x 7x 7x 9x 7x 2x 2x 9x 11x 22x 5x 5x 5x 22x 28x 28x 28x 28x 28x 26x 2x 2x 2x 2x 2x 2x 22x 22x 163x 163x 163x 1x 162x 1x 161x 146x 353x 28x 28x 15x 4x 4x 11x 11x 11x 22x 22x 22x 1x 5x 5x 5x 1x 13x 13x 13x 13x 13x 13x 13x 13x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 1x 1x 1x 54x 54x 54x 29x 29x 29x 25x 1x 44x 44x 44x 48x 48x 23x 25x 48x 44x 44x 44x 1x 364x 364x 372x 372x 364x 364x 1x 334x 326x 8x 8x 8x 8x 1x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 99x 53x 53x 152x 152x 152x 152x 152x 73x 16x 16x 16x 16x 39x 39x 39x 39x 9x 9x 9x 9x 9x 9x 9x 9x 73x 73x 73x 73x 73x 8x 8x 1x 1x 7x 4x 3x 3x 73x 1x 1x 1x 1x 73x 7x 7x 7x 73x 73x 73x 73x 73x 327x 73x 73x 208x 208x 152x 53x 53x 53x 53x 1x | /*! * VisualEditor UserInterface DiffElement class. * * @copyright See AUTHORS.txt * @license The MIT License (MIT); see LICENSE.txt */ /** * Creates a ve.ui.DiffElement object. * * @class * @extends OO.ui.Element * * @constructor * @param {ve.dm.VisualDiff} [visualDiff] Diff to visualize * @param {Object} [config] */ ve.ui.DiffElement = function VeUiDiffElement( visualDiff, config ) { const diff = visualDiff.diff; // Parent constructor ve.ui.DiffElement.super.call( this, config ); this.elementId = 0; this.$overlays = $( '<div>' ).addClass( 've-ui-diffElement-overlays' ); this.$content = $( '<div>' ).addClass( 've-ui-diffElement-content' ); this.$messages = $( '<div>' ).addClass( 've-ui-diffElement-messages' ); this.$document = $( '<div>' ).addClass( 've-ui-diffElement-document' ); this.$sidebar = $( '<div>' ).addClass( 've-ui-diffElement-sidebar' ); this.descriptions = new ve.ui.ChangeDescriptionsSelectWidget(); this.descriptions.connect( this, { highlight: 'onDescriptionsHighlight' } ); // Set to an empty array before a series of diff computations to collect descriptions. // Set back to null after collecting values. this.descriptionItemsStack = null; this.$document.on( { mousemove: this.onDocumentMouseMove.bind( this ) } ); this.renderDiff( diff.docDiff, diff.internalListDiff, diff.metaListDiff, visualDiff.newDoc.getHtmlDocument() ); if ( visualDiff.timedOut ) { const warning = new OO.ui.MessageWidget( { type: 'warning', classes: [ 've-ui-diffElement-warning' ], label: ve.msg( 'visualeditor-diff-timed-out' ) } ); this.$messages.append( warning.$element ); } // DOM this.$element .append( this.$messages, this.$content.append( this.$document, this.$overlays ), this.$sidebar.append( this.descriptions.$element ) ) .addClass( 've-ui-diffElement' ); }; /* Inheritance */ OO.inheritClass( ve.ui.DiffElement, OO.ui.Element ); /* Static methods */ /** * Compare attribute sets between two elements * * @param {Object} oldAttributes Old attributes * @param {Object} newAttributes New attributes * @return {Object} Keyed set of attributes */ ve.ui.DiffElement.static.compareAttributes = function ( oldAttributes, newAttributes ) { function compareKeys( a, b ) { if ( typeof a === 'object' && typeof b === 'object' ) { return ve.compare( a, b ); } else { return a === b; } } const attributeChanges = {}; for ( const key in oldAttributes ) { if ( !compareKeys( oldAttributes[ key ], newAttributes[ key ] ) ) { attributeChanges[ key ] = { from: oldAttributes[ key ], to: newAttributes[ key ] }; } } for ( const key in newAttributes ) { if ( !Object.prototype.hasOwnProperty.call( oldAttributes, key ) && newAttributes[ key ] !== undefined ) { attributeChanges[ key ] = { from: oldAttributes[ key ], to: newAttributes[ key ] }; } } return attributeChanges; }; /** * Get the original linear data from a node * * @param {ve.dm.Node} node Node * @return {Array} Linear data */ ve.ui.DiffElement.static.getDataFromNode = function ( node ) { const doc = node.getRoot().getDocument(); return doc.getData( node.getOuterRange() ); }; /* Methods */ /** * Get a diff element in the document from its elementId * * @param {number} elementId ID * @return {jQuery} Element */ ve.ui.DiffElement.prototype.getDiffElementById = function ( elementId ) { return this.$document.find( '[data-diff-id=' + elementId + ']' ); }; /** * Handle description item hightlight events * * @param {OO.ui.OptionWidget} item Description item */ ve.ui.DiffElement.prototype.onDescriptionsHighlight = function ( item ) { if ( this.lastItem ) { this.getDiffElementById( this.lastItem.getData() ).css( 'outline', '' ); this.$overlays.empty(); } if ( item ) { const overlayRect = this.$overlays[ 0 ].getBoundingClientRect(); const elementRects = ve.ce.FocusableNode.static.getRectsForElement( this.getDiffElementById( item.getData() ), overlayRect ).rects; for ( let i = 0, l = elementRects.length; i < l; i++ ) { this.$overlays.append( $( '<div>' ).addClass( 've-ui-diffElement-highlight' ).css( { top: elementRects[ i ].top, left: elementRects[ i ].left, width: elementRects[ i ].width, height: elementRects[ i ].height } ) ); } this.lastItem = item; } }; /** * Handle document mouse move events * * @param {jQuery.Event} e Mouse move event */ ve.ui.DiffElement.prototype.onDocumentMouseMove = function ( e ) { const elementId = $( e.target ).closest( '[data-diff-id]' ).attr( 'data-diff-id' ); if ( elementId !== undefined ) { this.descriptions.highlightItem( this.descriptions.findItemFromData( +elementId ) ); } else { this.descriptions.highlightItem(); } }; /** * Reposition the description items so they are not above their position in the document */ ve.ui.DiffElement.prototype.positionDescriptions = function () { this.descriptions.getItems().forEach( ( item ) => { item.$element.css( 'margin-top', '' ); const itemRect = item.$element[ 0 ].getBoundingClientRect(); const $element = this.getDiffElementById( item.getData() ); if ( !$element.length ) { // Changed element isn't visible - probably shouldn't happen return; } const elementRect = ve.ce.FocusableNode.static.getRectsForElement( $element ).boundingRect; // elementRect can currently be null for meta items, e.g. <link> if ( elementRect && elementRect.top > itemRect.top ) { item.$element.css( 'margin-top', elementRect.top - itemRect.top - 5 ); } } ); this.$document.css( 'min-height', this.$sidebar.height() ); }; /** * Process a diff queue, skipping over sequential nodes with no changes * * @param {Array[]} queue Diff queue * @return {Array.<Array|null>} */ ve.ui.DiffElement.prototype.processQueue = function processQueue( queue ) { let hasChanges = false, lastItemSpacer = false, needsSpacer = false, headingContext = null, headingContextSpacer = false; const processedQueue = []; function isUnchanged( item ) { return !item || ( item[ 2 ] === 'none' && !item[ 3 ] ); } function addSpacer() { processedQueue.push( null ); lastItemSpacer = true; } function addItem( item ) { processedQueue.push( item ); lastItemSpacer = false; } function isHeading( item ) { switch ( item[ 0 ] ) { case 'getNodeData': case 'getNodeElements': return item[ 1 ] instanceof ve.dm.HeadingNode; case 'getChangedNodeData': case 'getChangedNodeElements': return item[ 3 ] instanceof ve.dm.HeadingNode; } } for ( let k = 0, klen = queue.length; k < klen; k++ ) { if ( !isUnchanged( queue[ k - 1 ] ) || !isUnchanged( queue[ k ] ) || !isUnchanged( queue[ k + 1 ] ) ) { hasChanges = true; if ( headingContext ) { // Don't render headingContext if current or next node is a heading if ( !isHeading( queue[ k ] ) && !isHeading( queue[ k + 1 ] ) ) { if ( headingContextSpacer ) { addSpacer(); } addItem( headingContext ); } else if ( isHeading( queue[ k + 1 ] ) ) { // Skipping the context header becuase the next node is a heading // so reinstate the spacer. needsSpacer = true; } headingContext = null; } if ( needsSpacer && !lastItemSpacer ) { addSpacer(); needsSpacer = false; } addItem( queue[ k ] ); if ( isHeading( queue[ k ] ) ) { // Heading was rendered, no need to show it as context headingContext = null; } } else { // Heading skipped, maybe show as context later if ( isHeading( queue[ k ] ) ) { headingContext = isUnchanged( queue[ k ] ) ? queue[ k ] : null; headingContextSpacer = needsSpacer; needsSpacer = false; } else { needsSpacer = true; } } } // Trailing spacer if ( hasChanges && needsSpacer && !lastItemSpacer ) { addSpacer(); } return processedQueue; }; /** * @param {Array.<Array|null>} queue Diff queue * @param {HTMLElement} parentNode Parent node to render to * @param {HTMLElement} spacerNode Spacer node template */ ve.ui.DiffElement.prototype.renderQueue = function ( queue, parentNode, spacerNode ) { queue.forEach( ( item ) => { if ( item ) { const elements = this[ item[ 0 ] ].apply( this, item.slice( 1 ) ); while ( elements.length ) { parentNode.appendChild( parentNode.ownerDocument.adoptNode( elements[ 0 ] ) ); elements.shift(); } } else { parentNode.appendChild( parentNode.ownerDocument.adoptNode( spacerNode.cloneNode( true ) ) ); } } ); }; /** * Render the diff * * @param {Object} diff Object describing the diff * @param {Object} internalListDiff Object describing the diff of the internal list * @param {Object} metaListDiff Object describing the diff of the meta list * @param {HTMLDocument} newHtmlDocument HTML document of context, for resolving attributes */ ve.ui.DiffElement.prototype.renderDiff = function ( diff, internalListDiff, metaListDiff, newHtmlDocument ) { const documentNode = this.$document[ 0 ], diffQueue = []; let internalListDiffQueue = []; const documentSpacerNode = document.createElement( 'div' ); documentSpacerNode.setAttribute( 'class', 've-ui-diffElement-spacer' ); documentSpacerNode.appendChild( document.createTextNode( '⋮' ) ); const internalListSpacerNode = document.createElement( 'li' ); internalListSpacerNode.setAttribute( 'class', 've-ui-diffElement-internalListSpacer' ); internalListSpacerNode.appendChild( documentSpacerNode.cloneNode( true ) ); const referencesListDiffs = {}; Object.keys( internalListDiff.groups ).forEach( ( group ) => { const referencesListContainer = document.createElement( 'ol' ); const internalListGroup = internalListDiff.groups[ group ]; this.iterateDiff( internalListGroup, { insert: function ( newNode, newIndex ) { internalListDiffQueue.push( [ 'getInternalListNodeElements', newNode, 'insert', null, newIndex ] ); }, remove: function ( oldNode, oldIndex ) { internalListDiffQueue.push( [ 'getInternalListNodeElements', oldNode, 'remove', null, oldIndex ] ); }, move: function ( newNode, move, newIndex ) { internalListDiffQueue.push( [ 'getInternalListNodeElements', newNode, 'none', move, newIndex ] ); }, changed: function ( nodeDiff, oldNode, newNode, move, oldIndex, newIndex ) { internalListDiffQueue.push( [ 'getInternalListChangedNodeElements', nodeDiff, oldNode, newNode, move, newIndex ] ); } } ); this.descriptionItemsStack = []; this.renderQueue( this.processQueue( internalListDiffQueue ), referencesListContainer, internalListSpacerNode ); referencesListDiffs[ group ] = { element: referencesListContainer, action: internalListGroup.changes ? 'change' : 'none', descriptionItemsStack: this.descriptionItemsStack, shown: false }; this.descriptionItemsStack = null; internalListDiffQueue = []; } ); this.descriptionItemsStack = []; let referencesListDiff; function handleRefList( node, move ) { Iif ( node.type === 'mwReferencesList' && ( referencesListDiff = referencesListDiffs[ node.element.attributes.listGroup ] ) ) { // New node is a references list node. If a reference has // changed, the references list nodes appear unchanged, // because of how the internal list works. However, we // already have the HTML for the diffed references list, // (which contains details of changes if there are any) so // just get that. diffQueue.push( [ 'getRefListNodeElements', referencesListDiff.element, referencesListDiff.action, move, referencesListDiff.descriptionItemsStack ] ); referencesListDiff.shown = true; return true; } return false; } this.iterateDiff( diff, { insert: function ( newNode ) { Eif ( !handleRefList( newNode, null ) ) { diffQueue.push( [ 'getNodeElements', newNode, 'insert', null ] ); } }, remove: function ( oldNode ) { Eif ( !handleRefList( oldNode, null ) ) { diffQueue.push( [ 'getNodeElements', oldNode, 'remove', null ] ); } }, move: function ( newNode, move ) { diffQueue.push( [ 'getNodeElements', newNode, 'none', move ] ); }, preChanged: function ( oldNode, newNode, move ) { return handleRefList( newNode, move ); }, changed: function ( nodeDiff, oldNode, newNode, move ) { diffQueue.push( [ 'getChangedNodeElements', nodeDiff, oldNode, newNode, move ] ); } } ); // Show any ref list diffs that weren't picked up by the main diff loop above, // e.g. during a section diff. Object.keys( referencesListDiffs ).forEach( ( group ) => { referencesListDiff = referencesListDiffs[ group ]; Eif ( !referencesListDiff.shown ) { diffQueue.push( [ 'getRefListNodeElements', referencesListDiff.element, referencesListDiff.action, null, referencesListDiff.descriptionItemsStack ] ); } } ); this.renderQueue( this.processQueue( diffQueue ), documentNode, documentSpacerNode ); this.renderMetaListDiff( metaListDiff, documentNode, documentSpacerNode ); this.descriptions.addItems( this.descriptionItemsStack ); this.descriptionItemsStack = null; ve.resolveAttributes( documentNode, newHtmlDocument, ve.dm.Converter.static.computedAttributes ); if ( !documentNode.children.length ) { const noChanges = document.createElement( 'div' ); noChanges.setAttribute( 'class', 've-ui-diffElement-no-changes' ); noChanges.appendChild( document.createTextNode( ve.msg( 'visualeditor-diff-no-changes' ) ) ); documentNode.innerHTML = ''; documentNode.appendChild( noChanges ); } this.$element .toggleClass( 've-ui-diffElement-hasDescriptions', !this.descriptions.isEmpty() ); }; ve.ui.DiffElement.prototype.renderMetaListDiff = function ( metaListDiff, documentNode, documentSpacerNode ) { Object.keys( metaListDiff ).forEach( ( group ) => { const handler = ve.ui.metaListDiffRegistry.lookup( group ); Eif ( handler ) { const diffQueue = []; this.iterateDiff( metaListDiff[ group ], { insert: ( newNode ) => { diffQueue.push( [ 'getNodeElements', newNode, 'insert', null ] ); }, remove: ( oldNode ) => { diffQueue.push( [ 'getNodeElements', oldNode, 'remove', null ] ); }, move: ( newNode, move ) => { diffQueue.push( [ 'getNodeElements', newNode, 'none', move ] ); }, changed: ( nodeDiff, oldNode, newNode, move ) => { diffQueue.push( [ 'getChangedNodeElements', nodeDiff, oldNode, newNode, move ] ); } } ); handler( this, diffQueue, documentNode, documentSpacerNode ); } } ); }; /** * Get the HTML for the diff of a removed, inserted, or unchanged-but-moved node. * * @param {ve.dm.Node} node The node being diffed. Will be from the old * document if it has been removed, or the new document if it has been inserted * or moved * @param {string} action 'remove', 'insert' or, if moved, 'none' * @param {string|null} move 'up' or 'down' if the node has moved * @return {HTMLElement[]} Elements (not owned by window.document) */ ve.ui.DiffElement.prototype.getNodeElements = function ( node, action, move ) { const nodeData = this.getNodeData( node, action, move ); return this.wrapNodeData( node.getRoot().getDocument(), nodeData ); }; /** * Get the DOM from linear data and wrap it for the diff. * * @param {ve.dm.Document} nodeDoc Node's document model * @param {Array} nodeData Linear data for the diff * @return {HTMLElement[]} Elements (not owned by window.document) */ ve.ui.DiffElement.prototype.wrapNodeData = function ( nodeDoc, nodeData ) { const documentSlice = nodeDoc.cloneWithData( nodeData ); documentSlice.getStore().merge( nodeDoc.getStore() ); const nodeElements = ve.dm.converter.getDomFromModel( documentSlice, ve.dm.Converter.static.PREVIEW_MODE ).body; // Convert NodeList to real array return Array.prototype.slice.call( nodeElements.childNodes ); }; /** * Get the linear data for the diff of a removed, inserted, or * unchanged-but-moved node. * * @param {ve.dm.Node} node * @param {string} action 'remove', 'insert' or, if moved, 'none' * @param {string|null} move 'up' or 'down' if the node has moved * @return {Array} Linear Data */ ve.ui.DiffElement.prototype.getNodeData = function ( node, action, move ) { // Get the linear model for the node const nodeData = this.constructor.static.getDataFromNode( node ); // Add the classes to the outer element this.addAttributesToElement( nodeData, 0, { 'data-diff-action': action } ); this.markMove( move, nodeData ); return nodeData; }; /** * Get the HTML for the diff of a node that has been changed. * * @param {Object} diff Object describing the diff * @param {ve.dm.Node} oldNode Node from the old document * @param {ve.dm.Node} newNode Corresponding node from the new document * @param {string|null} move 'up' or 'down' if the node has moved * @return {HTMLElement[]} Elements (not owned by window.document) */ ve.ui.DiffElement.prototype.getChangedNodeElements = function ( diff, oldNode, newNode, move ) { const nodeData = this.getChangedNodeData( diff, oldNode, newNode, move ); return this.wrapNodeData( newNode.getRoot().getDocument(), nodeData ); }; /** * Get the linear data for the diff of a node that has been changed. * * @param {Object} diff Object describing the diff * @param {ve.dm.Node} oldNode Node from the old document * @param {ve.dm.Node} newNode Corresponding node from the new document * @param {string|null} move 'up' or 'down' if the node has moved * @param {boolean} [noTreeDiff] Don't perform a tree diff of the nodes (used internally to avoid recursion) * @return {Array|boolean} Linear data for the diff, or false */ ve.ui.DiffElement.prototype.getChangedNodeData = function ( diff, oldNode, newNode, move, noTreeDiff ) { let nodeData; // Choose the appropriate method for the type of node if ( newNode.isDiffedAsLeaf() ) { nodeData = this.getChangedLeafNodeData( newNode, diff, move ); } else if ( newNode.isDiffedAsList() ) { nodeData = this.getChangedListNodeData( newNode, diff ); } else if ( newNode.isDiffedAsDocument() ) { nodeData = this.getChangedDocListData( newNode, diff ); } else if ( !noTreeDiff ) { nodeData = this.getChangedTreeNodeData( oldNode, newNode, diff ); } else { return false; } this.markMove( move, nodeData ); return nodeData; }; /** * Get the linear data for the diff of a leaf-like node that has been changed. * * @param {ve.dm.Node} newNode Corresponding node from the new document * @param {Object} diff Object describing the diff * @return {Array} Linear data for the diff */ ve.ui.DiffElement.prototype.getChangedLeafNodeData = function ( newNode, diff ) { const nodeData = this.constructor.static.getDataFromNode( newNode ), linearDiff = diff.linearDiff, attributeChange = diff.attributeChange; if ( linearDiff ) { // If there is a content change, splice it in const annotatedData = this.annotateNode( linearDiff, newNode ); ve.batchSplice( nodeData, 1, newNode.length, annotatedData ); } if ( attributeChange ) { // If there is no content change, just add change class this.addAttributesToElement( nodeData, 0, { 'data-diff-action': 'structural-change' } ); const item = this.compareNodeAttributes( nodeData, 0, attributeChange ); if ( item ) { this.descriptionItemsStack.push( item ); } } return nodeData; }; /** * Append list item * * @private * @param {Array} diffData * @param {number} insertIndex * @param {ve.dm.ListNode} listNode * @param {Array} listNodeData List node opening * @param {Array} listItemData * @param {number} depthChange * @return {number} */ ve.ui.DiffElement.prototype.appendListItem = function ( diffData, insertIndex, listNode, listNodeData, listItemData, depthChange ) { if ( depthChange === 0 ) { // Current list item belongs to the same list as the previous list item ve.batchSplice( diffData, insertIndex, 0, listItemData ); insertIndex += listItemData.length; } else if ( depthChange > 0 ) { // Begin a new nested list, with this node's ancestor nodes const linearData = []; linearData.unshift( listNodeData[ 0 ] ); let k, klen; const doc = listNode.getRoot().getDocument(); // Nested list may be nested by multiple levels for ( k = 0, klen = depthChange - 1; k < klen; k++ ) { const listItemNode = listNode.parent; linearData.unshift( doc.data.data[ listItemNode.getOuterRange().from ] ); listNode = listItemNode.parent; linearData.unshift( doc.data.data[ listNode.getOuterRange().from ] ); } // Splice in the content, and splice that into the diff data ve.batchSplice( linearData, linearData.length, 0, listItemData ); for ( k = 0, klen = depthChange - 1; k < klen; k++ ) { linearData.push( { type: '/list' } ); linearData.push( { type: '/listItem' } ); } linearData.push( { type: '/list' } ); // Splice into previous list item insertIndex -= 1; ve.batchSplice( diffData, insertIndex, 0, linearData ); // Adjust the insertIndex to be at the end of this content insertIndex += 2 * ( depthChange - 1 ) + 1 + listItemData.length; } else Eif ( depthChange < 0 ) { // Skip over close elements to get out of nested list(s) insertIndex += 2 * -depthChange; // Splice in the data and adjust the insert index ve.batchSplice( diffData, insertIndex, 0, listItemData ); insertIndex += listItemData.length; } return insertIndex; }; /** * Get the linear data for a document-like node that has been changed * * @param {ve.dm.Node} newDoclistNode Node from new document * @param {Object} diff Object describing the diff * @param {boolean} neverProcess Never process the diffQueue (always show the whole document) * @return {Array} Linear data for the diff */ ve.ui.DiffElement.prototype.getChangedDocListData = function ( newDoclistNode, diff, neverProcess ) { const diffData = []; let diffQueue = []; const spacerData = [ { type: 'div' }, '⋮', { type: '/div' } ]; this.addAttributesToElement( spacerData, 0, { class: 've-ui-diffElement-spacer' } ); this.iterateDiff( diff, { insert: ( newNode ) => { diffQueue.push( [ 'getNodeData', newNode, 'insert', null ] ); }, remove: ( oldNode ) => { diffQueue.push( [ 'getNodeData', oldNode, 'remove', null ] ); }, move: ( newNode, move ) => { diffQueue.push( [ 'getNodeData', newNode, 'none', move ] ); }, changed: ( nodeDiff, oldNode, newNode, move ) => { diffQueue.push( [ 'getChangedNodeData', nodeDiff, oldNode, newNode, move ] ); } } ); let hasAttributeChanges = false; const newDoclistNodeData = this.constructor.static.getDataFromNode( newDoclistNode ); if ( diff.attributeChange ) { const item = this.compareNodeAttributes( newDoclistNodeData, 0, diff.attributeChange ); if ( item ) { this.descriptionItemsStack.push( item ); hasAttributeChanges = true; } } // When the doc cotainer has attribute changes, show the whole node. // Otherwise use processQueue to filter out unchanged context if ( !neverProcess && !hasAttributeChanges ) { diffQueue = this.processQueue( diffQueue ); } diffQueue.forEach( ( diffItem ) => { if ( diffItem ) { ve.batchPush( diffData, this[ diffItem[ 0 ] ].apply( this, diffItem.slice( 1 ) ) ); } else { ve.batchPush( diffData, spacerData.slice() ); } } ); // Wrap in newDocListNode diffData.unshift( newDoclistNodeData[ 0 ] ); diffData.push( newDoclistNodeData[ newDoclistNodeData.length - 1 ] ); return diffData; }; /** * Iterate over a diff object and run more meaningful callbacks * * @param {Object|Array} diff Diff object, or array (InternalListDiff) * @param {Object} callbacks Callbacks * @param {Function} callbacks.insert Node inserted, arguments: * {ve.dm.Node} newNode * {number} newIndex * @param {Function} callbacks.remove Node removed, arguments: * {ve.dm.Node} oldNode * {number} oldIndex * @param {Function} callbacks.move Node moved, arguments: * {ve.dm.Node} newNode * {number} newIndex * {string|null} move * @param {Function} callbacks.changed Node changed, arguments: * {Object} nodeDiff * {ve.dm.Node} oldNode * {ve.dm.Node} newNode * {number} oldIndex * {number} newIndex * {string|null} move */ ve.ui.DiffElement.prototype.iterateDiff = function ( diff, callbacks ) { // Internal list diffs set 'diff' to a number to shortcut computing the list diff // for fully inserted/removed lists. // TODO: Remove this special case and use a regular list diff if ( Array.isArray( diff ) ) { diff.forEach( ( item ) => { let node; switch ( item.diff ) { case 1: node = diff.newList.children[ item.nodeIndex ]; callbacks.insert( node, item.indexOrder ); break; case -1: node = diff.oldList.children[ item.nodeIndex ]; callbacks.remove( node, item.indexOrder ); break; } } ); return; } const len = Math.max( diff.oldNodes.length, diff.newNodes.length ); for ( let i = 0, j = 0; i < len || j < len; i++, j++ ) { const move = diff.moves[ j ] === 0 ? null : diff.moves[ j ]; if ( diff.oldNodes[ i ] === undefined ) { // Everything else in the new doc list is an insert while ( j < diff.newNodes.length ) { callbacks.insert( diff.newNodes[ j ], j ); j++; } } else if ( diff.newNodes[ j ] === undefined ) { // Everything else in the old doc is a remove while ( i < diff.oldNodes.length ) { callbacks.remove( diff.oldNodes[ i ], i ); i++; } } else if ( diff.remove.indexOf( i ) !== -1 ) { // The old node is a remove. Decrement the new node index // to compare the same new node to the next old node callbacks.remove( diff.oldNodes[ i ], i ); j--; } else if ( diff.insert.indexOf( j ) !== -1 ) { // The new node is an insert. Decrement the old node index // to compare the same old node to the next new node callbacks.insert( diff.newNodes[ j ], j ); i--; } else Iif ( callbacks.preChanged && callbacks.preChanged( diff.oldNodes[ i ], diff.newNodes[ j ], move, i, j ) ) { // preChanged ran } else if ( typeof diff.newToOld[ j ] === 'number' ) { // The old and new node are exactly the same callbacks.move( diff.newNodes[ j ], move, j ); } else { const oldNodeIndex = diff.newToOld[ j ].node; const oldNode = diff.oldNodes[ oldNodeIndex ]; const newNode = diff.newNodes[ diff.oldToNew[ oldNodeIndex ].node ]; const nodeDiff = diff.oldToNew[ oldNodeIndex ].diff; // The new node is modified from the old node callbacks.changed( nodeDiff, oldNode, newNode, move, i, j ); } } }; /** * Get the linear data for the diff of a list-like node that has been changed. * * @param {ve.dm.Node} newListNode Corresponding node from the new document * @param {Object} diff Object describing the diff * @return {Array} Linear data for the diff */ ve.ui.DiffElement.prototype.getChangedListNodeData = function ( newListNode, diff ) { const diffData = []; // These will be adjusted for each item let insertIndex = 1; let depth = -1; const listNodesInfoCache = new Map(); const listDiffItems = []; this.iterateDiff( diff, { insert: ( newNode, index ) => { listDiffItems.push( { node: newNode, metadata: diff.newList.metadata[ index ], action: 'insert' } ); }, remove: ( oldNode, index ) => { listDiffItems.push( { node: oldNode, metadata: diff.oldList.metadata[ index ], action: 'remove' } ); }, move: ( newNode, move, index ) => { listDiffItems.push( { node: newNode, metadata: diff.newList.metadata[ index ], action: 'none', move: move } ); }, changed: ( nodeDiff, oldNode, newNode, move, oldIndex, newIndex ) => { listDiffItems.push( { node: newNode, metadata: diff.newList.metadata[ newIndex ], diff: nodeDiff, move: move } ); } } ); const processedListDiffItems = []; let lastItemSpacer = false; let lastShownDepth = 0; const lastItemAtDepth = {}; listDiffItems.forEach( ( item, i ) => { function isUnchanged( queueItem ) { return !queueItem || ( queueItem.action === 'none' && !queueItem.move ); } if ( isUnchanged( item ) && isUnchanged( listDiffItems[ i - 1 ] ) && isUnchanged( listDiffItems[ i + 1 ] ) ) { if ( !lastItemSpacer ) { processedListDiffItems.push( { node: null, metadata: item.metadata, action: 'none' } ); lastItemSpacer = true; } } else { while ( lastShownDepth < item.metadata.depth - 1 ) { lastShownDepth++; if ( lastItemAtDepth[ lastShownDepth ] ) { processedListDiffItems.push( { node: null, metadata: lastItemAtDepth[ lastShownDepth ].metadata, action: 'none' } ); } } processedListDiffItems.push( item ); lastItemSpacer = false; lastShownDepth = item.metadata.depth; } lastItemAtDepth[ item.metadata.depth ] = item; } ); // Splice in each item with its diff annotations processedListDiffItems.forEach( ( item ) => { let contentData; let isSpacer = false; if ( !item.diff ) { if ( !item.node ) { contentData = [ { type: 'paragraph' }, '…', { type: '/paragraph' } ]; isSpacer = true; this.addAttributesToElement( contentData, 0, { 'data-diff-action': 'none' } ); } else { // Get the linear data for the list item's content contentData = this.getNodeData( item.node, item.action, item.move || null ); } } else { // Item is changed. Get the linear data for the diff contentData = this.getChangedNodeData( item.diff, null, item.node, item.move || null ); } // Calculate the change in depth const newDepth = item.metadata.depth; const depthChange = newDepth - depth; // Get linear data. Also get list node, since may need ancestors const listNode = item.metadata.listNode; let listNodeInfo; if ( !listNodesInfoCache.has( listNode ) ) { // Only re-fetch list node data once per list listNodeInfo = { data: [ this.constructor.static.getDataFromNode( listNode )[ 0 ] ], changeDone: false }; listNodesInfoCache.set( listNode, listNodeInfo ); } else { listNodeInfo = listNodesInfoCache.get( listNode ); } const listItemNode = item.metadata.listItem; // Get linear data of list item let listItemData = this.constructor.static.getDataFromNode( listItemNode ); if ( isSpacer ) { this.addAttributesToElement( listItemData, 0, { 'data-diff-list-spacer': '' } ); } else { // TODO: Make this a node property, instead of a magic attribute if ( listNode.getAttribute( 'style' ) === 'number' ) { // Manually number list items for <ol>'s which contain removals // TODO: Consider if the <ol> contains a `start` attribute (not currently handled by DM) const indexInOwnList = listNode.children.indexOf( listItemNode ); this.addAttributesToElement( listItemData, 0, { value: indexInOwnList + 1 } ); } if ( item.action === 'none' && !item.move ) { this.addAttributesToElement( listItemData, 0, { 'data-diff-list-none': '' } ); } } // e.g. AlienBlockNode, content node is same as 'listItem', so don't duplicate content if ( item.node === listItemNode ) { listItemData = contentData; } else { ve.batchSplice( listItemData, 1, listItemData.length - 2, contentData ); } // Check for attribute changes if ( item.diff && item.diff.attributeChange ) { const attributeChange = { oldAttributes: {}, newAttributes: {} }; [ 'listNodeAttributeChange', 'depthChange', 'listItemAttributeChange' ].forEach( ( listChangeType ) => { if ( item.diff.attributeChange[ listChangeType ] ) { if ( listChangeType === 'listNodeAttributeChange' && depthChange > 0 ) { const change = this.compareNodeAttributes( listNodeInfo.data, 0, item.diff.attributeChange[ listChangeType ] ); Eif ( change ) { this.descriptionItemsStack.push( change ); listNodeInfo.changeDone = true; } } else if ( listChangeType !== 'listNodeAttributeChange' || !listNodeInfo.changeDone ) { ve.extendObject( attributeChange.oldAttributes, item.diff.attributeChange[ listChangeType ].oldAttributes ); ve.extendObject( attributeChange.newAttributes, item.diff.attributeChange[ listChangeType ].newAttributes ); } } } ); const listItemChange = this.compareNodeAttributes( listItemData, 0, attributeChange ); if ( listItemChange ) { this.descriptionItemsStack.push( listItemChange ); } } if ( item.metadata.isContinued ) { ve.batchSplice( diffData, insertIndex - 1, 0, contentData ); insertIndex += contentData.length; } else { // Record the index to splice in the next list item data into the diffData insertIndex = this.appendListItem( diffData, insertIndex, listNode, listNodeInfo.data, listItemData, depthChange ); } depth = newDepth; } ); return diffData; }; /** * Get the linear data for the diff of a tree-like node that has been changed. * Any node that is not leaf-like or list-like is treated as tree-like. * * @param {ve.dm.Node} oldTreeNode Node from the old document * @param {ve.dm.Node} newTreeNode Corresponding node from the new document * @param {Object} diff Object describing the diff * @return {Array} Linear data for the diff */ ve.ui.DiffElement.prototype.getChangedTreeNodeData = function ( oldTreeNode, newTreeNode, diff ) { const nodeData = this.constructor.static.getDataFromNode( newTreeNode ), nodeRange = newTreeNode.getOuterRange(), treeDiff = diff.treeDiff, diffInfo = diff.diffInfo, oldNodes = diff.oldTreeOrderedNodes, newNodes = diff.newTreeOrderedNodes, correspondingNodes = diff.correspondingNodes, structuralRemoves = [], highestRemovedAncestors = {}; /** * Splice in the removed data for the subtree rooted at this node, from the old * document. * * @param {number} nodeIndex The index of this node in the subtree rooted at * this document child */ const highlightRemovedNode = ( nodeIndex ) => { const findRemovedAncestor = ( n ) => { if ( !n.parent || structuralRemoves.indexOf( n.parent.index ) === -1 ) { return n.index; } else { return findRemovedAncestor( n.parent ); } }; const getRemoveData = ( n, index ) => { const data = this.constructor.static.getDataFromNode( n.node ); this.addAttributesToElement( data, 0, { 'data-diff-action': 'remove' } ); while ( n && n.index !== index ) { n = n.parent; const tempData = this.constructor.static.getDataFromNode( n.node ); data.unshift( tempData[ 0 ] ); data.push( tempData[ tempData.length - 1 ] ); this.addAttributesToElement( data, 0, { 'data-diff-action': 'structural-remove' } ); } return data; }; const orderedNode = oldNodes[ nodeIndex ]; const node = orderedNode.node; if ( node.isDiffedAsTree() && node.hasChildren() ) { // Record that the node has been removed, but don't display it, for now // TODO: describe the change for the attribute diff structuralRemoves.push( nodeIndex ); } else { // Display the removed node, and all its ancestors, up to the first ancestor that // hasn't been removed. const highestRemovedAncestor = oldNodes[ findRemovedAncestor( orderedNode ) ]; const removeData = getRemoveData( orderedNode, highestRemovedAncestor.index ); let insertIndex; // Work out where to insert the removed subtree if ( highestRemovedAncestor.index in highestRemovedAncestors ) { // The highest removed ancestor has already been spliced into nodeData, so remove // it from this subtree and splice the rest of this subtree in removeData.shift(); removeData.pop(); insertIndex = highestRemovedAncestors[ highestRemovedAncestor.index ]; } else Iif ( !highestRemovedAncestor.parent ) { // If this node is a child of the document node, then it won't have a "previous // node" (see below), in which case, insert it just before its corresponding // node in the new document. insertIndex = newNodes[ correspondingNodes.oldToNew[ highestRemovedAncestor.index ] ] .node.getOuterRange().from - nodeRange.from; } else { // Find the node that corresponds to the "previous node" of this node. The // "previous node" is either: // - the rightmost left sibling that corresponds to a node in the new document // - or if there isn't one, then this node's parent (which must correspond to // a node in the new document, or this node would have been marked already // processed) const siblingNodes = highestRemovedAncestor.parent.children; let newPreviousNodeIndex; for ( let x = 0, xlen = siblingNodes.length; x < xlen; x++ ) { if ( siblingNodes[ x ].index === highestRemovedAncestor.index ) { break; } else { const oldPreviousNodeIndex = siblingNodes[ x ].index; Eif ( correspondingNodes.oldToNew[ oldPreviousNodeIndex ] !== undefined ) { newPreviousNodeIndex = correspondingNodes.oldToNew[ oldPreviousNodeIndex ]; } } } // If previous node was found among siblings, insert the removed subtree just // after its corresponding node in the new document. Otherwise insert the // removed subtree just inside its parent node's corresponding node. if ( newPreviousNodeIndex !== undefined ) { insertIndex = newNodes[ newPreviousNodeIndex ].node.getOuterRange().to - nodeRange.from; } else { newPreviousNodeIndex = correspondingNodes.oldToNew[ highestRemovedAncestor.parent.index ]; insertIndex = newNodes[ newPreviousNodeIndex ].node.getRange().from - nodeRange.from; } // If more content branch node descendants of the highest removed node have // also been removed, record the index where their subtrees will need to be // spliced in. highestRemovedAncestors[ highestRemovedAncestor.index ] = insertIndex + 1; } ve.batchSplice( nodeData, insertIndex, 0, removeData ); } }; /** * Mark this node as inserted. * * @param {number} nodeIndex The index of this node in the subtree rooted at * this document child */ const highlightInsertedNode = ( nodeIndex ) => { // Find index of first data element for this node const node = newNodes[ nodeIndex ].node; const nodeRangeStart = node.getOuterRange().from - nodeRange.from; // Add insert class this.addAttributesToElement( nodeData, nodeRangeStart, { 'data-diff-action': ( node.isDiffedAsTree() && node.hasChildren() ) ? 'structural-insert' : 'insert' } ); }; /** * Mark this node as changed and, if it is a content branch node, splice in * the diff data. * * @param {number} oldIdx Old node index * @param {number} newIdx New node index * @param {Object} info Diff information relating to this node's change */ const highlightChangedNode = ( oldIdx, newIdx, info ) => { // The new node was changed. // Get data for this node const oldNode = oldNodes[ oldIdx ].node; const newNode = newNodes[ newIdx ].node; const nodeRangeStart = newNode.getOuterRange().from - nodeRange.from; let nodeDiffData = this.getChangedNodeData( info, oldNode, newNode, null, true ); if ( nodeDiffData ) { // Diff was handled e.g. by leaf/list/doc differ ve.batchSplice( nodeData, nodeRangeStart, newNode.getOuterLength(), nodeDiffData ); // TODO: Check if there were actually changes in the sub-diff } else { Iif ( info.linearDiff ) { // If there is a content change, splice it in nodeDiffData = info.linearDiff; const annotatedData = this.annotateNode( nodeDiffData, newNode ); ve.batchSplice( nodeData, nodeRangeStart + 1, newNode.getLength(), annotatedData ); } Eif ( info.attributeChange ) { // If there is no content change, just add change class this.addAttributesToElement( nodeData, nodeRangeStart, { 'data-diff-action': 'structural-change' } ); const item = this.compareNodeAttributes( nodeData, nodeRangeStart, info.attributeChange ); Eif ( item ) { this.descriptionItemsStack.push( item ); } } } }; // Iterate backwards over trees so that changes are made from right to left // of the data, to avoid having to update ranges const len = Math.max( oldNodes.length, newNodes.length ); for ( let i = 0, j = 0; i < len && j < len; i++, j++ ) { const newIndex = newNodes.length - 1 - i; const oldIndex = oldNodes.length - 1 - j; if ( newIndex < 0 ) { // The rest of the nodes have been removed highlightRemovedNode( oldIndex ); } else if ( oldIndex < 0 ) { // The rest of the nodes have been inserted highlightInsertedNode( newIndex ); } else if ( correspondingNodes.newToOld[ newIndex ] === oldIndex ) { // The new node was changed. for ( let k = 0, klen = treeDiff.length; k < klen; k++ ) { if ( treeDiff[ k ][ 0 ] === oldIndex && treeDiff[ k ][ 1 ] === newIndex ) { Iif ( !diffInfo[ k ] ) { // We are treating these nodes as removed and inserted highlightInsertedNode( newIndex ); highlightRemovedNode( oldIndex ); } else { // There could be any combination of content, attribute and type changes highlightChangedNode( oldIndex, newIndex, diffInfo[ k ] ); } } } } else if ( correspondingNodes.newToOld[ newIndex ] === undefined ) { // The new node was inserted. highlightInsertedNode( newIndex ); j--; } else Eif ( correspondingNodes.newToOld[ newIndex ] < oldIndex ) { // The old node was removed. highlightRemovedNode( oldIndex ); i--; } } // Push new description items from the queue this.descriptions.addItems( this.descriptionItemsStack ); this.descriptionItemsStack = []; return nodeData; }; /** * Add the relevant attributes to references list node HTML, whether changed or * unchanged (but not inserted or removed - in these cases we just use * getNodeElements). * * @param {HTMLElement} referencesListContainer Div containing the references list * @param {string} action 'change' or 'none' * @param {string|null} move 'up' or 'down' if the node has moved * @param {OO.ui.OptionWidget[]} items Change descriptions for the reference list * @return {HTMLElement[]} Elements to display */ ve.ui.DiffElement.prototype.getRefListNodeElements = function ( referencesListContainer, action, move, items ) { this.markMove( move, referencesListContainer ); this.descriptionItemsStack.push.apply( this.descriptionItemsStack, items ); return [ referencesListContainer ]; }; /** * Get the HTML for the diff of a single internal list item that has been removed * from the old document, inserted into the new document, or that is unchanged. * * @param {ve.dm.InternalItemNode} internalListItemNode Internal list item node * @param {string} action 'remove', 'insert' or 'none' * @param {string|null} move 'up' or 'down' if the node has moved * @param {number} index * @return {HTMLElement[]} Elements (not owned by window.document) */ ve.ui.DiffElement.prototype.getInternalListNodeElements = function ( internalListItemNode, action, move, index ) { const contents = internalListItemNode.children, listItemNode = document.createElement( 'li' ); if ( contents.length ) { contents.forEach( ( node ) => { const elements = this.getNodeElements( node, action, move ); listItemNode.appendChild( listItemNode.ownerDocument.adoptNode( elements[ 0 ] ) ); } ); } else E{ // TODO: This is MW-Cite-specific behaviour that VE core // should know nothing about. Move to MWDiffElement? $( listItemNode ).append( $( '<span>' ) .addClass( 've-ce-mwReferencesListNode-muted' ) .text( ve.msg( 'cite-ve-referenceslist-missingref-in-list' ) ) ).attr( 'data-diff-action', action ); } listItemNode.setAttribute( 'value', index + 1 ); return [ listItemNode ]; }; /** * Get the HTML for the linear diff of a single internal list item that has changed * from the old document to the new document. * * @param {Object} diff List item diff * @param {ve.dm.InternalItemNode} oldNode * @param {ve.dm.InternalItemNode} newNode * @param {string|null} move 'up' or 'down' if the node has moved * @param {number} newIndex * @return {HTMLElement[]} HTML elements to display the linear diff */ ve.ui.DiffElement.prototype.getInternalListChangedNodeElements = function ( diff, oldNode, newNode, move, newIndex ) { const listItemNode = document.createElement( 'li' ); let data = this.getChangedDocListData( newNode, diff, true ); // Remove internal list wrapper data = data.slice( 1, -1 ); this.markMove( move, listItemNode ); const newDoc = newNode.getRoot().getDocument(); const documentSlice = newDoc.cloneWithData( data, true, true ); const body = ve.dm.converter.getDomFromModel( documentSlice, ve.dm.Converter.static.PREVIEW_MODE ).body; while ( body.childNodes.length ) { listItemNode.appendChild( listItemNode.ownerDocument.adoptNode( body.childNodes[ 0 ] ) ); } listItemNode.setAttribute( 'value', newIndex + 1 ); return [ listItemNode ]; }; /** * Compare attributes of two nodes * * @param {Array} data Linear data containing new node * @param {number} offset Offset in data * @param {Object} attributeChange Attribute change object containing oldAttributes and newAttributes * @return {OO.ui.OptionWidget|null} Change description item, or null if nothing to describe */ ve.ui.DiffElement.prototype.compareNodeAttributes = function ( data, offset, attributeChange ) { const attributeChanges = this.constructor.static.compareAttributes( attributeChange.oldAttributes, attributeChange.newAttributes ); const changes = ve.dm.modelRegistry.lookup( data[ offset ].type ).static.describeChanges( attributeChanges, attributeChange.newAttributes, data[ offset ] ); // Don't describe the same change twice if ( changes.length && ( !( data[ offset ].internal ) || !( data[ offset ].internal.diff ) || data[ offset ].internal.diff[ 'data-diff-id' ] === undefined ) ) { const item = this.getChangeDescriptionItem( changes ); this.addAttributesToElement( data, offset, { 'data-diff-id': item.getData() } ); return item; } return null; }; /** * Get a change description item from a set of changes * * @param {Array} changes List of changes, each change being either text or a Node array * @param {string[]} classes Additional classes * @return {OO.ui.OptionWidget} Change description item */ ve.ui.DiffElement.prototype.getChangeDescriptionItem = function ( changes, classes ) { const elementId = this.elementId; let $label = $( [] ); for ( let i = 0, l = changes.length; i < l; i++ ) { const $change = $( '<div>' ); if ( typeof changes[ i ] === 'string' ) { $change.text( changes[ i ] ); } else { // changes[ i ] is definitely not an HTML string in this branch // eslint-disable-next-line no-jquery/no-append-html $change.append( changes[ i ] ); } $label = $label.add( $change ); } // eslint-disable-next-line mediawiki/class-doc const item = new OO.ui.OptionWidget( { label: $label, data: elementId, classes: [ 've-ui-diffElement-attributeChange', ...classes || [] ] } ); this.elementId++; return item; }; /** * Mark an element with attributes to be added later by the converter. * * @param {Array} data Data containing element to be marked * @param {number} offset Offset of element to be marked * @param {Object} attributes Attributes to set */ ve.ui.DiffElement.prototype.addAttributesToElement = function ( data, offset, attributes ) { const newElement = ve.copy( data[ offset ] ); // NB we modify the linear data here, but then this is a cloned document. for ( const key in attributes ) { Eif ( attributes[ key ] !== undefined ) { ve.setProp( newElement, 'internal', 'diff', key, attributes[ key ] ); } } // Don't let any nodes get unwrapped ve.deleteProp( newElement, 'internal', 'generated' ); data.splice( offset, 1, newElement ); }; /** * Mark an HTML element or data element as moved * * @param {string|null} move 'up' or 'down' if the node has moved * @param {HTMLElement|Array} elementOrData Linear data or HTMLElement * @param {number} [offset=0] Linear mode offset */ ve.ui.DiffElement.prototype.markMove = function ( move, elementOrData, offset ) { if ( !move ) { return; } // The following messages are used here: // * visualeditor-diff-moved-up // * visualeditor-diff-moved-down // The following classes are used here: // * ve-ui-diffElement-moved-up // * ve-ui-diffElement-moved-down const item = this.getChangeDescriptionItem( [ ve.msg( 'visualeditor-diff-moved-' + move ) ], [ 've-ui-diffElement-moved-' + move ] ); if ( Array.isArray( elementOrData ) ) { this.addAttributesToElement( elementOrData, offset || 0, { 'data-diff-move': move, 'data-diff-id': item.getData() } ); } else E{ elementOrData.setAttribute( 'data-diff-move', move ); elementOrData.setAttribute( 'data-diff-id', item.getData() ); } this.descriptionItemsStack.push( item ); }; /** * Annotate some data to highlight diff * * @param {Array} linearDiff Linear diff, mapping arrays of linear data to diff * actions (remove, insert or retain) * @param {ve.dm.Node} newNode Node from the new document * @return {Array} Data with annotations added */ ve.ui.DiffElement.prototype.annotateNode = function ( linearDiff, newNode ) { const DIFF_DELETE = ve.DiffMatchPatch.static.DIFF_DELETE, DIFF_INSERT = ve.DiffMatchPatch.static.DIFF_INSERT, DIFF_CHANGE_DELETE = ve.DiffMatchPatch.static.DIFF_CHANGE_DELETE, DIFF_CHANGE_INSERT = ve.DiffMatchPatch.static.DIFF_CHANGE_INSERT, items = [], newDoc = newNode.getRoot().getDocument(); let start = 0; // The starting index for a range for building an annotation // Make a new document from the diff const diffDocData = linearDiff[ 0 ][ 1 ].slice(); const ilen = linearDiff.length; for ( let i = 1; i < ilen; i++ ) { ve.batchPush( diffDocData, linearDiff[ i ][ 1 ] ); } const diffDoc = newDoc.cloneWithData( diffDocData ); // Add spans with the appropriate attributes for removes and inserts // TODO: do insert and remove outside of loop for ( let i = 0; i < ilen; i++ ) { const end = start + linearDiff[ i ][ 1 ].length; Eif ( start !== end ) { const range = new ve.Range( start, end ); const type = linearDiff[ i ][ 0 ]; if ( type !== 0 ) { let typeAsString, domElementType, annType; switch ( type ) { case DIFF_DELETE: typeAsString = 'remove'; domElementType = 'del'; annType = 'textStyle/delete'; break; case DIFF_INSERT: typeAsString = 'insert'; domElementType = 'ins'; annType = 'textStyle/insert'; break; case DIFF_CHANGE_DELETE: typeAsString = 'change-remove'; domElementType = 'span'; annType = 'textStyle/span'; break; case DIFF_CHANGE_INSERT: typeAsString = 'change-insert'; domElementType = 'span'; annType = 'textStyle/span'; break; } const domElement = document.createElement( domElementType ); domElement.setAttribute( 'data-diff-action', typeAsString ); const domElements = [ domElement ]; const changes = []; if ( linearDiff[ i ].annotationChanges ) { linearDiff[ i ].annotationChanges.forEach( ( annotationChange ) => { let attributeChanges; if ( annotationChange.oldAnnotation && annotationChange.newAnnotation ) { attributeChanges = this.constructor.static.compareAttributes( annotationChange.oldAnnotation.getAttributes(), annotationChange.newAnnotation.getAttributes() ); ve.batchPush( changes, ve.dm.modelRegistry.lookup( annotationChange.newAnnotation.getType() ).static.describeChanges( attributeChanges, annotationChange.newAnnotation.getAttributes(), annotationChange.newAnnotation.getElement() ) ); } else if ( annotationChange.newAnnotation ) { ve.batchPush( changes, annotationChange.newAnnotation.describeAdded() ); } else Eif ( annotationChange.oldAnnotation ) { ve.batchPush( changes, annotationChange.oldAnnotation.describeRemoved() ); } } ); } if ( linearDiff[ i ].attributeChanges ) { const element = linearDiff[ i ][ 1 ][ 0 ]; linearDiff[ i ].attributeChanges.forEach( ( attributeChange ) => { const attributeChanges = this.constructor.static.compareAttributes( attributeChange.oldAttributes, attributeChange.newAttributes ); ve.batchPush( changes, ve.dm.modelRegistry.lookup( element.type ).static.describeChanges( attributeChanges, element.attributes, element ) ); } ); } if ( changes.length ) { const item = this.getChangeDescriptionItem( changes ); domElement.setAttribute( 'data-diff-id', item.getData() ); items.push( item ); } const originalDomElementsHash = diffDoc.getStore().hash( domElements, domElements.map( ve.getNodeHtml ).join( '' ) ); const annHash = diffDoc.getStore().hash( ve.dm.annotationFactory.create( annType, { type: annType, originalDomElementsHash: originalDomElementsHash } ) ); // Insert annotation above annotations that span the entire range // and at least one character more const annHashLists = []; for ( let j = Math.max( 0, range.start - 1 ); j < Math.min( range.end + 1, diffDoc.data.getLength() ); j++ ) { annHashLists[ j ] = diffDoc.data.getAnnotationHashesFromOffset( j ); } const height = Math.min( ve.getCommonStartSequenceLength( annHashLists.slice( Math.max( 0, range.start - 1 ), range.end ) ), ve.getCommonStartSequenceLength( annHashLists.slice( range.start, Math.min( range.end + 1, diffDoc.data.getLength() ) ) ) ); for ( let j = range.start; j < range.end; j++ ) { annHashLists[ j ].splice( height, 0, annHash ); diffDoc.data.setAnnotationHashesAtOffset( j, annHashLists[ j ] ); } } } start = end; } this.descriptionItemsStack.push.apply( this.descriptionItemsStack, items ); // Merge the stores and get the data newDoc.getStore().merge( diffDoc.getStore() ); const annotatedLinearDiff = diffDoc.getData( { start: 0, end: diffDoc.getLength() } ); return annotatedLinearDiff; }; ve.ui.metaListDiffRegistry = new OO.Registry(); |