Skip to content

Commit

Permalink
Fix issue #592
Browse files Browse the repository at this point in the history
  • Loading branch information
YoranBrondsema committed Mar 31, 2018
1 parent e363b9b commit d318a2b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/js/utils/cursor/position.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,12 @@ Position = class Position {
sectionOffset += postNode.length;
}
position = new Position(section, sectionOffset);
} else if (offset >= elementNode.childNodes.length) {

// This is to deal with how Firefox handles triple-click selections.
// See https://stackoverflow.com/a/21234837/1269194 for an
// explanation.
position = section.tailPosition();
} else {
// The offset is 0 if the cursor is on a non-atom-wrapper element node
// (e.g., a <br> tag in a blank markup section)
Expand Down
34 changes: 34 additions & 0 deletions tests/unit/utils/cursor-position-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,40 @@ test('#fromNode when node is root element and offset is > 0', (assert) => {
assert.positionIsEqual(position, editor.post.tailPosition());
});

/**
* On Firefox, triple-clicking results in a different selection that on Chrome
* and others. Imagine we have the following content:
*
* <p>abc</p>
*
* Chrome:
* anchorNode: <TextNode>
* anchorOffset: 0
* focusNode: <TextNode>
* focusOffset: 3
*
* Firefox:
* anchorNode: <p>
* anchorOffset: 0
* focusNode: <p>
* focusOffset: 1
*
* So when getting the position for `focusNode`/`focusOffset`, we have to get
* the tail of section.
*/
test('#fromNode when offset refers to one past the number of child nodes of the node', function(assert) {
editor = Helpers.mobiledoc.renderInto(editorElement,
({post, markupSection, marker}) => {
return post([markupSection('p', [marker('abc')])]);
});

let renderTree = editor._renderTree;
let elementNode = editorElement.firstChild;
let position = Position.fromNode(renderTree, elementNode, 1);

assert.positionIsEqual(position, editor.post.tailPosition());
});

test('#fromNode when node is card section element or next to it', (assert) => {
let editorOptions = { cards: [{
name: 'some-card',
Expand Down

0 comments on commit d318a2b

Please sign in to comment.