Pieter Edelman wrote:
On Tue, 16 Mar 2004 16:31:02 +0000, Neil wrote:
Pieter Edelman wrote:
I tried to get the cursor position with contentWindow.getSelection().focusOffset, but this gives me only the position in the current text element, not in the entire string.
A DOM selection has a number of properties, here are a few that might
help, or lxr for nsISelection:
/**
* The node representing one end of the selection. */
readonly attribute nsIDOMNode anchorNode;
/**
* The offset within the (text) node where the selection begins. */
readonly attribute long anchorOffset;
/**
* The node with keyboard focus.
*/
readonly attribute nsIDOMNode focusNode;
/**
* The offset within the (text) node where focus starts. */
readonly attribute long focusOffset;
/**
* Collapses the selection to a single point, at the specified offset *
in the given DOM node. When the selection is collapsed, and the content
* is focused and editable, the caret will blink there. * @param
parentNode The given dom node where the selection will be set *
@param offset Where in given dom node to place the selection
(the offset into the given node)
*/
void collapse(in nsIDOMNode parentNode, in long offset);
Thanks for your response, but I'm still stuck. Let me give an example, say
I want to turn every occurence of "is" in my edited text automatically
green while the rest remains unstyled. So I watch every keypress, and
whenever the sequence "is" is typed, the innerHTML of the BODY element is
replaced by a modified innerHTML.
So I start typing:
"Firefox i"
So far, so good.
"Firefox is"
Now my script kicks in and replaces this string with: "Firefox <font
color="green">is</font>". Naturally, the cursor is then placed at the
beginning of this string by mozilla. But I want it to be restored to the
original position, so before the replacement I queried the cursor
position using:
selection = [...].contentWindow.getSelection()
offset = selection.focusOffset;
node = selection.focusNode;
Now I want to restore this cursor position, but the string is now broke up
in severeal text nodes:
- #text: Firefox
- FONT
- #text: is
So, with some nasty recursive functions I get a reference to the "is"
node, to which the cursor should go at offset 2, and I issue:
selection = [...].contentWindow.getSelection()
selection.collapse(reference, 2);
And poof, I get an error and we will never now what firefox is...
Does anybody know how this can be done?
This sounds like it might be better addressed in n.p.m.editor, setting
followup.
--
Warning: May contain traces of nuts.
_______________________________________________
mozilla-editor mailing list
[EMAIL PROTECTED]
http://mail.mozilla.org/listinfo/mozilla-editor