micha wrote:
function get_selection()
{
//$sel contains the marked text, can be empty
var $sel;
// ie
if(document.selection)
{
$sel = document.selection.createRange().text;
}
// mozilla
else
{
$sel =
document.getElementById('textarea_id').value.subst ring(document.getElementById('editorfenster').sele ctionStart, document.getElementById('textarea_id').selectionEn d);
}
return ($sel);
}
function replace_selection($replace)
{
//replaces the marked text by $replace
// ie
if(document.selection)
{
document.getElementById('textarea_id').focus();
document.selection.createRange().text = $replace;
}
//mozilla
else
{
$before_replace =
document.getElementById('textarea_id').value.subst ring(0,
document.getElementById('textarea_id').selectionSt art);
$after_replace =
document.getElementById('textarea_id').value.subst ring(document.getElementById('textarea_id').select ionEnd, document.getElementById('textarea_id').value.lengt h);
document.getElementById('textarea_id').value = $before_replace +
$replace + $after_replace;
}
Try to avoid using non-downwards-compatible features like
document.getElementById() when forms are involved. Therefore, and
for other reasons, the above should be rewritten (quick hack):
/**
* @argument number|string|HTMLFormElement f
* @argument number|string c
*/
function getFormControl(f, c)
{
var oControl = null;
if (typeof f != "undefined")
{
if (typeof f == "number" || typeof f == "string")
{
f = document.forms[f];
}
if (f)
{
var es = null;
oControl = (es = f.elements) && es[c];
}
}
return oControl;
}
var getSelection = function()
{
return null;
};
// IE
if (document.selection)
{
/**
* @argument number|string|HTMLFormElement f
* @argument number|string c
*/
getSelection = function(f, c)
{
var oControl = getFormControl(f, c);
if (oControl)
{
return document.selection.createRange().text;
}
return "";
};
}
// Mozilla
else
{
getSelection = function(f, c)
{
var oControl = getFormControl(f, c);
if (oControl)
{
return oControl.value.substring(
oControl.selectionStart,
oControl.selectionEnd + 1);
}
return "";
};
}
How to replace the selection was not required, however once I'm at it:
/**
* @author
* (C) 2003, 2004 Thomas Lahn <ty******@PointedEars.de>
* Distributed under the GNU GPL v2 and above.
* @optional Object|string o
* Object to be determined an method, i.e. a
* <code>Function</code> object assigned as property of
* another object. May also be a string to be evaluated
* and so is applicable to unknown properties.
* @return type boolean
* <code>true</code> if <code>o</code> is a method,
* <code>false</code> otherwise.
* @see #isMethodType()
*/
function isMethod(m)
{
var t;
(m = eval(m)) && (t = typeof m);
return (t == "function" || t == "object");
}
var replaceSelection = function()
{
return null;
};
// IE
if (document.selection)
{
/**
* @argument number|string|HTMLFormElement f
* @argument number|string c
* @argument string sReplacement
*/
replaceSelection = function(f, c, sReplacement)
{
var oControl = getFormControl(f, c);
if (oControl)
{
if (isMethod(oControl.focus))
{
oControl.focus();
}
document.selection.createRange().text = sReplacement;
return true;
}
return false;
}
}
// Mozilla
else
{
replaceSelection = function(f, c, sReplacement)
{
var oControl = getFormControl(f, c);
if (oControl)
{
var start = oControl.selectionStart;
oControl.value = oControl.value.replace(
new RegExp([
"((.|\n){", start, "})",
"(.|\n){", oControl.selectionEnd - start + 1, "}"
].join("")),
"$1" + sReplacement);
return true;
}
return false;
};
}
BTW: Even if JS allow variable identifiers to start with `$', it is not
required. These are script languages, but they are not shell script,
Perl or PHP. And you should reconsider your Pretty Printing style.
PointedEars