Changeset 1176 for branches


Ignore:
Timestamp:
04/09/09 17:07:27 (10 years ago)
Author:
douglas
Message:

Add a testrunner for double-checking this code.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/Ticket1226/paraHandlerBest.js

    r1164 r1176  
    856856} 
    857857/* 
     858* Test suite for this, because it's really tough to get right. 
     859*/ 
     860EnterParagraphs.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/* 
    858933* Determine if a cursor points to the end of it's containing node. 
    859934*/ 
     
    9901065    // position on the newly duplicated tree. 
    9911066 
    992     // Move insertNode from the inside outwards towards the root, moving any content nodes as we go.  We'll make sure that we can do the same with sourceNode 
     1067    // Move insertNode from the inside outwards towards the root, moving any 
     1068    // content nodes as we go.  We'll make sure that we can do the same with 
     1069    // sourceNode 
    9931070    while (insertNode != root.parentNode) 
    9941071    { 
     
    14141491    // XXX TODO 
    14151492  } 
    1416   else if (wrapNode.nodeName.toLowerCase() in {dt:'',dd:'',li:'',h1:'',h2:'',h3:'',h4:'',h5:'',h6:'',p:''}) 
     1493  else if (wrapNode.nodeName.toLowerCase() in {h1:'',h2:'',h3:'',h4:'',h5:'',h6:'',p:''}) 
    14171494  { 
    14181495    // Split wrapNode into two. 
     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. 
    14191506    var newCursor = splitTree(wrapNode, cursorParent, cursorOffset, doc); 
    14201507  } 
Note: See TracChangeset for help on using the changeset viewer.