Skip to content

Commit

Permalink
Merge pull request #12 from narcolepticsnowman/master
Browse files Browse the repository at this point in the history
Fixed issue where bookmarks were left in script and style tags.
  • Loading branch information
w8tcha committed Sep 2, 2014
2 parents ffce289 + 19a2036 commit 6e24f83
Showing 1 changed file with 38 additions and 23 deletions.
61 changes: 38 additions & 23 deletions textselection/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,39 +36,54 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
range = new CKEDITOR.dom.range(editor.document),
walker,
startNode,
endNode;
endNode,
isTextNode = false;

range.setStartAt(doc.getBody(), CKEDITOR.POSITION_AFTER_START);
range.setEndAt(doc.getBody(), CKEDITOR.POSITION_BEFORE_END);
walker = new CKEDITOR.dom.walker(range);
// walker.type = CKEDITOR.NODE_COMMENT;
walker.evaluator = function (comment) {
//



var match = /cke_bookmark_\d+(\w)/.exec(comment.$.nodeValue);
// walker.type = CKEDITOR.NODE_COMMENT;
walker.evaluator = function (node) {
//
var match = /cke_bookmark_\d+(\w)/.exec(node.$.nodeValue);
if (match) {

if (match[1] === 'S') {
startNode = comment;
} else if (match[1] === 'E') {
endNode = comment;
if(decodeURIComponent(node.$.nodeValue)
.match(/<!--cke_bookmark_[0-9]+S-->.*<!--cke_bookmark_[0-9]+E-->/)){
isTextNode = true;
startNode = endNode = node;
return false;
} else {
if (match[1] === 'S') {
startNode = node;
} else if (match[1] === 'E') {
endNode = node;
return false;
}
}
}
};
walker.lastForward();
range.setStartAfter(startNode);
range.setEndBefore(endNode);
range.select();
// Scroll into view for non-IE.
// Scroll into view for non-IE.
// Scroll into view for non-IE.
// Scroll into view for non-IE.
if (!CKEDITOR.env.ie || (CKEDITOR.env.ie && CKEDITOR.env.version === 9)) {
editor.getSelection().getStartElement().scrollIntoView(true);
} // Remove the comments node which are out of range.
startNode.remove();
endNode.remove();
} // Remove the comments node which are out of range.
if(isTextNode){
//remove all of our bookmarks from the text node
//then remove all of the cke_protected bits that added because we had a comment
//whatever code is supposed to clean these cke_protected up doesn't work
//when there's two comments in a row like: <!--{cke_protected}{C}--><!--{cke_protected}{C}-->
startNode.$.nodeValue = decodeURIComponent(startNode.$.nodeValue).
replace(/<!--cke_bookmark_[0-9]+[SE]-->/g,'').
replace(/<!--[\s]*\{cke_protected}[\s]*\{C}[\s]*-->/g,'');
} else {
//just remove the comment nodes
startNode.remove();
endNode.remove();
}
}
}, null, null, 10);

Expand All @@ -79,15 +94,15 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
}
var sel = editor.getSelection(), range;
if (sel && (range = sel.getRanges()[0])) {
wysiwygBookmark = range.createBookmark(true);
wysiwygBookmark = range.createBookmark(editor);
}
}
});
// Build text range right after wysiwyg has unloaded.
editor.on('afterModeUnload', function (evt) {
if (editor.mode === 'wysiwyg' && wysiwygBookmark) {
textRange = new CKEDITOR.dom.textRange(evt.data);
textRange.moveToBookmark(wysiwygBookmark);
textRange.moveToBookmark(wysiwygBookmark, editor);

evt.data = textRange.content;
}
Expand Down Expand Up @@ -292,7 +307,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
* text removed.
* @param {Object} bookmark Exactly the one created by CKEDITOR.dom.range.createBookmark( true ).
*/
moveToBookmark: function(bookmark) {
moveToBookmark: function(bookmark, editor) {
var content = this.content;
function removeBookmarkText(bookmarkId) {

Expand Down Expand Up @@ -343,7 +358,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
this.endOffset = end;
},

createBookmark: function() {
createBookmark: function(editor) {
// Enlarge the range to avoid tag partial selection.
this.enlarge();
var content = this.content,
Expand Down Expand Up @@ -382,4 +397,4 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
// Seamless selection range across different modes.
CKEDITOR.config.syncSelection = true;

var textRange,sourceBookmark;
var textRange,sourceBookmark;

0 comments on commit 6e24f83

Please sign in to comment.