Skip to content

Commit

Permalink
fix error with rangy
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton Lantsov committed Sep 15, 2014
1 parent 392ff56 commit 3b35308
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 87 deletions.
57 changes: 30 additions & 27 deletions dist/wysihtml5x-toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,11 @@ var wysihtml5 = {
(function(factory, global) {
if (typeof define == "function" && define.amd) {
// AMD. Register as an anonymous module.
define(factory);
window.rangy = factory();
define('rangy',factory);
/*
TODO: look into this properly.
} else if (typeof exports == "object") {
// Node/CommonJS style for Browserify
module.exports = factory;
Expand Down Expand Up @@ -395,11 +396,11 @@ var wysihtml5 = {
throw new Error("required module '" + moduleName + "' not supported");
}
}

// Now run initializer
this.initializer(this);
},

fail: function(reason) {
this.initialized = true;
this.supported = false;
Expand All @@ -419,7 +420,7 @@ var wysihtml5 = {
return new Error("Error in Rangy " + this.name + " module: " + msg);
}
};

function createModule(isCore, name, dependencies, initFunc) {
var newModule = new Module(name, dependencies, function(module) {
if (!module.initialized) {
Expand Down Expand Up @@ -503,7 +504,7 @@ var wysihtml5 = {
addListener(window, "load", loadHandler);

/*----------------------------------------------------------------------------------------------------------------*/

// DOM utility methods used by Rangy
api.createCoreModule("DomUtil", [], function(api, module) {
var UNDEF = "undefined";
Expand Down Expand Up @@ -1479,7 +1480,7 @@ var wysihtml5 = {
}
range.setStartAndEnd(sc, so, ec, eo);
}

function rangeToHtml(range) {
assertRangeValid(range);
var container = range.commonAncestorContainer.parentNode.cloneNode(false);
Expand Down Expand Up @@ -1778,7 +1779,7 @@ var wysihtml5 = {
this.setStartAfter(node);
this.collapse(true);
},

getBookmark: function(containerNode) {
var doc = getRangeDocument(this);
var preSelectionRange = api.createRange(doc);
Expand All @@ -1798,7 +1799,7 @@ var wysihtml5 = {
containerNode: containerNode
};
},

moveToBookmark: function(bookmark) {
var containerNode = bookmark.containerNode;
var charIndex = 0;
Expand Down Expand Up @@ -1840,11 +1841,11 @@ var wysihtml5 = {
isValid: function() {
return isRangeValid(this);
},

inspect: function() {
return inspect(this);
},

detach: function() {
// In DOM4, detach() is now a no-op.
}
Expand Down Expand Up @@ -1981,7 +1982,7 @@ var wysihtml5 = {

boundaryUpdater(this, sc, so, ec, eo);
},

setBoundary: function(node, offset, isStart) {
this["set" + (isStart ? "Start" : "End")](node, offset);
},
Expand Down Expand Up @@ -2176,7 +2177,7 @@ var wysihtml5 = {

/*----------------------------------------------------------------------------------------------------------------*/

// Wrappers for the browser's native DOM Range and/or TextRange implementation
// Wrappers for the browser's native DOM Range and/or TextRange implementation
api.createCoreModule("WrappedRange", ["DomRange"], function(api, module) {
var WrappedRange, WrappedTextRange;
var dom = api.dom;
Expand Down Expand Up @@ -2442,7 +2443,7 @@ var wysihtml5 = {
};
})();
}

if (api.features.implementsTextRange) {
/*
This is a workaround for a bug where IE returns the wrong container element from the TextRange's parentElement()
Expand Down Expand Up @@ -2564,11 +2565,11 @@ var wysihtml5 = {
For the particular case of a boundary within a text node containing rendered line breaks (within a
<pre> element, for example), we need a slightly complicated approach to get the boundary's offset in
IE. The facts:
- Each line break is represented as \r in the text node's data/nodeValue properties
- Each line break is represented as \r\n in the TextRange's 'text' property
- The 'text' property of the TextRange does not contain trailing line breaks
To get round the problem presented by the final fact above, we can use the fact that TextRange's
moveStart() and moveEnd() methods return the actual number of characters moved, which is not
necessarily the same as the number of characters it was instructed to move. The simplest approach is
Expand All @@ -2577,13 +2578,13 @@ var wysihtml5 = {
"move-negative-gazillion" method). However, this is extremely slow when the document is large and
the range is near the end of it. Clearly doing the mirror image (i.e. moving the range boundaries to
the end of the document) has the same problem.
Another approach that works is to use moveStart() to move the start boundary of the range up to the
end boundary one character at a time and incrementing a counter with the value returned by the
moveStart() call. However, the check for whether the start boundary has reached the end boundary is
expensive, so this method is slow (although unlike "move-negative-gazillion" is largely unaffected
by the location of the range within the document).
The approach used below is a hybrid of the two methods above. It uses the fact that a string
containing the TextRange's 'text' property with each \r\n converted to a single \r character cannot
be longer than the text of the TextRange, so the start of the range is moved that length initially
Expand Down Expand Up @@ -2839,7 +2840,7 @@ var wysihtml5 = {
function getDocSelection(winParam) {
return getWindow(winParam, "getDocSelection").document.selection;
}

function winSelectionIsBackward(sel) {
var backward = false;
if (sel.anchorNode) {
Expand Down Expand Up @@ -2890,7 +2891,7 @@ var wysihtml5 = {
// Test for existence of native selection extend() method
var selectionHasExtend = isHostMethod(testSelection, "extend");
features.selectionHasExtend = selectionHasExtend;

// Test if rangeCount exists
var selectionHasRangeCount = (typeof testSelection.rangeCount == NUMBER);
features.selectionHasRangeCount = selectionHasRangeCount;
Expand Down Expand Up @@ -2924,11 +2925,11 @@ var wysihtml5 = {
var originalSelectionRangeCount = sel.rangeCount;
var selectionHasMultipleRanges = (originalSelectionRangeCount > 1);
var originalSelectionRanges = [];
var originalSelectionBackward = winSelectionIsBackward(sel);
var originalSelectionBackward = winSelectionIsBackward(sel);
for (var i = 0; i < originalSelectionRangeCount; ++i) {
originalSelectionRanges[i] = sel.getRangeAt(i);
}

// Create some test elements
var body = getBody(document);
var testEl = body.appendChild( document.createElement("div") );
Expand Down Expand Up @@ -3647,7 +3648,7 @@ var wysihtml5 = {
} );
return results;
};

function createStartOrEndSetter(isStart) {
return function(node, offset) {
var range;
Expand All @@ -3664,7 +3665,7 @@ var wysihtml5 = {

selProto.setStart = createStartOrEndSetter(true);
selProto.setEnd = createStartOrEndSetter(false);

// Add select() method to Range prototype. Any existing selection will be removed.
api.rangePrototype.select = function(direction) {
getSelection( this.getDocument() ).setSingleRange(this, direction);
Expand Down Expand Up @@ -3730,7 +3731,7 @@ var wysihtml5 = {
if (isTextRange(range)) {
return range;
} else {
throw module.createError("getNativeTextRange: selection is a control selection");
throw module.createError("getNativeTextRange: selection is a control selection");
}
} else if (this.rangeCount > 0) {
return api.WrappedTextRange.rangeToTextRange( this.getRangeAt(0) );
Expand Down Expand Up @@ -3788,12 +3789,13 @@ var wysihtml5 = {
win = null;
});
});


/*----------------------------------------------------------------------------------------------------------------*/

return api;
}, this);;/**
}, this);
;/**
* Selection save and restore module for Rangy.
* Saves and restores user selections using marker invisible elements in the DOM.
*
Expand Down Expand Up @@ -9548,6 +9550,7 @@ wysihtml5.quirks.ensureProperClearing = (function() {
* - to use custom tags
* - to detect and replace similar css classes via reg exp
*/

(function(wysihtml5, rangy) {
var defaultTagName = "span";

Expand Down
4 changes: 2 additions & 2 deletions dist/wysihtml5x-toolbar.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/wysihtml5x-toolbar.min.map

Large diffs are not rendered by default.

Loading

0 comments on commit 3b35308

Please sign in to comment.