Created
January 8, 2012 11:47
-
-
Save XP1/1578084 to your computer and use it in GitHub Desktop.
Fix TinyMCE: Fixes TinyMCE's `setRng` function by replacing it with the fixed one.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Fix TinyMCE | |
// @version 1.01 | |
// @description Fixes TinyMCE's `setRng` function by replacing it with the fixed one. | |
// @author XP1 (https://github.com/XP1/) | |
// @namespace https://gist.github.com/1578084/ | |
// @include http*://hp.com/* | |
// @include http*://*.hp.com/* | |
// ==/UserScript== | |
/*jslint browser: true, vars: true, white: true, maxerr: 50, indent: 4 */ | |
(function (console, window) | |
{ | |
"use strict"; | |
var userScript = { | |
name: "Fix TinyMCE" | |
}; | |
function removeEventListenerAfterFiring(numberOfTimes, callback) | |
{ | |
var remaining = numberOfTimes; | |
return function listener(event) | |
{ | |
remaining -= 1; | |
if (remaining <= 0) | |
{ | |
var target = event.target; | |
var type = event.type; | |
target.removeEventListener(type, listener, false); | |
target.removeEventListener(type, listener, true); | |
} | |
callback(); | |
}; | |
} | |
var handleException = function handleException(theFunction) | |
{ | |
try | |
{ | |
theFunction(); | |
} | |
catch (exception) | |
{ | |
console.error("[" + userScript.name + "]: Exception: " + exception + "\n\nStack:\n" + exception.stack + "\n\nStacktrace:\n" + exception.stacktrace); | |
} | |
}; | |
// Jason Frame's bug fix, on November 08, 2011: | |
// Fix exception in setRng for webkit browser where adding range isn't successful and causes an exception setting the selected range: | |
// https://github.com/tinymce/tinymce/blob/347623591030c58bd06fd60c597dac93372d1921/jscripts/tiny_mce/classes/dom/Selection.js | |
/** | |
* Changes the selection to the specified DOM range. | |
* | |
* @method setRng | |
* @param {Range} r Range to select. | |
*/ | |
var fixedSetRng = function fixedSetRng(r) | |
{ | |
var s, t = this; | |
if (!t.tridentSel) | |
{ | |
s = t.getSel(); | |
if (s) | |
{ | |
t.explicitRange = r; | |
try | |
{ | |
s.removeAllRanges(); | |
} | |
catch (ex0) | |
{ | |
// IE9 might throw errors here don't know why | |
} | |
s.addRange(r); | |
// adding range isn't always successful so we need to check range count otherwise an exception can occur | |
t.selectedRange = s.rangeCount > 0 ? s.getRangeAt(0) : null; | |
} | |
} | |
else | |
{ | |
// Is W3C Range | |
if (r.cloneRange) | |
{ | |
t.tridentSel.addRange(r); | |
return; | |
} | |
// Is IE specific range | |
try | |
{ | |
r.select(); | |
} | |
catch (ex1) | |
{ | |
// Needed for some odd IE bug #1843306 | |
} | |
} | |
}; | |
function fixSetRng() | |
{ | |
var SelectionPrototype = window.tinymce.dom.Selection.prototype; | |
var setRng = SelectionPrototype.setRng; | |
// Check for the presence of Jason Frame's bug fix, on November 08, 2011. | |
if (typeof setRng === "function" && setRng.toString().indexOf("rangeCount") === -1) | |
{ | |
SelectionPrototype.setRng = fixedSetRng; | |
console.log("[" + userScript.name + "]: Replaced the \"setRng\" function with the fixed \"setRng\" function."); | |
} | |
} | |
function initialize() | |
{ | |
var functions = [fixSetRng]; | |
var i = null; | |
var length = functions.length; | |
for (i = 0; i < length; i += 1) | |
{ | |
handleException(functions[i]); | |
} | |
} | |
window.addEventListener("DOMContentLoaded", removeEventListenerAfterFiring(1, initialize), false); | |
}(this.console, this)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment