Jump to content
  • Advertisement

This topic is 5486 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Testing..



//
// htmlArea v1.[05] - Copyright (c) [2002] interactivetools.com, inc.
// A free WYSIWYG editor replacement for <textarea> fields.
//
// For more information visit:
// http://www.interactivetools.com/products/htmlarea/
//

/* ---------------------------------------------------------------------- * Function : editor_generate
Description : replace textarea with wysiwyg editor
Usage : editor_generate("textarea_id",[height],[width]);
Arguments : objname - ID of textarea to replace
w - width of wysiwyg editor
h - height of wysiwyg editor
\* ---------------------------------------------------------------------- */


function editor_generate(objname,w,h) {


// Default Settings
_editor_url = 'include/';
var imgURL = 'images/editor/'; //_editor_url + 'images/'; // images url

// set size to specified size or size of original object
var obj = document.all[objname];
if (!w) {
if (obj.style.width) { width = obj.style.width; } // use css style
else if (obj.cols) { width = (obj.cols * [8]) + [22]; } // col width + toolbar
else { width = '[100]%'; } // default
}
if (!h) {
if (obj.style.height) { height = obj.style.height; } // use css style
else if (obj.rows) { height = obj.rows * [17] } // row height
else { height = '[200]'; } // default
}

// Check for IE [5].[5]+ on Windows
var Agent, VInfo, MSIE, Ver, Win32, Opera;
Agent = navigator.userAgent;
VInfo = Array(); // version info
VInfo = Agent.split(";")
MSIE = Agent.indexOf('MSIE') > [0];
Ver = VInfo[[1]].substr([6],[3]);
Win32 = Agent.indexOf('Windows') > [0] && Agent.indexOf('Mac') < [0] && Agent.indexOf('Windows CE') < [0];
Opera = Agent.indexOf('Opera') > -[1];
if (!MSIE || Opera || Ver < [5].[5] || !Win32) { return; }

var editor = ''
+ '<table border=[0] cellspacing=[0] cellpadding=[0] bgcolor="buttonface" style="padding: [1] [0] [0] [0]" width=' + width + ' unselectable="on"><tr><td>\n'

+ '<table border=[0] cellspacing=[2] cellpadding=[0] bgcolor="buttonface" style="float: left;" unselectable="on">\n'
+ ' <tr>\n'
+ ' <td style="border-width: [0]; padding: [2] [0] [0] [3];">\n'
+ ' <select id="_' +objname+ '_FontName" onChange="editor_action(this.id)" unselectable="on">\n'
+ ' <option value="arial, helvetica, sans-serif">Arial</option>\n'
+ ' <option value="courier new, courier, mono">Courier New</option>\n'
+ ' <option value="Georgia, Times New Roman, Times, Serif">Georgia</option>\n'
+ ' <option value="Tahoma, Arial, Helvetica, sans-serif">Tahoma</option>\n'
+ ' <option value="times new roman, times, serif">Times New Roman</option>\n'
+ ' <option value="Verdana, Arial, Helvetica, sans-serif">Verdana</option>\n'
+ ' <option value="wingdings">WingDings</option>\n'
+ ' </select>'
+ ' </td>\n'
+ ' </tr>\n'
+ '</table>\n'

+ '<table border=[0] cellspacing=[2] cellpadding=[0] bgcolor="buttonface" style="float: left;" unselectable="on">\n'
+ ' <tr>\n'
+ ' <td style="border-width: [0]; padding: [2] [1] [0] [0];">\n'
+ '<select id="_' +objname+ '_FontSize" onChange="editor_action(this.id)" style="width:38px" unselectable="on">\n'
+ ' <option value=[1]>[1]</option><option value=[2]>[2]</option><option value=[3]>[3]</option><option value=[4]>[4]</option><option value=[5]>[5]</option><option value=[6]>[6]</option><option value=[7]>[7]</option>\n'
+ ' </select>\n\n'
+ ' </td>\n'
+ ' </tr>\n'
+ '</table>\n'

+ '<table border=[0] cellspacing=[2] cellpadding=[0] bgcolor="buttonface" style="float: left;" unselectable="on"><tr><td style="border: inset 1px;">\n'
+ '<button title="Bold" id="_' +objname+ '_Bold" class="btn" onClick="editor_action(this.id)" unselectable="on"><img src="' +imgURL+ 'ed_format_bold.gif" unselectable="on"></button>'
+ '<button title="Italic" id="_' +objname+ '_Italic" class="btn" onClick="editor_action(this.id)" unselectable="on"><img src="' +imgURL+ 'ed_format_italic.gif" unselectable="on"></button>'
+ '<button title="Underline" id="_' +objname+ '_Underline" class="btn" onClick="editor_action(this.id)" unselectable="on"><img src="' +imgURL+ 'ed_format_underline.gif" unselectable="on">\n'
+ '</td></tr></table>\n'

// uncomment to add these buttons
+ '<table border=[0] cellspacing=[2] cellpadding=[0] bgcolor="buttonface" style="float: left;" unselectable="on"><tr><td style="border: inset 1px;">\n'
+ '<button title="Strikethrough" id="_' +objname+ '_StrikeThrough" class="btn" onClick="editor_action(this.id)" unselectable="on"><img src="' +imgURL+ 'ed_format_strike.gif" unselectable="on"></button>'
+ '<button title="Subscript" id="_' +objname+ '_SubScript" class="btn" onClick="editor_action(this.id)" unselectable="on"><img src="' +imgURL+ 'ed_format_sub.gif" unselectable="on"></button>'
+ '<button title="Superscript" id="_' +objname+ '_SuperScript" class="btn" onClick="editor_action(this.id)" unselectable="on"><img src="' +imgURL+ 'ed_format_sup.gif" unselectable="on">\n'
+ '</td></tr></table>\n'

+ '<table border=[0] cellspacing=[2] cellpadding=[0] bgcolor="buttonface" style="float: left;" unselectable="on"><tr><td style="border: inset 1px;">\n'
+ '<button title="Justify Left" id="_' +objname+ '_JustifyLeft" class="btn" onClick="editor_action(this.id)" unselectable="on"><img src="' +imgURL+ 'ed_align_left.gif" unselectable="on"></button>'
+ '<button title="Justify Center" id="_' +objname+ '_JustifyCenter" class="btn" onClick="editor_action(this.id)" unselectable="on"><img src="' +imgURL+ 'ed_align_center.gif" unselectable="on"></button>'
+ '<button title="Justify Right" id="_' +objname+ '_JustifyRight" class="btn" onClick="editor_action(this.id)" unselectable="on"><img src="' +imgURL+ 'ed_align_right.gif" unselectable="on">\n'
+ '</td></tr></table>\n'

+ '<table border=[0] cellspacing=[2] cellpadding=[0] bgcolor="buttonface" style="float: left;" unselectable="on" unselectable="on"><tr><td style="border: inset 1px;">\n'
+ '<button title="Ordered List" id="_' +objname+ '_InsertOrderedList" class="btn" onClick="editor_action(this.id)" unselectable="on"><img src="' +imgURL+ 'ed_list_num.gif" unselectable="on"></button>'
+ '<button title="Bulleted List" id="_' +objname+ '_InsertUnorderedList" class="btn" onClick="editor_action(this.id)" unselectable="on"><img src="' +imgURL+ 'ed_list_bullet.gif" unselectable="on">\n'
+ '<button title="Decrease Indent" id="_' +objname+ '_Outdent" class="btn" onClick="editor_action(this.id)" unselectable="on"><img src="' +imgURL+ 'ed_indent_less.gif" unselectable="on"></button>'
+ '<button title="Increase Indent" id="_' +objname+ '_Indent" class="btn" onClick="editor_action(this.id)" unselectable="on"><img src="' +imgURL+ 'ed_indent_more.gif" unselectable="on">\n'
+ '</td></tr></table><BR><BR>\n'

+ '<table border=[0] cellspacing=[2] cellpadding=[0] bgcolor="buttonface" style="float: left;" unselectable="on" unselectable="on"><tr><td style="border: inset 1px;">\n'
+ '<button title="Font Color" id="_' +objname+ '_ForeColor" class="btn" onClick="editor_action(this.id)" unselectable="on"><img src="' +imgURL+ 'ed_color_fg.gif" unselectable="on"></button>'
+ '<button title="Background Color" id="_' +objname+ '_BackColor" class="btn" onClick="editor_action(this.id)" unselectable="on"><img src="' +imgURL+ 'ed_color_bg.gif" unselectable="on">\n'
+ '</td></tr></table>\n'

+ '<table border=[0] cellspacing=[2] cellpadding=[0] bgcolor="buttonface" style="float: left;" unselectable="on"><tr><td style="border: inset 1px;">\n'
+ '<button title="Horizontal Rule" id="_' +objname+ '_InsertHorizontalRule" class="btn" onClick="editor_action(this.id)" unselectable="on"><img src="' +imgURL+ 'ed_hr.gif" unselectable="on"></button>'
+ '<button title="Insert Web Link" id="_' +objname+ '_CreateLink" class="btn" onClick="editor_action(this.id)" unselectable="on"><img src="' +imgURL+ 'ed_link.gif" unselectable="on"></button>'
+ '<button title="Insert Image" id="_' +objname+ '_InsertImage" class="btn" onClick="editor_action(this.id)" unselectable="on"><img src="' +imgURL+ 'ed_image.gif" unselectable="on">\n'
+ '</td></tr></table>\n'

+ '<table border=[0] cellspacing=[2] cellpadding=[0] bgcolor="buttonface" style="float: left;" unselectable="on"><tr><td style="border: inset 1px;">\n'
// + '<button title="View HTML Source" id="_' +objname+ '_HtmlMode" class="btn" onClick="editor_setmode(this.id)" unselectable="on"><img src="' +imgURL+ 'ed_html.gif" unselectable="on"></button>'

// -----------
// CAUTION: You may NOT remove or hide this button. If displays the "About this editor" popup
// and is required by the license agreement for the editor.
+ '<button title="About this editor" id="_' +objname+ '_about" class="btn" onClick="editor_action(this.id)" unselectable="on"><img src="' +imgURL+ 'ed_about.gif" unselectable="on"></button>'
// -----------

+ '</td></tr></table>\n'
+ '</td></tr></table>\n'

+ '<textarea ID="_' +objname + '_editor" style="width:' +width+ '; height:' +height+ '; margin-top: -1px; margin-bottom: -1px;"></textarea>'

+ '<input type="hidden" name="' +objname+ '" value="">'
// + '<textarea ID="' +objname+ '" rows=[12] cols=[80]></textarea>'
;

// create editor
var contents = document.all[objname].value; // get original contents
document.all[objname].outerHTML = editor; // create editor frame
document.all['_'+objname+'_editor'].value = contents; // set contents

editor_setmode('_' +objname+ '_HtmlMode', 'init'); // switch to wysiwyg mode

}

