Fog Creek Software
Discussion Board

Text editing & formatting

Hi there! I want to write a simple text editor and have some basic formatting capabilities. I know how to handle the text how to edit it and how to know things like what part of the text is highlighted?

Now my question: How should I do formatting? The end result will be HTML so there will be <strong> and <em> Tags and all that. But what if there is a part of the text in bold and then the user selects some part of that together with some other part and makes that bold as well? How can I make sure I won't have strange nested tags?

I think in order to do it all well I have to store the formatting with every single character and condense that to the HTML markup afterwards. Any better ideas?

Tuesday, February 24, 2004

Consider actually editing a DOM.

i like i
Tuesday, February 24, 2004

One way that I've seen this handled (class ComposedText in the VisualWorks Smalltalk system) is to define a class called "Region" that has a Style, a FirstPosition, and a LastPosition as basic properties.  The implication is that the Style applies to all of the characters between FirstPosition and LastPosition.  To manage the formatting, you manage the list of Regions over the text. 

The benefit of using this approach is that you can build logic into the Region to make region maintenance easy.  If you put a "color=red" formatting into the middle of a region that is "bold", you can have the region break itself up into three new regions, the first part that is bold, the middle part that is bold and red, and the end that is bold.  Similarly for other operations like region merging.  You can then walk the regions and emit the proper tags for HTML.

another idea
Tuesday, February 24, 2004

The GoF 'Design Patterns' book uses a text editor (called 'Lexi' IIRC) as a sample application for showing you how they work and interact. Sounds like a good start to me.

Chris Winters
Tuesday, February 24, 2004

How would you do this without a DOM?

Wednesday, February 25, 2004

*  Recent Topics

*  Fog Creek Home