473,769 Members | 2,220 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Rich Text Editor - problem with generated HTML

I'm doing a Rich Text Editor (WYSIWYG) in javascript for a game for
kids. I'm doing a special case in with every keystroke from A-Z creates
a background and foreground color for that letter, witch is the same.
The problem is editing doesn't work that well.

For example when I type: ABCDFG , I get this code generated and
displayed by the Rich Text Editor:

<P><FONT style="BACKGROU ND-COLOR: #ffff00" color=#ffff00>a <FONT
style="BACKGROU ND-COLOR: #3399cc" color=#3399cc>b <FONT
style="BACKGROU ND-COLOR: #ffccff" color=#ffccff>c <FONT
style="BACKGROU ND-COLOR: #cccccc" color=#cccccc>d <FONT
style="BACKGROU ND-COLOR: #ffcc99" color=#ffcc99>f <FONT
style="BACKGROU ND-COLOR: #d21894"
color=#d21894>g </FONT></FONT></FONT></FONT></FONT></FONT></P>

The markup looks ugly but it works.

If I put my cursor between the "D" and "F", for example, and try to
insert the letter "E" I get this code:

<P><FONT style="BACKGROU ND-COLOR: #ff9900"
color=#ff9900>a bcdefg</FONT></P>

I just want the letter "E" inserted in its own tag without overwriting
the other letters. The whole line ends with the color of "E". :-(

By the way, I'm using execCommand wih IE6 sp2. If you think or know a
better way of solving this without execCommand or anything, please help
:~(

Any ideas, suggestions, code samples?

Thank you...

Jul 11 '06 #1
4 3124
Welcome to the world of wysiwyg editing. I'm currently working on an
editor for a social networking site, and have found that the code
generated is usually not pretty. How does your logic work when you
press a key? I'm guessing that it's something like
on key down
1. get the letter from the keycode
2. execCommand to set corresponding fore / back color for that
letter.

Instead of on keydown, maybe on keypress, get all the text nodes from
the editor document, and if they don't have a parent node of font (or
span, whatever you want), assign a parent node with the color you want.

Something like (mix of pseudo and javascript)
on keypress
nodes = editor.document .childNodes;
foreach in nodes as i
if nodes[ i ] != text node
continue;
color = possible_colors[ nodes[ i ].lowerCase ];
span_tag = new span tag;
span_tag.color = color;
span_tag.append Child( nodes[ i ].cloneNode( false ) )
editor.document .replaceChild( span_tag, nodes[ i ] )

pbreah wrote:
I'm doing a Rich Text Editor (WYSIWYG) in javascript for a game for
kids. I'm doing a special case in with every keystroke from A-Z creates
a background and foreground color for that letter, witch is the same.
The problem is editing doesn't work that well.

For example when I type: ABCDFG , I get this code generated and
displayed by the Rich Text Editor:

<P><FONT style="BACKGROU ND-COLOR: #ffff00" color=#ffff00>a <FONT
style="BACKGROU ND-COLOR: #3399cc" color=#3399cc>b <FONT
style="BACKGROU ND-COLOR: #ffccff" color=#ffccff>c <FONT
style="BACKGROU ND-COLOR: #cccccc" color=#cccccc>d <FONT
style="BACKGROU ND-COLOR: #ffcc99" color=#ffcc99>f <FONT
style="BACKGROU ND-COLOR: #d21894"
color=#d21894>g </FONT></FONT></FONT></FONT></FONT></FONT></P>

The markup looks ugly but it works.

If I put my cursor between the "D" and "F", for example, and try to
insert the letter "E" I get this code:

<P><FONT style="BACKGROU ND-COLOR: #ff9900"
color=#ff9900>a bcdefg</FONT></P>

I just want the letter "E" inserted in its own tag without overwriting
the other letters. The whole line ends with the color of "E". :-(

By the way, I'm using execCommand wih IE6 sp2. If you think or know a
better way of solving this without execCommand or anything, please help
:~(

Any ideas, suggestions, code samples?

Thank you...
Jul 11 '06 #2
I realized this approach won't work, since your text is likely to be
inside of various tags. Therefore iterating through document.childN odes
won't work. It'd have to be a recursive function to catch all the text
nodes. Sorry
bobzimuta wrote:
Welcome to the world of wysiwyg editing. I'm currently working on an
editor for a social networking site, and have found that the code
generated is usually not pretty. How does your logic work when you
press a key? I'm guessing that it's something like
on key down
1. get the letter from the keycode
2. execCommand to set corresponding fore / back color for that
letter.

Instead of on keydown, maybe on keypress, get all the text nodes from
the editor document, and if they don't have a parent node of font (or
span, whatever you want), assign a parent node with the color you want.

Something like (mix of pseudo and javascript)
on keypress
nodes = editor.document .childNodes;
foreach in nodes as i
if nodes[ i ] != text node
continue;
color = possible_colors[ nodes[ i ].lowerCase ];
span_tag = new span tag;
span_tag.color = color;
span_tag.append Child( nodes[ i ].cloneNode( false ) )
editor.document .replaceChild( span_tag, nodes[ i ] )

pbreah wrote:
I'm doing a Rich Text Editor (WYSIWYG) in javascript for a game for
kids. I'm doing a special case in with every keystroke from A-Z creates
a background and foreground color for that letter, witch is the same.
The problem is editing doesn't work that well.

For example when I type: ABCDFG , I get this code generated and
displayed by the Rich Text Editor:

<P><FONT style="BACKGROU ND-COLOR: #ffff00" color=#ffff00>a <FONT
style="BACKGROU ND-COLOR: #3399cc" color=#3399cc>b <FONT
style="BACKGROU ND-COLOR: #ffccff" color=#ffccff>c <FONT
style="BACKGROU ND-COLOR: #cccccc" color=#cccccc>d <FONT
style="BACKGROU ND-COLOR: #ffcc99" color=#ffcc99>f <FONT
style="BACKGROU ND-COLOR: #d21894"
color=#d21894>g </FONT></FONT></FONT></FONT></FONT></FONT></P>

The markup looks ugly but it works.

If I put my cursor between the "D" and "F", for example, and try to
insert the letter "E" I get this code:

<P><FONT style="BACKGROU ND-COLOR: #ff9900"
color=#ff9900>a bcdefg</FONT></P>

I just want the letter "E" inserted in its own tag without overwriting
the other letters. The whole line ends with the color of "E". :-(

By the way, I'm using execCommand wih IE6 sp2. If you think or know a
better way of solving this without execCommand or anything, please help
:~(

Any ideas, suggestions, code samples?

Thank you...
Jul 11 '06 #3
I use an on keypress event function. It calls a forecolor and backcolor
function. Like so: (this is the code i'm working on). I'm trying to get
it to work...

function kb_handler(evt) {
evt = evt || window.event;
var keyCode = evt.keyCode || evt.charCode;
var key = String.fromChar Code(keyCode).t oLowerCase();
var rte = 'rte1';

if(keyCode == 32)
{
dlgColorPalette (rte, 'hilitecolor', '');
setColor("#0000 00");
dlgColorPalette (rte, 'forecolor', '');
setColor("#0000 00");
}
else
{
switch (key)
{
case 'a':
dlgColorPalette (rte, 'hilitecolor', '');
setColor("#ffff 00");
dlgColorPalette (rte, 'forecolor', '');
setColor("#ffff 00");
break;
// .... and so on for all letters...
default:
dlgColorPalette (rte, 'hilitecolor', '');
setColor('#0000 00');
dlgColorPalette (rte, 'forecolor', '');
setColor('#ffff ff');
}
}
}

function setColor(color) {
var rte = currentRTE;
var parentCommand = parent.command;

if (document.all) {
var sel = frames[rte].document.selec tion;
if (parentCommand == "hilitecolo r") parentCommand = "backcolor" ;
if (sel != null) {
var newRng = sel.createRange ();
newRng = rng;
newRng.select() ;
}
}

rteCommand(rte, parentCommand, color);
showHideElement ('cp' + rte, "hide");
}

function dlgColorPalette (rte, command) {
setRange(rte);
parent.command = command;
currentRTE = rte;
}

function setRange(rte) {
var oRTE;
if (document.all) {
oRTE = frames[rte];
var selection = oRTE.document.s election;
if (selection != null) rng = selection.creat eRange();
} else {
oRTE = document.getEle mentById(rte).c ontentWindow;
var selection = oRTE.getSelecti on();
rng = selection.getRa ngeAt(selection .rangeCount - 1).cloneRange() ;
}
}

function rteCommand(rte, command, option) {
//function to perform command
var oRTE;
if (document.all) {
oRTE = frames[rte];
} else {
oRTE = document.getEle mentById(rte).c ontentWindow;
}

try {
oRTE.focus();
oRTE.document.e xecCommand(comm and, false, option);
oRTE.focus();
} catch (e) {
// alert(e);
// setTimeout("rte Command('" + rte + "', '" + command + "', '" +
option + "');", 10);
}
}

Jul 11 '06 #4
Any more ideas...? please help...

Jul 12 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
5134
by: somaboy mx | last post by:
I'm looking for a javascript-based rich text/wysiwyg html editor that can be easily integrated in a php cms. I don't require any advanced features (tables, images etc) , just basic text formatting. Of course, it should be preferrably free. Does anyone have a suggestion? ..soma
1
3770
by: IkBenHet | last post by:
Hello, I found this script to create a simple rich text form (http://programmabilities.com/xml/index.php?id=17): <html> <head> <title>Rich Text Editor</title> </head> <body>
1
7769
by: PC User | last post by:
I found this Rich Text Editor and I've been trying to recreate it in my own application. I've had trouble with the COMCTL.ImageListCtrl and the COMCTL.Toolbar to recreate the toolbar. And I've had trouble with COMCTL.SBarCtrl in recreating the status bar. Does anyone have any experience with these or know of any website that explaines how to use these? http://www.access-programmers.co.uk/forums/attachment.php?attachmentid=5545 ...
2
2996
by: VB Programmer | last post by:
I want to create an email message with a HTML body and email it out. I know how to create/send the email (for the most part.) 1. Any ideas or examples of using an ASP.NET rich text editor so that a user can edit rich text, then save the results as an HTML page (either straight to a page or into a db)? 2. How about the opposite: An HTML page the is opened up in the editor so the user can modify it (with rich text) and save it (like to...
8
1533
by: bizsolutiondev | last post by:
Hi all, I am looking for a browser side WYSIWIG HTML editor for our ASP.NET based content management system (CMS). For the past few months, I've had a Firefox RTE solution in place, and it worked marginally well, although we've encountered enough issues to make us look elsewhere. The HTML code it generated was, oft times, a complete mess. Is there an industry-standard solution that delivers clean HTML code?
7
2990
by: Prasad | last post by:
Hi all, I am trying to develop a simple rich text editor I do only require bold, itlaic, underline.. The code for IE is <script> function displayEditor(editor, html, width, height) { document.writeln('<iframe id="' + editor + '" name="' + editor +
36
9344
by: karen987 | last post by:
My newsweblog has ASP pages with news articles and a commenting system. The comments are posted, and to read them the reader clicks on the headline of the comment, and it opens up in a pop up window. I added this "whizzywig rich text editor" to the comments textbox where readers comment on articles. Whizzywig is a javascript rich text editor, and appears to work fine. I type the comment using the editor when i need to and then click...
11
5378
by: karen987 | last post by:
I have a web page which used to work fine until i added a rich text editor. the pages are in ASP and it is a news weblog, with a comments section where people click the headline of a comment and it opens into a pop up page ccalled "comment_view.asp" which has a reply button. If anyone wants to reply to the message, they click reply and it takes them to the parent page (view.asp) where there is a form for them to fill in,and submit the...
16
11134
by: Neil | last post by:
I posted a few days ago that it seems to me that the Access 2007 rich text feature does not support: a) full text justification; b) programmatic manipulation. I was hoping that someone might know one way or the other whether that was true or not, or could point me to an article or help text that would. What I have seen so far online and in Access 2007 help seems to confirm the above. But that (or at least (b)) seems incredible that it...
0
9579
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9422
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10206
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9984
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9851
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7403
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5293
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.