/* ---------------------------------------------------------------------- * Function : editor_action
Description : perform an editor command on selected editor content
Usage :
Arguments : button_id - button id string with editor and action name
\* ---------------------------------------------------------------------- */


function editor_action(button_id) {

var BtnParts = Array();
BtnParts = button_id.split("_");
var objname = button_id.replace(/^_(.*)_[^_]*$/, '$[1]');
var cmdID = BtnParts[ BtnParts.length-[1] ];
var button_obj = document.all[button_id];
var editor_obj = document.all["_" +objname + "_editor"];

// check editor mode (don't perform actions in textedit mode)
if (editor_obj.tagName.toLowerCase() == 'textarea') { return; }

var editdoc = editor_obj.contentWindow.document;
_editor_focus(editor_obj);

// execute command for font pulldowns
var idx = button_obj.selectedIndex;
if (idx != null) {
var val = button_obj[ idx ].value;
editdoc.execCommand(cmdID,[0],val);
}

// execute command for fgcolor & bgcolor buttons
else if (cmdID == 'ForeColor' || cmdID == 'BackColor') {
// figure our optimal window placement for popup dialog
var posX = event.screenX;
var posY = event.screenY + [20];
var screenW = screen.width; // screen size
var screenH = screen.height - [20]; // take taskbar into account
if (posX + [232] > screenW) { posX = posX - [232] - [40]; } // if mouse too far right
if (posY + [164] > screenH) { posY = posY - [164] - [80]; } // if mouse too far down
var wPosition = "dialogLeft:" +posX+ "; dialogTop:" +posY;

var oldcolor = _dec_to_rgb(editdoc.queryCommandValue(cmdID));
var newcolor = showModalDialog(_editor_url + "select_color.html", oldcolor,
"dialogWidth:238px; dialogHeight: 187px; "
+ "resizable: no; help: no; status: no; scroll: no; "
+ wPosition);
if (newcolor != null) { editdoc.execCommand(cmdID, false, "#"+newcolor); }
}

// execute command for buttons
else {
// subscript & superscript, disable one before enabling the other
if (cmdID.toLowerCase() == 'subscript' && editdoc.queryCommandState('superscript')) { editdoc.execCommand('superscript'); }
if (cmdID.toLowerCase() == 'superscript' && editdoc.queryCommandState('subscript')) { editdoc.execCommand('subscript'); }

// insert link
if (cmdID.toLowerCase() == 'createlink'){
editdoc.execCommand(cmdID,[1]);
}

// insert image
else if (cmdID.toLowerCase() == 'insertimage'){
showModalDialog(_editor_url + "insert_image.html", editdoc, "resizable: no; help: no; status: no; scroll: no; ");
}

// insert image
else if (cmdID.toLowerCase() == 'about'){
var html = '<HTML><HEAD><TITLE>About</TITLE>\n'
+ '<style>\n'
+ ' html,body,textarea { font-family: verdana,arial; font-size: 9pt; };\n'
+ '</style></HEAD>\n'
+ '<BODY style="background: threedface; color: #[000000]" topmargin=[5] leftmargin=[12]>\n\n'
+ '<span style="font-family: arial black, arial; font-size: 28px; letter-spacing: -2px;">htmlArea</span> by interactivetools.com<br>\n'
+ 'A free WYSIWYG editor replacement for <textarea> fields.<br>\n\n'
+ '<p><textarea style="width:[100]%; height:120px" readonly>\n'
+ 'Copyright (c) [2002] interactivetools.com, inc.\n\n'
+ 'Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\n'
+ 'a) The above copyright notice, this permission notice, and the "About this editor" button that appears as a question mark in the editor interface, shall be included in all copies or substantial portions of the Software.\n\n'
+ 'b) The "About this editor" button that appears as a question mark ("?") in the editor interface must always be visible in the editor interface and bring up the original "About" dialog window when clicked.\n\n'
+ 'c) The "About" dialog window and its contents, including the link to interactivetools.com can not be amended.\n\n'
+ 'THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n'
+ '</textarea>\n\n'
+ '<p>For more information visit:<br>\n'
+ '<a href="http://www.interactivetools.com/products/htmlarea/" target="_blank">http://www.interactivetools.com/products/htmlarea/</a><br><br>\n'
+ '</body></html>\n\n';

var popup = window.open('', 'ColorPicker',
"location=no,menubar=no,toolbar=no,directories=no,status=no," +
"height=[275],width=[450],resizable=no,scrollbars=no");
popup.document.write(html);
}

// all other commands
else {
editdoc.execCommand(cmdID);
}
}

editor_updateUI(objname);
}

