Skip to content

Commit ea01c25

Browse files
committed
Merge pull request #53 from beyounic/bug-range-makers
Bug: cursor save/restore marker breaks normalizeTags()
2 parents 2901f66 + 164afe1 commit ea01c25

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-3
lines changed

spec/parser.spec.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,5 +351,13 @@ describe('Parser', function() {
351351
});
352352
});
353353

354-
});
355354

355+
describe('latestChild()', function() {
356+
it('returns the deepest last child', function() {
357+
var source = linkWithSpan;
358+
var target = document.createTextNode('bar');
359+
expect(parser.latestChild(source).isEqualNode(target)).toEqual(true);
360+
});
361+
});
362+
363+
});

src/behavior.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ var behavior = (function() {
119119
return;
120120

121121
if(container.childNodes.length > 0)
122-
cursor.moveAtEnd(container);
122+
cursor.moveAtTextEnd(container);
123123
else
124124
cursor.moveAtBeginning(container);
125125
cursor.setSelection();
@@ -153,7 +153,7 @@ var behavior = (function() {
153153
case 'before':
154154
previous = block.previous(element);
155155
if (previous) {
156-
cursor.moveAtEnd(previous);
156+
cursor.moveAtTextEnd(previous);
157157
cursor.setSelection();
158158
}
159159
break;

src/cursor.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ var Cursor = (function() {
147147
if (this.isSelection) return new Cursor(this.host, this.range);
148148
},
149149

150+
moveAtTextEnd: function(element) {
151+
return this.moveAtEnd(parser.latestChild(element));
152+
},
153+
150154
setHost: function(element) {
151155
this.host = parser.getHost(element);
152156
if (!this.host) {

src/parser.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,20 @@ var parser = (function() {
238238
}
239239

240240
return true;
241+
},
242+
243+
/**
244+
* Return the deepest last child of a node.
245+
*
246+
* @method latestChild
247+
* @param {HTMLElement} container The container to iterate on.
248+
* @return {HTMLElement} THe deepest last child in the container.
249+
*/
250+
latestChild: function(container) {
251+
if(container.lastChild)
252+
return this.latestChild(container.lastChild);
253+
else
254+
return container;
241255
}
242256
};
243257
})();

0 commit comments

Comments
 (0)