I have written a function to clip too long words/sentences so it won’t be longer than maxLength. The original text is stringToClip. symbolsString is a string of images that must appear (should not be clipped). SubCls is the style of the text. The output of the function will be inserted into a <td width=maxLength+5></td>
I think the textRange object is buggy. It does not really work properly. The function beahves totally differently if I activate the alert lines. How could it be??? I have never seen such a thing.
Please advise….
function doLineBreak(stringToClip,symbolsString,SubCls,maxLength) { //this function reshapes the subject line
var symbolsStringLength = 0;
var pointsLength = 14;
//alert(maxLength);
document.frames[“editPastingTextareaSubject”].document.open(‘text/html’,’replace’);
document.frames[“editPastingTextareaSubject”].document.write(‘<html><head><LINK REL=”stylesheet” TYPE=”text/css” HREF=”styles/ie.css” TITLE=”hi”></head><body style=”vertical-aling:middle; margin-top:0; padding-top:0;” class=’+SubCls+’></body></html>’);
document.frames[“editPastingTextareaSubject”].document.close();
if (symbolsString!=””) {
document.frames[“editPastingTextareaSubject”].document.body.innerHTML = symbolsString;
var tr1 = document.frames[“editPastingTextareaSubject”].document.body.createTextRange();
tr1.select();
symbolsStringLength = tr1.boundingWidth;
//alert(“symbols width = “+symbolsStringLength);
}
/* //buggy language
document.frames[“editPastingTextareaSubject”].document.body.innerHTML = ‘<div style=”vertical-aling:middle; margin-top:0; padding-top:0;” class=’+SubCls+’>’+’…’+'</div>’;
var tr2 = document.frames[“editPastingTextareaSubject”].document.body.createTextRange();
tr2.select();
pointsLength = tr2.boundingWidth;
//alert(“points width = “+pointsLength);
*/
/////////
document.frames[“editPastingTextareaSubject”].document.body.innerHTML = stringToClip;
var tr = document.frames[“editPastingTextareaSubject”].document.body.createTextRange();
tr.select(); //NOT just for me
//alert(tr.htmlText);
var sLine = tr.duplicate();
sLine.select();
//alert(“before=”+sLine.boundingWidth);
//sLine.collapse(true);
sLine.setEndPoint(“EndToStart”,tr);
sLine.select();
//alert(“after=”+sLine.boundingWidth);
while (sLine.boundingWidth<maxLength-symbolsStringLength && sLine.compareEndPoints(“EndToEnd”,tr)<0) {
sLine.moveEnd(“character”,1);
//if (sLine.text.charAt(sLine.text.length-1)==”n”) {
// linesNumberCounter++;
//}
//if (sLine.text.charAt(0)==” “) { //throw away spaces in the left edge
// sLine.moveStart(“character”,1);
//}
sLine.select();
//alert(“currentSelect=”+sLine.boundingWidth);
}
sLine.select();
sLine.boundingWidth;
//alert(“now get the clipped=”+sLine.boundingWidth+”n”+sLine.htmlText);
if (sLine.boundingWidth>maxLength-symbolsStringLength) {
if (sLine.text.lastIndexOf(” “)==-1) {
sLine.moveEnd(“character”,-1);
}
else if (sLine.text.lastIndexOf(” “)>0) {
sLine.moveEnd(“character”,-(sLine.text.length – sLine.text.lastIndexOf(” “) ));
}
sLine.select();
//alert(“now very neat clipped=”+sLine.boundingWidth+”n”+sLine.htmlText);
}
//alert(“e=”+sLine.htmlText);
//sLine.collapse(false);
//sLine.text = “… “+symbolsString.split(“<”).join(“<“).split(“>”).join(“>”);
//alert(“n=”+sLine.htmlText);
sLine.collapse(false);
//now don’t show the rest
sLine.setEndPoint(“EndToEnd”,tr)
sLine.select();
//alert(“before erasing=”+sLine.boundingWidth);
var points = “”;
if (sLine.boundingWidth>0) {
points = “…”;
var tempTR = tr.duplicate();
tempTR.setEndPoint(“EndToStart”,sLine)
while (tempTR.boundingWidth>maxLength-symbolsStringLength-pointsLength) {
tempTR.moveEnd(“character”,-1);
tempTR.select();
sLine.moveStart(“character”,-1);
sLine.select();
}
}
sLine.text = “”;
sLine.select();
//alert(“after erasing=”+sLine.boundingWidth);
document.frames[“editPastingTextareaSubject”].document.body.innerHTML += points+symbolsString;
//alert (“k=”+document.frames[“editPastingTextareaSubject”].document.body.innerHTML);
return document.frames[“editPastingTextareaSubject”].document.body.innerHTML;
}