/* ---------------------------------------------------------------------- * Function : editor_updateUI
Description : update button status, selected fonts, and hidden output field.
Usage :
Arguments : objname - ID of textarea to replace
runDelay: -[1] = run now, no matter what
[0] = run now, if allowed
[1000] = run in [1] sec, if allowed at that point
\* ---------------------------------------------------------------------- */


function editor_updateUI(objname,runDelay) {
var editor_obj = document.all["_" +objname+ "_editor"]; // html editor object
if (runDelay == null) { runDelay = [0]; }
var editdoc, editEvent;

// setup timer for delayed updates (some events take time to complete)
if (runDelay > [0]) { return setTimeout(function(){ editor_updateUI(objname); }, runDelay); }

// don't execute more than [3] times a second (eg: too soon after last execution)
if (this.tooSoon == [1] && runDelay >= [0]) { this.queue = [1]; return; } // queue all but urgent events
this.tooSoon = [1];
setTimeout(function(){
this.tooSoon = [0];
if (this.queue) { editor_updateUI(objname,-[1]); };
this.queue = [0];
}, [333]); // [1]/[3] second

// check editor mode and update hidden output field
if (editor_obj.tagName.toLowerCase() == 'textarea') { // textedit mode
document.all[objname].value = editor_obj.value; // update hidden output field
return;
} else { // WYSIWYG mode
editdoc = editor_obj.contentWindow.document; // get iframe editor document object
editEvent = editor_obj.contentWindow.event;
_fix_placeholder_urls(editdoc);
document.all[objname].value = editdoc.body.innerHTML; // update hidden output field
}

// update button states
var IDList = Array('Bold','Italic','Underline','JustifyLeft','JustifyCenter','JustifyRight','InsertOrderedList','InsertUnorderedList');
for (i=[0]; i<IDList.length; i++) { // for each button
var button_obj = document.all["_" +objname+ "_" +IDList]; // get button object
if (button_obj == null) { continue; } // if no btn obj???
var cmdActive = editdoc.queryCommandState( IDList );

if (!cmdActive) { // option is OK
if (button_obj.className != 'btn') { button_obj.className = 'btn'; }
if (button_obj.disabled != false) { button_obj.disabled = false; }
} else if (cmdActive) { // option already applied or mixed content
if (button_obj.className != 'btnDN') { button_obj.className = 'btnDN'; }
if (button_obj.disabled != false) { button_obj.disabled = false; }
}

}

// Loop over font pulldowns
var IDList = Array('FontName','FontSize');
for (i=[0]; i<IDList.length; i++) {
var cmdActive = editdoc.queryCommandState( IDList );
var button_obj = document.all["_" +objname+ "_" +IDList]; // button object
button_obj.disabled = false;
}

// Get Font Name and Size
var fontname = editdoc.queryCommandValue('FontName');
var fontsize = editdoc.queryCommandValue('FontSize');
if (fontname != null) { fontname = fontname.toLowerCase(); }

// Set Font face pulldown
var fontname_obj = document.all["_" +objname+ "_FontName"];
if (fontname == null) { fontname_obj.value = fontname; }
else {
var foundfont;
var fonts = fontname_obj.length;
for (i=[0]; i<fonts; i++) {
var thisfont = fontname_obj.text.toLowerCase();
if (thisfont == fontname) {
fontname_obj.selectedIndex = i;
foundfont = [1];
}
}
if (foundfont != [1]) { fontname_obj.value = fontname; } // for fonts not in list
}

// Set Font size pulldown
var fontsize_obj = document.all["_" +objname+ "_FontSize"];
if (fontsize == null) { fontsize_obj.value = fontsize;}
else {
for (i=[0]; i<[7]; i++) {
var thissize = fontsize_obj.text;
if (thissize == fontsize) { fontsize_obj.selectedIndex = i; }
}
}
}

