On Tue, Mar 22, 2011 at 11:54 AM, Tim Down <[email protected]> wrote: > On 20 March 2011 00:12, Will Rock <[email protected]> wrote: >> Hey all, >> >> I'm playing around with contentEditable and I'm current working with >> styling (bold, italic, etc...) of the current selection >> (window.getSelection()). Now, I know I can do this via >> document.execCommand (in certain browsers), but I'd like to implement >> my own method. I can easily apply a style node (b, i, u, etc...) to >> the selected text, but I also know that I need to be aware that the >> style may already be applied to parts of the (or the entire) >> selection. For instance, selecting part of a bold sentence, then >> applying bold to that part will result in that part being plain, and >> the original bold element will be broken up into 2 bold elements with >> textnodes, and just a textnode in between them. >> >> So, I'm wondering if there exists a pattern for tree traversal to >> check for this type of thing. For those of you not familar with the >> Selection object, you get a startContainer and endContainer for the >> start and end node of the current selection, respectively. You also >> get a commonAncestor. All of the elements are there, I just don't want >> to re-invent the wheel. >> >> Thanks! > > Two things: first, I've implemented this in Rangy, my cross-browser > range and selection library (http://code.google.com/p/rangy/). There's > a module that applies/removes a CSS class to a range by surrounding > and removing spans around text nodes within the range > (http://code.google.com/p/rangy/wiki/CSSClassApplierModule), and I'm > working on a more generic module that will provide an alternative to > document.execCommand() (demo here, using an example "bold" command). > > Second, WHATWG and specifically a man called Aryeh Gregor is > attempting to standardize document.execCommand() at the moment. Aryeh > has written an algorithm for running commands and some example code, > although it's a long way from being finalized. Here's a sample mail > from the thread, also with links to the current working version of the > spec and example code: > http://lists.whatwg.org/htdig.cgi/whatwg-whatwg.org/2011-March/030930.html. > When this stabilizes a little, I intend to implement Aryeh's algorithm > in Rangy. > > Tim > > -- > To view archived discussions from the original JSMentors Mailman list: > http://www.mail-archive.com/[email protected]/ > > To search via a non-Google archive, visit here: > http://www.mail-archive.com/[email protected]/ > > To unsubscribe from this group, send email to > [email protected] >
Yes, I have read his very good comments about this on the "whathwg" list. What he is doing is really appreciated, I am encountering that problem myself in the text produced by my old Midas API rich text component. This will solve the tags multiplication happening in the source HTML when people unaware of this fact use it as a minimal text processor. My temporary fix has since been using server-side DOM processing/replacements before inserting content in the db but I would rather avoid having to do that on the server. When appropriate server security enforcements should block the write and warn the users, especially, the text produced should not be modified without the user knowing it. Let's hope next releases of the HTML specifications include Aryeh work/ideas/improvements. -- Diego -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/[email protected]/ To search via a non-Google archive, visit here: http://www.mail-archive.com/[email protected]/ To unsubscribe from this group, send email to [email protected]
