Permalink
Browse files

bugfixes with wrong selection ranges

  • Loading branch information...
1 parent 35b4170 commit 3c42bf8b4523b9a375d3754dd62a14c09c31cefa Harut committed Jul 21, 2011
Showing with 52 additions and 19 deletions.
  1. +20 −12 src/js/masha.js
  2. +32 −7 test/cases.html
View
@@ -290,30 +290,37 @@ MaSha.prototype = {
},
deserializeRange: function(serialized){
- var result = /^([^,]+),([^,]+)({([^}]+)})?$/.exec(serialized);
+ var result = /^([^,]+),([^,]+)$/.exec(serialized);
+ var bits1 = result[1].split(":");
+ var bits2 = result[2].split(":");
+ // XXX this if is ugly
+ if(parseInt(bits1[0], 10) < parseInt(bits2[0], 10) ||
+ (bits1[0] == bits2[0] && parseInt(bits1[1], 10) <= parseInt(bits2[1], 10))){
- var start = this.deserializePosition(result[1], 'start'),
- end = this.deserializePosition(result[2], 'end');
+ var start = this.deserializePosition(bits1, 'start'),
+ end = this.deserializePosition(bits2, 'end');
- if (start.node && end.node){
- var range = document.createRange();
- range.setStart(start.node, start.offset);
- range.setEnd(end.node, end.offset);
- return range;
+ if (start.node && end.node){
+ var range = document.createRange();
+ range.setStart(start.node, start.offset);
+ range.setEnd(end.node, end.offset);
+ return range;
+ }
}
+
+
+
if (window.console && (typeof console['warn'] == 'function')){
console['warn']('Cannot deserialize range: ' + serialized);
}
},
- deserializePosition: function(serialized, pos){
+ deserializePosition: function(bits, pos){
// deserializes №OfBlock:№OfWord pair
- var bits = serialized.split(":");
// getting block
var node = this.blocks[parseInt(bits[0], 10)];
var pos_text;
-
var offset, stepCount = 0, exit = false;
// getting word in all text nodes
while (node) {
@@ -332,7 +339,7 @@ MaSha.prototype = {
// word not found yet, trying next container
node = this.nextNode(node, /.*/)
node = node? node._container: null;
- if (this.isFirstTextNode(node)){
+ if (node && this.isFirstTextNode(node)){
node = null;
}
}
@@ -487,6 +494,7 @@ MaSha.prototype = {
// sentence end detected
// XXX rewrite
var text = range.toString();
+ // XXX support not only latin and russian?
if (text.match(/(\.|\?|\!)\s+[A-ZА-ЯЁ]/)){
return apply();
}
View
@@ -134,6 +134,14 @@
console.error(test + ' failure. Got:\n', output_html, '\n instead of:\n', aim);
}
}
+ function assert(aim, result, silent){
+ var test = arguments.callee.caller.name_;//toString();
+ if(aim && !silent){
+ console.log(test + ' ok')
+ } else if(!aim) {
+ console.error(test + ' failure:', result);
+ }
+ }
function assertHashEqual(sel, hash, silent){
var test = arguments.callee.caller.name_;//toString();
var got_hash = sel.options.location.hash.replace('sel=', '');
@@ -320,14 +328,7 @@
var aim = '<p>Zero text </p><p><span class="num0">First text</span>,</p><p> Second text</p>';
assertHTMLEqual(aim);
},
- regressionCommaOutsideTag: function(){
- var html = '<p><b><a>Ivanov</a>:</b> some text</p>';
- var sel = new_selection(html);
- createRange(0, 0, 2, 3);
- var aim = '<p><b><a><span class="num0">Ivanov</span></a><span class="num0">:</span></b><span class="num0"> some</span> text</p>';
- assertHTMLEqual(aim);
- },
selectCrossDivs: function(){
var html = '<p>Zero text</p><p>First text</p><p>Second text</p>';
var sel = new_selection(html);
@@ -399,6 +400,30 @@
assertSelectionEqual('Word. Some', 0, 'silent');
assertHashEqual(sel, '1:1,1:2');
+ },
+
+ regressionCommaOutsideTag: function(){
+ var html = '<p><b><a>Ivanov</a>:</b> some text</p>';
+ var sel = new_selection(html);
+ createRange(0, 0, 2, 3);
+
+ var aim = '<p><b><a><span class="num0">Ivanov</span></a><span class="num0">:</span></b><span class="num0"> some</span> text</p>';
+ assertHTMLEqual(aim);
+ },
+ regressionStartGtStop: function(){
+ var html = '<p>Some text of few short words.</p>';
+ var sel = new_selection(html, '1:3,1:2');
+ assert($('.num0').length == 0, $('.num0').length);
+ },
+ regressionStartEqStop: function(){
+ var html = '<p>Some text of few short words.</p>';
+ var sel = new_selection(html, '1:3,1:3');
+ assertSelectionEqual('of');
+ },
+ regressionEndGtWords: function(){
+ var html = '<p>Some text of few short words.</p>';
+ var sel = new_selection(html, '1:3,1:10');
+ assert($('.num0').length == 0, $('.num0').length);
}
// XXX add tests for nbsp for IE
// XXX selection removing tests!

0 comments on commit 3c42bf8

Please sign in to comment.