/* ---------------------------------------------------------------------- * Function : editor_setmode
Description : change mode between WYSIWYG and HTML editor
Usage : editor_setmode(object_id, mode);
Arguments : button_id - button id string with editor and action name
mode - init, textedit, or wysiwyg
\* ---------------------------------------------------------------------- */


function editor_setmode(button_id, mode) {

var BtnParts = Array();
BtnParts = button_id.split("_");
var objname = button_id.replace(/^_(.*)_[^_]*$/, '$[1]');
var cmdID = BtnParts[ BtnParts.length-[1] ];
var editor_obj = document.all["_" +objname + "_editor"];
var editdoc; // set below


// define different editors
var TextEdit = '<textarea ID="_' +objname + '_editor" style="width:' +editor_obj.style.width+ '; height:' +editor_obj.style.height+ '; margin-top: -1px; margin-bottom: -1px;"></textarea>';
var RichEdit = '<iframe ID="_' +objname+ '_editor" style="width:' +editor_obj.style.width+ '; height:' +editor_obj.style.height+ ';"></iframe>';

//
// Switch to TEXTEDIT mode
//

if (mode == "textedit" || editor_obj.tagName.toLowerCase() == 'iframe') {
editdoc = editor_obj.contentWindow.document;
var contents = editdoc.body.createTextRange().htmlText;
editor_obj.outerHTML = TextEdit;
editor_obj = document.all["_" +objname + "_editor"];
editor_obj.value = contents;
editor_updateUI(objname);

// disable buttons
var IDList = Array('Bold','Italic','Underline','StrikeThrough','SubScript','SuperScript','JustifyLeft','JustifyCenter','JustifyRight','InsertOrderedList','InsertUnorderedList','Outdent','Indent','ForeColor','BackColor','InsertHorizontalRule','CreateLink','InsertImage');
for (i=[0]; i<IDList.length; i++) { // for each button
var button_obj = document.all["_" +objname+ "_" +IDList]; // get button object
if (button_obj == null) { continue; } // if no btn obj???
button_obj.className = 'btnNA';
button_obj.disabled = true;
}

// disable font pulldowns
var IDList = Array('FontName','FontSize');
for (i=[0]; i<IDList.length; i++) {
var button_obj = document.all["_" +objname+ "_" +IDList]; // button object
if (button_obj == null) { continue; } // if no btn obj???
button_obj.disabled = true;
}

// set event handlers
editor_obj.onkeypress = function() { editor_updateUI(objname); }
editor_obj.onkeyup = function() { editor_updateUI(objname); }
editor_obj.onmouseup = function() { editor_updateUI(objname); }
editor_obj.ondrop = function() { editor_updateUI(objname, [100]); } // these events fire before they occur
editor_obj.oncut = function() { editor_updateUI(objname, [100]); }
editor_obj.onpaste = function() { editor_updateUI(objname, [100]); }
editor_obj.onblur = function() { editor_updateUI(objname, -[1]); }

// update hidden output field
document.all[objname].value = editor_obj.value;

_editor_focus(editor_obj);
}

//
// Switch to WYSIWYG mode
//

else {
var contents = editor_obj.value;

// create editor
editor_obj.outerHTML = RichEdit;
editor_obj = document.all["_" +objname + "_editor"];

// get iframe document object
editdoc = editor_obj.contentWindow.document;

// set editor contents (and default styles for editor)
editdoc.open();
editdoc.write(''
+ '<html><head>\n'
+ '<style>\n'
+ 'body { background-color: #2C2C30; color: #FFFFFF; font-family: "Verdana"; font-size: x-small; } \n'
+ '</style>\n'
+ '</head>\n'
+ '<body contenteditable="true" topmargin=[1] leftmargin=[1]>'
+ contents
+ '</body>\n'
+ '</html>\n'
);
editdoc.close();
editdoc.execCommand('ForeColor', false, "#FFFFFF");

// enable buttons
var IDList = Array('Bold','Italic','Underline','StrikeThrough','SubScript','SuperScript','JustifyLeft','JustifyCenter','JustifyRight','InsertOrderedList','InsertUnorderedList','Outdent','Indent','ForeColor','BackColor','InsertHorizontalRule','CreateLink','InsertImage');
for (i=[0]; i<IDList.length; i++) {
var button_obj = document.all["_" +objname+ "_" +IDList];
if (button_obj == null) { continue; }
button_obj.className = 'btn';
button_obj.disabled = false;
}

// set event handlers
editdoc.onkeypress = function() { editor_updateUI(objname); }
editdoc.onkeyup = function() { editor_updateUI(objname); }
editdoc.onmouseup = function() { editor_updateUI(objname); }
editdoc.body.ondrop = function() { editor_updateUI(objname, [100]); } // these events fire before they occur
editdoc.body.oncut = function() { editor_updateUI(objname, [100]); }
editdoc.body.onpaste = function() { editor_updateUI(objname, [100]); }
editdoc.body.onblur = function() { editor_updateUI(objname, -[1]); }

// set initial value
editor_obj.onload = function() { editdoc.body.innerHTML = document.all[objname].value; }

// update hidden output field
_fix_placeholder_urls(editdoc);
document.all[objname].value = editdoc.body.innerHTML; // update hidden output field

// bring focus to editor
if (mode != 'init') { // don't focus on page load, only on mode switch
_editor_focus(editor_obj);
}

}

// Call update UI
if (mode != 'init') { // don't update UI on page load, only on mode switch
editor_updateUI(objname);
}

}

