| | 858 | * Test suite for this, because it's really tough to get right. |
| | 859 | */ |
| | 860 | EnterParagraphs.RunTests = function(xinha) |
| | 861 | { |
| | 862 | function test(before, cursorBefore, after, cursorAfter) { |
| | 863 | xinha.setHTML(before); |
| | 864 | // Do something |
| | 865 | var cAnchor, cOffset; |
| | 866 | |
| | 867 | var mockEvent = { |
| | 868 | preventDefault: function() {console.log("Preventing default.");}, |
| | 869 | stopPropagation: function() {console.log("Stopping propagation.");}, |
| | 870 | } |
| | 871 | function setCursor(commands) { |
| | 872 | cAnchor = xinha._doc.body; |
| | 873 | cOffset = 0; |
| | 874 | for (var index=0; index<commands.length; ++index) { |
| | 875 | var command = commands[index]; |
| | 876 | if ('id' == command[0]) { |
| | 877 | cAnchor = xinha._doc.getElementById(command[1]); |
| | 878 | } else if ('child' == command[0]) { |
| | 879 | cAnchor = cAnchor.childNodes[command[1]]; |
| | 880 | } else if ('next' == command[0]) { |
| | 881 | for (var next=command[1]; next > 0 && cAnchor.nextSibling; --next) { |
| | 882 | cAnchor = cAnchor.nextSibling; |
| | 883 | } |
| | 884 | } else if ('previous' == command[0]) { |
| | 885 | for (var previous=command[1]; previous > 0 && cAnchor.previousSibling; --previous) { |
| | 886 | cAnchor = cAnchor.previousSibling; |
| | 887 | } |
| | 888 | } else if ('offset' == command[0]) { |
| | 889 | cOffset = command[1]; |
| | 890 | } |
| | 891 | } |
| | 892 | } |
| | 893 | |
| | 894 | setCursor(cursorBefore); |
| | 895 | |
| | 896 | var selection = xinha.getSelection(); |
| | 897 | var range = xinha.createRange(selection); |
| | 898 | |
| | 899 | range.setStart(cAnchor, cOffset); |
| | 900 | range.setEnd(cAnchor, cOffset); |
| | 901 | selection.removeAllRanges(); |
| | 902 | selection.addRange(range); |
| | 903 | |
| | 904 | return; |
| | 905 | // Breakline |
| | 906 | xinha.plugins['EnterParagraphs'].instance.breakLine(mockEvent, xinha._doc); |
| | 907 | |
| | 908 | var selection = xinha.getSelection(); |
| | 909 | var range = xinha.createRange(selection); |
| | 910 | |
| | 911 | setCursor(cursorAfter); |
| | 912 | |
| | 913 | if ((selection.anchorNode != cAnchor) || (selection.anchorOffset != cOffset)) { |
| | 914 | console.error('Actual anchor:', selection.anchorNode, 'Actual offset:', selection.anchorOffset, 'Expected anchor:', cAnchor, 'Expected offset:', cOffset); |
| | 915 | } |
| | 916 | |
| | 917 | result = xinha.getInnerHTML(); |
| | 918 | if (result == after) { |
| | 919 | console.info('Was', before, 'Got', after) |
| | 920 | } else { |
| | 921 | console.error('Was', before, 'Expected', after, 'Got', result) |
| | 922 | } |
| | 923 | } |
| | 924 | contentBackup = xinha.getInnerHTML(); |
| | 925 | console.group('Running tests:'); |
| | 926 | test('<h1 id="before">hi</h1>', [['id', 'before'], ['child', 0], ['offset', 2]], '<h1 id="before">hi</h1><h1></h1>', [['id', 'before'], ['next', 1], ['offset', 2]]); |
| | 927 | console.groupEnd(); |
| | 928 | return; |
| | 929 | xinha.setHTML(contentBackup); |
| | 930 | // EnterParagraphs.RunTests(xinha_editors['myTextArea']) |
| | 931 | } |
| | 932 | /* |
| | 1496 | var newCursor = splitTree(wrapNode, cursorParent, cursorOffset, doc); |
| | 1497 | } |
| | 1498 | else if (wrapNode.nodeName.toLowerCase() in {dt:'',dd:'',li:''}) |
| | 1499 | { |
| | 1500 | // To the bane of software developers the world over, users expect to be |
| | 1501 | // able to hit enter twice to end a list, whether at the end or in the |
| | 1502 | // middle. This means that we need to have special handling for list items |
| | 1503 | // to check for the second return. We do this by testing to see if the |
| | 1504 | // current list item is empty, and if so, deleting it, splitting the list |
| | 1505 | // into two if necessary, and inserting a paragraph. |