On 20 March 2011 00:12, Will Rock <nospamchic...@gmail.com> 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/jsmentors@jsmentors.com/ To search via a non-Google archive, visit here: http://www.mail-archive.com/jsmentors@googlegroups.com/ To unsubscribe from this group, send email to jsmentors+unsubscr...@googlegroups.com