/* ---------------------------------------------------------------------- * Function : _editor_focus
Description : bring focus to the editor
Usage : editor_focus(editor_obj);
Arguments : editor_obj - editor object
\* ---------------------------------------------------------------------- */


function _editor_focus(editor_obj) {

// check editor mode
if (editor_obj.tagName.toLowerCase() == 'textarea') { // textarea
var myfunc = function() { editor_obj.focus(); };
setTimeout(myfunc,[100]); // doesn't work all the time without delay
}

else { // wysiwyg
var editdoc = editor_obj.contentWindow.document; // get iframe editor document object
var editorRange = editdoc.body.createTextRange(); // editor range
var curRange = editdoc.selection.createRange(); // selection range

if (curRange.length == null && // make sure it's not a controlRange
!editorRange.inRange(curRange)) { // is selection in editor range
editorRange.collapse(); // move to start of range
editorRange.select(); // select
curRange = editorRange;
}
}

}

/* ---------------------------------------------------------------------- * Function : _dec_to_rgb
Description : convert dec color value to rgb hex
Usage : var hex = _dec_to_rgb('[65535]'); // returns FFFF00
Arguments : value - dec value
\* ---------------------------------------------------------------------- */


function _dec_to_rgb(value) {
var hex_string = "";
for (var hexpair = [0]; hexpair < [3]; hexpair++) {
var byte = value & 0xFF; // get low byte
value >>= [8]; // drop low byte
var nybble2 = byte & 0x0F; // get low nybble ([4] bits)
var nybble1 = (byte >> [4]) & 0x0F; // get high nybble
hex_string += nybble1.toString([16]); // convert nybble to hex
hex_string += nybble2.toString([16]); // convert nybble to hex
}
return hex_string.toUpperCase();
}

