Skip to content

Commit

Permalink
Rewrite getSelectedText method.
Browse files Browse the repository at this point in the history
Fixes #57.

* Fixes bug where it could return text outside selection
* Inserts new line between blocks.
  • Loading branch information
neilj authored and seonim-ryu committed Feb 5, 2020
1 parent 25dfd94 commit 08628b2
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 78 deletions.
79 changes: 41 additions & 38 deletions libs/Squire/build/squire-raw.js
Original file line number Diff line number Diff line change
Expand Up @@ -611,43 +611,6 @@ var getNodeAfter = function ( node, offset ) {

// ---

var forEachTextNodeInRange = function ( range, fn ) {
range = range.cloneRange();
moveRangeBoundariesDownTree( range );

var startContainer = range.startContainer,
endContainer = range.endContainer,
root = range.commonAncestorContainer,
walker = new TreeWalker(
root, SHOW_TEXT, function (/* node */) {
return true;
}, false ),
textnode = walker.currentNode = startContainer;

while ( !fn( textnode, range ) &&
textnode !== endContainer &&
( textnode = walker.nextNode() ) ) {}
};

var getTextContentInRange = function ( range ) {
var textContent = '';
forEachTextNodeInRange( range, function ( textnode, range ) {
var value = textnode.data;
if ( value && ( /\S/.test( value ) ) ) {
if ( textnode === range.endContainer ) {
value = value.slice( 0, range.endOffset );
}
if ( textnode === range.startContainer ) {
value = value.slice( range.startOffset );
}
textContent += value;
}
});
return textContent;
};

// ---

var insertNodeInRange = function ( range, node ) {
// Insert at start.
var startContainer = range.startContainer,
Expand Down Expand Up @@ -1392,7 +1355,47 @@ proto.getSelection = function () {
};

proto.getSelectedText = function () {
return getTextContentInRange( this.getSelection() );
var range = this.getSelection(),
walker = new TreeWalker(
range.commonAncestorContainer,
SHOW_TEXT|SHOW_ELEMENT,
function ( node ) {
return isNodeContainedInRange( range, node, true );
}
),
startContainer = range.startContainer,
endContainer = range.endContainer,
node = walker.currentNode = startContainer,
textContent = '',
addedTextInBlock = false,
value;

if ( !walker.filter( node ) ) {
node = walker.nextNode();
}

while ( node ) {
if ( node.nodeType === TEXT_NODE ) {
value = node.data;
if ( value && ( /\S/.test( value ) ) ) {
if ( node === endContainer ) {
value = value.slice( 0, range.endOffset );
}
if ( node === startContainer ) {
value = value.slice( range.startOffset );
}
textContent += value;
addedTextInBlock = true;
}
} else if ( node.nodeName === 'BR' ||
addedTextInBlock && !isInline( node ) ) {
textContent += '\n';
addedTextInBlock = false;
}
node = walker.nextNode();
}

return textContent;
};

proto.getPath = function () {
Expand Down
4 changes: 2 additions & 2 deletions libs/Squire/build/squire.js

Large diffs are not rendered by default.

42 changes: 41 additions & 1 deletion libs/Squire/source/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,47 @@ proto.getSelection = function () {
};

proto.getSelectedText = function () {
return getTextContentInRange( this.getSelection() );
var range = this.getSelection(),
walker = new TreeWalker(
range.commonAncestorContainer,
SHOW_TEXT|SHOW_ELEMENT,
function ( node ) {
return isNodeContainedInRange( range, node, true );
}
),
startContainer = range.startContainer,
endContainer = range.endContainer,
node = walker.currentNode = startContainer,
textContent = '',
addedTextInBlock = false,
value;

if ( !walker.filter( node ) ) {
node = walker.nextNode();
}

while ( node ) {
if ( node.nodeType === TEXT_NODE ) {
value = node.data;
if ( value && ( /\S/.test( value ) ) ) {
if ( node === endContainer ) {
value = value.slice( 0, range.endOffset );
}
if ( node === startContainer ) {
value = value.slice( range.startOffset );
}
textContent += value;
addedTextInBlock = true;
}
} else if ( node.nodeName === 'BR' ||
addedTextInBlock && !isInline( node ) ) {
textContent += '\n';
addedTextInBlock = false;
}
node = walker.nextNode();
}

return textContent;
};

proto.getPath = function () {
Expand Down
37 changes: 0 additions & 37 deletions libs/Squire/source/Range.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,43 +27,6 @@ var getNodeAfter = function ( node, offset ) {

// ---

var forEachTextNodeInRange = function ( range, fn ) {
range = range.cloneRange();
moveRangeBoundariesDownTree( range );

var startContainer = range.startContainer,
endContainer = range.endContainer,
root = range.commonAncestorContainer,
walker = new TreeWalker(
root, SHOW_TEXT, function (/* node */) {
return true;
}, false ),
textnode = walker.currentNode = startContainer;

while ( !fn( textnode, range ) &&
textnode !== endContainer &&
( textnode = walker.nextNode() ) ) {}
};

var getTextContentInRange = function ( range ) {
var textContent = '';
forEachTextNodeInRange( range, function ( textnode, range ) {
var value = textnode.data;
if ( value && ( /\S/.test( value ) ) ) {
if ( textnode === range.endContainer ) {
value = value.slice( 0, range.endOffset );
}
if ( textnode === range.startContainer ) {
value = value.slice( range.startOffset );
}
textContent += value;
}
});
return textContent;
};

// ---

var insertNodeInRange = function ( range, node ) {
// Insert at start.
var startContainer = range.startContainer,
Expand Down

0 comments on commit 08628b2

Please sign in to comment.