[whatwg] Proposed updates for the command API's

Ian Hickson ian at hixie.ch
Wed Aug 13 14:55:34 PDT 2008

On Wed, 13 Aug 2008, Dav Glass wrote:
> If I wanted to make a button that took the selection, and opened a more 
> info window for extra data (just a simple example).

Why not do:

   function showInfo() {

     // get a copy of the current selection ranges
     var ranges = [];
     var selection = getSelection();
     for (var i = 0; i < selection.rangeCount; i += 1)

     // get list of elements in range
     var elements = [];
     var traversal = document.createTreeWalker(document, 1, null, false);
     function boundaryToNode(container, offset) {
       var element = container;
       if (element.nodeType == 1) {
         if (offset >= element.length) {
           traversal.currentNode = element;
           return traversal.nextNode();
         } else {
           return element.childNodes[offset];
     for (var i = 0; i < ranges.length; i += 1) {
       var end = boundaryToNode(ranges[i].endContainer, 
       traversal.currentNode = boundaryToNode(ranges[i].startContainer, 
       do {
         if (traversal.currentNode.nodeType == 1)
       } while (traversal.nextNode() != end);

     // do whatever you want with elements
     // ...

> Using this method, I could add a class to the selection. Then open a 
> more info window, which would force the selection to be removed (that I 
> could cache).
> But I have access to all the items that it modified, not just the range 
> that it changed. Since I have access to all of the nodes that changed, I 
> can manipulate them after the selection has been removed and focus has 
> been lost.
> Then when I am finished, I can use my cached data from the selection to 
> create the selection again.

It seems like you can already do this in a more flexible manner with the 
current selection, range and traversal features, no?

> AFAIK, a selection object gives you the data about some of the elements, 
> but not all of the containing elements (especially in the situation I 
> have as an example on the site).

I'm not sure I understand what you mean.

> There is also the use case of someone selecting part of a node (like in 
> between tags) and executing a command, some commands will auto select 
> the node others won't.
> The modifiedNodes command would allow us to get that data.

But why would you need that data? (This is different from the "show 
information about the selection" case you mentioned earlier.)

Ian Hickson               U+1047E                )\._.,--....,'``.    fL
http://ln.hixie.ch/       U+263A                /,   _.. \   _\  ;`._ ,.
Things that are impossible just take longer.   `._.-(,_..'--(,_..'`-.;.'

More information about the whatwg mailing list