/* ---------------------------------------------------------------------- * Function : _fix_placeholder_urls
Description : editor make relative urls absolute, this change them back
if the url contains a placeholder ("***")
Usage : _fix_placeholder_urls(editdoc)
Arguments : editdoc - reference to editor document
\* ---------------------------------------------------------------------- */


function _fix_placeholder_urls(editdoc) {
var i;

// for links
for (i=[0]; i < editdoc.links.length; i++) {
editdoc.links.href = editdoc.links.href.replace(/^[^*]*(\*\*\*)/, "$[1]");
}

// for images
for (i=[0]; i < editdoc.images.length; i++) {
editdoc.images.src = editdoc.images.src.replace(/^[^*]*(\*\*\*)/, "$[1]");
}

}

/* ---------------------------------------------------------------------- * Function : editor_insertHTML
Description : insert string at current cursor position in editor. If
two strings are specifed, surround selected text with them.
Usage : editor_insertHTML(objname, str1, [str2], reqSelection)
Arguments : objname - ID of textarea
str1 - HTML or text to insert
str2 - HTML or text to insert (optional argument)
reqSelection - ([1] or [0]) give error if no text selected
\* ---------------------------------------------------------------------- */


function editor_insertHTML(objname, str1,str2, reqSel) {
var editor_obj = document.all["_" +objname + "_editor"]; // editor object
if (str1 == null) { str1 = ''; }
if (str2 == null) { str2 = ''; }

// for non-wysiwyg capable browsers just add to end of textbox
if (document.all[objname] && editor_obj == null) {
document.all[objname].focus();
document.all[objname].value = document.all[objname].value + str1 + str2;
return;
}

// error checking
if (editor_obj == null) { return alert("Unable to insert HTML. Invalid object name '" +objname+ "'."); }

_editor_focus(editor_obj);

var tagname = editor_obj.tagName.toLowerCase();
var sRange;

// insertHTML for wysiwyg iframe
if (tagname == 'iframe') {
var editdoc = editor_obj.contentWindow.document;
sRange = editdoc.selection.createRange();
var sHtml = sRange.htmlText;

// check for control ranges
if (sRange.length) { return alert("Unable to insert HTML. Try highlighting content instead of selecting it."); }

// insert HTML
var oldHandler = window.onerror;
window.onerror = function() { alert("Unable to insert HTML for current selection."); return true; } // partial table selections cause errors
if (sHtml.length) { // if content selected
if (str2) { sRange.pasteHTML(str1 +sHtml+ str2) } // surround
else { sRange.pasteHTML(str1); } // overwrite
} else { // if insertion point only
if (reqSel) { return alert("Unable to insert HTML. You must select something first."); }
sRange.pasteHTML(str1 + str2); // insert strings
}
window.onerror = oldHandler;
}

// insertHTML for plaintext textarea
else if (tagname == 'textarea') {
editor_obj.focus();
sRange = document.selection.createRange();
var sText = sRange.text;

// insert HTML
if (sText.length) { // if content selected
if (str2) { sRange.text = str1 +sText+ str2; } // surround
else { sRange.text = str1; } // overwrite
} else { // if insertion point only
if (reqSel) { return alert("Unable to insert HTML. You must select something first."); }
sRange.text = str1 + str2; // insert strings
}
}
else { alert("Unable to insert HTML. Unknown object tag type '" +tagname+ "'."); }

// move to end of new content
sRange.collapse(false); // move to end of range
sRange.select(); // re-select

}

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


Share this post


Link to post
Share on other sites
Advertisement
I was just testing out the new syntax highlighter.. I wrote it to support c/c++,c#,vb,vbscript,jscript,javascript,x86 assembly, and java.

---
Michael Tanczos

Share this post


Link to post
Share on other sites
Just one line. . .


Cool, the css changes depending on the browser used. I'm going to have to change my user agent string to read something other than IE3 to use this wonderful new feature, though.

Share this post


Link to post
Share on other sites
This is borked. The post above didn't have the [] surrounding the 0 when I made the post. It's showed up in a couple of other people's posts as well.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
This is borked. The post above didn't have the [] surrounding the 0 when I made the post. It's showed up in a couple of other people's posts as well.


WHoooooooooops.. late change. I used to only show numbers if they were an index of an array, but later changed the code to highlight all numbers. Forgot to take out the brackets. =P

---
Michael Tanczos

Share this post


Link to post
Share on other sites
Python:



"""
simpleminded session save/restore based on an idea by Gerrit Holl

readline module required! (implies won't work on Windows)

mark the beginning of a session by calling mark(). Save the current
session since the last mark() call by calling save(). Load a saved
session by calling load().

load from your PYTHONSTARTUP file as

from save_session import mark, save, load

"""


__all__ = ['mark', 'save', 'load']

try:
import readline
except ImportError:
raise ImportError, "session save/restore requires readline module"

import os
import sys

# needs to match the filename the user uses!
_histfile = os.path.expanduser("~/.pyhist")

# where we save sessions
_session_file = os.path.expanduser("~/.pysession")

# mark with something valid at the interpreter but unlikely to be executed
# by the user
_marker = '((1.0+999.0j, "mark", 999.0-1.0j))'

def save():
"""save the readline history since last call to mark()"""
end = readline.get_current_history_length() - 1
session = []
item = readline.get_history_item(end)
while item != _marker:
session.insert(0, item+"\n")
end -= 1
item = readline.get_history_item(end)

file(_session_file, 'w').writelines(session)
print >> sys.stderr, "saved session to", _session_file

def load():
"""load the last session saved by a call to save()"""
try:
execfile(_session_file, sys._getframe(1).f_globals)
except:
print >> sys.stderr, ("Session load failed. Check for error in %s" %
_session_file)
else:
print >> sys.stderr, "loaded session from", _session_file

def mark():
"""mark the start of a new session to save later"""
readline.add_history(_marker)



#!/usr/bin/env python

"""Demonstrate one heuristic approach to determining a string's encoding."""

import sys

def decode_heuristically(s, enc=None, denc=sys.getdefaultencoding()):
"""Try interpreting s using several possible encodings.
The return value is a three-element tuple. The first element is either an
ASCII string or a Unicode object. The second element is 1
if the decoder had to punt and delete some characters from the input
to successfully generate a Unicode object."""

if isinstance(s, unicode):
return s, 0, "utf-8"
try:
x = unicode(s, "ascii")
# if it's ascii, we're done
return s, 0, "ascii"
except UnicodeError:
encodings = ["utf-8","iso-8859-1","cp1252","iso-8859-15"]
# if the default encoding is not ascii it's a good thing to try
if denc != "ascii": encodings.insert(0, denc)
# always try any caller-provided encoding first
if enc: encodings.insert(0, enc)
for enc in encodings:

# Most of the characters between 0x80 and 0x9F are displayable
# in cp1252 but are control characters in iso-8859-1. Skip
# iso-8859-1 if they are found, even though the unicode() call
# might well succeed.

if (enc in ("iso-8859-15", "iso-8859-1") and
re.search(r"[\x80-\x9f]", s) is not None):
continue

# Characters in the given range are more likely to be
# symbols used in iso-8859-15, so even though unicode()
# may accept such strings with those encodings, skip them.

if (enc in ("iso-8859-1", "cp1252") and
re.search(r"[\xa4\xa6\xa8\xb4\xb8\xbc-\xbe]", s) is not None):
continue

try:
x = unicode(s, enc)
except UnicodeError:
pass
else:
if x.encode(enc) == s:
return x, 0, enc

# nothing worked perfectly - try again, but use the "ignore" parameter
# and return the longest result
output = [(unicode(s, enc, "ignore"), enc) for enc in encodings]
output = [(len(x), x) for x in output]
output.sort()
x, enc = output[-1][1]
return x, 1, enc

def decode_by_counting(s, enc=None, denc=sys.getdefaultencoding(),
_str="strict"):
"""Try interpreting s using several possible encodings.
The return value is as above for decode_heuristically but uses
a different method from David Eppstein:
http://mail.python.org/pipermail/python-list/2004-April/215185.html
"""

if isinstance(s, unicode):
return s, 0, "utf-8"
try:
x = unicode(s, "ascii")
# if it's ascii, we're done
return s, 0, "ascii"
except UnicodeError:
encodings = ["utf-8","iso-8859-1","cp1252","iso-8859-15"]
# if the default encoding is not ascii it's a good thing to try
if denc != "ascii": encodings.insert(0, denc)
# always try any caller-provided encoding first
if enc: encodings.insert(0, enc)
scores = []
for enc in encodings:
try:
x = unicode(s, enc, _str)
except UnicodeError:
score = -len(s)
else:
score = len([c for c in x if c.isalnum() or c.isspace()])
scores.append((score, x, enc))
if scores:
scores.sort()
score, x, enc = scores[-1]
return x, 0, enc
if _str == "strict":
x, punt, enc = return decode_by_counting(s, enc, denc, "ignore")
return x, 1, enc





#
# This file contains the Python code from Program 4.9 of
# "Data Structures and Algorithms
# with Object-Oriented Design Patterns in Python"
# by Bruno R. Preiss.
#
# Copyright (c) 2003 by Bruno R. Preiss, P.Eng. All rights reserved.
#
# http://www.brpreiss.com/books/opus7/programs/pgm04_09.txt
#
class DenseMatrix(Matrix):

def __init__(self, rows, cols):
super(DenseMatrix, self).__init__(rows, cols)
self._array = MultiDimensionalArray(rows, cols)

def __getitem__(self, (i, j)):
return self._array[i,j]

def __setitem__(self, (i, j), value):
self._array[i,j] = value

# ...



# Vigenere Cipher Program
#

#import everything
import os, os.path, string

#Pretty Title
print "*************************************"
print "* Vigenere Coder / Decoder *"
print "*************************************"
print

# Get input directory, see if it exists, and change into it
def get_dir():
directory = raw_input("Path to text file directory: ")
if not os.path.isdir(directory):
print "Invalid directory"
directory = get_dir()
return directory
directory = get_dir()
os.chdir(directory)

#Get filename, test for file
def get_file():
input_file = raw_input("Name of text file: ")
if not os.path.isfile(input_file):
print "Invalid file name."
input_file = get_file()
return input_file
in_file = get_file()

# Open and read input text
source = open(in_file, "r")
source_txt = source.read()
source.close()

#put in standard case to make encode easier
item = string.lower(source_txt)

#Encode or decode?
cypher = raw_input("[E]ncode or [D]ecode: ")
cypher = string.upper(cypher)

#prepare outfile
out_name = raw_input("Name of Output File: ")
outfile = open(out_name, "a")
out_text = []

#Request Code / Decode Key
key = raw_input("Cypher Key: ")
key = string.lower(key)


#make len(keyword) equal len(item)
padded_key = key
while len(padded_key) < len(source_txt):
padded_key = padded_key + key


#convert padded_key to ascii number
num_key = []
for p in padded_key:
num_p = ord(p)
num_key.append(num_p)


#convert source_txt to ascii number
num_source = []
for i in source_txt:
num_i = ord(i)
num_source.append(num_i)


#add num values together
num_cypher = []
index = 0
for num in num_source:
if cypher == "E":
new_num = num_source[index] + num_key[index]
elif cypher == "D":
new_num = num_source[index] - num_key[index]
else:
pass
#Adjust for numbers too big
if new_num > 255:
new_num = (new_num - 255)
elif new_num < 0:
new_num = (new_num + 255)
else:
pass
num_cypher.append(new_num)
index = index + 1

#convert ascii num to letters
cypher_chr = ""
for nc in num_cypher:
cypher_chr = cypher_chr + chr(nc)

#Write new text to out file
outfile.write(cypher_chr)
outfile.close()

#Tell them we are all done now
if cypher == "E":
print "Text encrypted."
elif cypher =="D":
print "Text decoded."
else:
print "I don't know what happened. Probably nothing."

Share this post


Link to post
Share on other sites
degenerate python source test case:



"""
a multiline string literal containing a " character
heck throw in a ' character while we're at it
"""


import sys

"""
a multiline string literal containing a " character
heck throw in a ' character while we're at it
"""

Share this post


Link to post
Share on other sites
Variant:


'''
a multiline string literal containing a ' character
heck throw in a " character while we're at it
'''


import sys

'''
a multiline string literal containing a ' character
heck throw in a " character while we're at it
'''


-> Eats one of the '

Oh by the way, is there a way to fix line continuations ( \ at end of line), it screws with multiline preprocessor macros.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!